This commit is contained in:
Timothy Jaeryang Baek committed 2026-10-31 17:16:19 +09:00
1 parent 5f45546809
commit 3bb962f2bb
4 files changed
+226 -16

No files matched your search

@@ -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<import('prosemirror-view').EditorView, 'state' | 'dispatch' | 'editable' | 'focus'>} 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;
},