如果有人正在查找有关 Froala v3 的信息,并且使用 React,我确实遇到了一些问题,无法让它按我想要的方式工作。 Froala 总是在插入 html 之前创建换行符。我的问题可能与通过插件方法打开自定义模式有关,因此 Froala 失去了上下文。以下是我尝试的一些修复:
在自定义插件内部使用:this.selection.save();保存光标位置
如果这能解决您的问题,请先尝试,否则请继续。在插入 HTML 恢复选择之前插入 html 和 undoSavestep 以获取 Froala 更新。
this.selection.restore();
this.html.insert(html);
this.undo.saveStep();
React 中的完整示例:
Froala.RegisterCommand('customcommand', {
title: mytitle,
focus: true,
undo: true,
refreshAfterCallback: true,
callback(_, val) {
this.selection.save();
return displayDialog((removeDialog) => ({
children: (
<DialogComponent
removeDialog={removeDialog}
submit={(html) => {
removeDialog();
this.selection.restore();
this.html.insert(html);
}}
/>
),
}));
},
如果这没有帮助,那么您可以尝试将其放在回调的承诺示例中:
callback: function () {
this.selection.save();
let result = new Promise((resolve) => {
displayDialog((removeDialog => ({
children: (
<DialogComponent
removeDialog={removeDialog}
submit={(html) => {
removeDialog();
resolve(html);
}}
/>
),
})));
});
result.then((html) => {
this.selection.restore();
this.html.insert(html);
this.undo.saveStep(); // Make froala update https://github.com/froala/wysiwyg-editor/issues/1578
});
这是迄今为止我得到的最接近的东西。这会将元素附加到几乎正确的位置。我只是不明白为什么插入符号位置没有保持不变,而 Froala 总是附加到编辑器的底部。如果其他人有更好的想法,我也在寻找答案。