mirror of
https://github.com/HKUDS/DeepTutor.git
synced 2026-12-02 00:39:47 +08:00
Add rich text and learning blocks, media uploads, page trees, and revisioned notebook storage. Present AI suggestions at their target blocks with durable accept and dismiss actions. Unify saved outputs as Favorites with typed expandable blocks, stable source links, atomic batch insertion, nesting, dragging, and undo/redo. Validation: 99 frontend tests and 99 backend tests pass in an isolated snapshot, along with TypeScript, scoped ESLint, dependency architecture, and locale parity checks.
36 lines
1.4 KiB
TypeScript
36 lines
1.4 KiB
TypeScript
import type { NoteBlock } from '@/lib/notes-api'
|
|
|
|
const find = (items: NoteBlock[], id: string): NoteBlock | undefined => {
|
|
for (const block of items) {
|
|
if (block.id === id) return block
|
|
const child = find(block.children || [], id)
|
|
if (child) return child
|
|
}
|
|
}
|
|
|
|
export function canMoveNoteBlock(blocks: NoteBlock[], sourceId: string, targetId: string) {
|
|
const source = find(blocks, sourceId)
|
|
return !!source && sourceId !== targetId && !find(source.children || [], targetId) && !!find(blocks, targetId)
|
|
}
|
|
|
|
/** Relocate a complete subtree without regenerating any of its stable IDs. */
|
|
export function moveNoteBlock(blocks: NoteBlock[], sourceId: string, targetId: string, side: 'before' | 'after') {
|
|
if (!canMoveNoteBlock(blocks, sourceId, targetId)) return null
|
|
const result = structuredClone(blocks)
|
|
const source = find(result, sourceId)
|
|
if (!source) return null
|
|
const remove = (items: NoteBlock[]): boolean => {
|
|
const index = items.findIndex(block => block.id === sourceId)
|
|
if (index >= 0) { items.splice(index, 1); return true }
|
|
return items.some(block => remove(block.children || []))
|
|
}
|
|
const insert = (items: NoteBlock[]): boolean => {
|
|
const index = items.findIndex(block => block.id === targetId)
|
|
if (index >= 0) { items.splice(index + Number(side === 'after'), 0, source); return true }
|
|
return items.some(block => insert(block.children || []))
|
|
}
|
|
remove(result)
|
|
insert(result)
|
|
return result
|
|
}
|