feat(lesson-preparation): expanded-slice(节点展开状态)
This commit is contained in:
56
src/modules/lesson-preparation/hooks/expanded-slice.ts
Normal file
56
src/modules/lesson-preparation/hooks/expanded-slice.ts
Normal file
@@ -0,0 +1,56 @@
|
|||||||
|
import type { StateCreator } from "zustand";
|
||||||
|
import type { EditorState } from "./use-lesson-plan-editor";
|
||||||
|
|
||||||
|
export interface ExpandedSlice {
|
||||||
|
/** 已展开到正文流的节点 ID */
|
||||||
|
expandedNodeIds: string[];
|
||||||
|
/** v3 迁移过来的失效锚点提示(按 planId 记录是否已 dismiss) */
|
||||||
|
legacyAnchorDismissed: Record<string, boolean>;
|
||||||
|
toggleExpand: (nodeId: string) => void;
|
||||||
|
setExpanded: (nodeIds: string[]) => void;
|
||||||
|
isExpanded: (nodeId: string) => boolean;
|
||||||
|
dismissLegacyAnchor: (planId: string) => void;
|
||||||
|
/** 同步 doc.expandedNodeIds(保存/加载时调用) */
|
||||||
|
syncFromDoc: (expandedNodeIds: string[]) => void;
|
||||||
|
}
|
||||||
|
|
||||||
|
export const createExpandedSlice: StateCreator<
|
||||||
|
EditorState,
|
||||||
|
[],
|
||||||
|
[],
|
||||||
|
ExpandedSlice
|
||||||
|
> = (set, get) => ({
|
||||||
|
expandedNodeIds: [],
|
||||||
|
legacyAnchorDismissed: {},
|
||||||
|
|
||||||
|
toggleExpand: (nodeId) => {
|
||||||
|
get().pushHistory();
|
||||||
|
set((s) => {
|
||||||
|
const isExp = s.expandedNodeIds.includes(nodeId);
|
||||||
|
const next = isExp
|
||||||
|
? s.expandedNodeIds.filter((id) => id !== nodeId)
|
||||||
|
: [...s.expandedNodeIds, nodeId];
|
||||||
|
// 同步到 doc
|
||||||
|
return {
|
||||||
|
expandedNodeIds: next,
|
||||||
|
doc: { ...s.doc, expandedNodeIds: next },
|
||||||
|
isDirty: true,
|
||||||
|
};
|
||||||
|
});
|
||||||
|
},
|
||||||
|
|
||||||
|
setExpanded: (nodeIds) =>
|
||||||
|
set((s) => ({
|
||||||
|
expandedNodeIds: nodeIds,
|
||||||
|
doc: { ...s.doc, expandedNodeIds: nodeIds },
|
||||||
|
})),
|
||||||
|
|
||||||
|
isExpanded: (nodeId) => get().expandedNodeIds.includes(nodeId),
|
||||||
|
|
||||||
|
dismissLegacyAnchor: (planId) =>
|
||||||
|
set((s) => ({
|
||||||
|
legacyAnchorDismissed: { ...s.legacyAnchorDismissed, [planId]: true },
|
||||||
|
})),
|
||||||
|
|
||||||
|
syncFromDoc: (expandedNodeIds) => set({ expandedNodeIds }),
|
||||||
|
});
|
||||||
Reference in New Issue
Block a user