File
Blob: src/client/components/editor/lib/block-drag-state.ts
| 1 | import { NodeSelection } from "@tiptap/pm/state"; |
| 2 | import type { EditorView } from "@tiptap/pm/view"; |
| 3 | |
| 4 | type InternalDraggingState = NonNullable<EditorView["dragging"]> & { |
| 5 | node?: NodeSelection; |
| 6 | }; |
| 7 | |
| 8 | type BlockDragView = Pick<EditorView, "dispatch" | "state"> & { |
| 9 | dragging: InternalDraggingState | null; |
| 10 | }; |
| 11 | |
| 12 | export function primeTopLevelBlockDragState(view: BlockDragView, pos: number): boolean { |
| 13 | if (pos < 0 || pos > view.state.doc.content.size) { |
| 14 | return false; |
| 15 | } |
| 16 | |
| 17 | const node = view.state.doc.nodeAt(pos); |
| 18 | if (!node || !NodeSelection.isSelectable(node)) { |
| 19 | return false; |
| 20 | } |
| 21 | |
| 22 | const selection = NodeSelection.create(view.state.doc, pos); |
| 23 | view.dragging = { |
| 24 | slice: view.state.doc.slice(pos, pos + node.nodeSize), |
| 25 | move: true, |
| 26 | node: selection, |
| 27 | }; |
| 28 | view.dispatch(view.state.tr.setSelection(selection)); |
| 29 | return true; |
| 30 | } |