Align drag handle to first line, hide on layout images, tidy heading controls

This commit is contained in:
Tom Moor committed 2026-10-09 23:15:12 -04:00
1 parent 990c0268e4
commit 2662a4a43b
3 files changed
+149 -22

No files matched your search

+128 -8
View File
@@ -1,3 +1,4 @@
import type { Node as ProsemirrorNode } from "prosemirror-model";
import type { EditorState } from "prosemirror-state";
import { NodeSelection, Plugin, PluginKey } from "prosemirror-state";
import type { EditorView } from "prosemirror-view";
@@ -7,6 +8,8 @@ import { findParentNodeClosestToPos } from "@shared/editor/queries/findParentNod
const HANDLE_CLASS = "block-drag-handle";
const HANDLE_SIZE = 24;
// Vertical center of the icon within the handle, including its 2px offset.
const ICON_CENTER_Y = HANDLE_SIZE / 2 + 2;
const META_KEY = "drag-handle";
const LIST_ITEM_TYPES = ["list_item", "checkbox_item"];
const LIST_TYPES = ["bullet_list", "ordered_list", "checkbox_list"];
@@ -27,6 +30,8 @@ type PluginState = {
} | null;
const pluginKey = new PluginKey<PluginState>(META_KEY);
const HOVER_META_KEY = "drag-handle-hover";
const hoverPluginKey = new PluginKey<number | null>(HOVER_META_KEY);
/**
* Renders a floating drag handle to the left of block-level nodes when the
@@ -42,6 +47,43 @@ export default class DragHandle extends Extension {
get plugins() {
return [
// Marks the heading that owns the handle, so its level label in the
// gutter can be hidden.
new Plugin<number | null>({
key: hoverPluginKey,
state: {
init: () => null,
apply: (tr, value) => {
const meta = tr.getMeta(HOVER_META_KEY) as
| { pos: number | null }
| undefined;
if (meta) {
return meta.pos;
}
if (value !== null && tr.docChanged) {
const newPos = tr.mapping.map(value);
return tr.doc.nodeAt(newPos)?.type.name === "heading"
? newPos
: null;
}
return value;
},
},
props: {
decorations: (state) => {
const pos = hoverPluginKey.getState(state) ?? null;
const node = pos === null ? null : state.doc.nodeAt(pos);
if (pos === null || !node) {
return DecorationSet.empty;
}
return DecorationSet.create(state.doc, [
Decoration.node(pos, pos + node.nodeSize, {
class: "drag-handle-target",
}),
]);
},
},
}),
new Plugin<PluginState>({
key: pluginKey,
state: {
@@ -122,23 +164,45 @@ export default class DragHandle extends Extension {
const rect = next.element.getBoundingClientRect();
const offsetX = next.isCheckboxItem ? 0 : next.isListItem ? 40 : 24;
const offsetY = 2;
// Center the icon on the first line of text when the block has
// one, otherwise align it with the top of the block.
const lineMiddle =
next.element === view.nodeDOM(next.pos)
? getFirstLineMiddle(view, next.pos, rect)
: null;
// RTL blocks lay out their gutter on the right, so mirror the
// handle to the opposite edge to match the rest of the editor's
// :dir(rtl) handling.
const isRTL =
window.getComputedStyle(next.element).direction === "rtl";
handle.style.top = `${rect.top - offsetY}px`;
handle.style.top =
lineMiddle === null
? `${rect.top - offsetY}px`
: `${lineMiddle - ICON_CENTER_Y}px`;
handle.style.left = isRTL
? `${rect.right + offsetX - HANDLE_SIZE}px`
: `${rect.left - offsetX}px`;
handle.style.opacity = "1";
handle.style.pointerEvents = "auto";
setHoveredHeading(
view.state.doc.nodeAt(next.pos)?.type.name === "heading"
? next.pos
: null
);
};
const hide = () => {
target = null;
handle.style.opacity = "0";
handle.style.pointerEvents = "none";
setHoveredHeading(null);
};
const setHoveredHeading = (pos: number | null) => {
if ((hoverPluginKey.getState(view.state) ?? null) === pos) {
return;
}
view.dispatch(view.state.tr.setMeta(HOVER_META_KEY, { pos }));
};
const onMouseMove = (event: MouseEvent) => {
@@ -150,7 +214,7 @@ export default class DragHandle extends Extension {
// target. Re-resolving from a cursor in the gutter can land on
// a different (often parent) block, causing the handle to
// flicker between the hovered block and its parent.
if (isOverElement(handle, event)) {
if (target && isOverElement(handle, event)) {
return;
}
const next = findTarget(view, event);
@@ -293,21 +357,22 @@ function createHandle(): HTMLElement {
handle.draggable = true;
handle.contentEditable = "false";
handle.setAttribute("aria-label", "Drag to reorder");
// Reset the native button chrome so only the icon background shows.
// Reset the native button chrome so only the icon shows. The icon is a
// mask, so its color comes from the global background-color rule.
handle.style.appearance = "none";
handle.style.border = "0";
handle.style.padding = "0";
handle.style.backgroundColor = "transparent";
handle.style.position = "fixed";
handle.style.width = `${HANDLE_SIZE}px`;
handle.style.height = `${HANDLE_SIZE}px`;
handle.style.cursor = "grab";
handle.style.opacity = "0";
handle.style.pointerEvents = "none";
handle.style.transition = "opacity 150ms ease-in-out";
handle.style.backgroundImage = `url("${HANDLE_ICON}")`;
handle.style.backgroundRepeat = "no-repeat";
handle.style.backgroundPosition = "0 2px";
handle.style.transition =
"opacity 150ms ease-in-out, background-color 150ms ease-in-out";
handle.style.maskImage = `url("${HANDLE_ICON}")`;
handle.style.maskRepeat = "no-repeat";
handle.style.maskPosition = "0 2px";
handle.style.zIndex = "1";
return handle;
}
@@ -359,6 +424,12 @@ function findTarget(view: EditorView, event: MouseEvent): Target | null {
if (resolved === null) {
return null;
}
// Floated and full-width images sit outside the content column, so a
// handle in the gutter would not line up with them.
const node = view.state.doc.nodeAt(resolved.pos);
if (node && hasLayoutImage(node)) {
return null;
}
const dom = view.nodeDOM(resolved.pos);
if (!(dom instanceof HTMLElement)) {
return null;
@@ -474,3 +545,52 @@ function resolveTargetPos(
}
return null;
}
function hasLayoutImage(node: ProsemirrorNode): boolean {
if (!node.isTextblock) {
return false;
}
let found = false;
node.forEach((child) => {
if (child.type.name === "image" && child.attrs.layoutClass) {
found = true;
}
});
return found;
}
function getFirstLineMiddle(
view: EditorView,
pos: number,
rect: DOMRect
): number | null {
// Descend to the first textblock, and only use it when it begins with
// text – inline atoms such as images have no meaningful line.
let node = view.state.doc.nodeAt(pos);
let nodePos = pos;
while (node && !node.isTextblock) {
node = node.firstChild;
nodePos += 1;
}
if (!node?.firstChild?.isText) {
return null;
}
const textPos = nodePos + 1;
let coords: { top: number; bottom: number };
try {
coords = view.coordsAtPos(textPos, 1);
} catch {
return null;
}
// Ignore text that is hidden or rendered outside the block, such as the
// source of a rendered math block.
if (
coords.bottom <= coords.top ||
coords.top < rect.top - 1 ||
coords.bottom > rect.bottom + 1
) {
return null;
}
return (coords.top + coords.bottom) / 2;
}
+12 -13
View File
@@ -722,13 +722,10 @@ width: 100%;
}
}
&:hover {
&:hover,
&.drag-handle-target {
.heading-anchor {
opacity: 0.75;
&:hover {
opacity: 1;
}
opacity: 1;
}
}
}
@@ -1156,7 +1153,8 @@ h6:not(.placeholder):not([data-heading-prefix])::before {
&:not(.placeholder):not([data-heading-prefix])::before {
opacity: 1;
}
&:hover:not(.placeholder):not([data-heading-prefix])::before {
&:hover:not(.placeholder):not([data-heading-prefix])::before,
&.drag-handle-target:not(.placeholder):not([data-heading-prefix])::before {
opacity: 0;
}
}
@@ -1215,15 +1213,15 @@ h6:not(.placeholder):not([data-heading-prefix])::before {
left: 0;
top: 0;
bottom: 0;
/* Offset further left than the default so the anchor clears the block
drag handle, which occupies the gutter immediately left of the heading. */
margin-left: -52px;
width: 26px;
/* Sit immediately left of the block drag handle in the gutter. */
margin-left: -38px;
width: 18px;
align-items: center;
justify-content: center;
opacity: 0;
user-select: none;
color: ${props.theme.text};
color: ${props.theme.textTertiary};
transition: color 150ms ease-in-out;
cursor: var(--pointer);
background: none;
outline: none;
@@ -1237,11 +1235,12 @@ h6:not(.placeholder):not([data-heading-prefix])::before {
&:hover {
opacity: 1;
color: ${props.theme.text};
}
&:dir(rtl) {
margin-left: 0;
margin-right: -52px;
margin-right: -38px;
}
}
+9 -1
View File
@@ -176,7 +176,15 @@ export default createGlobalStyle<Props>`
}
/* Block drag handle is appended to document.body, outside the styled
EditorContainer scope, so the print rule has to live globally. */
EditorContainer scope, so its rules have to live globally. */
.block-drag-handle {
background-color: ${s("textTertiary")};
&:hover {
background-color: ${s("text")};
}
}
@media print {
.block-drag-handle {
display: none !important;