为Quill blot实现自定义编辑器

Ach*_*him 8 quill

我正在尝试根据我的需要自定义Quill编辑器.我设法实现并插入自定义墨迹,如https://quilljs.com/guides/cloning-medium-with-parchment/中所述但我需要编辑数据,这些数据附加到我的墨点,如链接的URL例如.Quill的默认实现显示链接的小"内联"编辑框.我想自己实现类似的东西,但只是不明白.我没有在文档和指南中找到任何暗示.阅读Quill的源代码,我无法弄清楚链接的编辑对话框的实现位置.任何起点都将非常感激.

Kam*_*ski 6

我试过类似的东西。正确的做法应该是创建一个模块。不幸的是,正如您已经知道的那样,这并不像看起来那么容易。

让我向您指出一些有用的资源,它们对我理解如何为 quill 创建扩展有很大帮助。Quills 维护者正在策划很棒的羽毛笔列表。

我建议特别关注

  • quill-emoji它包含在书写时显示工具提示表情符号的代码
  • quill-form也许某些表单扩展有一些代码可以为您指明正确的方向

这是我使用自定义 quill 模块对其进行的尝试。

const InlineBlot = Quill.import('blots/inline');

class NamedLinkBlot extends InlineBlot {
  static create(value) {
    const node = super.create(value);

    node.setAttribute('href', value);
    node.setAttribute('target', '_blank');
    return node;
  }
}
NamedLinkBlot.blotName = 'namedlink';
NamedLinkBlot.tagName = 'A';

Quill.register('formats/namedlink', NamedLinkBlot);

const Tooltip = Quill.import('ui/tooltip');


class NamedLinkTooltip extends Tooltip {
  show() {
    super.show();
    this.root.classList.add('ql-editing');
  }


}

NamedLinkTooltip.TEMPLATE = [
  '<a class="ql-preview" target="_blank" href="about:blank"></a>',
  '<input type="text" data-link="https://quilljs.com">',
  'Url displayed',
  '<input type="text" data-name="Link name">',
  '<a class="ql-action"></a>',
  '<a class="ql-remove"></a>',
].join('');


const QuillModule = Quill.import('core/module');

class NamedLinkModule extends QuillModule {
  constructor(quill, options) {
    super(quill, options);
    this.tooltip = new NamedLinkTooltip(this.quill, options.bounds);
    this.quill.getModule('toolbar').addHandler('namedlink', this.namedLinkHandler.bind(this));
  }

  namedLinkHandler(value) {
    if (value) {
      var range = this.quill.getSelection();
      if (range == null || range.length === 0) return;
      var preview = this.quill.getText(range);
      this.tooltip.show();
    }
  }
}

Quill.register('modules/namedlink', NamedLinkModule);

const quill = new Quill('#editor', {
    theme: 'snow',
    modules: {
      namedlink: {},
      toolbar: {
        container: [
          'bold',
          'link',
          'namedlink'
        ]
      }
    }
  });
Run Code Online (Sandbox Code Playgroud)

代码笔演示

要查看工具提示:

  1. 选择任何单词
  2. 单击链接按钮右侧的隐形按钮,您的光标将变为手形。

需要解决的主要问题:

  • 为了自定义工具提示,您需要从SnowTooltip复制大部分代码 主要痛点是您无法轻松扩展该工具提示。
  • 您还需要调整事件侦听器的代码,但它应该很简单