In case someone is looking information about Froala v3 and with React I had this exactly some problem and couldn't get it work how I wanted. Froala always creates newline before inserting html. My problem is probably related to opening a custom modal through plugin method so then Froala loses context. Here are some fixes I tried:
Inside custom plugin save the cursor position using: this.selection.save();
Then try first if this solves your problem, if not please follow along. Before inserting the HTML restore selection insert html and undoSavestep to get Froala update.
this.selection.restore();
this.html.insert(html);
this.undo.saveStep();
Full example in 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);
}}
/>
),
}));
},
If this doesn't help then you can try putting it inside promise example of callback:
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
});
This is the closest thing I have got so far. This appends the element almost to the correct position. I just don't understand why the caret position doesn't stay in place and Froala always appends to the bottom of the editor. If anyone else has better ideas I am looking for answer as well.