🐛 fix: page content switch mismatch (#11758)

* try to fix page

* try to fix page switch
This commit is contained in:
Arvin Xu
2026-01-24 01:45:40 +08:00
committed by GitHub
parent 547be72566
commit fdc8f957bc
3 changed files with 26 additions and 6 deletions

View File

@@ -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();

View File

@@ -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 />;

View File

@@ -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;