Use EditorStyleHelper class names and named constants in drag handle, translate its label

This commit is contained in:
Tom Moor committed 2026-10-09 23:51:33 -04:00
1 parent dd9d215c76
commit 1824dfee7f
8 files changed
+72 -33

No files matched your search

+15 -11
View File
@@ -1,14 +1,19 @@
import { t } from "i18next";
import { NodeSelection, Plugin, PluginKey } from "prosemirror-state";
import type { EditorView } from "prosemirror-view";
import { Decoration, DecorationSet } from "prosemirror-view";
import Extension from "@shared/editor/lib/Extension";
import { EditorStyleHelper } from "@shared/editor/styles/EditorStyleHelper";
import type { DragTargetKind } from "~/editor/plugins/DragTarget";
import { DragTarget } from "~/editor/plugins/DragTarget";
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;
// Vertical offset of the icon within the handle, matches its mask-position.
const ICON_OFFSET_Y = 2;
// Vertical center of the icon within the handle.
const ICON_CENTER_Y = HANDLE_SIZE / 2 + ICON_OFFSET_Y;
// Tolerance when checking that text is rendered inside its block.
const RECT_TOLERANCE = 1;
const META_KEY = "drag-handle";
// Horizontal distance from the block's edge to the handle, per kind of block.
const GUTTER_OFFSET: Record<DragTargetKind, number> = {
@@ -71,7 +76,7 @@ export default class DragHandle extends Extension {
}
return DecorationSet.create(state.doc, [
Decoration.node(pos, pos + node.nodeSize, {
class: "drag-handle-target",
class: EditorStyleHelper.dragHandleTarget,
}),
]);
},
@@ -114,7 +119,7 @@ export default class DragHandle extends Extension {
}
return DecorationSet.create(state.doc, [
Decoration.node(dragState.pos, dragState.pos + dragState.size, {
class: "dragging-source",
class: EditorStyleHelper.draggingSource,
}),
]);
},
@@ -158,7 +163,6 @@ export default class DragHandle extends Extension {
const position = (next: DragTarget) => {
const rect = next.element.getBoundingClientRect();
const offsetX = GUTTER_OFFSET[next.kind];
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 =
@@ -172,7 +176,7 @@ export default class DragHandle extends Extension {
window.getComputedStyle(next.element).direction === "rtl";
handle.style.top =
lineMiddle === null
? `${rect.top - offsetY}px`
? `${rect.top - ICON_OFFSET_Y}px`
: `${lineMiddle - ICON_CENTER_Y}px`;
handle.style.left = isRTL
? `${rect.right + offsetX - HANDLE_SIZE}px`
@@ -368,10 +372,10 @@ export default class DragHandle extends Extension {
function createHandle(): HTMLElement {
const handle = document.createElement("button");
handle.type = "button";
handle.className = HANDLE_CLASS;
handle.className = EditorStyleHelper.blockDragHandle;
handle.draggable = true;
handle.contentEditable = "false";
handle.setAttribute("aria-label", "Drag to reorder");
handle.setAttribute("aria-label", t("Drag to reorder"));
return handle;
}
@@ -413,8 +417,8 @@ function getFirstLineMiddle(
// source of a rendered math block.
if (
coords.bottom <= coords.top ||
coords.top < rect.top - 1 ||
coords.bottom > rect.bottom + 1
coords.top < rect.top - RECT_TOLERANCE ||
coords.bottom > rect.bottom + RECT_TOLERANCE
) {
return null;
}
+19 -7
View File
@@ -2,6 +2,7 @@ import type { Node as ProsemirrorNode } from "prosemirror-model";
import type { EditorState } from "prosemirror-state";
import type { EditorView } from "prosemirror-view";
import { findParentNodeClosestToPos } from "@shared/editor/queries/findParentNode";
import { EditorStyleHelper } from "@shared/editor/styles/EditorStyleHelper";
/** The kind of block that a drag target points at. */
export type DragTargetKind = "block" | "listItem" | "checkboxItem";
@@ -15,6 +16,10 @@ export interface ResolvedDragTarget {
}
const LIST_TYPES = ["bullet_list", "ordered_list", "checkbox_list"];
// Distance outside the content column in which the cursor still finds a block.
const GUTTER_HIT_WIDTH = 60;
// Inset from the content edges, so that posAtCoords lands inside the block.
const CONTENT_INSET = 1;
/**
* A block in the editor that the drag handle can attach to. Holds the
@@ -56,9 +61,12 @@ export class DragTarget {
const style = window.getComputedStyle(view.dom);
const paddingLeft = parseFloat(style.paddingLeft) || 0;
const paddingRight = parseFloat(style.paddingRight) || 0;
const contentLeft = rect.left + paddingLeft + 1;
const contentRight = rect.right - paddingRight - 1;
if (event.clientX < contentLeft - 60 || event.clientX > contentRight + 60) {
const contentLeft = rect.left + paddingLeft + CONTENT_INSET;
const contentRight = rect.right - paddingRight - CONTENT_INSET;
if (
event.clientX < contentLeft - GUTTER_HIT_WIDTH ||
event.clientX > contentRight + GUTTER_HIT_WIDTH
) {
return null;
}
const projectedX = Math.max(
@@ -107,7 +115,7 @@ export class DragTarget {
if (!element.isConnected) {
return null;
}
if (element.classList.contains("mermaid-diagram-wrapper")) {
if (element.classList.contains(EditorStyleHelper.mermaidDiagram)) {
const pos = this.mermaidPos(view, element);
return pos === null ? null : new DragTarget(pos, element, "block");
}
@@ -227,15 +235,19 @@ export class DragTarget {
// Look at the element directly under the cursor, and at the projected X
// inside the editor so we still find the diagram when the cursor sits in
// the left gutter where the handle is rendered.
const eventTarget = event.target as HTMLElement | null;
const eventTarget = event.target instanceof Element ? event.target : null;
const projectedTarget =
projectedX !== event.clientX
? document.elementFromPoint(projectedX, event.clientY)
: null;
const diagram =
eventTarget?.closest<HTMLElement>(".mermaid-diagram-wrapper") ??
eventTarget?.closest<HTMLElement>(
`.${EditorStyleHelper.mermaidDiagram}`
) ??
(projectedTarget instanceof Element
? projectedTarget.closest<HTMLElement>(".mermaid-diagram-wrapper")
? projectedTarget.closest<HTMLElement>(
`.${EditorStyleHelper.mermaidDiagram}`
)
: null);
if (!diagram) {
return null;
@@ -5,6 +5,7 @@ import { ShrinkIcon, GrowIcon, CloseIcon } from "outline-icons";
import styled, { useTheme } from "styled-components";
import Icon from "@shared/components/Icon";
import { richExtensions } from "@shared/editor/nodes";
import { EditorStyleHelper } from "@shared/editor/styles/EditorStyleHelper";
import { canUseElementFullscreen } from "@shared/utils/browser";
import { s, depths, hover } from "@shared/styles";
import { cloneDeep } from "es-toolkit/compat";
@@ -426,7 +427,7 @@ const SlideContent = styled.div`
.image-wrapper,
.image-wrapper img,
.mermaid-diagram-wrapper {
.${EditorStyleHelper.mermaidDiagram} {
pointer-events: none !important;
}
+8 -8
View File
@@ -723,7 +723,7 @@ width: 100%;
}
&:hover,
&.drag-handle-target {
&.${EditorStyleHelper.dragHandleTarget} {
.heading-anchor {
opacity: 1;
}
@@ -1002,7 +1002,7 @@ th .image .image-wrapper img {
}
.ProseMirror.dragging .ProseMirror-selectednode,
.ProseMirror .dragging-source {
.ProseMirror .${EditorStyleHelper.draggingSource} {
opacity: 0.25;
}
@@ -1154,7 +1154,7 @@ h6:not(.placeholder):not([data-heading-prefix])::before {
opacity: 1;
}
&:hover:not(.placeholder):not([data-heading-prefix])::before,
&.drag-handle-target:not(.placeholder):not([data-heading-prefix])::before {
&.${EditorStyleHelper.dragHandleTarget}:not(.placeholder):not([data-heading-prefix])::before {
opacity: 0;
}
}
@@ -1809,7 +1809,7 @@ mark {
}
&:is(.code-active)
+ .mermaid-diagram-wrapper:not(.parse-error):not(.empty) {
+ .${EditorStyleHelper.mermaidDiagram}:not(.parse-error):not(.empty) {
cursor: zoom-in;
}
@@ -1824,7 +1824,7 @@ mark {
&.ProseMirror-selectednode {
outline: none;
& + .mermaid-diagram-wrapper {
& + .${EditorStyleHelper.mermaidDiagram} {
&:not(.parse-error):not(.empty) {
cursor: zoom-in;
}
@@ -1838,7 +1838,7 @@ mark {
height: 0;
overflow: hidden;
& + .mermaid-diagram-wrapper:not(.parse-error):not(.empty) {
& + .${EditorStyleHelper.mermaidDiagram}:not(.parse-error):not(.empty) {
cursor: zoom-in;
}
}
@@ -1854,7 +1854,7 @@ mark {
}
}
.mermaid-diagram-wrapper {
.${EditorStyleHelper.mermaidDiagram} {
display: none;
}
}
@@ -1972,7 +1972,7 @@ mark {
}
}
.mermaid-diagram-wrapper {
.${EditorStyleHelper.mermaidDiagram} {
display: flex;
align-items: center;
justify-content: center;
+9 -4
View File
@@ -18,6 +18,7 @@ import { LightboxImageFactory } from "../lib/Lightbox";
import { hashString } from "../../utils/string";
import { LRUCache } from "../../utils/LRUCache";
import { sanitizeUrl } from "../../utils/urls";
import { EditorStyleHelper } from "../styles/EditorStyleHelper";
import { isModKey } from "../../utils/keyboard";
export const pluginKey = new PluginKey("mermaid");
@@ -98,11 +99,11 @@ class MermaidRenderer {
constructor() {
this.diagramId = uuidv4();
this.elementId = `mermaid-diagram-wrapper-${this.diagramId}`;
this.elementId = `${EditorStyleHelper.mermaidDiagram}-${this.diagramId}`;
this.element =
document.getElementById(this.elementId) || document.createElement("div");
this.element.id = this.elementId;
this.element.classList.add("mermaid-diagram-wrapper");
this.element.classList.add(EditorStyleHelper.mermaidDiagram);
}
render = async (block: { node: Node; pos: number }, isDark: boolean) => {
@@ -541,7 +542,9 @@ export default function Mermaid({
},
mousedown(view, event) {
const target = event.target as HTMLElement;
const diagram = target?.closest(".mermaid-diagram-wrapper");
const diagram = target?.closest(
`.${EditorStyleHelper.mermaidDiagram}`
);
if (!diagram) {
return false;
}
@@ -591,7 +594,9 @@ export default function Mermaid({
},
mouseup(view, event) {
const target = event.target as HTMLElement;
const diagram = target?.closest(".mermaid-diagram-wrapper");
const diagram = target?.closest(
`.${EditorStyleHelper.mermaidDiagram}`
);
if (!diagram) {
return false;
}
+16
View File
@@ -164,6 +164,22 @@ export class EditorStyleHelper {
/** Drag indicator on bottom side of cell */
static readonly tableDragIndicatorBottom = "table-drag-indicator-bottom";
// Drag and drop
/** Floating handle shown beside the hovered block to drag it */
static readonly blockDragHandle = "block-drag-handle";
/** Block that the drag handle is currently attached to */
static readonly dragHandleTarget = "drag-handle-target";
/** Block that is being dragged */
static readonly draggingSource = "dragging-source";
// Diagrams
/** Container for a rendered Mermaid diagram */
static readonly mermaidDiagram = "mermaid-diagram-wrapper";
// Global
/** Minimum padding around editor */
@@ -726,6 +726,7 @@
"More options": "More options",
"Type '/' to insert": "Type '/' to insert",
"Keep typing to filter": "Keep typing to filter",
"Drag to reorder": "Drag to reorder",
"Replace file": "Replace file",
"Delete file": "Delete file",
"Show preview": "Show preview",
+2 -2
View File
@@ -180,7 +180,7 @@ export default createGlobalStyle<Props>`
/* Block drag handle is appended to document.body, outside the styled
EditorContainer scope, so its rules have to live globally. */
.block-drag-handle {
.${EditorStyleHelper.blockDragHandle} {
appearance: none;
border: 0;
padding: 0;
@@ -212,7 +212,7 @@ export default createGlobalStyle<Props>`
}
@media print {
.block-drag-handle {
.${EditorStyleHelper.blockDragHandle} {
display: none !important;
}
}