Overlapping Inline Annotations with Quill

后端 未结 1 1208
挽巷
挽巷 2020-12-20 06:58

In my application, users can create margin-comments anywhere in the document body, and the comment-anchor ranges can overlap arbitrarily, like this:

This [ab         


        
相关标签:
1条回答
  • 2020-12-20 07:41

    First, I would use a class attributor instead, as it seems unnecessary that a comment get its own DOM node and instead could just be a class applied to other nodes.

    Secondly and to your point, most of the time formatting is an overwriting behavior (formatting text color to red, and then blue, makes it blue, not [red, blue]) but you can change this with something like this:

    class Comment extends Parchment.Attributor.Class {
      constructor(attrName = 'comment', keyName = 'comment') {
        super(attrName, keyName, { scope: Parchment.Scope.INLINE_ATTRIBUTE });
      }
    
      add(node, value) {
        if (!this.canAdd(node, value)) return false;
        const array = Array.isArray(value) ? value : [value];
        array.forEach((id) => {
          node.classList.add(`${this.keyName}-${id}`);
        });
        return true;
      }
    
      remove(node, id) {
        if (id == null) {
          super.remove(node);
        } else {
          node.classList.remove(`${this.keyName}-${id}`);
          if (node.classList.length === 0) {
            node.removeAttribute('class');
          }
        }
      }
    
      value(node) {
        const prefix = `${this.keyName}-`;
        const list = _.filter(node.classList, (c) => {
          return c.startsWith(prefix);
        }).map((c) => {
          return c.slice(prefix.length);
        });
        return (list.length > 0) ? list : null;
      }
    }
    
    Quill.register({ 'formats/comment': new Comment() });
    
    0 讨论(0)
提交回复
热议问题