Files
DeepTutor/web/features/notes/model/clip-editing.ts
Bingxi Zhao (Frank) 857a82e74a feat(notes): add block notebooks with inline reviews and linked favorites
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.
2026-10-17 23:15:23 +08:00

52 lines
1.5 KiB
TypeScript

import { closeHistory } from '@tiptap/pm/history'
import type { Transaction } from '@tiptap/pm/state'
import type { FavoriteSnapshot } from '@/lib/favorite-content'
export interface NoteClipInput {
title: string
markdown: string
sourceUrl: string
sourceId?: string
favorite?: FavoriteSnapshot
}
export function noteClip(input: NoteClipInput) {
return {
type: 'clip' as const,
props: {
title: input.title,
markdown: input.markdown,
sourceUrl: input.sourceUrl,
sourceId: input.sourceId || '',
favoriteType: input.favorite?.type || '',
summary: input.favorite?.summary || '',
metadata: JSON.stringify(input.favorite?.metadata || {}),
expanded: false,
},
}
}
interface ClipEditor {
transact: <T>(callback: (tr: Transaction) => T) => T
getTextCursorPosition: () => { block: { id: string } }
insertBlocks: (
blocks: ReturnType<typeof noteClip>[],
id: string,
side: 'after'
) => { id: string }[]
setTextCursorPosition: (id: string, placement: 'end') => void
}
/** Preserve list order and nesting; a whole picker submission is one undo step. */
export function insertNoteClips(editor: ClipEditor, inputs: NoteClipInput[]) {
if (!inputs.length) return
const anchor = editor.getTextCursorPosition().block.id
editor.transact(tr => {
closeHistory(tr)
const inserted = editor.insertBlocks(inputs.map(noteClip), anchor, 'after')
const last = inserted.at(-1)
if (last) editor.setTextCursorPosition(last.id, 'end')
})
editor.transact(tr => closeHistory(tr))
}