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-editor): keep persisted node ids when loading documents (#20444)

Innei4285e56bbf56
diff --git a/src/features/PageEditor/usePageDraft.ts b/src/features/PageEditor/usePageDraft.ts
index 7faabab715f..5d34f0651de 100644
--- a/src/features/PageEditor/usePageDraft.ts
+++ b/src/features/PageEditor/usePageDraft.ts
@@ -153,7 +153,7 @@ export const usePageDraft = (): void => {
onOk: () => {
try {
if (draft.editorData && typeof draft.editorData === 'object') {
- editor.setDocument('json', JSON.stringify(draft.editorData));
+ editor.setDocument('json', JSON.stringify(draft.editorData), { keepId: true });
}
} catch (error) {
log('failed to restore draft for %s: %O', documentId, error);
diff --git a/src/routes/(main)/agent/features/Conversation/Header/HeaderActions/useMenu.tsx b/src/routes/(main)/agent/features/Conversation/Header/HeaderActions/useMenu.tsx
index 306c577ce48..da34e62f39e 100644
--- a/src/routes/(main)/agent/features/Conversation/Header/HeaderActions/useMenu.tsx
+++ b/src/routes/(main)/agent/features/Conversation/Header/HeaderActions/useMenu.tsx
@@ -126,7 +126,7 @@ export const useMenu = (): { menuHeader?: ReactNode; menuItems: () => DropdownIt
`header-actions-history-${docId}`,
);
- editor.setDocument('json', JSON.stringify(result.editorData));
+ editor.setDocument('json', JSON.stringify(result.editorData), { keepId: true });
markDirty(docId);
await performSave(docId, undefined, {
restoreFromHistoryId: item.id,
diff --git a/src/store/document/slices/editor/action.persistentId.test.ts b/src/store/document/slices/editor/action.persistentId.test.ts
new file mode 100644
index 00000000000..11bec3dbd76
--- /dev/null
+++ b/src/store/document/slices/editor/action.persistentId.test.ts
@@ -0,0 +1,87 @@
+import { CommonPlugin, type IEditor, Kernel } from '@lobehub/editor';
+import { act, renderHook } from '@testing-library/react';
+import { describe, expect, it, vi } from 'vitest';
+
+import { useDocumentStore } from '../../store';
+
+vi.mock('@/services/document', () => ({ documentService: {} }));
+vi.mock('@/services/notebook', () => ({ notebookService: {} }));
+
+const paragraph = (nodeId: string, textId: string, text: string) => ({
+ $: { properties: { nodeId } },
+ children: [
+ {
+ $: { properties: { nodeId: textId } },
+ detail: 0,
+ format: 0,
+ id: textId,
+ mode: 'normal',
+ style: '',
+ text,
+ type: 'text',
+ version: 1,
+ },
+ ],
+ direction: null,
+ format: '',
+ id: nodeId,
+ indent: 0,
+ textFormat: 0,
+ textStyle: '',
+ type: 'paragraph',
+ version: 1,
+});
+
+const persistedEditorData = {
+ root: {
+ children: [
+ paragraph('6w5t9drdmo', 'hot6lgl8jp', 'Alpha paragraph'),
+ paragraph('7oxxhtqflo', 'n238mjg8x9', 'Beta paragraph'),
+ ],
+ direction: null,
+ format: '',
+ indent: 0,
+ type: 'root',
+ version: 1,
+ },
+};
+
+const createEditor = () => {
+ const editor = new Kernel() as unknown as IEditor;
+ editor.registerPlugins([CommonPlugin]);
+ editor.initNodeEditor();
+ return editor;
+};
+
+const topLevelIds = (editor: IEditor) =>
+ (editor.getDocument('json') as unknown as typeof persistedEditorData).root.children.map(
+ (node) => node.id,
+ );
+
+describe('DocumentStore - persisted node ids', () => {
+ it.each([
+ ['page', undefined],
+ ['notebook', 'skillMarkdown'],
+ ] as const)(
+ 'keeps stored node ids when hydrating a %s document',
+ async (sourceType, contentFormat) => {
+ const { result } = renderHook(() => useDocumentStore());
+ const editor = createEditor();
+
+ act(() => {
+ result.current.initDocumentWithEditor({
+ content: 'Alpha paragraph\n\nBeta paragraph',
+ contentFormat,
+ documentId: 'doc-persisted-ids',
+ editor,
+ editorData: structuredClone(persistedEditorData),
+ sourceType,
+ });
+ });
+
+ await act(() => result.current.onEditorInit(editor));
+
+ expect(topLevelIds(editor)).toEqual(['6w5t9drdmo', '7oxxhtqflo']);
+ },
+ );
+});
diff --git a/src/store/document/slices/editor/action.test.ts b/src/store/document/slices/editor/action.test.ts
index ea48f3cd53e..a9a98f17184 100644
--- a/src/store/document/slices/editor/action.test.ts
+++ b/src/store/document/slices/editor/action.test.ts
@@ -297,7 +297,9 @@ name: skill-name
});
expect(mockEditor.setDocument).toHaveBeenCalledTimes(1);
- expect(mockEditor.setDocument).toHaveBeenCalledWith('json', JSON.stringify(editorData));
+ expect(mockEditor.setDocument).toHaveBeenCalledWith('json', JSON.stringify(editorData), {
+ keepId: true,
+ });
});
it('should fall back to editable body when SKILL.md editorData cannot be loaded', () => {
@@ -338,7 +340,14 @@ name: skill-name
expect(consoleWarn).toHaveBeenCalledWith(
'[DocumentStore] Failed to load SKILL.md editorData, falling back to markdown',
);
- expect(mockEditor.setDocument).toHaveBeenNthCalledWith(1, 'json', JSON.stringify(editorData));
+ expect(mockEditor.setDocument).toHaveBeenNthCalledWith(
+ 1,
+ 'json',
+ JSON.stringify(editorData),
+ {
+ keepId: true,
+ },
+ );
expect(mockEditor.setDocument).toHaveBeenNthCalledWith(2, 'markdown', '# Body');
consoleWarn.mockRestore();
@@ -362,7 +371,9 @@ name: skill-name
result.current.onEditorInit(mockEditor);
});
- expect(mockEditor.setDocument).toHaveBeenCalledWith('json', JSON.stringify(editorData));
+ expect(mockEditor.setDocument).toHaveBeenCalledWith('json', JSON.stringify(editorData), {
+ keepId: true,
+ });
});
it('should reset editor content when target document is empty', () => {
diff --git a/src/store/document/slices/editor/action.ts b/src/store/document/slices/editor/action.ts
index a3486885edd..14968fea8b4 100644
--- a/src/store/document/slices/editor/action.ts
+++ b/src/store/document/slices/editor/action.ts
@@ -263,7 +263,7 @@ export class EditorActionImpl {
if (doc.contentFormat === 'skillMarkdown') {
if (hasValidEditorData) {
try {
- editor.setDocument('json', JSON.stringify(doc.editorData));
+ editor.setDocument('json', JSON.stringify(doc.editorData), { keepId: true });
return;
} catch {
console.warn(
@@ -285,7 +285,7 @@ export class EditorActionImpl {
// Set content from document state
if (hasValidEditorData) {
try {
- editor.setDocument('json', JSON.stringify(doc.editorData));
+ editor.setDocument('json', JSON.stringify(doc.editorData), { keepId: true });
return;
} catch {
// Fallback to markdown if JSON fails