我正在尝试用一个自定义的Blot来扩展羽毛笔,允许在<p>标记中使用换行符。根据advice given by the library author,我最终得到了如下所示的代码:
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;
}我在一个Angular 10项目中使用了ngx-quill包装器。我的Quill模块定义如下:
QuillModule.forRoot({
format: 'json',
modules: {
keyboard: {
bindings: {
"shift enter": {
key: 13,
shiftKey: true,
handler: shiftEnterHandler
}
}
},
clipboard: {
matchers: [
[ "BR", brMatcher ]
],
}
},
}),但是,每当我按下Shift+Enter键时,光标就会向前移动,但insertEmbed()调用似乎没有任何效果。我做错了什么?
发布于 2021-08-03 13:03:37
看起来你只是忘了给Quill.register(SoftLineBreakBlot)打电话
所以:
...
export class SoftLineBreakBlot extends Embed {
static blotName = 'softbreak';
static tagName = 'br';
static className = 'softbreak';
}
...变成:
...
export class SoftLineBreakBlot extends Embed {
static blotName = 'softbreak';
static tagName = 'br';
static className = 'softbreak';
}
Quill.register(SoftLineBreakBlot);
...https://stackoverflow.com/questions/63489524
复制相似问题