chapter re-ordering

This commit is contained in:
2026-02-21 10:42:01 +10:00
parent 740faae8c8
commit ca49a407da
8 changed files with 162 additions and 9 deletions

View File

@@ -35,6 +35,9 @@ interface EditorState {
analysisMode: AnalysisMode
setAnalysisMode: (mode: AnalysisMode) => void
// File tree reordering
moveNode: (dirPath: string, fromIdx: number, toIdx: number) => void
// Word counts
projectWordCount: number
setProjectWordCount: (count: number) => void
@@ -52,6 +55,37 @@ export const useEditorStore = create<EditorState>((set, get) => ({
fileTree: [],
setFileTree: (fileTree) => set({ fileTree }),
moveNode: (dirPath, fromIdx, toIdx) => {
set((s) => {
let newTree: typeof s.fileTree
if (dirPath === '__root__') {
newTree = [...s.fileTree]
const [moved] = newTree.splice(fromIdx, 1)
newTree.splice(toIdx, 0, moved)
} else {
newTree = s.fileTree.map((node) => {
if (node.path !== dirPath || !node.children) return node
const children = [...node.children]
const [moved] = children.splice(fromIdx, 1)
children.splice(toIdx, 0, moved)
return { ...node, children }
})
}
const orderMap: Record<string, string[]> = {}
orderMap['__root__'] = newTree.map((n) => n.name)
for (const node of newTree) {
if (node.type === 'directory' && node.children) {
orderMap[node.path] = node.children.map((c) => c.name)
}
}
window.api.saveOrder(orderMap).catch(console.error)
return { fileTree: newTree }
})
},
activeFilePath: null,
activeFileContent: '',
isDirty: false,