简体   繁体   English

创建简单的Rich-Text WYSIWYG编辑器的最佳方法是什么?

[英]What's the best method for creating a simple Rich-Text WYSIWYG editor?

I need to create a simple rich-text editor that saves its contents to an XML file using arbitrary markup to indicate special text styles (eg: [b]...[/b] for bold and [i]...[/i] for italic ). 我需要创建一个简单的富文本编辑器,使用任意标记将其内容保存到XML文件中,以表示特殊的文本样式(例如: [b]...[/b]表示粗体[i]...[/i]斜体 )。 All the backend PHP stuff seems fairly straightforward, but the front-end WYSIWYG portion of the feature seems a bit more convoluted. 所有后端PHP的东西看起来都相当简单,但是该功能的前端WYSIWYG部分似乎有些复杂。 I've been reticent to use one of the currently-available JavaScript-based WYSIWYG editors because the rich-text options I want to allow are so limited, and these applications are so fully-featured that it almost seems like more work to stip them down to the functions I need. 我一直不愿使用一种当前可用的基于JavaScript的WYSIWYG编辑器,因为我要允许的富文本选项非常有限,而且这些应用程序功能如此强大,以至于似乎需要更多工作来提示他们到我需要的功能。

So, in setting out to create a bare-bones rich-text editor, I've encountered three approaches: 因此,在着手创建准系统的富文本编辑器时,我遇到了三种方法:

  • The first two approaches use the contentEditable or designMode properties to create an editable element, and the execCommand() method to apply new text styles to a selected range. 前两种方法使用contentEditabledesignMode属性创建可编辑元素,并使用execCommand()方法将新的文本样式应用于选定范围。
    • The first option uses a standard div element, executes all styling commands on that elements contents. 第一个选项使用标准div元素,在该元素的内容上执行所有样式命令。
  • The second option uses the editible body of a window enclosed in an iframe , then passes any styling commands initiated from buttons in the parent document into its contentWindow to alter selected ranges in the contained body. 第二个选项使用内置在iframe中的窗口的可编辑body ,然后将从父文档中的按钮启动的所有样式命令传递到其contentWindow以更改所包含主体中的选定范围。 This seems like several extra steps to accomplish the same effect as option one, but I suppose the isolation of the editable content in its own document has its advantages. 这似乎需要几个额外的步骤来实现与选项一相同的效果,但是我认为隔离其自己的文档中的可编辑内容具有其优势。
  • The third option uses a textarea overlaying a div , and uses the oninput JS event to update the background div's innerHTML to match the input textarea's value whenever it changes. 第三个选项使用在div覆盖的textarea ,并使用oninput JS事件更新背景div的innerHTML以在输入的textarea value发生更改时进行匹配。 Obviously, this requires some string finagling to to convert elements like newline characters in the textarea to <br/> in the div, but this would allow me to preserve the integrity of my [/] markup, while relegating the potentially-messy DOM manipulation to front-end display only. 显然,这需要一些字符串finagling到像元素转换newline字符在文本区域,以<br/>在div,但这将允许我保留我的完整性[/]标记,而贬谪潜在的,凌乱的DOM操作仅限前端显示。

I can see benefits and drawbacks for each method. 我可以看到每种方法的优缺点。 the contentEditable solutions seem initially the simplest, but support for this features tends to vary across browsers, and each browser that DOES support it seems to manipulate the DOM differently when implementing execCommand() . contentEditable解决方案最初似乎是最简单的,但是对此功能的支持在不同的浏览器中会有所不同,并且每个支持DOES的浏览器在实现execCommand()时似乎都以不同的方式操作DOM。 As mentioned before, the textarea/div solution seems like the best way to preserve my arbitrary styling conventions, but the custom string-manipulation procedure to display rich text in the output div could get pretty hairy. 如前所述,textarea / div解决方案似乎是保留我的任意样式约定的最佳方法,但是用于在输出div中显示富文本的自定义字符串操作过程可能会变得很麻烦。

So, I submit to you my question: Given the development goals I've outlined, which method would you choose, and why? 因此,我向您提出我的问题:鉴于我已概述的开发目标, 您将选择哪种方法,为什么? And of course, if there's another method I'm overlooking that might better serve my purpose, please enlighten me! 当然,如果我忽略了另一种方法可能会更好地达到我的目的,请赐教!

Thanks in advance! 提前致谢!

Have you looked at http://php.net/manual/en/book.bbcode.php ? 您是否看过http://php.net/manual/zh/book.bbcode.php This is your answer. 这就是你的答案。 If you are having doubts, then you are doing something wrong. 如果您有疑问,那么您做错了什么。 :-) :-)

Then use JS to track keyup event and simple AJAX to print preview of the input. 然后使用JS跟踪keyup事件,并使用简单的AJAX打印输入的预览。 Just like in stackoverflow. 就像在stackoverflow中一样。

NB It would be far more efficient to generate the preview using plain-js BBcode approach. 注意:使用纯js BBcode方法生成预览将效率更高。 However, do not overcomplicate stuff unless you necessary need it. 但是,除非必要,请不要使内容过于复杂。

The problem with BBCode, Markdown, ... is that it's not that trivial for genpop. BBCode,Markdown等的问题是,对于genpop来说,它并不那么琐碎。 I suggest looking at widgEditor , it is by far the simplest WYSIWYG editor I've seen to date. 我建议看一下widgEditor ,它是迄今为止我见过的最简单的WYSIWYG编辑器。 It was developed some time ago, so I am not sure about compatibility, but it sure is an inspiration. 它是在一段时间之前开发的,因此我不确定兼容性,但这肯定是一个启发。

I would have included this only as a comment, since it does not directly answer your question, but I am fairly new to SA and could not find out how to do that. 我本来只能将其作为评论,因为它不能直接回答您的问题,但是我对SA还是陌生的,无法找到解决方法。 Sorry. 抱歉。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM