'use client' import * as React from 'react' import type { TopLevelBlockDrop } from '@admin/utils/editor/top-level-block-drop' import type { Editor } from '@tiptap/react' import { getTopLevelBlockDrop } from '@admin/utils/editor/get-top-level-block-drop' import { moveTopLevelBlockTo } from '@admin/utils/editor/move-top-level-block-to' const EDGE_SCROLL_PX = 48 const EDGE_SCROLL_STEP_PX = 12 const DRAG_THRESHOLD_PX = 6 interface BlockDragSession { container: HTMLElement fromIndex: number startY: number grabOffset: number isDragging: boolean drop: TopLevelBlockDrop | null } // The phone focus view's block handle (Paper `Posts / New · Writing · Block / Mobile`): a tap calls // `onTap` (the dock switches to block actions), a press that travels past 6px drags the block. The // handle captures the pointer, follows the finger, and shows a drop line between blocks; near the // top or the visible bottom (above the keyboard) the editor scrolls. Releasing moves the block and // hands focus back to the editor. export function useContentEditorBlockDrag(editor: Editor, onTap: () => void) { const sessionRef = React.useRef(null) const [drag, setDrag] = React.useState<{ handleTop: number; dropTop: number | null } | null>(null) const update = (clientY: number) => { const session = sessionRef.current if (!session) return if (!session.isDragging && Math.abs(clientY - session.startY) < DRAG_THRESHOLD_PX) return session.isDragging = true const scroller = editor.view.dom.parentElement if (scroller) { const rect = scroller.getBoundingClientRect() const visibleBottom = Math.min( rect.bottom, (window.visualViewport?.offsetTop ?? 0) + (window.visualViewport?.height ?? rect.bottom) ) if (clientY < rect.top + EDGE_SCROLL_PX) scroller.scrollBy(0, -EDGE_SCROLL_STEP_PX) else if (clientY > visibleBottom - EDGE_SCROLL_PX) scroller.scrollBy(0, EDGE_SCROLL_STEP_PX) } const pointerTop = clientY - session.container.getBoundingClientRect().top session.drop = getTopLevelBlockDrop(editor, session.container, session.fromIndex, pointerTop) setDrag({ handleTop: pointerTop - session.grabOffset, dropTop: session.drop?.top ?? null }) } const end = (commit: boolean) => { const session = sessionRef.current sessionRef.current = null setDrag(null) if (commit && session && !session.isDragging) onTap() if (commit && session?.isDragging && session.drop) { moveTopLevelBlockTo(editor, session.fromIndex, session.drop.index) } editor.view.focus() } const onPointerDown = (event: React.PointerEvent) => { event.preventDefault() const container = event.currentTarget.parentElement if (!container) return event.currentTarget.setPointerCapture(event.pointerId) const handleRect = event.currentTarget.getBoundingClientRect() sessionRef.current = { container, fromIndex: editor.state.selection.$from.index(0), startY: event.clientY, grabOffset: event.clientY - handleRect.top, isDragging: false, drop: null } } const onPointerMove = (event: React.PointerEvent) => update(event.clientY) const onPointerUp = () => end(true) const onPointerCancel = () => end(false) return { drag, onPointerDown, onPointerMove, onPointerUp, onPointerCancel } }