import type { TopLevelBlockDrop } from './top-level-block-drop' import type { Editor } from '@tiptap/react' // Resolves the slot under the pointer while a top-level block is dragged: every other block whose // middle sits above the pointer goes before it, and the drop line sits in the gap that leaves. export function getTopLevelBlockDrop( editor: Editor, container: HTMLElement, fromIndex: number, pointerTop: number ): TopLevelBlockDrop | null { const containerTop = container.getBoundingClientRect().top const rects: { top: number; bottom: number }[] = [] editor.state.doc.forEach((_node, offset, index) => { if (index === fromIndex) return const element = editor.view.nodeDOM(offset) if (!(element instanceof HTMLElement)) return const rect = element.getBoundingClientRect() rects.push({ top: rect.top - containerTop, bottom: rect.bottom - containerTop }) }) if (rects.length === 0) return null const index = rects.filter((rect) => (rect.top + rect.bottom) / 2 < pointerTop).length const before = rects[index - 1] const after = rects[index] const top = before && after ? (before.bottom + after.top) / 2 : before ? before.bottom + 4 : after.top - 4 return { index, top } }