javascript - Quill BlockEmbeds 可以使用任意标签吗?

标签 javascript quill parchment

我有一堆组件(html 和逻辑片段),我希望能够将它们嵌入到 Quill 文档中,但我不确定如何开始。每个组件都有一个根元素,但 tagName 是任意的(有 asidedivsection 等标签)。每个组件都有完全非 Quill 的编辑体验(在别处处理),因此理想情况下它们的增量看起来像这样:

{
  ops: [
    { insert: 'Hello', attributes: { bold: true } },
    { insert: { component: 'domain.com/components/image/instances/foo' } },
    { insert: 'World!\n' }
  ]
}

我相信我在文档中的某处读到 block 级 Blots 必须指定 tagName className,但我不能找到引用。 Allexamples我可以找到使用 BlockEmbed 指定 tagNameParchment docs不要真的解释它。是否有正确的方法可以做到这一点,是否有任何我应该注意的边缘情况?

所有这些组件都是 block 级的,所以(根据我对 this issue 的阅读)我认为选择应该不是问题,对吧?

最佳答案

看看herehere

如果您的目的是创建一个 QUILL 中不存在的标签 你要做的是这样的: 配置您的自定义标签

 var Embed = Quill.import('blots/embed');
class MyCustomTag extends Embed {
    static create(paramValue) {
        let node = super.create();
        node.innerHTML = paramValue;
        //node.setAttribute('contenteditable', 'false');
        //node.addEventListener('click', MyCustomTag.onClick);
        return node;
    }

    static value(node) {
        return node.innerHTML;
    }
}

MyCustomTag.blotName = 'my-custom-tag';
MyCustomTag.className = 'my-custom-tag';
MyCustomTag.tagName = 'my-custom-tag';
//in case you need to inject an event from outside
/* MyCustomTag.onClick = function(){
     //do something
 }*/

Quill.register(MyCustomTag);

使用您的自定义标签

this.editor.insertEmbed(
0, //INDEX_WHERE_YOU_TO_INSERT_THE_CONTENT, 
'my-custom-tag',//THE NAME OF YOUR CUSTOM TAG
 '<span>MY CONTENT</SPAN>'// THE CONTENT YOUR TO INSERT 
);

注意,默认情况下,此自定义将获得属性 display: block 你可以通过 css 规则来定位它,例如

my-custom-tag {
   display : inline;
}

关于javascript - Quill BlockEmbeds 可以使用任意标签吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45421941/

相关文章:

javascript - 无法从 Objective C 插件返回 Ionic/Cordova 应用程序

javascript - javascript 表单中 grabFormSelects() 的用途是什么?

node.js - 通过node.js和mongodb实现/使用quill的正确方法是什么?

ruby-on-rails - quill js羊皮纸错误

swift - 如果我们在羊皮纸的 HeaderExample 中替换 UIView 而不是 UIImageView,则渐变背景不起作用

javascript - 使用 d3 渲染 geoJSON

javascript - $(window).scrollTop() == $(document).height() - $(window).height() 的含义

javascript - Quill 在 h1 之后添加了一个不需要的 <br>

angular - 羽毛笔 Angular 错误 : NullInjectorError: No provider for InjectionToken config

javascript - Quill.js 扩展未在增量中捕获的 Blot/Parchment 更改