Skip to content

🤯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.

🐛 fix(page): keep stale editor content out of another Page's save (#21235)

Lobehub Coder3753e5569df6
diff --git a/src/store/document/slices/editor/action.identity.test.ts b/src/store/document/slices/editor/action.identity.test.ts
index 25b938c6373..80433c156f4 100644
--- a/src/store/document/slices/editor/action.identity.test.ts
+++ b/src/store/document/slices/editor/action.identity.test.ts
@@ -144,4 +144,33 @@ describe('document identity and metadata-only saves', () => {
createdAt: 123,
});
});
+
+ it('does not write the previous editor body into the next document before its editor takes over', async () => {
+ const previous = makeEditor('A body');
+ seed('A', 'A body', previous.editor);
+ await useDocumentStore.getState().onEditorInit(previous.editor, 'A');
+
+ // The next canvas loads B into its own editor while the store still points at
+ // the previous editor, which holds A's body.
+ const next = makeEditor('');
+ seed('B', '', previous.editor);
+ await useDocumentStore.getState().onEditorInit(next.editor, 'B');
+ useDocumentStore.getState().handleContentChange();
+ await useDocumentStore.getState().performSave('B', undefined, { saveSource: 'manual' });
+
+ expect(useDocumentStore.getState().documents.B.content).toBe('');
+ expect(useDocumentStore.getState().documents.B.isDirty).toBeFalsy();
+ expect(documentService.updateDocument).not.toHaveBeenCalled();
+ });
+
+ it('syncs editor changes once the editor holding the document is in the store', async () => {
+ const { editor } = makeEditor('B typed');
+ seed('B', '', editor);
+ await useDocumentStore.getState().onEditorInit(editor, 'B');
+
+ useDocumentStore.getState().handleContentChange();
+
+ expect(useDocumentStore.getState().documents.B.content).toBe('B typed');
+ expect(useDocumentStore.getState().documents.B.isDirty).toBe(true);
+ });
});
diff --git a/src/store/document/slices/editor/action.ts b/src/store/document/slices/editor/action.ts
index f5c6f1cfaaa..c75995b1aed 100644
--- a/src/store/document/slices/editor/action.ts
+++ b/src/store/document/slices/editor/action.ts
@@ -17,6 +17,7 @@ import type { DocumentStore } from '../../store';
import { documentDetailResource } from '../document/projection';
import type { DocumentFormatFields } from './contentFormat';
import { hasDocumentFormatFields, resolveDocumentContentFormat } from './contentFormat';
+import type { EditorContentState } from './initialState';
import type { DocumentDispatch } from './reducer';
import { documentReducer } from './reducer';
@@ -53,6 +54,8 @@ export class EditorActionImpl {
readonly #get: () => DocumentStore;
readonly #set: Setter;
readonly #inflightSaves = new Map<string, Promise<void>>();
+ /** Document whose body each editor instance was last loaded with. */
+ readonly #editorDocumentIds = new WeakMap<IEditor, string>();
constructor(set: Setter, get: () => DocumentStore, _api?: unknown) {
void _api;
@@ -91,6 +94,12 @@ export class EditorActionImpl {
const id = documentId || activeDocumentId;
if (!editor || !id || id !== activeDocumentId) return false;
+ // A document switch moves `activeDocumentId` before the editor in the store
+ // holds the new body (each canvas mount creates its own editor, and the store
+ // keeps the previous one until it updates). Reading it now would write the
+ // previous document's body into this one.
+ const loadedDocumentId = this.#editorDocumentIds.get(editor);
+ if (loadedDocumentId !== undefined && loadedDocumentId !== id) return false;
const doc = documents[id];
if (!doc || doc.contentFormat === 'html') return false;
@@ -275,6 +284,11 @@ export class EditorActionImpl {
if (!doc || doc.contentFormat === 'html') return;
+ this.#loadDocumentIntoEditor(editor, doc);
+ this.#editorDocumentIds.set(editor, documentId);
+ };
+
+ #loadDocumentIntoEditor = (editor: IEditor, doc: EditorContentState): void => {
// Check if editorData is valid and non-empty
const editorDataRoot = (doc.editorData as { root?: { children?: unknown[] } } | null)?.root;
// A childless root (empty-parse shell) throws Lexical error #38 on hydration;