扩展 Quill 以支持软换行

Pin*_*ing 5 typescript quill angular

我正在尝试使用自定义 Blot 来扩展 Quill,以允许标签内换行<p>。根据库作者给出的建议,我最终得到了如下所示的代码:

import * as Quill from 'quill';

const Delta = Quill.import('delta');
const Embed = Quill.import('blots/embed');

export class SoftLineBreakBlot extends Embed {
    static blotName = 'softbreak';
    static tagName = 'br';  
    static className = 'softbreak';
}

export function shiftEnterHandler(this: any, range) {    
    const currentLeaf = this.quill.getLeaf(range.index)[0];
    const nextLeaf = this.quill.getLeaf(range.index + 1)[0];    
    this.quill.insertEmbed(range.index, "softbreak", true, Quill.sources.USER);    
    // Insert a second break if:
    // At the end of the editor, OR next leaf has a different parent (<p>)
    if (nextLeaf === null || currentLeaf.parent !== nextLeaf.parent) {
      this.quill.insertEmbed(range.index, "softbreak", true, Quill.sources.USER);
    }
    // Now that we've inserted a line break, move the cursor forward
    this.quill.setSelection(range.index + 1, Quill.sources.SILENT);    
}

export function brMatcher(node, delta) {
    let newDelta = new Delta();
    newDelta.insert({softbreak: true});
    return newDelta;
}

Run Code Online (Sandbox Code Playgroud)

ngx-quill在 Angular 10 项目中使用 Quill 的包装器。我的 Quill 模块定义如下:

QuillModule.forRoot({
      format: 'json',
      modules: {
        keyboard: {
          bindings: {
            "shift enter": {
              key: 13,
              shiftKey: true,
              handler: shiftEnterHandler
            }
          }
        },
        clipboard: {
          matchers: [            
             [ "BR", brMatcher ]
          ],          
        }
      },
    }),
Run Code Online (Sandbox Code Playgroud)

但是,每当我按 Shift+Enter 时,光标都会向前移动,但调用insertEmbed()似乎没有任何效果。我究竟做错了什么?

小智 2

看来你只是忘记打电话Quill.register(SoftLineBreakBlot)

所以:

...
export class SoftLineBreakBlot extends Embed {
    static blotName = 'softbreak';
    static tagName = 'br';  
    static className = 'softbreak';
}
...
Run Code Online (Sandbox Code Playgroud)

变成:

...
export class SoftLineBreakBlot extends Embed {
    static blotName = 'softbreak';
    static tagName = 'br';  
    static className = 'softbreak';
}
Quill.register(SoftLineBreakBlot);
...
Run Code Online (Sandbox Code Playgroud)