@opencode/ui 2.2.7 → 2.3.0

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.
Files changed (151) hide show
  1. package/dist/components/diagram-preview.d.ts +9 -0
  2. package/dist/components/image-zoom.d.ts +4 -0
  3. package/dist/components/provider-icon.d.ts +2 -0
  4. package/dist/data-display/badge/badge.d.ts +1 -1
  5. package/dist/data-display/diff-changes/diff-changes.d.ts +2 -0
  6. package/dist/data-display/project-avatar/project-avatar.d.ts +2 -0
  7. package/dist/feedback/toast/toast.d.ts +2 -0
  8. package/dist/forms/redirect-paste/redirect-paste.d.ts +23 -0
  9. package/dist/i18n/am.d.ts +1 -0
  10. package/dist/i18n/ar.d.ts +1 -0
  11. package/dist/i18n/az.d.ts +1 -0
  12. package/dist/i18n/bg.d.ts +1 -0
  13. package/dist/i18n/bn.d.ts +1 -0
  14. package/dist/i18n/br.d.ts +1 -0
  15. package/dist/i18n/bs.d.ts +1 -0
  16. package/dist/i18n/ca.d.ts +1 -0
  17. package/dist/i18n/cs.d.ts +1 -0
  18. package/dist/i18n/da.d.ts +1 -0
  19. package/dist/i18n/de.d.ts +1 -0
  20. package/dist/i18n/dv.d.ts +1 -0
  21. package/dist/i18n/dz.d.ts +1 -0
  22. package/dist/i18n/el.d.ts +1 -0
  23. package/dist/i18n/en.d.ts +52 -0
  24. package/dist/i18n/es.d.ts +1 -0
  25. package/dist/i18n/et.d.ts +1 -0
  26. package/dist/i18n/fa.d.ts +1 -0
  27. package/dist/i18n/fi.d.ts +1 -0
  28. package/dist/i18n/fo.d.ts +1 -0
  29. package/dist/i18n/fr.d.ts +1 -0
  30. package/dist/i18n/he.d.ts +1 -0
  31. package/dist/i18n/hi.d.ts +1 -0
  32. package/dist/i18n/hr.d.ts +1 -0
  33. package/dist/i18n/hu.d.ts +1 -0
  34. package/dist/i18n/hy.d.ts +1 -0
  35. package/dist/i18n/id.d.ts +1 -0
  36. package/dist/i18n/is.d.ts +1 -0
  37. package/dist/i18n/it.d.ts +1 -0
  38. package/dist/i18n/ja.d.ts +1 -0
  39. package/dist/i18n/ka.d.ts +1 -0
  40. package/dist/i18n/km.d.ts +1 -0
  41. package/dist/i18n/ko.d.ts +1 -0
  42. package/dist/i18n/lo.d.ts +1 -0
  43. package/dist/i18n/lt.d.ts +1 -0
  44. package/dist/i18n/lv.d.ts +1 -0
  45. package/dist/i18n/mk.d.ts +1 -0
  46. package/dist/i18n/mn.d.ts +1 -0
  47. package/dist/i18n/ms.d.ts +1 -0
  48. package/dist/i18n/my.d.ts +1 -0
  49. package/dist/i18n/ne.d.ts +1 -0
  50. package/dist/i18n/nl.d.ts +1 -0
  51. package/dist/i18n/pa.d.ts +1 -0
  52. package/dist/i18n/pl.d.ts +1 -0
  53. package/dist/i18n/ro.d.ts +1 -0
  54. package/dist/i18n/ru.d.ts +1 -0
  55. package/dist/i18n/si.d.ts +1 -0
  56. package/dist/i18n/sk.d.ts +1 -0
  57. package/dist/i18n/sl.d.ts +1 -0
  58. package/dist/i18n/sq.d.ts +1 -0
  59. package/dist/i18n/sr.d.ts +1 -0
  60. package/dist/i18n/sv.d.ts +1 -0
  61. package/dist/i18n/tg.d.ts +1 -0
  62. package/dist/i18n/th.d.ts +1 -0
  63. package/dist/i18n/tk.d.ts +1 -0
  64. package/dist/i18n/tr.d.ts +1 -0
  65. package/dist/i18n/uk.d.ts +1 -0
  66. package/dist/i18n/ur.d.ts +1 -0
  67. package/dist/i18n/uz.d.ts +1 -0
  68. package/dist/i18n/vi.d.ts +1 -0
  69. package/dist/i18n/zh.d.ts +1 -0
  70. package/dist/i18n/zht.d.ts +1 -0
  71. package/dist/icons/catalog.d.ts +4 -0
  72. package/package.json +7 -3
  73. package/src/components/diagram-preview.css +95 -0
  74. package/src/components/diagram-preview.tsx +332 -0
  75. package/src/components/image-preview.tsx +6 -20
  76. package/src/components/image-zoom.ts +18 -0
  77. package/src/components/provider-icon.tsx +7 -2
  78. package/src/data-display/badge/badge.css +6 -0
  79. package/src/data-display/badge/badge.tsx +1 -1
  80. package/src/data-display/diff-changes/diff-changes.tsx +10 -2
  81. package/src/data-display/project-avatar/project-avatar.css +13 -0
  82. package/src/data-display/project-avatar/project-avatar.tsx +14 -1
  83. package/src/feedback/toast/toast.tsx +7 -1
  84. package/src/forms/redirect-paste/redirect-paste.tsx +75 -0
  85. package/src/i18n/am.ts +1 -0
  86. package/src/i18n/ar.ts +1 -0
  87. package/src/i18n/az.ts +1 -0
  88. package/src/i18n/bg.ts +1 -0
  89. package/src/i18n/bn.ts +1 -0
  90. package/src/i18n/br.ts +1 -0
  91. package/src/i18n/bs.ts +1 -0
  92. package/src/i18n/ca.ts +1 -0
  93. package/src/i18n/cs.ts +1 -0
  94. package/src/i18n/da.ts +1 -0
  95. package/src/i18n/de.ts +1 -0
  96. package/src/i18n/dv.ts +1 -0
  97. package/src/i18n/dz.ts +1 -0
  98. package/src/i18n/el.ts +1 -0
  99. package/src/i18n/en.ts +53 -0
  100. package/src/i18n/es.ts +1 -0
  101. package/src/i18n/et.ts +1 -0
  102. package/src/i18n/fa.ts +1 -0
  103. package/src/i18n/fi.ts +1 -0
  104. package/src/i18n/fo.ts +1 -0
  105. package/src/i18n/fr.ts +1 -0
  106. package/src/i18n/he.ts +1 -0
  107. package/src/i18n/hi.ts +1 -0
  108. package/src/i18n/hr.ts +1 -0
  109. package/src/i18n/hu.ts +1 -0
  110. package/src/i18n/hy.ts +1 -0
  111. package/src/i18n/id.ts +1 -0
  112. package/src/i18n/is.ts +1 -0
  113. package/src/i18n/it.ts +1 -0
  114. package/src/i18n/ja.ts +1 -0
  115. package/src/i18n/ka.ts +1 -0
  116. package/src/i18n/km.ts +1 -0
  117. package/src/i18n/ko.ts +1 -0
  118. package/src/i18n/lo.ts +1 -0
  119. package/src/i18n/lt.ts +1 -0
  120. package/src/i18n/lv.ts +1 -0
  121. package/src/i18n/mk.ts +1 -0
  122. package/src/i18n/mn.ts +1 -0
  123. package/src/i18n/ms.ts +1 -0
  124. package/src/i18n/my.ts +1 -0
  125. package/src/i18n/ne.ts +1 -0
  126. package/src/i18n/nl.ts +1 -0
  127. package/src/i18n/no.ts +1 -0
  128. package/src/i18n/pa.ts +1 -0
  129. package/src/i18n/pl.ts +1 -0
  130. package/src/i18n/ro.ts +1 -0
  131. package/src/i18n/ru.ts +1 -0
  132. package/src/i18n/si.ts +1 -0
  133. package/src/i18n/sk.ts +1 -0
  134. package/src/i18n/sl.ts +1 -0
  135. package/src/i18n/sq.ts +1 -0
  136. package/src/i18n/sr.ts +1 -0
  137. package/src/i18n/sv.ts +1 -0
  138. package/src/i18n/tg.ts +1 -0
  139. package/src/i18n/th.ts +1 -0
  140. package/src/i18n/tk.ts +1 -0
  141. package/src/i18n/tr.ts +1 -0
  142. package/src/i18n/uk.ts +1 -0
  143. package/src/i18n/ur.ts +1 -0
  144. package/src/i18n/uz.ts +1 -0
  145. package/src/i18n/vi.ts +1 -0
  146. package/src/i18n/zh.ts +1 -0
  147. package/src/i18n/zht.ts +1 -0
  148. package/src/icons/catalog.ts +4 -0
  149. package/src/icons/icon/additional-icons.ts +4 -2
  150. package/src/icons/new-icons.ts +0 -1
  151. package/src/styles/index.css +1 -0
package/dist/i18n/sr.d.ts CHANGED
@@ -173,6 +173,7 @@ export declare const dict: {
173
173
  "ui.common.dismiss": string;
174
174
  "ui.common.close": string;
175
175
  "ui.common.next": string;
176
+ "ui.common.continue": string;
176
177
  "ui.common.submit": string;
177
178
  "ui.common.showMore": string;
178
179
  "ui.permission.deny": string;
package/dist/i18n/sv.d.ts CHANGED
@@ -196,6 +196,7 @@ export declare const dict: {
196
196
  "ui.common.dismiss": string;
197
197
  "ui.common.close": string;
198
198
  "ui.common.next": string;
199
+ "ui.common.continue": string;
199
200
  "ui.common.submit": string;
200
201
  "ui.common.showMore": string;
201
202
  "ui.permission.deny": string;
package/dist/i18n/tg.d.ts CHANGED
@@ -159,6 +159,7 @@ export declare const dict: {
159
159
  "ui.common.dismiss": string;
160
160
  "ui.common.close": string;
161
161
  "ui.common.next": string;
162
+ "ui.common.continue": string;
162
163
  "ui.common.submit": string;
163
164
  "ui.common.showMore": string;
164
165
  "ui.permission.deny": string;
package/dist/i18n/th.d.ts CHANGED
@@ -155,6 +155,7 @@ export declare const dict: {
155
155
  "ui.common.dismiss": string;
156
156
  "ui.common.close": string;
157
157
  "ui.common.next": string;
158
+ "ui.common.continue": string;
158
159
  "ui.common.submit": string;
159
160
  "ui.common.showMore": string;
160
161
  "ui.permission.deny": string;
package/dist/i18n/tk.d.ts CHANGED
@@ -159,6 +159,7 @@ export declare const dict: {
159
159
  "ui.common.dismiss": string;
160
160
  "ui.common.close": string;
161
161
  "ui.common.next": string;
162
+ "ui.common.continue": string;
162
163
  "ui.common.submit": string;
163
164
  "ui.common.showMore": string;
164
165
  "ui.permission.deny": string;
package/dist/i18n/tr.d.ts CHANGED
@@ -156,6 +156,7 @@ export declare const dict: {
156
156
  "ui.common.dismiss": string;
157
157
  "ui.common.close": string;
158
158
  "ui.common.next": string;
159
+ "ui.common.continue": string;
159
160
  "ui.common.submit": string;
160
161
  "ui.common.showMore": string;
161
162
  "ui.common.moreCount.one": string;
package/dist/i18n/uk.d.ts CHANGED
@@ -187,6 +187,7 @@ export declare const dict: {
187
187
  "ui.common.dismiss": string;
188
188
  "ui.common.close": string;
189
189
  "ui.common.next": string;
190
+ "ui.common.continue": string;
190
191
  "ui.common.submit": string;
191
192
  "ui.common.showMore": string;
192
193
  "ui.permission.deny": string;
package/dist/i18n/ur.d.ts CHANGED
@@ -159,6 +159,7 @@ export declare const dict: {
159
159
  "ui.common.dismiss": string;
160
160
  "ui.common.close": string;
161
161
  "ui.common.next": string;
162
+ "ui.common.continue": string;
162
163
  "ui.common.submit": string;
163
164
  "ui.common.showMore": string;
164
165
  "ui.permission.deny": string;
package/dist/i18n/uz.d.ts CHANGED
@@ -159,6 +159,7 @@ export declare const dict: {
159
159
  "ui.common.dismiss": string;
160
160
  "ui.common.close": string;
161
161
  "ui.common.next": string;
162
+ "ui.common.continue": string;
162
163
  "ui.common.submit": string;
163
164
  "ui.common.showMore": string;
164
165
  "ui.permission.deny": string;
package/dist/i18n/vi.d.ts CHANGED
@@ -164,6 +164,7 @@ export declare const dict: {
164
164
  "ui.common.dismiss": string;
165
165
  "ui.common.close": string;
166
166
  "ui.common.next": string;
167
+ "ui.common.continue": string;
167
168
  "ui.common.submit": string;
168
169
  "ui.common.showMore": string;
169
170
  "ui.permission.deny": string;
package/dist/i18n/zh.d.ts CHANGED
@@ -152,6 +152,7 @@ export declare const dict: {
152
152
  "ui.common.dismiss": string;
153
153
  "ui.common.close": string;
154
154
  "ui.common.next": string;
155
+ "ui.common.continue": string;
155
156
  "ui.common.submit": string;
156
157
  "ui.common.showMore": string;
157
158
  "ui.permission.deny": string;
@@ -152,6 +152,7 @@ export declare const dict: {
152
152
  "ui.common.dismiss": string;
153
153
  "ui.common.close": string;
154
154
  "ui.common.next": string;
155
+ "ui.common.continue": string;
155
156
  "ui.common.submit": string;
156
157
  "ui.common.showMore": string;
157
158
  "ui.permission.deny": string;
@@ -260,6 +260,10 @@ declare const icons: {
260
260
  viewBox: string;
261
261
  body: string;
262
262
  };
263
+ "outline-maximize": {
264
+ viewBox: string;
265
+ body: string;
266
+ };
263
267
  "outline-hexagonal-warning": {
264
268
  viewBox: string;
265
269
  body: string;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@opencode/ui",
3
- "version": "2.2.7",
3
+ "version": "2.3.0",
4
4
  "type": "module",
5
5
  "license": "MIT",
6
6
  "repository": {
@@ -154,6 +154,10 @@
154
154
  "types": "./dist/forms/radio/radio.d.ts",
155
155
  "import": "./src/forms/radio/radio.tsx"
156
156
  },
157
+ "./redirect-paste": {
158
+ "types": "./dist/forms/redirect-paste/redirect-paste.d.ts",
159
+ "import": "./src/forms/redirect-paste/redirect-paste.tsx"
160
+ },
157
161
  "./search-list": {
158
162
  "types": "./dist/forms/search-list/search-list.d.ts",
159
163
  "import": "./src/forms/search-list/search-list.tsx"
@@ -290,7 +294,7 @@
290
294
  "generate:v2-oc2": "bun run script/build-oc2-v2-overrides.ts"
291
295
  },
292
296
  "devDependencies": {
293
- "@hona/strobe": "0.28.0",
297
+ "@hona/strobe": "0.34.0",
294
298
  "@solidjs/meta": "0.29.4",
295
299
  "@tsconfig/node22": "22.0.2",
296
300
  "@types/bun": "1.4.3",
@@ -305,7 +309,7 @@
305
309
  "dependencies": {
306
310
  "@kitlangton/solid-motion": "0.2.2",
307
311
  "@kobalte/core": "0.13.13",
308
- "@opencode/util": "2.2.7",
312
+ "@opencode/util": "2.3.0",
309
313
  "@pierre/diffs": "1.6.1",
310
314
  "@pierre/highlights": "0.2.0",
311
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(pixels(event, event.deltaY)), point(event), bounds)
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 : shortcut(event.key)
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: -pixels(event, horizontal ? event.deltaY : event.deltaX),
912
- y: horizontal ? 0 : -pixels(event, event.deltaY),
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)