如果有人正在寻找关于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);
}}
/>
),
}));
},
如果这样没有帮助,你可以尝试将它放在promise的回调示例中:
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();
});
这是我目前为止得到的最接近的东西。它几乎将元素附加到正确的位置。我只是不明白为什么插入符位置不会保持不变,而Froala总是附加到编辑器底部。如果有其他人有更好的想法,我也在寻找答案。