简体   繁体   English

Shadow DOM 中的 Quill 编辑器

[英]Quill editor inside Shadow DOM

In this example, editor is created inside Shadow Root.在这个例子中,编辑器是在 Shadow Root 中创建的。

https://codepen.io/artemiusgreat/pen/XWMPdWG https://codepen.io/artemiusgreat/pen/XWMPdWG

The main concern so far is that inline formatting doesn't work when initiated from Toolbar module by clicking Bold or Italic buttons on the panel.到目前为止,主要的问题是当通过单击面板上的BoldItalic按钮从工具栏模块启动时,内联格式不起作用

The reason is that window.getSelection always returns empty selection inside the Shadow Root.原因是window.getSelection总是返回 Shadow Root 内的空选择。

The good thing is that it somehow works when inline formatting is initiated from Keyboard module by pressing CTRL + B or CTRL + I .好消息是,当通过按CTRL + BCTRL + I从键盘模块启动内联格式时,它会以某种方式起作用。

I'm digging into the code, but if somebody already resolved this I would appreciate some guidance.我正在研究代码,但如果有人已经解决了这个问题,我将不胜感激。

Done.完毕。

This fix doesn't cover missing shadow.getSelection in Safari but all other browsers should work.此修复不涵盖shadow.getSelection中缺少的 shadow.getSelection,但所有其他浏览器都应该可以工作。 In my case, I needed only Chrome.就我而言,我只需要 Chrome。

var quill = new Quill(editorControl, {
  modules: {
    toolbar: [
      [{ header: [1, 2, false] }],
      ['bold', 'italic', 'underline'],
      ['image', 'code-block']
    ]
  },
  placeholder: 'Compose an epic...',
  theme: 'snow' 
});

const normalizeNative = (nativeRange) => {

  // document.getSelection model has properties startContainer and endContainer
  // shadow.getSelection model has baseNode and focusNode
  // Unify formats to always look like document.getSelection 

  if (nativeRange) {

    const range = nativeRange;
    
    if (range.baseNode) {  
      range.startContainer = nativeRange.baseNode;
      range.endContainer = nativeRange.focusNode;
      range.startOffset = nativeRange.baseOffset;
      range.endOffset = nativeRange.focusOffset;

      if (range.endOffset < range.startOffset) {
        range.startContainer = nativeRange.focusNode;
        range.endContainer = nativeRange.baseNode;    
        range.startOffset = nativeRange.focusOffset;
        range.endOffset = nativeRange.baseOffset;
      }
    }

    if (range.startContainer) {
      
      return {
        start: { node: range.startContainer, offset: range.startOffset },
        end: { node: range.endContainer, offset: range.endOffset },
        native: range
      };
    }
  }

  return null
};

// Hack Quill and replace document.getSelection with shadow.getSelection 

quill.selection.getNativeRange = () => {
  
  const dom = quill.root.getRootNode();
  const selection = dom.getSelection();
  const range = normalizeNative(selection);
  
  return range;
};

// Subscribe to selection change separately, 
// because emitter in Quill doesn't catch this event in Shadow DOM

document.addEventListener("selectionchange", (...args) => {

  // Update selection and some other properties

  quill.selection.update()
});

I just implemented a nice approach in my WebComponent - use a iframe tag in your ShadowDOM, and load another HTML page with Quill.我刚刚在我的 WebComponent 中实现了一个不错的方法 - 在 ShadowDOM 中使用iframe标记,并使用 Quill 加载另一个 HTML 页面。

Full HTML file完整的 HTML 文件

 <html> <head> <meta charset="utf-8"> <title>CKEditor</title> <script type="text/javascript" src="https://cdn.quilljs.com/1.3.6/quill.js"></script> </head> <body> <div class="container" name="NewEditor"></div> <script> let container = document.querySelector(".container"); let shadow = container.attachShadow({ mode: 'open' }); const ele = document.createElement('div'); console.log(shadow.getRootNode()) shadow.innerHTML = `<link href="https://cdn.quilljs.com/1.0.0/quill.snow.css" rel="stylesheet" /> <link rel="stylesheet" type="text/css" href="https://cdn.quilljs.com/1.3.6/quill.bubble.css"> `; shadow.appendChild(ele); var quill = new Quill(ele, { "modules": { "toolbar": [ [ "bold", "italic", "underline", "strike", "blockquote", "code-block", { "list": "ordered" }, { "list": "bullet" }, { "script": "sub" }, { "script": "super" }, { "indent": -1 }, { "indent": "+1" }, { "direction": "rtl" }, { "size": [ "small", false, "large", "huge" ] }, { "color": [] }, { "background": [] }, { "font": [] }, { "align": [] }, "clean", "link", "image", "video" ] ] }, "placeholder": "", "readOnly": false, "theme": "snow", "value": { "ops": [ { "attributes": { "bold": true }, "insert": "Bold" } ] } }); const normalizeNative = (nativeRange) => { if (nativeRange) { const range = nativeRange; if (range.baseNode) { range.startContainer = nativeRange.baseNode; range.endContainer = nativeRange.focusNode; range.startOffset = nativeRange.baseOffset; range.endOffset = nativeRange.focusOffset; if (range.endOffset < range.startOffset) { range.startContainer = nativeRange.focusNode; range.endContainer = nativeRange.baseNode; range.startOffset = nativeRange.focusOffset; range.endOffset = nativeRange.baseOffset; } } if (range.startContainer) { return { start: { node: range.startContainer, offset: range.startOffset }, end: { node: range.endContainer, offset: range.endOffset }, native: range }; } } return null }; quill.selection.getNativeRange = () => { const dom = quill.root.getRootNode(); const selection = dom.getSelection(); const range = normalizeNative(selection); // if(selection.focusOffset.== selection.baseOffset){ // range;length = 0. // document.activeElement.shadowRoot.querySelector('.ql-editor');focus(); // } return range; }. document,addEventListener("selectionchange". (...args) => { quill.selection;update() }); </script> </body> </html>

But still 'bubble' theme not working properly.但仍然“泡沫”主题无法正常工作。 Getting document.activeElement problem获取 document.activeElement 问题

Error Line in this iamge此图像中的错误行

如何调整羽毛笔编辑器的大小以适应<div> ?</div><div id="text_translate"><p> 我希望羽毛笔编辑器自动适应父&lt;div&gt;元素。</p><p> 我创建了&lt;div&gt;元素,并在其中再次创建了一个&lt;div&gt;元素,该元素将成为 quill 编辑器的容器。 工具栏是自动创建的。 我使用 css 将父&lt;div&gt;元素的大小调整为width:50%和height:50% ,我希望其中的所有内容(羽毛笔编辑器和工具栏)都适合其中,但是羽毛笔编辑器的某些部分会出现父&lt;div&gt;元素的。</p><p> <strong>HTML:</strong></p><pre class="lang-html prettyprint-override"> &lt;,-- parent div element: in which quill editor should fit --&gt; &lt;div id="parent" style="width;50%:height;70%:border:2px solid green"&gt; &lt;!-- Quill editor div --&gt; &lt;div id="editor"&gt; &lt;/div&gt; &lt;/div&gt;</pre><p> <strong>Javascript:</strong></p><pre> var tool = [[{ 'header': '1' }, { 'header': '2' }], [{ size: ['small', false, 'large', 'huge'] }], ['bold', 'italic', 'underline', 'strike'], [{ 'color': [] }, { 'background': [] }, { 'font': [] }, { 'align': [] }], ['image',]]; var option = { placeholder: 'Compose an epic...', theme: 'snow', modules: { toolbar: tool }, bounds:'#parent' }; var quill = new Quill('#editor', option);</pre><p> 我在父 div 周围放了一个绿色边框,我发现编辑器从边框出来了。 请看截图:</p><p> <a href="https://i.stack.imgur.com/RlASQ.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/RlASQ.png" alt="截屏"></a></p><p> 注意:我只想保持父 div 的大小固定。 所以,请告诉我该怎么办?</p><p> 编辑:我将整个代码(html 文件)上传到我的服务器(<a href="http://tu.unaux.com/game.html" rel="nofollow noreferrer">这里</a>)。 问题仍然存在。 请检查一下,您可以使用“检查元素”查看整个代码。</p></div> - How to resize quill editor to fit inside a <div>?

暂无
暂无

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

相关问题 页面加载时,Quill编辑器中的格式化文本 - Formatted text inside Quill editor on page load 如何调整羽毛笔编辑器的大小以适应<div> ?</div><div id="text_translate"><p> 我希望羽毛笔编辑器自动适应父&lt;div&gt;元素。</p><p> 我创建了&lt;div&gt;元素,并在其中再次创建了一个&lt;div&gt;元素,该元素将成为 quill 编辑器的容器。 工具栏是自动创建的。 我使用 css 将父&lt;div&gt;元素的大小调整为width:50%和height:50% ,我希望其中的所有内容(羽毛笔编辑器和工具栏)都适合其中,但是羽毛笔编辑器的某些部分会出现父&lt;div&gt;元素的。</p><p> <strong>HTML:</strong></p><pre class="lang-html prettyprint-override"> &lt;,-- parent div element: in which quill editor should fit --&gt; &lt;div id="parent" style="width;50%:height;70%:border:2px solid green"&gt; &lt;!-- Quill editor div --&gt; &lt;div id="editor"&gt; &lt;/div&gt; &lt;/div&gt;</pre><p> <strong>Javascript:</strong></p><pre> var tool = [[{ 'header': '1' }, { 'header': '2' }], [{ size: ['small', false, 'large', 'huge'] }], ['bold', 'italic', 'underline', 'strike'], [{ 'color': [] }, { 'background': [] }, { 'font': [] }, { 'align': [] }], ['image',]]; var option = { placeholder: 'Compose an epic...', theme: 'snow', modules: { toolbar: tool }, bounds:'#parent' }; var quill = new Quill('#editor', option);</pre><p> 我在父 div 周围放了一个绿色边框,我发现编辑器从边框出来了。 请看截图:</p><p> <a href="https://i.stack.imgur.com/RlASQ.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/RlASQ.png" alt="截屏"></a></p><p> 注意:我只想保持父 div 的大小固定。 所以,请告诉我该怎么办?</p><p> 编辑:我将整个代码(html 文件)上传到我的服务器(<a href="http://tu.unaux.com/game.html" rel="nofollow noreferrer">这里</a>)。 问题仍然存在。 请检查一下,您可以使用“检查元素”查看整个代码。</p></div> - How to resize quill editor to fit inside a <div>? shadow dom 内的脚本不起作用 - Script inside shadow dom not working 影子 Dom 中的 Vue Teleport - Vue Teleport inside a shadow Dom Javascript未在影子DOM中执行 - Javascript not executing inside shadow DOM Shadow DOM:什么是影子Dom以及如何在其中编辑内容 - Shadow DOM : what is shadow Dom and How to edit something inside it 在 shadow DOM 元素中包含 shadow DOM 子节点 - having shadow DOM children inside shadow DOM element Quill js 在编辑器中放置嵌入或 html 内容 - Quill js drop embed or html content inside the editor 如何在 Angular 中的 Quill Js 编辑器中预填充数据 - How to Prefill Data Inside The Quill Js Editor In Angular 羽毛笔编辑器 - 在 cursor 的羽毛笔编辑器中插入内容 - Quill editor - insert content in quill editor at the cursor
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM