扩展 Quill 以支持软换行
Extending Quill to support soft line breaks
我正在尝试使用自定义印迹扩展 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;
}
我在 Angular 10 项目中使用围绕 Quill 的 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()
调用似乎没有任何效果。我做错了什么?
你好像忘了打电话给 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);
...
我正在尝试使用自定义印迹扩展 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;
}
我在 Angular 10 项目中使用围绕 Quill 的 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()
调用似乎没有任何效果。我做错了什么?
你好像忘了打电话给 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);
...