简体   繁体   English

将所见即所得编辑器中的内容复制到 textarea

[英]Copy content from WYSIWYG editor to textarea

I try to copy the content from a WYSIWYG-Editor (What you see is what you get), to a textarea and It needs to be structured.我尝试将内容从 WYSIWYG 编辑器(所见即所得)复制到文本区域,并且需要对其进行结构化。 I am using Quill https://www.quilljs.com/docs/quickstart我正在使用 Quill https://www.quilljs.com/docs/quickstart

I added a textarea, the content from the Editor is pasted into it, on click at the button Edit HTML .我添加了一个 textarea,将编辑器中的内容粘贴到其中,单击按钮Edit HTML

$("#editHTML").click(function() {
   var content = $("#editor .ql-editor").html();
   $("#htmlEditForm").html(content);
});

It works, but as you can see it is not structured at all.它有效,但正如您所见,它根本没有结构化。 Everything is in one line:一切都在一行中:

在此处输入图片说明

I need it like this:我需要这样:

<p>FOO</p>
<p>BAR</p>
<p class="ql-align-center"><span style="color: rgb(230, 0, 0);">I am centered</span></p>

I need it to be in a textarea, because I want to use it as HTML editor.我需要将它放在 textarea 中,因为我想将它用作 HTML 编辑器。 Later on I will add another button which pastes the html code back into the WYSIWYG-editor.稍后我将添加另一个按钮,将 html 代码粘贴回 WYSIWYG 编辑器。

The goal is, to give advanced users the possibilty to edit the HTML.目标是让高级用户可以编辑 HTML。

I am not sure how I can do this, I was thinking of replacing any closing tags with this: eg </p> -> </p>\\n but it just outputs </p>\\n in the textarea instead of creating a new line.我不确定如何做到这一点,我想用这个替换任何结束标记:例如</p> -> </p>\\n但它只是在 textarea 中输出</p>\\n而不是创建一条新线。

Is this even possible?这甚至可能吗?

UPDATE:更新:

I tried to use getContents as suggested:我尝试按照建议使用getContents

var toolbarOptions = [
  ['image'],
  [{ 'header': [1, 2, 3, 4, 5, 6, false] }],
  ['bold', 'italic', 'underline', 'strike'],        // toggled buttons
  [{ 'color': [] }, { 'background': [] }],          // dropdown with defaults from theme
  [{ 'align': [] }],
  ['blockquote', 'code-block'],


  [{ 'list': 'ordered'}, { 'list': 'bullet' }],
  [{ 'script': 'sub'}, { 'script': 'super' }],      // superscript/subscript
  [{ 'direction': 'rtl' }],                         // text direction
  [{ 'font': [ 'arial', 'Monotype Corsiva', 'fantasy', 'serif' ] }],


  ['clean']                                         // remove formatting button
];

var quill = new Quill('#editor', {
  theme: 'snow',
  modules: {
      toolbar: toolbarOptions
  }
});

$("#editHTML").click(function() {
    $("#htmlEditForm").html(quill.getContents());
});

But the textarea stays empty.但 textarea 保持空白。

quill.getContents() does just return an object: quill.getContents()只返回一个对象:

在此处输入图片说明

Unfortunately Quill is not meant to do this.不幸的是,Quill 并不打算这样做。 Quill uses it's own document model called Parchment . Quill 使用它自己的名为Parchment的文档模型 It tracks everything that changes in the editor so that it can understand it's contents and make changes, but it has some limits on what can be added.它跟踪编辑器中的所有更改,以便它可以理解其内容并进行更改,但它对可以添加的内容有一些限制。 For example, you can't add an element that doesn't match an existing blot/format.例如,您不能添加与现有印迹/格式不匹配的元素。

If someone were to edit the HTML arbitrarily and add a div for example, the editor's document model would no longer match it's contents.例如,如果有人随意编辑 HTML 并添加一个 div,则编辑器的文档模型将不再与其内容匹配。 When switching back to the wysiwyg view, some functions wouldn't work until it tries to recreate it's document model and messes up the changes made in the HTML view.当切换回所见即所得视图时,某些功能将无法工作,直到它尝试重新创建其文档模型并弄乱 HTML 视图中所做的更改。

Edit编辑

Also, for the original problem of how to format the HTML with new lines and indention, what your seeing is how it's formatted.此外,对于如何使用新行和缩进格式化 HTML 的原始问题,您所看到的是它的格式化方式。 The editor is not breaking to new lines for each block element or adding indentation for nesting as someone would when they're writing code.编辑器不会像编写代码时那样为每个块元素换行或添加缩进以进行嵌套。

If you want it to be formatted in some way, you'd have to do it manually.如果您希望以某种方式对其进行格式化,则必须手动进行。 Either by parsing the getContents() output or by writing or finding a script that can format HTML.通过解析getContents()输出或通过编写或查找可以格式化 HTML 的脚本。

您可以将文本区域与允许代码编辑功能的 Ace 编辑器 ( https://ace.c9.io/ ) 结合使用。

Instead of detecting .click event use the built-in quill mechanism 'text-change' ... also to get the HTML of the rich text editor all in one go use .container.childNodes[0].innerHTML不是检测 .click 事件,而是使用内置的 quill 机制“text-change”……也可以一次性获取富文本编辑器的 HTML,使用 .container.childNodes[0].innerHTML

quill.on('text-change', function (delta, oldDelta, source) {
  // your code here e.g.
  $("#htmlEditForm").html(quill.container.childNodes[0].innerHTML);
});

Text area is not capable of showing you the formats of rich text editors.文本区域无法显示富文本编辑器的格式。 A simple solution can be strip all tags if you do not want the tags in text area.如果您不希望文本区域中的标签,一个简单的解决方案可以是去除所有标签。 Or if you want tags like bold, italic etc you have to use WYSIWYG editor.或者,如果您想要粗体、斜体等标签,则必须使用 WYSIWYG 编辑器。 You can disable all tools of the editor and it would be looking like a textarea.您可以禁用编辑器的所有工具,它看起来像一个文本区域。

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

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