diff --git a/app/editor/extensions/DragHandle.ts b/app/editor/extensions/DragHandle.ts index 24accb654b..bea92000a6 100644 --- a/app/editor/extensions/DragHandle.ts +++ b/app/editor/extensions/DragHandle.ts @@ -61,8 +61,8 @@ export default class DragHandle extends Extension { return meta.pos; } if (value !== null && tr.docChanged) { - const newPos = tr.mapping.map(value); - return tr.doc.nodeAt(newPos)?.type.name === "heading" + const { pos: newPos, deleted } = tr.mapping.mapResult(value, 1); + return !deleted && tr.doc.nodeAt(newPos)?.type.name === "heading" ? newPos : null; } @@ -154,13 +154,15 @@ export default class DragHandle extends Extension { const onDragEnd = () => { view.dom.classList.remove("dragging"); + // The handle is outside the editor DOM, so ProseMirror's own + // dragend cleanup does not run for it. + view.dragging = null; if (pluginKey.getState(view.state)) { view.dispatch(view.state.tr.setMeta(META_KEY, { pos: null })); } }; - const show = (next: Target) => { - target = next; + const position = (next: Target) => { const rect = next.element.getBoundingClientRect(); const offsetX = next.isCheckboxItem ? 0 : next.isListItem ? 40 : 24; const offsetY = 2; @@ -184,6 +186,11 @@ export default class DragHandle extends Extension { : `${rect.left - offsetX}px`; handle.style.opacity = "1"; handle.style.pointerEvents = "auto"; + }; + + const show = (next: Target) => { + target = next; + position(next); setHoveredHeading( view.state.doc.nodeAt(next.pos)?.type.name === "heading" ? next.pos @@ -191,10 +198,14 @@ export default class DragHandle extends Extension { ); }; - const hide = () => { + const hideHandle = () => { target = null; handle.style.opacity = "0"; handle.style.pointerEvents = "none"; + }; + + const hide = () => { + hideHandle(); setHoveredHeading(null); }; @@ -331,6 +342,22 @@ export default class DragHandle extends Extension { handle.addEventListener("click", onClick); return { + update: (_view, prevState) => { + if (!target || view.state.doc === prevState.doc) { + return; + } + // Positions shift when the document changes, e.g. from a + // collaborator's edit, so resolve the target again from its DOM. + const pos = resolveElementPos(view, target.element); + if (pos === null) { + hideHandle(); + return; + } + target = { ...target, pos }; + if (!pluginKey.getState(view.state)) { + position(target); + } + }, destroy: () => { window.removeEventListener("mousemove", onMouseMove); window.removeEventListener("scroll", onScroll, { capture: true }); @@ -442,6 +469,51 @@ function findTarget(view: EditorView, event: MouseEvent): Target | null { }; } +function resolveElementPos( + view: EditorView, + element: HTMLElement +): number | null { + if (!element.isConnected) { + return null; + } + if (element.classList.contains("mermaid-diagram-wrapper")) { + return resolveMermaidPos(view, element); + } + const parent = element.parentNode; + if (!parent) { + return null; + } + let pos: number; + try { + pos = view.posAtDOM(parent, Array.from(parent.childNodes).indexOf(element)); + } catch { + return null; + } + return view.nodeDOM(pos) === element ? pos : null; +} + +function resolveMermaidPos( + view: EditorView, + diagram: HTMLElement +): number | null { + const codeBlockDom = diagram.previousElementSibling; + if (!(codeBlockDom instanceof HTMLElement)) { + return null; + } + let pos: number; + try { + pos = view.posAtDOM(codeBlockDom, 0); + } catch { + return null; + } + if (pos < 0) { + return null; + } + const $pos = view.state.doc.resolve(pos); + const blockPos = $pos.depth > 0 ? $pos.before($pos.depth) : 0; + return view.state.doc.nodeAt(blockPos) ? blockPos : null; +} + function findMermaidTarget( view: EditorView, event: MouseEvent, @@ -463,22 +535,8 @@ function findMermaidTarget( if (!diagram) { return null; } - const codeBlockDom = diagram.previousElementSibling; - if (!(codeBlockDom instanceof HTMLElement)) { - return null; - } - let pos: number; - try { - pos = view.posAtDOM(codeBlockDom, 0); - } catch { - return null; - } - if (pos < 0) { - return null; - } - const $pos = view.state.doc.resolve(pos); - const blockPos = $pos.depth > 0 ? $pos.before($pos.depth) : 0; - if (!view.state.doc.nodeAt(blockPos)) { + const blockPos = resolveMermaidPos(view, diagram); + if (blockPos === null) { return null; } return { diff --git a/shared/editor/components/Styles.ts b/shared/editor/components/Styles.ts index b240b2fc4e..8d39c3d1ce 100644 --- a/shared/editor/components/Styles.ts +++ b/shared/editor/components/Styles.ts @@ -1614,11 +1614,6 @@ ul.checkbox_list > li.checked > div > p { color: ${props.theme.textTertiary}; } -ul li.ProseMirror-selectednode::after, -ol li.ProseMirror-selectednode::after { - display: none; -} - ul.checkbox_list { & > li > span[contenteditable="false"] { cursor: text; @@ -2635,13 +2630,6 @@ table { animation: ProseMirror-cursor-blink 1.1s steps(2, start) infinite; } - -.folded-content, -.folded-content + .mermaid-diagram-wrapper { - display: none; - user-select: none; -} - @keyframes ProseMirror-cursor-blink { to { visibility: hidden; diff --git a/shared/editor/extensions/Mermaid.ts b/shared/editor/extensions/Mermaid.ts index 76c1888d6e..63795e1e15 100644 --- a/shared/editor/extensions/Mermaid.ts +++ b/shared/editor/extensions/Mermaid.ts @@ -416,9 +416,9 @@ export default function Mermaid({ const node = state.selection.$head.parent; const previousNode = oldState.selection.$head.parent; - // For a NodeSelection on a top-level code_fence, $head.parent - // resolves to the doc rather than the code_fence — so also inspect - // the selected node directly to catch e.g. drag-and-drop reordering. + // For a NodeSelection, $head.parent resolves to the selection's + // parent — so also inspect the selected node and its descendants to + // catch e.g. drag-and-drop of a container that holds a diagram. const selectedNode = state.selection instanceof NodeSelection ? state.selection.node @@ -431,8 +431,8 @@ export default function Mermaid({ transaction.docChanged && (isMermaid(node) || isMermaid(previousNode) || - (!!selectedNode && isMermaid(selectedNode)) || - (!!previousSelectedNode && isMermaid(previousSelectedNode))); + containsMermaid(selectedNode) || + containsMermaid(previousSelectedNode)); // @ts-expect-error accessing private field. const isPaste = transaction.meta?.paste; @@ -620,3 +620,21 @@ export default function Mermaid({ }, }); } + +function containsMermaid(node: Node | null): boolean { + if (!node) { + return false; + } + if (isMermaid(node)) { + return true; + } + let found = false; + node.descendants((child) => { + if (found) { + return false; + } + found = isMermaid(child); + return !found; + }); + return found; +}