From 3bb962f2bbd82ebe8ccad6f64304956aee2f4f97 Mon Sep 17 00:00:00 2001 From: Timothy Jaeryang Baek Date: Sat, 31 Oct 2026 17:16:19 +0900 Subject: [PATCH] refac --- .../components/common/RichTextInput.svelte | 5 + .../common/RichTextInput/BlockHandle.svelte | 140 ++++++++++++++++++ .../RichTextInput/listDragHandlePlugin.js | 95 ++++++++++-- src/lib/components/notes/NoteEditor.svelte | 2 +- 4 files changed, 226 insertions(+), 16 deletions(-) create mode 100644 src/lib/components/common/RichTextInput/BlockHandle.svelte diff --git a/src/lib/components/common/RichTextInput.svelte b/src/lib/components/common/RichTextInput.svelte index 108cae2582..1cd1f3f841 100644 --- a/src/lib/components/common/RichTextInput.svelte +++ b/src/lib/components/common/RichTextInput.svelte @@ -834,6 +834,7 @@ }); import { listDragHandlePlugin } from './RichTextInput/listDragHandlePlugin.js'; + import BlockHandle from './RichTextInput/BlockHandle.svelte'; const ListItemDragHandle = Extension.create({ name: 'listItemDragHandle', @@ -1549,3 +1550,7 @@ dir="auto" class="relative w-full min-w-full {className} {!editable ? 'cursor-not-allowed' : ''}" /> + +{#if dragHandle && editor && editable} + +{/if} diff --git a/src/lib/components/common/RichTextInput/BlockHandle.svelte b/src/lib/components/common/RichTextInput/BlockHandle.svelte new file mode 100644 index 0000000000..efdde8f442 --- /dev/null +++ b/src/lib/components/common/RichTextInput/BlockHandle.svelte @@ -0,0 +1,140 @@ + + +
+ + + + +
+ + + + +
+
+
diff --git a/src/lib/components/common/RichTextInput/listDragHandlePlugin.js b/src/lib/components/common/RichTextInput/listDragHandlePlugin.js index dace30197f..d0ce93f380 100644 --- a/src/lib/components/common/RichTextInput/listDragHandlePlugin.js +++ b/src/lib/components/common/RichTextInput/listDragHandlePlugin.js @@ -1,10 +1,35 @@ import { Plugin, PluginKey, NodeSelection } from 'prosemirror-state'; import { Decoration, DecorationSet } from 'prosemirror-view'; import { Fragment } from 'prosemirror-model'; +import { closeHistory } from 'prosemirror-history'; +import { yUndoPluginKey } from 'y-prosemirror'; export const listPointerDragKey = new PluginKey('listPointerDrag'); +/** + * @param {Pick} view + * @param {number} from + * @param {number} to + */ +export function moveBlock(view, from, to) { + if (!view.editable) return false; + const { state } = view; + const node = state.doc.nodeAt(from); + if (!node || state.doc.resolve(from).depth || state.doc.resolve(to).depth) return false; + if (to >= from && to <= from + node.nodeSize) return false; + const tr = state.tr.delete(from, from + node.nodeSize); + const target = tr.mapping.map(to); + tr.insert(target, node).setSelection(NodeSelection.create(tr.doc, target)); + const undoManager = yUndoPluginKey.getState(state)?.undoManager; + undoManager?.stopCapturing(); + view.dispatch(closeHistory(tr).scrollIntoView()); + undoManager?.stopCapturing(); + view.focus(); + return true; +} + export function listDragHandlePlugin(options = {}) { + let cleanupDrag = () => {}; const { itemTypeNames = ['listItem', 'taskItem', 'list_item'], @@ -206,10 +231,19 @@ export function listDragHandlePlugin(options = {}) { } function infoFromCoords(view, clientX, clientY) { - const result = view.posAtCoords({ left: clientX, top: clientY }); + const block = listPointerDragKey.getState(view.state)?.dragging?.block; + const bounds = view.dom.getBoundingClientRect(); + const result = view.posAtCoords({ + left: block ? Math.max(bounds.left + 1, Math.min(clientX, bounds.right - 1)) : clientX, + top: clientY + }); if (!result) return null; const $pos = view.state.doc.resolve(result.pos); - const li = findListItemAround($pos); + const start = $pos.depth ? $pos.before(1) : Math.min(result.pos, view.state.doc.content.size); + const node = view.state.doc.nodeAt(start); + const li = block + ? node && { node, start, end: start + node.nodeSize } + : findListItemAround($pos); if (!li) return null; const dom = /** @type {Element} */ (view.nodeDOM(li.start)); @@ -220,8 +254,11 @@ export function listDragHandlePlugin(options = {}) { const xFromLeft = isRTL ? rect.right - clientX : clientX - rect.left; const yInTopHalf = clientY - rect.top < rect.height / 2; - const mode = - xFromLeft <= outdentThresholdX + const mode = block + ? yInTopHalf + ? 'before' + : 'after' + : xFromLeft <= outdentThresholdX ? 'outdent' : xFromLeft >= intoThresholdX ? 'into' @@ -244,6 +281,8 @@ export function listDragHandlePlugin(options = {}) { ? buildHandleDecos(tr.doc) : prev.decorations.map(tr.mapping, tr.doc); let next = { ...prev, decorations }; + // Yjs can replace the entire document. Never drop using positions from before an edit. + if (tr.docChanged) next = { ...next, dragging: null, dropTarget: null }; const meta = tr.getMeta(listPointerDragKey); if (meta) { if (meta.type === 'set-drag') next = { ...next, dragging: meta.dragging }; @@ -254,6 +293,7 @@ export function listDragHandlePlugin(options = {}) { }; const decorationsProp = (state) => { + if (getEditor?.()?.isEditable === false) return null; const ps = listPointerDragKey.getState(state); if (!ps) return null; let deco = ps.decorations; @@ -386,20 +426,30 @@ export function listDragHandlePlugin(options = {}) { return new Plugin({ key: listPointerDragKey, state: { init: (_, state) => init(state), apply }, + view: () => ({ + update(view) { + if (!listPointerDragKey.getState(view.state)?.dragging || !view.editable) cleanupDrag(); + }, + destroy() { + cleanupDrag(); + } + }), props: { decorations: decorationsProp, handleDOMEvents: { mousedown(view, event) { + if (!view.editable || event.button !== 0) return false; const t = /** @type {HTMLElement} */ (event.target); - const handle = t.closest?.(`.${classHandle}`); + const handle = t.closest?.(`.${classHandle}, .pm-block-drag-handle`); if (!handle) return false; + cleanupDrag(); event.preventDefault(); + const block = handle.classList.contains('pm-block-drag-handle'); const getPos = handle.pmGetPos; - if (typeof getPos !== 'function') return true; + if (!block && typeof getPos !== 'function') return true; - const posInside = getPos(); - const fromStart = posInside - 1; + const fromStart = block ? Number(handle.getAttribute('data-block-pos')) : getPos() - 1; try { view.dispatch( @@ -409,7 +459,7 @@ export function listDragHandlePlugin(options = {}) { const startMouse = { x: event.clientX, y: event.clientY }; const ghostEl = ensureGhost(view, fromStart); - setDrag(view, { fromStart, startMouse, ghostEl, active: false }); + setDrag(view, { fromStart, startMouse, ghostEl, active: false, block }); const onMove = (e) => { const ps = listPointerDragKey.getState(view.state); @@ -443,11 +493,8 @@ export function listDragHandlePlugin(options = {}) { }; const endDrag = () => { - window.removeEventListener('mousemove', onMove, true); - window.removeEventListener('mouseup', endDrag, true); - const ps = listPointerDragKey.getState(view.state); - if (ps?.dragging?.ghostEl) ps.dragging.ghostEl.remove(); + cleanupDrag(); // Helper: figure out the list item node type name at/around a pos const getListItemTypeNameAt = (doc, pos) => { @@ -465,11 +512,13 @@ export function listDragHandlePlugin(options = {}) { return Array.from(itemTypesSet)[0]; }; - if (ps?.dragging && ps?.dropTarget && ps.dragging.active) { + if (view.editable && ps?.dragging && ps?.dropTarget && ps.dragging.active) { const { fromStart } = ps.dragging; const { toPos, mode } = ps.dropTarget; - const res = moveItem(view, fromStart, toPos); + const res = block + ? { ok: moveBlock(view, fromStart, toPos) } + : moveItem(view, fromStart, toPos); if (res.ok && typeof res.newStart === 'number' && getEditor) { const editor = getEditor(); @@ -493,8 +542,24 @@ export function listDragHandlePlugin(options = {}) { clearAll(view); }; + /** @param {KeyboardEvent | FocusEvent} event */ + const cancelDrag = (event) => { + if ('key' in event && event.key !== 'Escape') return; + cleanupDrag(); + clearAll(view); + }; + cleanupDrag = () => { + ghostEl.remove(); + window.removeEventListener('mousemove', onMove, true); + window.removeEventListener('mouseup', endDrag, true); + window.removeEventListener('keydown', cancelDrag, true); + window.removeEventListener('blur', cancelDrag); + cleanupDrag = () => {}; + }; window.addEventListener('mousemove', onMove, true); window.addEventListener('mouseup', endDrag, true); + window.addEventListener('keydown', cancelDrag, true); + window.addEventListener('blur', cancelDrag); return true; }, diff --git a/src/lib/components/notes/NoteEditor.svelte b/src/lib/components/notes/NoteEditor.svelte index de8ae05f43..ac4ef7dbf3 100644 --- a/src/lib/components/notes/NoteEditor.svelte +++ b/src/lib/components/notes/NoteEditor.svelte @@ -1578,7 +1578,7 @@ ${content} bind:this={inputElement} on:editor={(event) => (editor = event.detail)} id={`note-${note.id}-${renderedPage.id}`} - className="input-prose-sm px-0.5 flex-1 min-h-[12rem]" + className="input-prose-sm ps-4 pe-0.5 pt-2 flex-1 min-h-[12rem]" json={true} bind:value={renderedPage.content.json} html={editorHtml}