fix: model reload only once
This commit is contained in:
@@ -67,10 +67,11 @@ export default {
|
||||
};
|
||||
},
|
||||
watch: {
|
||||
modelValue(newValue = '') {
|
||||
if (newValue !== this.contentFromEditor()) {
|
||||
modelValue: {
|
||||
handler() {
|
||||
this.reloadState();
|
||||
}
|
||||
},
|
||||
once: true,
|
||||
},
|
||||
editorId() {
|
||||
this.reloadState();
|
||||
@@ -169,6 +170,9 @@ export default {
|
||||
}
|
||||
},
|
||||
reloadState() {
|
||||
const oldSelection = editorView.state.selection;
|
||||
const oldDoc = editorView.state.doc;
|
||||
|
||||
state = createState(
|
||||
this.modelValue,
|
||||
this.placeholder,
|
||||
@@ -176,8 +180,23 @@ export default {
|
||||
{ onImageUpload: this.openFileBrowser },
|
||||
this.enabledMenuOptions
|
||||
);
|
||||
|
||||
const tr = state.tr;
|
||||
// Only try to map and restore selection if there was a previous document
|
||||
if (oldDoc) {
|
||||
try {
|
||||
// Map the old selection to the new document structure
|
||||
const newSelection = Selection.near(
|
||||
tr.doc.resolve(Math.min(oldSelection.from, tr.doc.content.size))
|
||||
);
|
||||
tr.setSelection(newSelection);
|
||||
} catch (error) {
|
||||
console.warn('Could not restore cursor position:', error);
|
||||
}
|
||||
}
|
||||
state = state.apply(tr);
|
||||
editorView.updateState(state);
|
||||
this.focusEditorInputField();
|
||||
editorView.focus();
|
||||
},
|
||||
createEditorView() {
|
||||
editorView = new EditorView(this.$refs.editor, {
|
||||
|
||||
Reference in New Issue
Block a user