From ccfa109fcfef3a2c8fe6fffac082b806fbcfd6c3 Mon Sep 17 00:00:00 2001 From: windingwind <33902321+windingwind@users.noreply.github.com> Date: Fri, 19 Dec 2025 05:57:12 +0100 Subject: [PATCH] Fix note editor hard refresh after saving note (#5640) Fix note editor instances' editing state lost after saving --- chrome/content/zotero/elements/noteEditor.js | 6 ++- chrome/content/zotero/xpcom/editorInstance.js | 44 +++++++++++++++++++ 2 files changed, 48 insertions(+), 2 deletions(-) diff --git a/chrome/content/zotero/elements/noteEditor.js b/chrome/content/zotero/elements/noteEditor.js index 070bd671b1..a5e6a86f52 100644 --- a/chrome/content/zotero/elements/noteEditor.js +++ b/chrome/content/zotero/elements/noteEditor.js @@ -182,13 +182,15 @@ let state = extraData && extraData[id] && extraData[id].state; if (state) { if (extraData[id].noteEditorID !== this._editorInstance.instanceID) { - this.initEditor(state, true); + let noteData = { state, html: this._item.note }; + this._editorInstance.applyIncrementalUpdate(noteData, true); } } else { let curValue = this._item.note; if (curValue !== this._lastHtmlValue) { - this.initEditor(null, true); + let noteData = { html: curValue }; + this._editorInstance.applyIncrementalUpdate(noteData, true); } } this._lastHtmlValue = this._item.note; diff --git a/chrome/content/zotero/xpcom/editorInstance.js b/chrome/content/zotero/xpcom/editorInstance.js index 30b7efee18..7b15fb4f0f 100644 --- a/chrome/content/zotero/xpcom/editorInstance.js +++ b/chrome/content/zotero/xpcom/editorInstance.js @@ -230,6 +230,25 @@ class EditorInstance { } } + async reinit(state = null) { + let currentOptions = { + state: state || this._state, + item: this._item, + reloaded: true, + iframeWindow: this._iframeWindow, + popup: this._popup, + onNavigate: this.onNavigate, + viewMode: this._viewMode, + readOnly: this._readOnly, + disableUI: this._disableUI, + onReturn: this._onReturn, + placeholder: this._placeholder + }; + + await this.uninit(); + await this.init(currentOptions); + } + focus() { this._postMessage({ action: 'focus' }); } @@ -283,6 +302,26 @@ class EditorInstance { this._postMessage({ action: 'insertHTML', pos: null, html }); } } + + /** + * Apply external changes incrementally instead of reinitializing the entire editor + * @param {Object} noteData - The note data containing state and html + * @param {Object} [noteData.state] - state object of editor + * @param {String} noteData.html - html for fallback update + * @param {boolean} preserveSelection - Whether to preserve the current selection + */ + applyIncrementalUpdate(noteData, preserveSelection = true) { + if (this._readOnly || !this._iframeWindow) { + this.reinit(noteData.state || null); + return; + } + + this._postMessage({ + action: 'updateIncrementally', + noteData, + preserveSelection + }); + } _postMessage(message) { this._iframeWindow.postMessage({ instanceID: this.instanceID, message }, '*'); @@ -709,6 +748,11 @@ class EditorInstance { this._onReturn(); return; } + case 'incrementalUpdateFailed': { + // Incremental update failed, do full reinitialize + this.reinit(); + return; + } } } catch (e) {