mirror of
https://github.com/anomalyco/opencode.git
synced 2026-11-20 18:57:17 +03:00
feat(session-ui): maximize Mermaid diagrams to zoom and pan them (#59250)
This commit is contained in:
committed by
GitHub
Unverified
parent
1c53e40a7f
commit
b268a4e8b0
@@ -0,0 +1,95 @@
|
||||
[data-component="diagram-preview"] {
|
||||
position: fixed;
|
||||
inset: 0;
|
||||
z-index: 50;
|
||||
pointer-events: none;
|
||||
|
||||
/* The diagram is shown over the dialog stack's overlay, fitted to the window with room for the controls under it. */
|
||||
[data-slot="diagram-preview-content"] {
|
||||
position: absolute;
|
||||
inset: 0;
|
||||
pointer-events: auto;
|
||||
|
||||
&:focus-visible {
|
||||
outline: none;
|
||||
}
|
||||
}
|
||||
|
||||
@media (prefers-reduced-motion: no-preference) {
|
||||
[data-slot="diagram-preview-content"][data-expanded] {
|
||||
animation: diagram-preview-in 150ms ease-out;
|
||||
}
|
||||
}
|
||||
|
||||
[data-slot="diagram-preview-body"] {
|
||||
position: absolute;
|
||||
inset: 44px clamp(8px, 3vw, 36px);
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
padding: 12px;
|
||||
overflow: hidden;
|
||||
user-select: none;
|
||||
/* A zoomed diagram fades out in the padding instead of being cut at the edge; a fitted one never reaches it. */
|
||||
mask-image:
|
||||
linear-gradient(to right, transparent, #000 12px, #000 calc(100% - 12px), transparent),
|
||||
linear-gradient(to bottom, transparent, #000 12px, #000 calc(100% - 12px), transparent);
|
||||
mask-composite: intersect;
|
||||
|
||||
&[data-zoomed] {
|
||||
cursor: grab;
|
||||
touch-action: pinch-zoom;
|
||||
}
|
||||
|
||||
&[data-dragging] {
|
||||
cursor: grabbing;
|
||||
}
|
||||
}
|
||||
|
||||
[data-slot="diagram-preview-canvas"] {
|
||||
flex: none;
|
||||
border-radius: var(--radius-md);
|
||||
background: var(--v2-background-bg-layer-02);
|
||||
box-shadow: inset 0 0 0 0.5px var(--v2-border-border-base);
|
||||
transform-origin: center;
|
||||
|
||||
> svg {
|
||||
display: block;
|
||||
}
|
||||
}
|
||||
|
||||
[data-slot="diagram-preview-controls"] {
|
||||
position: absolute;
|
||||
bottom: 12px;
|
||||
inset-inline: 0;
|
||||
width: fit-content;
|
||||
margin-inline: auto;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 2px;
|
||||
padding: 2px;
|
||||
border-radius: 8px;
|
||||
background: var(--v2-background-bg-layer-01);
|
||||
box-shadow: var(--v2-elevation-floating);
|
||||
}
|
||||
|
||||
[data-slot="diagram-preview-zoom-level"] {
|
||||
min-width: 52px;
|
||||
justify-content: center;
|
||||
font-variant-numeric: tabular-nums;
|
||||
}
|
||||
|
||||
[data-slot="diagram-preview-divider"] {
|
||||
width: 1px;
|
||||
height: 16px;
|
||||
margin-inline: 2px;
|
||||
background: var(--v2-border-border-base);
|
||||
}
|
||||
}
|
||||
|
||||
@keyframes diagram-preview-in {
|
||||
from {
|
||||
opacity: 0;
|
||||
transform: scale(0.98);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,332 @@
|
||||
import { Dialog as Kobalte, useDialogContext } from "@kobalte/core/dialog"
|
||||
import { makeEventListener } from "@solid-primitives/event-listener"
|
||||
import { createResizeObserver } from "@solid-primitives/resize-observer"
|
||||
import { createMemo, createSignal, onCleanup } from "solid-js"
|
||||
import { useI18n } from "../context/i18n"
|
||||
import { Button } from "@opencode/ui/button"
|
||||
import { Icon } from "@opencode/ui/icon"
|
||||
import { IconButton } from "@opencode/ui/icon-button"
|
||||
import { frameLoop, prefersReducedMotion, Spring } from "../motion"
|
||||
import { FLIGHT_SPRING } from "./image-flight"
|
||||
import {
|
||||
clampView,
|
||||
detailScale,
|
||||
FIT_VIEW,
|
||||
panBy,
|
||||
wheelPixels,
|
||||
wheelZoomFactor,
|
||||
zoomAt,
|
||||
zoomShortcut,
|
||||
type ZoomView,
|
||||
} from "./image-zoom"
|
||||
|
||||
export interface DiagramPreviewProps {
|
||||
/** The diagram to show, which the preview takes over: pass a copy of one still on the page. */
|
||||
svg: SVGSVGElement
|
||||
}
|
||||
|
||||
const STEP = Math.SQRT2
|
||||
|
||||
// How far one arrow key press pans, as a share of the window; Shift pans a whole window.
|
||||
const ARROW_PAN = 0.15
|
||||
|
||||
// How far a diagram zooms in past the size it was drawn at: its labels are crisp at any size, so a few times is enough
|
||||
// to read the smallest.
|
||||
const MAX_ZOOM = 4
|
||||
|
||||
// The canvas around the diagram at its natural size, in pixels.
|
||||
const PADDING = 24
|
||||
|
||||
const CENTER = { x: 0, y: 0 }
|
||||
|
||||
/**
|
||||
* A diagram maximized over the page, zoomed and panned like an image preview. The diagram stays a live SVG, so it is
|
||||
* redrawn sharp at every zoom and keeps the page's fonts and theme.
|
||||
*/
|
||||
export function DiagramPreview(props: DiagramPreviewProps) {
|
||||
const i18n = useI18n()
|
||||
const dialog = useDialogContext()
|
||||
let viewport!: HTMLDivElement
|
||||
let canvas!: HTMLDivElement
|
||||
let close: HTMLButtonElement | undefined
|
||||
const [view, setView] = createSignal(FIT_VIEW)
|
||||
const [goal, setGoal] = createSignal(FIT_VIEW)
|
||||
const [fitRatio, setFitRatio] = createSignal(1)
|
||||
const [drag, setDrag] = createSignal<{ id: number; x: number; y: number }>()
|
||||
// Whether the press that ends in a click began on the backdrop around the diagram, and whether a pan moved since.
|
||||
let backdrop = false
|
||||
let panned = false
|
||||
const glide = [1, 0, 0].map((value) => Spring.fromDuration(value, FLIGHT_SPRING))
|
||||
let gliding: (() => void) | undefined
|
||||
|
||||
const percent = createMemo(() => new Intl.NumberFormat(i18n.locale(), { style: "percent", maximumFractionDigits: 0 }))
|
||||
|
||||
const bounds = () => ({
|
||||
width: canvas.offsetWidth,
|
||||
height: canvas.offsetHeight,
|
||||
viewportWidth: viewport.clientWidth,
|
||||
viewportHeight: viewport.clientHeight,
|
||||
maxScale: Math.max(1, MAX_ZOOM / fitRatio()),
|
||||
})
|
||||
|
||||
const show = (next: ZoomView) => {
|
||||
setView(next)
|
||||
canvas.style.transform = `translate(${next.x}px, ${next.y}px) scale(${next.scale})`
|
||||
}
|
||||
|
||||
const settle = () => {
|
||||
gliding?.()
|
||||
gliding = undefined
|
||||
}
|
||||
|
||||
onCleanup(settle)
|
||||
|
||||
// Pinch, wheel and drag follow the hand directly and take over from an eased zoom mid-flight.
|
||||
const follow = (next: ZoomView) => {
|
||||
settle()
|
||||
setGoal(next)
|
||||
show(next)
|
||||
}
|
||||
|
||||
const ease = (target: ZoomView) => {
|
||||
setGoal(target)
|
||||
|
||||
if (prefersReducedMotion()) return follow(target)
|
||||
|
||||
if (!gliding) [view().scale, view().x, view().y].forEach((value, index) => glide[index].snap(value))
|
||||
;[target.scale, target.x, target.y].forEach((value, index) => glide[index].set(value))
|
||||
gliding ??= frameLoop.add((dt) => {
|
||||
const [scale, x, y] = glide.map((spring) => spring.step(dt))
|
||||
show({ scale, x, y })
|
||||
|
||||
if (!glide.every((spring) => spring.resting)) return true
|
||||
gliding = undefined
|
||||
|
||||
return false
|
||||
})
|
||||
}
|
||||
|
||||
const step = (factor: number) => ease(zoomAt(goal(), goal().scale * factor, CENTER, bounds()))
|
||||
|
||||
// Fits the diagram to the window without enlarging it past the size it was drawn at, keeping the zoom it has. The
|
||||
// padding around it shrinks with it.
|
||||
const measure = () => {
|
||||
const width = props.svg.width.baseVal.value
|
||||
const height = props.svg.height.baseVal.value
|
||||
|
||||
// The window's own padding is where a zoomed diagram fades out, so a fitted one stays inside it.
|
||||
const inset = parseFloat(getComputedStyle(viewport).paddingLeft) * 2
|
||||
const room = { width: viewport.clientWidth - inset, height: viewport.clientHeight - inset }
|
||||
|
||||
if (room.width <= 0 || room.height <= 0) return
|
||||
const fit = Math.min(1, room.width / (width + PADDING * 2), room.height / (height + PADDING * 2))
|
||||
canvas.style.padding = `${PADDING * fit}px`
|
||||
props.svg.style.width = `${width * fit}px`
|
||||
props.svg.style.height = `${height * fit}px`
|
||||
setFitRatio(fit)
|
||||
follow(clampView(view(), bounds()))
|
||||
}
|
||||
|
||||
// Pele sizes the inline diagram to its column; here the preview sizes it.
|
||||
props.svg.style.maxWidth = "none"
|
||||
|
||||
createResizeObserver(() => viewport, measure)
|
||||
|
||||
const point = (event: MouseEvent) => {
|
||||
const rect = viewport.getBoundingClientRect()
|
||||
|
||||
return { x: event.clientX - rect.left - rect.width / 2, y: event.clientY - rect.top - rect.height / 2 }
|
||||
}
|
||||
|
||||
const detail = (event: MouseEvent) => {
|
||||
if (goal().scale > 1) return ease(FIT_VIEW)
|
||||
ease(zoomAt(goal(), detailScale(fitRatio()), point(event), bounds()))
|
||||
}
|
||||
|
||||
const grab = (event: PointerEvent) => {
|
||||
panned = false
|
||||
|
||||
if (event.button !== 0 || view().scale <= 1) return
|
||||
event.preventDefault()
|
||||
viewport.setPointerCapture(event.pointerId)
|
||||
setDrag({ id: event.pointerId, x: event.clientX, y: event.clientY })
|
||||
}
|
||||
|
||||
const pan = (event: PointerEvent) => {
|
||||
const current = drag()
|
||||
|
||||
if (current?.id !== event.pointerId) return
|
||||
backdrop = false
|
||||
panned = true
|
||||
setDrag({ id: current.id, x: event.clientX, y: event.clientY })
|
||||
follow(panBy(view(), { x: event.clientX - current.x, y: event.clientY - current.y }, bounds()))
|
||||
}
|
||||
|
||||
const release = (event: PointerEvent) => {
|
||||
if (drag()?.id === event.pointerId) setDrag(undefined)
|
||||
}
|
||||
|
||||
// The preview owns zoom gestures while it is open: the page behind must neither scroll nor zoom the whole window.
|
||||
makeEventListener(
|
||||
window,
|
||||
"wheel",
|
||||
(event: WheelEvent) => {
|
||||
const inside = event.target instanceof Node && viewport.contains(event.target)
|
||||
const zoom = event.ctrlKey || event.metaKey
|
||||
|
||||
if (!inside && !zoom) return
|
||||
event.preventDefault()
|
||||
event.stopPropagation()
|
||||
|
||||
if (!inside) return
|
||||
const current = view()
|
||||
|
||||
if (zoom) {
|
||||
follow(
|
||||
zoomAt(current, current.scale * wheelZoomFactor(wheelPixels(event, event.deltaY)), point(event), bounds()),
|
||||
)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
const horizontal = event.shiftKey && event.deltaX === 0
|
||||
follow(
|
||||
panBy(
|
||||
current,
|
||||
{
|
||||
x: -wheelPixels(event, horizontal ? event.deltaY : event.deltaX),
|
||||
y: horizontal ? 0 : -wheelPixels(event, event.deltaY),
|
||||
},
|
||||
bounds(),
|
||||
),
|
||||
)
|
||||
},
|
||||
{ passive: false, capture: true },
|
||||
)
|
||||
|
||||
// Captured ahead of the desktop shell, which zooms the whole window on the same shortcuts.
|
||||
makeEventListener(
|
||||
window,
|
||||
"keydown",
|
||||
(event: KeyboardEvent) => {
|
||||
const bare = !event.metaKey && !event.ctrlKey && !event.altKey
|
||||
const arrow = bare ? arrowDirection(event.key) : undefined
|
||||
|
||||
if (arrow) {
|
||||
event.preventDefault()
|
||||
event.stopPropagation()
|
||||
const distance = event.shiftKey ? 1 : ARROW_PAN
|
||||
|
||||
return ease(
|
||||
panBy(
|
||||
goal(),
|
||||
{ x: arrow.x * distance * viewport.clientWidth, y: arrow.y * distance * viewport.clientHeight },
|
||||
bounds(),
|
||||
),
|
||||
)
|
||||
}
|
||||
|
||||
const action = bare ? (event.key === "0" ? "fit" : undefined) : event.altKey ? undefined : zoomShortcut(event.key)
|
||||
|
||||
if (!action) return
|
||||
event.preventDefault()
|
||||
event.stopPropagation()
|
||||
|
||||
if (action === "fit") return ease(FIT_VIEW)
|
||||
step(action === "in" ? STEP : 1 / STEP)
|
||||
},
|
||||
{ capture: true },
|
||||
)
|
||||
|
||||
return (
|
||||
<div
|
||||
data-component="diagram-preview"
|
||||
// Zoomed in, the first Escape zooms back out; the dialog stack leaves it to the preview.
|
||||
data-owns-escape={goal().scale > 1 ? "" : undefined}
|
||||
>
|
||||
<Kobalte.Content
|
||||
data-slot="diagram-preview-content"
|
||||
aria-label={i18n.t("ui.diagramPreview.label")}
|
||||
onOpenAutoFocus={(event) => {
|
||||
event.preventDefault()
|
||||
close?.focus()
|
||||
}}
|
||||
onEscapeKeyDown={(event) => {
|
||||
if (goal().scale <= 1) return
|
||||
event.preventDefault()
|
||||
ease(FIT_VIEW)
|
||||
}}
|
||||
onPointerDown={(event: PointerEvent) => {
|
||||
backdrop = event.target === viewport
|
||||
}}
|
||||
onClick={(event: MouseEvent) => {
|
||||
// The preview fills the window, so a click on the backdrop around the diagram lands here.
|
||||
if (!backdrop || panned || event.target !== viewport) return
|
||||
dialog.close()
|
||||
}}
|
||||
>
|
||||
<div
|
||||
ref={viewport}
|
||||
data-slot="diagram-preview-body"
|
||||
data-zoomed={view().scale > 1 ? "" : undefined}
|
||||
data-dragging={drag() ? "" : undefined}
|
||||
onDblClick={detail}
|
||||
onPointerDown={grab}
|
||||
onPointerMove={pan}
|
||||
onPointerUp={release}
|
||||
onPointerCancel={release}
|
||||
>
|
||||
<div ref={canvas} data-slot="diagram-preview-canvas">
|
||||
{props.svg}
|
||||
</div>
|
||||
</div>
|
||||
<div data-slot="diagram-preview-controls">
|
||||
<IconButton
|
||||
data-slot="diagram-preview-zoom-out"
|
||||
icon={<Icon name="minus" />}
|
||||
variant="ghost"
|
||||
aria-label={i18n.t("ui.imagePreview.zoomOut")}
|
||||
onClick={() => step(1 / STEP)}
|
||||
/>
|
||||
<Button
|
||||
data-slot="diagram-preview-zoom-level"
|
||||
variant="ghost-muted"
|
||||
size="small"
|
||||
aria-label={i18n.t("ui.imagePreview.zoomToFit")}
|
||||
title={i18n.t("ui.imagePreview.zoomToFit")}
|
||||
onClick={() => ease(FIT_VIEW)}
|
||||
>
|
||||
{percent().format(view().scale * fitRatio())}
|
||||
</Button>
|
||||
<IconButton
|
||||
data-slot="diagram-preview-zoom-in"
|
||||
icon={<Icon name="plus" />}
|
||||
variant="ghost"
|
||||
aria-label={i18n.t("ui.imagePreview.zoomIn")}
|
||||
onClick={() => step(STEP)}
|
||||
/>
|
||||
<div data-slot="diagram-preview-divider" />
|
||||
<Kobalte.CloseButton
|
||||
ref={close}
|
||||
data-slot="diagram-preview-close"
|
||||
as={IconButton}
|
||||
icon={<Icon name="close" />}
|
||||
variant="ghost"
|
||||
aria-label={i18n.t("ui.common.close")}
|
||||
/>
|
||||
</div>
|
||||
</Kobalte.Content>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
// Arrow keys move the view over the diagram, so the diagram moves the other way.
|
||||
function arrowDirection(key: string) {
|
||||
if (key === "ArrowLeft") return { x: 1, y: 0 }
|
||||
|
||||
if (key === "ArrowRight") return { x: -1, y: 0 }
|
||||
|
||||
if (key === "ArrowUp") return { x: 0, y: 1 }
|
||||
|
||||
if (key === "ArrowDown") return { x: 0, y: -1 }
|
||||
}
|
||||
@@ -40,8 +40,10 @@ import {
|
||||
maxScale,
|
||||
panBy,
|
||||
viewBox,
|
||||
wheelPixels,
|
||||
wheelZoomFactor,
|
||||
zoomAt,
|
||||
zoomShortcut,
|
||||
type ZoomView,
|
||||
} from "./image-zoom"
|
||||
import {
|
||||
@@ -657,7 +659,7 @@ export function ImagePreview(props: ImagePreviewProps) {
|
||||
const current = view()
|
||||
|
||||
const next = pinch
|
||||
? zoomAt(current, current.scale * wheelZoomFactor(pixels(event, event.deltaY)), point(event), bounds)
|
||||
? zoomAt(current, current.scale * wheelZoomFactor(wheelPixels(event, event.deltaY)), point(event), bounds)
|
||||
: panBy(current, wheelPan(event), bounds)
|
||||
|
||||
follow(next.scale, next.x, next.y)
|
||||
@@ -682,7 +684,7 @@ export function ImagePreview(props: ImagePreviewProps) {
|
||||
return props.video.pause()
|
||||
}
|
||||
|
||||
const action = bare ? (event.key === "0" ? "fit" : undefined) : event.altKey ? undefined : shortcut(event.key)
|
||||
const action = bare ? (event.key === "0" ? "fit" : undefined) : event.altKey ? undefined : zoomShortcut(event.key)
|
||||
|
||||
if (!action) return
|
||||
event.preventDefault()
|
||||
@@ -895,28 +897,12 @@ function layout(element: HTMLElement, frame: Box, radius: number) {
|
||||
element.style.borderRadius = `${radius}px`
|
||||
}
|
||||
|
||||
function shortcut(key: string) {
|
||||
if (key === "=" || key === "+") return "in"
|
||||
|
||||
if (key === "-" || key === "_") return "out"
|
||||
|
||||
if (key === "0") return "fit"
|
||||
}
|
||||
|
||||
// A plain wheel pans; Shift turns a vertical wheel sideways.
|
||||
function wheelPan(event: WheelEvent) {
|
||||
const horizontal = event.shiftKey && event.deltaX === 0
|
||||
|
||||
return {
|
||||
x: -pixels(event, horizontal ? event.deltaY : event.deltaX),
|
||||
y: horizontal ? 0 : -pixels(event, event.deltaY),
|
||||
x: -wheelPixels(event, horizontal ? event.deltaY : event.deltaX),
|
||||
y: horizontal ? 0 : -wheelPixels(event, event.deltaY),
|
||||
}
|
||||
}
|
||||
|
||||
function pixels(event: WheelEvent, delta: number) {
|
||||
if (event.deltaMode === WheelEvent.DOM_DELTA_LINE) return delta * 16
|
||||
|
||||
if (event.deltaMode === WheelEvent.DOM_DELTA_PAGE) return delta * window.innerHeight
|
||||
|
||||
return delta
|
||||
}
|
||||
|
||||
@@ -85,6 +85,24 @@ export function wheelZoomFactor(deltaY: number) {
|
||||
return Math.exp(-clamp(deltaY, -30, 30) / 100)
|
||||
}
|
||||
|
||||
/** The zoom a Cmd/Ctrl shortcut asks for, from the key it is pressed with. */
|
||||
export function zoomShortcut(key: string) {
|
||||
if (key === "=" || key === "+") return "in"
|
||||
|
||||
if (key === "-" || key === "_") return "out"
|
||||
|
||||
if (key === "0") return "fit"
|
||||
}
|
||||
|
||||
/** A wheel event's delta in pixels, whether it counts in pixels, lines or pages. */
|
||||
export function wheelPixels(event: WheelEvent, delta: number) {
|
||||
if (event.deltaMode === WheelEvent.DOM_DELTA_LINE) return delta * 16
|
||||
|
||||
if (event.deltaMode === WheelEvent.DOM_DELTA_PAGE) return delta * window.innerHeight
|
||||
|
||||
return delta
|
||||
}
|
||||
|
||||
/** Keeps one axis of a zoomed image of `size` covering a `viewport` it overflows, and centered in one it fits. */
|
||||
export function clampAxis(offset: number, size: number, viewport: number) {
|
||||
const limit = Math.max(0, (size - viewport) / 2)
|
||||
|
||||
@@ -252,6 +252,8 @@ const source = {
|
||||
"ui.imagePreview.zoomIn": "Zoom in",
|
||||
"ui.imagePreview.zoomOut": "Zoom out",
|
||||
"ui.imagePreview.zoomToFit": "Zoom to fit",
|
||||
"ui.diagramPreview.label": "Diagram",
|
||||
"ui.diagramPreview.open": "Maximize diagram",
|
||||
"ui.scrollView.ariaLabel": "scrollable content",
|
||||
|
||||
"ui.tool.read": "Read",
|
||||
|
||||
@@ -262,6 +262,10 @@ const icons = {
|
||||
viewBox: "0 0 16 16",
|
||||
body: `<path d="M2 8.5V2H14V14H8M5 5.66667H10.3333V11M3.66797 12.333L9.94292 6.05806" stroke="currentColor" stroke-linecap="square"/>`,
|
||||
},
|
||||
"outline-maximize": {
|
||||
viewBox: "0 0 16 16",
|
||||
body: `<path d="M2.5 6V2.5H6M10 2.5H13.5V6M13.5 10V13.5H10M6 13.5H2.5V10" stroke="currentColor" stroke-linecap="square"/>`,
|
||||
},
|
||||
"outline-hexagonal-warning": {
|
||||
viewBox: "0 0 16 16",
|
||||
body: `<path d="M10.6667 1.33325H5.33272L1.33334 5.33325L1.33342 10.6917L5.3327 14.691L10.6667 14.6909L14.6911 10.6917V5.33255L10.6667 1.33325Z" stroke="currentColor"/><path d="M8 10.6667H8.00667" stroke="currentColor" stroke-linecap="square"/><path d="M8 7.99992V5.33325" stroke="currentColor" stroke-linecap="square"/>`,
|
||||
|
||||
@@ -19,6 +19,7 @@
|
||||
@import "../components/hover-card.css" layer(components);
|
||||
@import "../components/provider-icon.css" layer(components);
|
||||
@import "../components/dock-surface.css" layer(components);
|
||||
@import "../components/diagram-preview.css" layer(components);
|
||||
@import "../components/image-preview.css" layer(components);
|
||||
@import "../forms/inline-input/inline-input.css" layer(components);
|
||||
@import "../components/text-field.css" layer(components);
|
||||
|
||||
Reference in New Issue
Block a user