@opencode/ui 0.0.0-dev-1238 → 0.0.0-dev-1243
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/components/diagram-preview.d.ts +9 -0
- package/dist/components/image-zoom.d.ts +4 -0
- package/dist/i18n/en.d.ts +2 -0
- package/dist/icons/catalog.d.ts +4 -0
- package/package.json +2 -2
- package/src/components/diagram-preview.css +95 -0
- package/src/components/diagram-preview.tsx +332 -0
- package/src/components/image-preview.tsx +6 -20
- package/src/components/image-zoom.ts +18 -0
- package/src/i18n/en.ts +2 -0
- package/src/icons/catalog.ts +4 -0
- package/src/styles/index.css +1 -0
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export interface DiagramPreviewProps {
|
|
2
|
+
/** The diagram to show, which the preview takes over: pass a copy of one still on the page. */
|
|
3
|
+
svg: SVGSVGElement;
|
|
4
|
+
}
|
|
5
|
+
/**
|
|
6
|
+
* A diagram maximized over the page, zoomed and panned like an image preview. The diagram stays a live SVG, so it is
|
|
7
|
+
* redrawn sharp at every zoom and keeps the page's fonts and theme.
|
|
8
|
+
*/
|
|
9
|
+
export declare function DiagramPreview(props: DiagramPreviewProps): import("solid-js").JSX.Element;
|
|
@@ -45,5 +45,9 @@ export declare function viewBox(scale: number, x: number, y: number, size: {
|
|
|
45
45
|
}): string;
|
|
46
46
|
/** The zoom factor for one wheel event, bounded so a coarse mouse-wheel notch is a step rather than a jump. */
|
|
47
47
|
export declare function wheelZoomFactor(deltaY: number): number;
|
|
48
|
+
/** The zoom a Cmd/Ctrl shortcut asks for, from the key it is pressed with. */
|
|
49
|
+
export declare function zoomShortcut(key: string): "fit" | "in" | "out" | undefined;
|
|
50
|
+
/** A wheel event's delta in pixels, whether it counts in pixels, lines or pages. */
|
|
51
|
+
export declare function wheelPixels(event: WheelEvent, delta: number): number;
|
|
48
52
|
/** Keeps one axis of a zoomed image of `size` covering a `viewport` it overflows, and centered in one it fits. */
|
|
49
53
|
export declare function clampAxis(offset: number, size: number, viewport: number): number;
|
package/dist/i18n/en.d.ts
CHANGED
|
@@ -232,6 +232,8 @@ declare const source: {
|
|
|
232
232
|
"ui.imagePreview.zoomIn": string;
|
|
233
233
|
"ui.imagePreview.zoomOut": string;
|
|
234
234
|
"ui.imagePreview.zoomToFit": string;
|
|
235
|
+
"ui.diagramPreview.label": string;
|
|
236
|
+
"ui.diagramPreview.open": string;
|
|
235
237
|
"ui.scrollView.ariaLabel": string;
|
|
236
238
|
"ui.tool.read": string;
|
|
237
239
|
"ui.tool.loaded": string;
|
package/dist/icons/catalog.d.ts
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@opencode/ui",
|
|
3
|
-
"version": "0.0.0-dev-
|
|
3
|
+
"version": "0.0.0-dev-1243",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": {
|
|
@@ -309,7 +309,7 @@
|
|
|
309
309
|
"dependencies": {
|
|
310
310
|
"@kitlangton/solid-motion": "0.2.2",
|
|
311
311
|
"@kobalte/core": "0.13.13",
|
|
312
|
-
"@opencode/util": "0.0.0-dev-
|
|
312
|
+
"@opencode/util": "0.0.0-dev-1243",
|
|
313
313
|
"@pierre/diffs": "1.6.1",
|
|
314
314
|
"@pierre/highlights": "0.2.0",
|
|
315
315
|
"@solid-primitives/event-listener": "2.4.6",
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
[data-component="diagram-preview"] {
|
|
2
|
+
position: fixed;
|
|
3
|
+
inset: 0;
|
|
4
|
+
z-index: 50;
|
|
5
|
+
pointer-events: none;
|
|
6
|
+
|
|
7
|
+
/* The diagram is shown over the dialog stack's overlay, fitted to the window with room for the controls under it. */
|
|
8
|
+
[data-slot="diagram-preview-content"] {
|
|
9
|
+
position: absolute;
|
|
10
|
+
inset: 0;
|
|
11
|
+
pointer-events: auto;
|
|
12
|
+
|
|
13
|
+
&:focus-visible {
|
|
14
|
+
outline: none;
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
@media (prefers-reduced-motion: no-preference) {
|
|
19
|
+
[data-slot="diagram-preview-content"][data-expanded] {
|
|
20
|
+
animation: diagram-preview-in 150ms ease-out;
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
[data-slot="diagram-preview-body"] {
|
|
25
|
+
position: absolute;
|
|
26
|
+
inset: 44px clamp(8px, 3vw, 36px);
|
|
27
|
+
display: flex;
|
|
28
|
+
align-items: center;
|
|
29
|
+
justify-content: center;
|
|
30
|
+
padding: 12px;
|
|
31
|
+
overflow: hidden;
|
|
32
|
+
user-select: none;
|
|
33
|
+
/* A zoomed diagram fades out in the padding instead of being cut at the edge; a fitted one never reaches it. */
|
|
34
|
+
mask-image:
|
|
35
|
+
linear-gradient(to right, transparent, #000 12px, #000 calc(100% - 12px), transparent),
|
|
36
|
+
linear-gradient(to bottom, transparent, #000 12px, #000 calc(100% - 12px), transparent);
|
|
37
|
+
mask-composite: intersect;
|
|
38
|
+
|
|
39
|
+
&[data-zoomed] {
|
|
40
|
+
cursor: grab;
|
|
41
|
+
touch-action: pinch-zoom;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
&[data-dragging] {
|
|
45
|
+
cursor: grabbing;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
[data-slot="diagram-preview-canvas"] {
|
|
50
|
+
flex: none;
|
|
51
|
+
border-radius: var(--radius-md);
|
|
52
|
+
background: var(--v2-background-bg-layer-02);
|
|
53
|
+
box-shadow: inset 0 0 0 0.5px var(--v2-border-border-base);
|
|
54
|
+
transform-origin: center;
|
|
55
|
+
|
|
56
|
+
> svg {
|
|
57
|
+
display: block;
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
[data-slot="diagram-preview-controls"] {
|
|
62
|
+
position: absolute;
|
|
63
|
+
bottom: 12px;
|
|
64
|
+
inset-inline: 0;
|
|
65
|
+
width: fit-content;
|
|
66
|
+
margin-inline: auto;
|
|
67
|
+
display: flex;
|
|
68
|
+
align-items: center;
|
|
69
|
+
gap: 2px;
|
|
70
|
+
padding: 2px;
|
|
71
|
+
border-radius: 8px;
|
|
72
|
+
background: var(--v2-background-bg-layer-01);
|
|
73
|
+
box-shadow: var(--v2-elevation-floating);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
[data-slot="diagram-preview-zoom-level"] {
|
|
77
|
+
min-width: 52px;
|
|
78
|
+
justify-content: center;
|
|
79
|
+
font-variant-numeric: tabular-nums;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
[data-slot="diagram-preview-divider"] {
|
|
83
|
+
width: 1px;
|
|
84
|
+
height: 16px;
|
|
85
|
+
margin-inline: 2px;
|
|
86
|
+
background: var(--v2-border-border-base);
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
@keyframes diagram-preview-in {
|
|
91
|
+
from {
|
|
92
|
+
opacity: 0;
|
|
93
|
+
transform: scale(0.98);
|
|
94
|
+
}
|
|
95
|
+
}
|
|
@@ -0,0 +1,332 @@
|
|
|
1
|
+
import { Dialog as Kobalte, useDialogContext } from "@kobalte/core/dialog"
|
|
2
|
+
import { makeEventListener } from "@solid-primitives/event-listener"
|
|
3
|
+
import { createResizeObserver } from "@solid-primitives/resize-observer"
|
|
4
|
+
import { createMemo, createSignal, onCleanup } from "solid-js"
|
|
5
|
+
import { useI18n } from "../context/i18n"
|
|
6
|
+
import { Button } from "@opencode/ui/button"
|
|
7
|
+
import { Icon } from "@opencode/ui/icon"
|
|
8
|
+
import { IconButton } from "@opencode/ui/icon-button"
|
|
9
|
+
import { frameLoop, prefersReducedMotion, Spring } from "../motion"
|
|
10
|
+
import { FLIGHT_SPRING } from "./image-flight"
|
|
11
|
+
import {
|
|
12
|
+
clampView,
|
|
13
|
+
detailScale,
|
|
14
|
+
FIT_VIEW,
|
|
15
|
+
panBy,
|
|
16
|
+
wheelPixels,
|
|
17
|
+
wheelZoomFactor,
|
|
18
|
+
zoomAt,
|
|
19
|
+
zoomShortcut,
|
|
20
|
+
type ZoomView,
|
|
21
|
+
} from "./image-zoom"
|
|
22
|
+
|
|
23
|
+
export interface DiagramPreviewProps {
|
|
24
|
+
/** The diagram to show, which the preview takes over: pass a copy of one still on the page. */
|
|
25
|
+
svg: SVGSVGElement
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
const STEP = Math.SQRT2
|
|
29
|
+
|
|
30
|
+
// How far one arrow key press pans, as a share of the window; Shift pans a whole window.
|
|
31
|
+
const ARROW_PAN = 0.15
|
|
32
|
+
|
|
33
|
+
// 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
|
|
34
|
+
// to read the smallest.
|
|
35
|
+
const MAX_ZOOM = 4
|
|
36
|
+
|
|
37
|
+
// The canvas around the diagram at its natural size, in pixels.
|
|
38
|
+
const PADDING = 24
|
|
39
|
+
|
|
40
|
+
const CENTER = { x: 0, y: 0 }
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* A diagram maximized over the page, zoomed and panned like an image preview. The diagram stays a live SVG, so it is
|
|
44
|
+
* redrawn sharp at every zoom and keeps the page's fonts and theme.
|
|
45
|
+
*/
|
|
46
|
+
export function DiagramPreview(props: DiagramPreviewProps) {
|
|
47
|
+
const i18n = useI18n()
|
|
48
|
+
const dialog = useDialogContext()
|
|
49
|
+
let viewport!: HTMLDivElement
|
|
50
|
+
let canvas!: HTMLDivElement
|
|
51
|
+
let close: HTMLButtonElement | undefined
|
|
52
|
+
const [view, setView] = createSignal(FIT_VIEW)
|
|
53
|
+
const [goal, setGoal] = createSignal(FIT_VIEW)
|
|
54
|
+
const [fitRatio, setFitRatio] = createSignal(1)
|
|
55
|
+
const [drag, setDrag] = createSignal<{ id: number; x: number; y: number }>()
|
|
56
|
+
// Whether the press that ends in a click began on the backdrop around the diagram, and whether a pan moved since.
|
|
57
|
+
let backdrop = false
|
|
58
|
+
let panned = false
|
|
59
|
+
const glide = [1, 0, 0].map((value) => Spring.fromDuration(value, FLIGHT_SPRING))
|
|
60
|
+
let gliding: (() => void) | undefined
|
|
61
|
+
|
|
62
|
+
const percent = createMemo(() => new Intl.NumberFormat(i18n.locale(), { style: "percent", maximumFractionDigits: 0 }))
|
|
63
|
+
|
|
64
|
+
const bounds = () => ({
|
|
65
|
+
width: canvas.offsetWidth,
|
|
66
|
+
height: canvas.offsetHeight,
|
|
67
|
+
viewportWidth: viewport.clientWidth,
|
|
68
|
+
viewportHeight: viewport.clientHeight,
|
|
69
|
+
maxScale: Math.max(1, MAX_ZOOM / fitRatio()),
|
|
70
|
+
})
|
|
71
|
+
|
|
72
|
+
const show = (next: ZoomView) => {
|
|
73
|
+
setView(next)
|
|
74
|
+
canvas.style.transform = `translate(${next.x}px, ${next.y}px) scale(${next.scale})`
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
const settle = () => {
|
|
78
|
+
gliding?.()
|
|
79
|
+
gliding = undefined
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
onCleanup(settle)
|
|
83
|
+
|
|
84
|
+
// Pinch, wheel and drag follow the hand directly and take over from an eased zoom mid-flight.
|
|
85
|
+
const follow = (next: ZoomView) => {
|
|
86
|
+
settle()
|
|
87
|
+
setGoal(next)
|
|
88
|
+
show(next)
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
const ease = (target: ZoomView) => {
|
|
92
|
+
setGoal(target)
|
|
93
|
+
|
|
94
|
+
if (prefersReducedMotion()) return follow(target)
|
|
95
|
+
|
|
96
|
+
if (!gliding) [view().scale, view().x, view().y].forEach((value, index) => glide[index].snap(value))
|
|
97
|
+
;[target.scale, target.x, target.y].forEach((value, index) => glide[index].set(value))
|
|
98
|
+
gliding ??= frameLoop.add((dt) => {
|
|
99
|
+
const [scale, x, y] = glide.map((spring) => spring.step(dt))
|
|
100
|
+
show({ scale, x, y })
|
|
101
|
+
|
|
102
|
+
if (!glide.every((spring) => spring.resting)) return true
|
|
103
|
+
gliding = undefined
|
|
104
|
+
|
|
105
|
+
return false
|
|
106
|
+
})
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
const step = (factor: number) => ease(zoomAt(goal(), goal().scale * factor, CENTER, bounds()))
|
|
110
|
+
|
|
111
|
+
// Fits the diagram to the window without enlarging it past the size it was drawn at, keeping the zoom it has. The
|
|
112
|
+
// padding around it shrinks with it.
|
|
113
|
+
const measure = () => {
|
|
114
|
+
const width = props.svg.width.baseVal.value
|
|
115
|
+
const height = props.svg.height.baseVal.value
|
|
116
|
+
|
|
117
|
+
// The window's own padding is where a zoomed diagram fades out, so a fitted one stays inside it.
|
|
118
|
+
const inset = parseFloat(getComputedStyle(viewport).paddingLeft) * 2
|
|
119
|
+
const room = { width: viewport.clientWidth - inset, height: viewport.clientHeight - inset }
|
|
120
|
+
|
|
121
|
+
if (room.width <= 0 || room.height <= 0) return
|
|
122
|
+
const fit = Math.min(1, room.width / (width + PADDING * 2), room.height / (height + PADDING * 2))
|
|
123
|
+
canvas.style.padding = `${PADDING * fit}px`
|
|
124
|
+
props.svg.style.width = `${width * fit}px`
|
|
125
|
+
props.svg.style.height = `${height * fit}px`
|
|
126
|
+
setFitRatio(fit)
|
|
127
|
+
follow(clampView(view(), bounds()))
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
// Pele sizes the inline diagram to its column; here the preview sizes it.
|
|
131
|
+
props.svg.style.maxWidth = "none"
|
|
132
|
+
|
|
133
|
+
createResizeObserver(() => viewport, measure)
|
|
134
|
+
|
|
135
|
+
const point = (event: MouseEvent) => {
|
|
136
|
+
const rect = viewport.getBoundingClientRect()
|
|
137
|
+
|
|
138
|
+
return { x: event.clientX - rect.left - rect.width / 2, y: event.clientY - rect.top - rect.height / 2 }
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
const detail = (event: MouseEvent) => {
|
|
142
|
+
if (goal().scale > 1) return ease(FIT_VIEW)
|
|
143
|
+
ease(zoomAt(goal(), detailScale(fitRatio()), point(event), bounds()))
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
const grab = (event: PointerEvent) => {
|
|
147
|
+
panned = false
|
|
148
|
+
|
|
149
|
+
if (event.button !== 0 || view().scale <= 1) return
|
|
150
|
+
event.preventDefault()
|
|
151
|
+
viewport.setPointerCapture(event.pointerId)
|
|
152
|
+
setDrag({ id: event.pointerId, x: event.clientX, y: event.clientY })
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
const pan = (event: PointerEvent) => {
|
|
156
|
+
const current = drag()
|
|
157
|
+
|
|
158
|
+
if (current?.id !== event.pointerId) return
|
|
159
|
+
backdrop = false
|
|
160
|
+
panned = true
|
|
161
|
+
setDrag({ id: current.id, x: event.clientX, y: event.clientY })
|
|
162
|
+
follow(panBy(view(), { x: event.clientX - current.x, y: event.clientY - current.y }, bounds()))
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
const release = (event: PointerEvent) => {
|
|
166
|
+
if (drag()?.id === event.pointerId) setDrag(undefined)
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
// The preview owns zoom gestures while it is open: the page behind must neither scroll nor zoom the whole window.
|
|
170
|
+
makeEventListener(
|
|
171
|
+
window,
|
|
172
|
+
"wheel",
|
|
173
|
+
(event: WheelEvent) => {
|
|
174
|
+
const inside = event.target instanceof Node && viewport.contains(event.target)
|
|
175
|
+
const zoom = event.ctrlKey || event.metaKey
|
|
176
|
+
|
|
177
|
+
if (!inside && !zoom) return
|
|
178
|
+
event.preventDefault()
|
|
179
|
+
event.stopPropagation()
|
|
180
|
+
|
|
181
|
+
if (!inside) return
|
|
182
|
+
const current = view()
|
|
183
|
+
|
|
184
|
+
if (zoom) {
|
|
185
|
+
follow(
|
|
186
|
+
zoomAt(current, current.scale * wheelZoomFactor(wheelPixels(event, event.deltaY)), point(event), bounds()),
|
|
187
|
+
)
|
|
188
|
+
|
|
189
|
+
return
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
const horizontal = event.shiftKey && event.deltaX === 0
|
|
193
|
+
follow(
|
|
194
|
+
panBy(
|
|
195
|
+
current,
|
|
196
|
+
{
|
|
197
|
+
x: -wheelPixels(event, horizontal ? event.deltaY : event.deltaX),
|
|
198
|
+
y: horizontal ? 0 : -wheelPixels(event, event.deltaY),
|
|
199
|
+
},
|
|
200
|
+
bounds(),
|
|
201
|
+
),
|
|
202
|
+
)
|
|
203
|
+
},
|
|
204
|
+
{ passive: false, capture: true },
|
|
205
|
+
)
|
|
206
|
+
|
|
207
|
+
// Captured ahead of the desktop shell, which zooms the whole window on the same shortcuts.
|
|
208
|
+
makeEventListener(
|
|
209
|
+
window,
|
|
210
|
+
"keydown",
|
|
211
|
+
(event: KeyboardEvent) => {
|
|
212
|
+
const bare = !event.metaKey && !event.ctrlKey && !event.altKey
|
|
213
|
+
const arrow = bare ? arrowDirection(event.key) : undefined
|
|
214
|
+
|
|
215
|
+
if (arrow) {
|
|
216
|
+
event.preventDefault()
|
|
217
|
+
event.stopPropagation()
|
|
218
|
+
const distance = event.shiftKey ? 1 : ARROW_PAN
|
|
219
|
+
|
|
220
|
+
return ease(
|
|
221
|
+
panBy(
|
|
222
|
+
goal(),
|
|
223
|
+
{ x: arrow.x * distance * viewport.clientWidth, y: arrow.y * distance * viewport.clientHeight },
|
|
224
|
+
bounds(),
|
|
225
|
+
),
|
|
226
|
+
)
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
const action = bare ? (event.key === "0" ? "fit" : undefined) : event.altKey ? undefined : zoomShortcut(event.key)
|
|
230
|
+
|
|
231
|
+
if (!action) return
|
|
232
|
+
event.preventDefault()
|
|
233
|
+
event.stopPropagation()
|
|
234
|
+
|
|
235
|
+
if (action === "fit") return ease(FIT_VIEW)
|
|
236
|
+
step(action === "in" ? STEP : 1 / STEP)
|
|
237
|
+
},
|
|
238
|
+
{ capture: true },
|
|
239
|
+
)
|
|
240
|
+
|
|
241
|
+
return (
|
|
242
|
+
<div
|
|
243
|
+
data-component="diagram-preview"
|
|
244
|
+
// Zoomed in, the first Escape zooms back out; the dialog stack leaves it to the preview.
|
|
245
|
+
data-owns-escape={goal().scale > 1 ? "" : undefined}
|
|
246
|
+
>
|
|
247
|
+
<Kobalte.Content
|
|
248
|
+
data-slot="diagram-preview-content"
|
|
249
|
+
aria-label={i18n.t("ui.diagramPreview.label")}
|
|
250
|
+
onOpenAutoFocus={(event) => {
|
|
251
|
+
event.preventDefault()
|
|
252
|
+
close?.focus()
|
|
253
|
+
}}
|
|
254
|
+
onEscapeKeyDown={(event) => {
|
|
255
|
+
if (goal().scale <= 1) return
|
|
256
|
+
event.preventDefault()
|
|
257
|
+
ease(FIT_VIEW)
|
|
258
|
+
}}
|
|
259
|
+
onPointerDown={(event: PointerEvent) => {
|
|
260
|
+
backdrop = event.target === viewport
|
|
261
|
+
}}
|
|
262
|
+
onClick={(event: MouseEvent) => {
|
|
263
|
+
// The preview fills the window, so a click on the backdrop around the diagram lands here.
|
|
264
|
+
if (!backdrop || panned || event.target !== viewport) return
|
|
265
|
+
dialog.close()
|
|
266
|
+
}}
|
|
267
|
+
>
|
|
268
|
+
<div
|
|
269
|
+
ref={viewport}
|
|
270
|
+
data-slot="diagram-preview-body"
|
|
271
|
+
data-zoomed={view().scale > 1 ? "" : undefined}
|
|
272
|
+
data-dragging={drag() ? "" : undefined}
|
|
273
|
+
onDblClick={detail}
|
|
274
|
+
onPointerDown={grab}
|
|
275
|
+
onPointerMove={pan}
|
|
276
|
+
onPointerUp={release}
|
|
277
|
+
onPointerCancel={release}
|
|
278
|
+
>
|
|
279
|
+
<div ref={canvas} data-slot="diagram-preview-canvas">
|
|
280
|
+
{props.svg}
|
|
281
|
+
</div>
|
|
282
|
+
</div>
|
|
283
|
+
<div data-slot="diagram-preview-controls">
|
|
284
|
+
<IconButton
|
|
285
|
+
data-slot="diagram-preview-zoom-out"
|
|
286
|
+
icon={<Icon name="minus" />}
|
|
287
|
+
variant="ghost"
|
|
288
|
+
aria-label={i18n.t("ui.imagePreview.zoomOut")}
|
|
289
|
+
onClick={() => step(1 / STEP)}
|
|
290
|
+
/>
|
|
291
|
+
<Button
|
|
292
|
+
data-slot="diagram-preview-zoom-level"
|
|
293
|
+
variant="ghost-muted"
|
|
294
|
+
size="small"
|
|
295
|
+
aria-label={i18n.t("ui.imagePreview.zoomToFit")}
|
|
296
|
+
title={i18n.t("ui.imagePreview.zoomToFit")}
|
|
297
|
+
onClick={() => ease(FIT_VIEW)}
|
|
298
|
+
>
|
|
299
|
+
{percent().format(view().scale * fitRatio())}
|
|
300
|
+
</Button>
|
|
301
|
+
<IconButton
|
|
302
|
+
data-slot="diagram-preview-zoom-in"
|
|
303
|
+
icon={<Icon name="plus" />}
|
|
304
|
+
variant="ghost"
|
|
305
|
+
aria-label={i18n.t("ui.imagePreview.zoomIn")}
|
|
306
|
+
onClick={() => step(STEP)}
|
|
307
|
+
/>
|
|
308
|
+
<div data-slot="diagram-preview-divider" />
|
|
309
|
+
<Kobalte.CloseButton
|
|
310
|
+
ref={close}
|
|
311
|
+
data-slot="diagram-preview-close"
|
|
312
|
+
as={IconButton}
|
|
313
|
+
icon={<Icon name="close" />}
|
|
314
|
+
variant="ghost"
|
|
315
|
+
aria-label={i18n.t("ui.common.close")}
|
|
316
|
+
/>
|
|
317
|
+
</div>
|
|
318
|
+
</Kobalte.Content>
|
|
319
|
+
</div>
|
|
320
|
+
)
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
// Arrow keys move the view over the diagram, so the diagram moves the other way.
|
|
324
|
+
function arrowDirection(key: string) {
|
|
325
|
+
if (key === "ArrowLeft") return { x: 1, y: 0 }
|
|
326
|
+
|
|
327
|
+
if (key === "ArrowRight") return { x: -1, y: 0 }
|
|
328
|
+
|
|
329
|
+
if (key === "ArrowUp") return { x: 0, y: 1 }
|
|
330
|
+
|
|
331
|
+
if (key === "ArrowDown") return { x: 0, y: -1 }
|
|
332
|
+
}
|
|
@@ -40,8 +40,10 @@ import {
|
|
|
40
40
|
maxScale,
|
|
41
41
|
panBy,
|
|
42
42
|
viewBox,
|
|
43
|
+
wheelPixels,
|
|
43
44
|
wheelZoomFactor,
|
|
44
45
|
zoomAt,
|
|
46
|
+
zoomShortcut,
|
|
45
47
|
type ZoomView,
|
|
46
48
|
} from "./image-zoom"
|
|
47
49
|
import {
|
|
@@ -657,7 +659,7 @@ export function ImagePreview(props: ImagePreviewProps) {
|
|
|
657
659
|
const current = view()
|
|
658
660
|
|
|
659
661
|
const next = pinch
|
|
660
|
-
? zoomAt(current, current.scale * wheelZoomFactor(
|
|
662
|
+
? zoomAt(current, current.scale * wheelZoomFactor(wheelPixels(event, event.deltaY)), point(event), bounds)
|
|
661
663
|
: panBy(current, wheelPan(event), bounds)
|
|
662
664
|
|
|
663
665
|
follow(next.scale, next.x, next.y)
|
|
@@ -682,7 +684,7 @@ export function ImagePreview(props: ImagePreviewProps) {
|
|
|
682
684
|
return props.video.pause()
|
|
683
685
|
}
|
|
684
686
|
|
|
685
|
-
const action = bare ? (event.key === "0" ? "fit" : undefined) : event.altKey ? undefined :
|
|
687
|
+
const action = bare ? (event.key === "0" ? "fit" : undefined) : event.altKey ? undefined : zoomShortcut(event.key)
|
|
686
688
|
|
|
687
689
|
if (!action) return
|
|
688
690
|
event.preventDefault()
|
|
@@ -895,28 +897,12 @@ function layout(element: HTMLElement, frame: Box, radius: number) {
|
|
|
895
897
|
element.style.borderRadius = `${radius}px`
|
|
896
898
|
}
|
|
897
899
|
|
|
898
|
-
function shortcut(key: string) {
|
|
899
|
-
if (key === "=" || key === "+") return "in"
|
|
900
|
-
|
|
901
|
-
if (key === "-" || key === "_") return "out"
|
|
902
|
-
|
|
903
|
-
if (key === "0") return "fit"
|
|
904
|
-
}
|
|
905
|
-
|
|
906
900
|
// A plain wheel pans; Shift turns a vertical wheel sideways.
|
|
907
901
|
function wheelPan(event: WheelEvent) {
|
|
908
902
|
const horizontal = event.shiftKey && event.deltaX === 0
|
|
909
903
|
|
|
910
904
|
return {
|
|
911
|
-
x: -
|
|
912
|
-
y: horizontal ? 0 : -
|
|
905
|
+
x: -wheelPixels(event, horizontal ? event.deltaY : event.deltaX),
|
|
906
|
+
y: horizontal ? 0 : -wheelPixels(event, event.deltaY),
|
|
913
907
|
}
|
|
914
908
|
}
|
|
915
|
-
|
|
916
|
-
function pixels(event: WheelEvent, delta: number) {
|
|
917
|
-
if (event.deltaMode === WheelEvent.DOM_DELTA_LINE) return delta * 16
|
|
918
|
-
|
|
919
|
-
if (event.deltaMode === WheelEvent.DOM_DELTA_PAGE) return delta * window.innerHeight
|
|
920
|
-
|
|
921
|
-
return delta
|
|
922
|
-
}
|
|
@@ -85,6 +85,24 @@ export function wheelZoomFactor(deltaY: number) {
|
|
|
85
85
|
return Math.exp(-clamp(deltaY, -30, 30) / 100)
|
|
86
86
|
}
|
|
87
87
|
|
|
88
|
+
/** The zoom a Cmd/Ctrl shortcut asks for, from the key it is pressed with. */
|
|
89
|
+
export function zoomShortcut(key: string) {
|
|
90
|
+
if (key === "=" || key === "+") return "in"
|
|
91
|
+
|
|
92
|
+
if (key === "-" || key === "_") return "out"
|
|
93
|
+
|
|
94
|
+
if (key === "0") return "fit"
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/** A wheel event's delta in pixels, whether it counts in pixels, lines or pages. */
|
|
98
|
+
export function wheelPixels(event: WheelEvent, delta: number) {
|
|
99
|
+
if (event.deltaMode === WheelEvent.DOM_DELTA_LINE) return delta * 16
|
|
100
|
+
|
|
101
|
+
if (event.deltaMode === WheelEvent.DOM_DELTA_PAGE) return delta * window.innerHeight
|
|
102
|
+
|
|
103
|
+
return delta
|
|
104
|
+
}
|
|
105
|
+
|
|
88
106
|
/** Keeps one axis of a zoomed image of `size` covering a `viewport` it overflows, and centered in one it fits. */
|
|
89
107
|
export function clampAxis(offset: number, size: number, viewport: number) {
|
|
90
108
|
const limit = Math.max(0, (size - viewport) / 2)
|
package/src/i18n/en.ts
CHANGED
|
@@ -252,6 +252,8 @@ const source = {
|
|
|
252
252
|
"ui.imagePreview.zoomIn": "Zoom in",
|
|
253
253
|
"ui.imagePreview.zoomOut": "Zoom out",
|
|
254
254
|
"ui.imagePreview.zoomToFit": "Zoom to fit",
|
|
255
|
+
"ui.diagramPreview.label": "Diagram",
|
|
256
|
+
"ui.diagramPreview.open": "Maximize diagram",
|
|
255
257
|
"ui.scrollView.ariaLabel": "scrollable content",
|
|
256
258
|
|
|
257
259
|
"ui.tool.read": "Read",
|
package/src/icons/catalog.ts
CHANGED
|
@@ -262,6 +262,10 @@ const icons = {
|
|
|
262
262
|
viewBox: "0 0 16 16",
|
|
263
263
|
body: `<path d="M2 8.5V2H14V14H8M5 5.66667H10.3333V11M3.66797 12.333L9.94292 6.05806" stroke="currentColor" stroke-linecap="square"/>`,
|
|
264
264
|
},
|
|
265
|
+
"outline-maximize": {
|
|
266
|
+
viewBox: "0 0 16 16",
|
|
267
|
+
body: `<path d="M2.5 6V2.5H6M10 2.5H13.5V6M13.5 10V13.5H10M6 13.5H2.5V10" stroke="currentColor" stroke-linecap="square"/>`,
|
|
268
|
+
},
|
|
265
269
|
"outline-hexagonal-warning": {
|
|
266
270
|
viewBox: "0 0 16 16",
|
|
267
271
|
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"/>`,
|
package/src/styles/index.css
CHANGED
|
@@ -19,6 +19,7 @@
|
|
|
19
19
|
@import "../components/hover-card.css" layer(components);
|
|
20
20
|
@import "../components/provider-icon.css" layer(components);
|
|
21
21
|
@import "../components/dock-surface.css" layer(components);
|
|
22
|
+
@import "../components/diagram-preview.css" layer(components);
|
|
22
23
|
@import "../components/image-preview.css" layer(components);
|
|
23
24
|
@import "../forms/inline-input/inline-input.css" layer(components);
|
|
24
25
|
@import "../components/text-field.css" layer(components);
|