Skip to content

Commit fdc8f95

Browse files
authored
🐛 fix: page content switch mismatch (lobehub#11758)
* try to fix page * try to fix page switch
1 parent 547be72 commit fdc8f95

File tree

3 files changed

+26
-6
lines changed

3 files changed

+26
-6
lines changed

src/features/EditorCanvas/DiffAllToolbar.tsx

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ const useIsEditorInit = (editor: IEditor) => {
4242
if (!editor) return;
4343

4444
const onInit = () => {
45-
console.log('init: id', editor.getLexicalEditor()?._key);
4645
setEditInit(true);
4746
};
4847
editor.on('initialized', onInit);
@@ -103,13 +102,13 @@ interface DiffAllToolbarProps {
103102
const DiffAllToolbar = memo<DiffAllToolbarProps>(({ documentId }) => {
104103
const { t } = useTranslation('editor');
105104
const isDarkMode = useIsDark();
106-
const [editor, performSave, markDirty] = useDocumentStore((s) => [
105+
const [storeEditor, performSave, markDirty] = useDocumentStore((s) => [
107106
s.editor!,
108107
s.performSave,
109108
s.markDirty,
110109
]);
111110

112-
const hasPendingDiffs = useEditorHasPendingDiffs(editor);
111+
const hasPendingDiffs = useEditorHasPendingDiffs(storeEditor);
113112

114113
if (!hasPendingDiffs) return null;
115114

@@ -131,7 +130,7 @@ const DiffAllToolbar = memo<DiffAllToolbarProps>(({ documentId }) => {
131130
<Space>
132131
<Button
133132
onClick={async () => {
134-
editor?.dispatchCommand(LITEXML_DIFFNODE_ALL_COMMAND, {
133+
storeEditor?.dispatchCommand(LITEXML_DIFFNODE_ALL_COMMAND, {
135134
action: DiffAction.Reject,
136135
});
137136
await handleSave();
@@ -145,7 +144,7 @@ const DiffAllToolbar = memo<DiffAllToolbarProps>(({ documentId }) => {
145144
<Button
146145
color={'default'}
147146
onClick={async () => {
148-
editor?.dispatchCommand(LITEXML_DIFFNODE_ALL_COMMAND, {
147+
storeEditor?.dispatchCommand(LITEXML_DIFFNODE_ALL_COMMAND, {
149148
action: DiffAction.Accept,
150149
});
151150
await handleSave();

src/features/EditorCanvas/DocumentIdMode.tsx

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import { type IEditor } from '@lobehub/editor';
44
import { Alert, Skeleton } from '@lobehub/ui';
5-
import { memo } from 'react';
5+
import { memo, useEffect, useRef } from 'react';
66
import { useTranslation } from 'react-i18next';
77
import { createStoreUpdater } from 'zustand-utils';
88

@@ -82,6 +82,26 @@ const DocumentIdMode = memo<DocumentIdModeProps>(
8282
onContentChange?.();
8383
};
8484

85+
const isEditorInitialized = !!editor?.getLexicalEditor();
86+
87+
// 追踪已经为哪个 documentId 调用过 onEditorInit
88+
const initializedDocIdRef = useRef<string | null>(null);
89+
90+
// 关键修复:如果 editor 已经初始化,需要主动调用 onEditorInit
91+
// 因为 onInit 回调只在 editor 首次初始化时触发
92+
useEffect(() => {
93+
// 避免重复调用:只在 documentId 变化且 editor 已初始化时调用
94+
if (
95+
editor &&
96+
isEditorInitialized &&
97+
!isLoading &&
98+
initializedDocIdRef.current !== documentId
99+
) {
100+
initializedDocIdRef.current = documentId;
101+
onEditorInit(editor);
102+
}
103+
}, [documentId, editor, isEditorInitialized, isLoading, onEditorInit]);
104+
85105
// Show loading state
86106
if (isLoading) {
87107
return <EditorSkeleton />;

src/store/document/slices/document/action.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,7 @@ export const createDocumentSlice: StateCreator<
194194
// Check if this response is still for the current active document
195195
// This prevents race conditions when quickly switching between documents
196196
const currentActiveId = get().activeDocumentId;
197+
197198
if (currentActiveId && currentActiveId !== documentId) {
198199
// User has already switched to another document, discard this stale response
199200
return;

0 commit comments

Comments
 (0)