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 content switch mismatch (#11758)

Arvin Xufdc8f957bc9b
diff --git a/src/features/EditorCanvas/DiffAllToolbar.tsx b/src/features/EditorCanvas/DiffAllToolbar.tsx
index e8a3cf1832a..ed4f30f1d15 100644
--- a/src/features/EditorCanvas/DiffAllToolbar.tsx
+++ b/src/features/EditorCanvas/DiffAllToolbar.tsx
@@ -42,7 +42,6 @@ const useIsEditorInit = (editor: IEditor) => {
if (!editor) return;
const onInit = () => {
- console.log('init: id', editor.getLexicalEditor()?._key);
setEditInit(true);
};
editor.on('initialized', onInit);
@@ -103,13 +102,13 @@ interface DiffAllToolbarProps {
const DiffAllToolbar = memo<DiffAllToolbarProps>(({ documentId }) => {
const { t } = useTranslation('editor');
const isDarkMode = useIsDark();
- const [editor, performSave, markDirty] = useDocumentStore((s) => [
+ const [storeEditor, performSave, markDirty] = useDocumentStore((s) => [
s.editor!,
s.performSave,
s.markDirty,
]);
- const hasPendingDiffs = useEditorHasPendingDiffs(editor);
+ const hasPendingDiffs = useEditorHasPendingDiffs(storeEditor);
if (!hasPendingDiffs) return null;
@@ -131,7 +130,7 @@ const DiffAllToolbar = memo<DiffAllToolbarProps>(({ documentId }) => {
<Space>
<Button
onClick={async () => {
- editor?.dispatchCommand(LITEXML_DIFFNODE_ALL_COMMAND, {
+ storeEditor?.dispatchCommand(LITEXML_DIFFNODE_ALL_COMMAND, {
action: DiffAction.Reject,
});
await handleSave();
@@ -145,7 +144,7 @@ const DiffAllToolbar = memo<DiffAllToolbarProps>(({ documentId }) => {
<Button
color={'default'}
onClick={async () => {
- editor?.dispatchCommand(LITEXML_DIFFNODE_ALL_COMMAND, {
+ storeEditor?.dispatchCommand(LITEXML_DIFFNODE_ALL_COMMAND, {
action: DiffAction.Accept,
});
await handleSave();
diff --git a/src/features/EditorCanvas/DocumentIdMode.tsx b/src/features/EditorCanvas/DocumentIdMode.tsx
index e61bb2cc2f7..5c477e91270 100644
--- a/src/features/EditorCanvas/DocumentIdMode.tsx
+++ b/src/features/EditorCanvas/DocumentIdMode.tsx
@@ -2,7 +2,7 @@
import { type IEditor } from '@lobehub/editor';
import { Alert, Skeleton } from '@lobehub/ui';
-import { memo } from 'react';
+import { memo, useEffect, useRef } from 'react';
import { useTranslation } from 'react-i18next';
import { createStoreUpdater } from 'zustand-utils';
@@ -82,6 +82,26 @@ const DocumentIdMode = memo<DocumentIdModeProps>(
onContentChange?.();
};
+ const isEditorInitialized = !!editor?.getLexicalEditor();
+
+ // 追踪已经为哪个 documentId 调用过 onEditorInit
+ const initializedDocIdRef = useRef<string | null>(null);
+
+ // 关键修复:如果 editor 已经初始化,需要主动调用 onEditorInit
+ // 因为 onInit 回调只在 editor 首次初始化时触发
+ useEffect(() => {
+ // 避免重复调用:只在 documentId 变化且 editor 已初始化时调用
+ if (
+ editor &&
+ isEditorInitialized &&
+ !isLoading &&
+ initializedDocIdRef.current !== documentId
+ ) {
+ initializedDocIdRef.current = documentId;
+ onEditorInit(editor);
+ }
+ }, [documentId, editor, isEditorInitialized, isLoading, onEditorInit]);
+
// Show loading state
if (isLoading) {
return <EditorSkeleton />;
diff --git a/src/store/document/slices/document/action.ts b/src/store/document/slices/document/action.ts
index 9291c715855..e9cf02a75ed 100644
--- a/src/store/document/slices/document/action.ts
+++ b/src/store/document/slices/document/action.ts
@@ -194,6 +194,7 @@ export const createDocumentSlice: StateCreator<
// Check if this response is still for the current active document
// This prevents race conditions when quickly switching between documents
const currentActiveId = get().activeDocumentId;
+
if (currentActiveId && currentActiveId !== documentId) {
// User has already switched to another document, discard this stale response
return;