Files
multica/packages/views/editor/styles/mermaid.css
Naiyuan Qing 17ee59ccc5 feat(editor): standard Mermaid viewer with pan/zoom, exports and a real dialog (MUL-4908) (#5564)
* feat(editor): standard Mermaid viewer with pan/zoom, exports and a real dialog (MUL-4908)

Mermaid's fullscreen view was a bespoke lightbox: no visible close button, no
canvas interaction, and a toolbar that scrolled away with wide diagrams. Escape
also stopped working once the iframe took focus, which is why it read as
"impossible to close".

Replace it with a viewer built on the shared Dialog, so it inherits the
backdrop, focus trap, scroll lock, focus restore and Escape handling that HTML
preview already had.

The diagram stays in an `sandbox=""` iframe — isolation is not relaxed to buy
interactivity. Instead the iframe is `pointer-events: none` and pan/zoom is
applied from the host document as a transform on its wrapper. That inversion is
also what keeps Escape working: the iframe can never take focus.

- Viewer: fit-on-open, drag pan, wheel/pinch/keyboard zoom (25%-400%) anchored
  at the pointer, Fit / 100% / Reset, and a header toolbar that cannot scroll
  away. Panning is clamped so the canvas can never be lost.
- Export: `.mmd`, `.svg` and `.png`. Requires `htmlLabels: false` — browsers do
  not rasterize Mermaid's default HTML-in-foreignObject labels through an
  `<img>`; verified in Chromium that they paint zero pixels AND taint the
  canvas, so PNG export silently produced nothing.
- Inline: toolbar lifted out of the scroll container, natural-size rendering
  (small diagrams centered, wide ones scroll at a readable size with edge
  fades), copy source, and a parser message on the error fallback.
- Theme switches no longer close the viewer or discard zoom/position; the
  previous diagram stays on screen until its replacement is ready.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Co-authored-by: multica-agent <github@multica.ai>

* fix(editor): stop Mermaid drags selecting text and misfiring the viewer (MUL-4908)

Two gesture defects on the inline diagram, reported by Naiyuan.

1. Dragging a diagram started a native text selection. The iframe is a replaced
   element, so the whole diagram box got painted with the selection highlight
   and the drag ran on into the surrounding comment text. Fixed with
   `user-select: none` on the inline scroller and the viewer canvas.

   Deliberately NOT by preventDefault-ing pointerdown: verified in Chromium that
   it also drops the default focus, which silently kills the viewer's keyboard
   controls (+/-, 0, arrows).

2. Any drag ended in a `click`, so trying to look along a wide diagram opened
   the viewer on top of the user. The inline diagram had no drag handling at
   all — only `onClick`. Suppressing the selection alone would have made this
   fire more reliably, not less, so both had to land together.

   Past a 5px threshold the gesture is a drag for good: releasing no longer
   taps, and a horizontal drag pans the scroller instead. This holds even when
   the diagram has no room to scroll, so an unscrollable diagram cannot open on
   release either. A still click, a sub-threshold jitter, and the expand button
   all still open the viewer.

Touch is left alone — it already pans natively and its vertical drags belong to
the page; the browser announces its takeover with pointercancel. Mouse and pen
have no native drag-to-scroll and are panned here.

Verified in Chromium: still click opens; a 150px drag pans and does not open; a
drag on an unscrollable diagram does not open; 3px jitter still opens; and no
gesture selects text.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Co-authored-by: multica-agent <github@multica.ai>

---------

Co-authored-by: Walt <walt@multica.ai>
Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com>
Co-authored-by: multica-agent <github@multica.ai>
2026-07-17 16:07:37 +08:00

230 lines
6.9 KiB
CSS

/* Mermaid diagrams */
/* The outer container does NOT scroll — `.mermaid-diagram-scroll` inside it
does. That split is what keeps the toolbar pinned: when the container itself
was the scroller, the absolutely-positioned toolbar scrolled away with wide
diagrams and left no visible way to open or copy them. */
.rich-text-editor .mermaid-diagram {
background: var(--muted);
border: 1px solid var(--border);
border-radius: var(--radius);
margin: 0.75rem 0;
overflow: hidden;
padding: 1rem;
position: relative;
}
.rich-text-editor .mermaid-diagram-scroll {
display: flex;
/* Small diagrams sit centered at natural size; only oversized ones scroll.
Upscaling a small diagram to fill the column would just coarsen it. */
justify-content: center;
overflow-x: auto;
overflow-y: hidden;
cursor: zoom-in;
/* Dragging a diagram would otherwise start a native text selection: the
iframe is a replaced element, so the whole diagram box gets painted with
the selection highlight, and the drag runs on into the surrounding
comment text. Selecting prose *through* the diagram still works — this
only stops a selection from starting on the diagram itself. */
user-select: none;
}
.rich-text-editor .mermaid-diagram-frame {
border: 0;
display: block;
/* Natural width, never shrunk to the column: a wide diagram must keep a
readable font size and scroll sideways instead of being squashed until the
labels are unreadable. `flex: none` stops the flex parent from shrinking
it; a `max-width` here would defeat the scroll entirely. */
flex: none;
/* The iframe cannot forward clicks to this document, so it would swallow
the click-to-open gesture. Disabling pointer events hands wheel, click,
and drag to the scroll container instead. */
pointer-events: none;
}
/* Edge fades: a hint that the diagram continues past the container. Driven by
data attributes the component sets from scroll position, so a diagram that
fits shows no fade, and a scrolled-to-the-end one fades only on the left. */
.rich-text-editor .mermaid-diagram::before,
.rich-text-editor .mermaid-diagram::after {
content: "";
position: absolute;
top: 0;
bottom: 0;
width: 2rem;
pointer-events: none;
opacity: 0;
transition: opacity 0.15s;
z-index: 1;
}
.rich-text-editor .mermaid-diagram::before {
left: 0;
background: linear-gradient(to right, var(--muted), transparent);
}
.rich-text-editor .mermaid-diagram::after {
right: 0;
background: linear-gradient(to left, var(--muted), transparent);
}
.rich-text-editor .mermaid-diagram[data-overflow-start]::before,
.rich-text-editor .mermaid-diagram[data-overflow-end]::after {
opacity: 1;
}
.rich-text-editor .mermaid-diagram-loading,
.rich-text-editor .mermaid-diagram-error p {
color: var(--muted-foreground);
font-size: 0.8125rem;
margin: 0;
}
.rich-text-editor .mermaid-diagram-error-head {
display: flex;
align-items: center;
justify-content: space-between;
gap: 0.5rem;
}
.rich-text-editor .mermaid-diagram-error-head button {
display: flex;
align-items: center;
justify-content: center;
width: 1.75rem;
height: 1.75rem;
border-radius: calc(var(--radius) - 2px);
color: var(--muted-foreground);
flex: none;
}
.rich-text-editor .mermaid-diagram-error-head button:hover {
background: var(--secondary);
color: var(--foreground);
}
.rich-text-editor .mermaid-diagram-error-detail {
font-family: var(--font-mono, monospace);
font-size: 0.75rem;
margin: 0.375rem 0 0;
white-space: pre-wrap;
word-break: break-word;
}
/* The error state has no inner scroller, and the container clips; without its
own overflow a long source line would be cut off with no way to read it. */
.rich-text-editor .mermaid-diagram-error pre {
margin-bottom: 0;
overflow-x: auto;
}
/* Mermaid toolbar — dark pill, top-right corner */
.rich-text-editor .mermaid-diagram-toolbar {
position: absolute;
top: 0.5rem;
right: 0.5rem;
display: flex;
gap: 1px;
padding: 0.25rem;
background: color-mix(in srgb, black 75%, transparent);
backdrop-filter: blur(8px);
border-radius: var(--radius);
transition: opacity 0.15s;
z-index: 2;
}
/* Reveal-on-hover only where hovering exists. On touch there is no hover
state to enter, so the toolbar would have been permanently invisible and
the diagram effectively un-openable. */
@media (hover: hover) {
.rich-text-editor .mermaid-diagram-toolbar {
opacity: 0;
}
.rich-text-editor .mermaid-diagram:hover .mermaid-diagram-toolbar,
.rich-text-editor .mermaid-diagram-toolbar:focus-within {
opacity: 1;
}
}
.rich-text-editor .mermaid-diagram-toolbar button {
display: flex;
align-items: center;
justify-content: center;
width: 1.75rem;
height: 1.75rem;
border-radius: calc(var(--radius) - 2px);
color: white;
transition: background 0.15s;
}
.rich-text-editor .mermaid-diagram-toolbar button:hover {
background: color-mix(in srgb, white 15%, transparent);
}
/* Full-screen viewer canvas. Not scoped to .rich-text-editor: the viewer is
portaled to document.body by the Dialog and never renders inside the editor
subtree. */
.mermaid-viewer-canvas {
position: relative;
width: 100%;
/* Flex owns the height: the header is shrink-0 and the canvas takes the rest.
`min-height: 0` is what allows it to shrink below content size instead of
pushing the toolbar out of the dialog. */
flex: 1 1 auto;
min-height: 0;
overflow: hidden;
background: var(--muted);
cursor: grab;
/* Claim every touch gesture: browser pan/pinch would otherwise scroll or
zoom the page underneath instead of moving the diagram. */
touch-action: none;
/* A pan that leaves the canvas would otherwise start a native text selection
and highlight the toolbar text plus the whole iframe box. Done in CSS
rather than by preventDefault-ing pointerdown, which would also suppress
the default focus and silently kill the keyboard controls. */
user-select: none;
outline: none;
}
.mermaid-viewer-canvas:focus-visible {
outline: 2px solid var(--ring);
outline-offset: -2px;
}
.mermaid-viewer-canvas.is-panning {
cursor: grabbing;
}
.mermaid-viewer-content {
position: absolute;
top: 0;
left: 0;
transform-origin: 0 0;
will-change: transform;
}
/* Only discrete controls (toolbar buttons, +/-/0, arrow keys) ease between
states. Drag and wheel/pinch set `is-animated` off so the diagram tracks the
input exactly — easing direct manipulation reads as lag, not polish. */
.mermaid-viewer-content.is-animated {
transition: transform 150ms ease-out;
}
@media (prefers-reduced-motion: reduce) {
.mermaid-viewer-content.is-animated {
transition: none;
}
}
.mermaid-viewer-frame {
border: 0;
display: block;
background: transparent;
/* Same reason as the inline frame: interaction is owned by the canvas in
this document, so the sandbox never has to be relaxed to allow zooming. */
pointer-events: none;
}