@weasel-js/labkit 1.3.0 → 1.4.0-pre.1

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 (211) hide show
  1. package/README.md +226 -55
  2. package/dist/_dts/CanvasStackContext-LnCfqNBA.d.ts +42 -0
  3. package/dist/_dts/DrawCommand-B3bskUsC.d.ts +564 -0
  4. package/dist/_dts/{PrefsForm-BYa6cWnO.d.ts → PrefsForm-BkUJZx0A.d.ts} +4 -1
  5. package/dist/_dts/fitViewToBounds-dZ2UDB6e.d.ts +21 -0
  6. package/dist/_dts/frac-C-2c72Ij.d.ts +735 -0
  7. package/dist/_dts/index-BDVzvRzQ.d.ts +236 -0
  8. package/dist/_dts/shapeKinds-Cx_rxwsa.d.ts +87 -0
  9. package/dist/_dts/{DrawCommand-DBB45NfN.d.ts → types-C-gh9Ap-.d.ts} +1 -582
  10. package/dist/_dts/{types-DCGEcMO_.d.ts → types-D6s4b7if.d.ts} +1 -1
  11. package/dist/_dts/{types-1Sdxy_Pv.d.ts → types-DYMaEvM5.d.ts} +26 -1
  12. package/dist/_dts/{useTrialState-CsGMjhu9.d.ts → useTrialState-gmMvZqPc.d.ts} +7 -2
  13. package/dist/canvas/index.d.ts +110 -26
  14. package/dist/canvas/index.js +5 -4
  15. package/dist/chrome/index.d.ts +9 -6
  16. package/dist/chrome/index.js +6 -6
  17. package/dist/chunk-2AYGEN57.js +32 -0
  18. package/dist/chunk-2AYGEN57.js.map +1 -0
  19. package/dist/chunk-6OEYTYML.js +669 -0
  20. package/dist/chunk-6OEYTYML.js.map +1 -0
  21. package/dist/{chunk-BMW4TDP5.js → chunk-AE5CNVRU.js} +3 -3
  22. package/dist/{chunk-BMW4TDP5.js.map → chunk-AE5CNVRU.js.map} +1 -1
  23. package/dist/{chunk-CPUJ3QXL.js → chunk-CQLQPQ4P.js} +2 -2
  24. package/dist/chunk-CQLQPQ4P.js.map +1 -0
  25. package/dist/chunk-CTRKTLYZ.js +33 -0
  26. package/dist/chunk-CTRKTLYZ.js.map +1 -0
  27. package/dist/chunk-E2UQVZ44.js +458 -0
  28. package/dist/chunk-E2UQVZ44.js.map +1 -0
  29. package/dist/{chunk-LGKRIUSW.js → chunk-ESIQNQ6K.js} +32 -7
  30. package/dist/chunk-ESIQNQ6K.js.map +1 -0
  31. package/dist/{chunk-JDROYQM3.js → chunk-HHGVISVZ.js} +130 -131
  32. package/dist/chunk-HHGVISVZ.js.map +1 -0
  33. package/dist/{chunk-BDJXIBRZ.js → chunk-HMFODCOX.js} +126 -29
  34. package/dist/chunk-HMFODCOX.js.map +1 -0
  35. package/dist/{chunk-QCICIHEO.js → chunk-L5QJOOLV.js} +839 -320
  36. package/dist/chunk-L5QJOOLV.js.map +1 -0
  37. package/dist/{chunk-3XXPU73K.js → chunk-M75ZU6ZZ.js} +9 -7
  38. package/dist/chunk-M75ZU6ZZ.js.map +1 -0
  39. package/dist/{chunk-QRYPXSGP.js → chunk-QB5SM4BM.js} +16 -8
  40. package/dist/chunk-QB5SM4BM.js.map +1 -0
  41. package/dist/chunk-RBGKL7NF.js +7128 -0
  42. package/dist/chunk-RBGKL7NF.js.map +1 -0
  43. package/dist/{chunk-WTI26YTM.js → chunk-TO2FUOKF.js} +40 -13
  44. package/dist/chunk-TO2FUOKF.js.map +1 -0
  45. package/dist/{chunk-4TMMVIDM.js → chunk-XJ6N32QP.js} +50 -6
  46. package/dist/chunk-XJ6N32QP.js.map +1 -0
  47. package/dist/controls/index.d.ts +4 -28
  48. package/dist/controls/index.js +3 -3
  49. package/dist/dragdrop/index.d.ts +9 -5
  50. package/dist/dragdrop/index.js +3 -3
  51. package/dist/index.d.ts +517 -234
  52. package/dist/index.js +1769 -143
  53. package/dist/index.js.map +1 -1
  54. package/dist/layers/index.d.ts +6 -5
  55. package/dist/layers/index.js +3 -3
  56. package/dist/loupe/index.d.ts +187 -0
  57. package/dist/loupe/index.js +7 -0
  58. package/dist/loupe/index.js.map +1 -0
  59. package/dist/passthrough/weasel-canvas.d.ts +3 -1
  60. package/dist/passthrough/weasel-canvas.js +1 -1
  61. package/dist/passthrough/weasel-ui.d.ts +138 -91
  62. package/dist/passthrough/weasel-ui.js +2 -2
  63. package/dist/primitives/index.d.ts +3 -1
  64. package/dist/primitives/index.js +5 -5
  65. package/dist/state/index.d.ts +3 -3
  66. package/dist/state/index.js +2 -2
  67. package/dist/styles.css +229 -1
  68. package/dist/surface/index.d.ts +18 -2
  69. package/dist/surface/index.js +2 -2
  70. package/dist/ui/layers/index.js +2 -2
  71. package/dist/undo/index.d.ts +5 -4
  72. package/package.json +14 -7
  73. package/src/annotations/AnnotationOverlay.tsx +205 -0
  74. package/src/annotations/AnnotationTargets.tsx +37 -0
  75. package/src/annotations/Annotations.less +129 -0
  76. package/src/annotations/Annotations.meaning.test.tsx +111 -0
  77. package/src/annotations/Annotations.overlay.test.tsx +142 -0
  78. package/src/annotations/AnnotationsContext.ts +49 -0
  79. package/src/annotations/ExportMenu.test.tsx +86 -0
  80. package/src/annotations/ExportMenu.tsx +182 -0
  81. package/src/annotations/MarkList.tsx +75 -0
  82. package/src/annotations/capture.test.ts +140 -0
  83. package/src/annotations/capture.ts +237 -0
  84. package/src/annotations/drawOne.test.ts +56 -0
  85. package/src/annotations/drawOne.ts +47 -0
  86. package/src/annotations/frac.test.ts +59 -0
  87. package/src/annotations/frac.ts +66 -0
  88. package/src/annotations/history.test.ts +82 -0
  89. package/src/annotations/history.ts +118 -0
  90. package/src/annotations/index.ts +37 -0
  91. package/src/annotations/paint.test.ts +125 -0
  92. package/src/annotations/paint.ts +130 -0
  93. package/src/annotations/staleness.test.ts +56 -0
  94. package/src/annotations/staleness.ts +41 -0
  95. package/src/annotations/store.test.ts +247 -0
  96. package/src/annotations/store.ts +347 -0
  97. package/src/annotations/svgNodes.test.ts +77 -0
  98. package/src/annotations/svgNodes.ts +74 -0
  99. package/src/annotations/toolMap.test.ts +36 -0
  100. package/src/annotations/toolMap.ts +54 -0
  101. package/src/annotations/types.ts +229 -0
  102. package/src/annotations/view.test.ts +37 -0
  103. package/src/annotations/view.ts +40 -0
  104. package/src/canvas/AGENTS.md +55 -5
  105. package/src/canvas/CanvasStack.test.tsx +27 -1
  106. package/src/canvas/CanvasStack.tsx +29 -4
  107. package/src/canvas/CanvasStackContext.ts +23 -4
  108. package/src/canvas/camera.test.ts +78 -0
  109. package/src/canvas/camera.ts +62 -0
  110. package/src/canvas/canvasCoords.test.ts +26 -0
  111. package/src/canvas/canvasCoords.ts +20 -7
  112. package/src/canvas/index.ts +16 -1
  113. package/src/canvas/useLayerScheduler.ts +7 -3
  114. package/src/canvas/usePanZoom.test.ts +36 -5
  115. package/src/canvas/usePanZoom.ts +20 -15
  116. package/src/canvas/worldSpec.test.ts +63 -0
  117. package/src/canvas/worldSpec.ts +48 -0
  118. package/src/chrome/builtins.test.ts +31 -1
  119. package/src/chrome/builtins.tsx +63 -19
  120. package/src/chrome/regions/PaletteRegion.test.tsx +22 -1
  121. package/src/chrome/regions/PaletteRegion.tsx +4 -0
  122. package/src/chrome/regions/SidebarRegion.tsx +37 -13
  123. package/src/chrome/regions/ToolbarRegion.tsx +2 -1
  124. package/src/chrome/regions/ViewportRegion.tsx +9 -1
  125. package/src/chrome/regions/regions.test.tsx +6 -1
  126. package/src/chrome/types.ts +20 -0
  127. package/src/controls/ControlPanel.test.tsx +38 -0
  128. package/src/controls/ControlPanel.tsx +50 -3
  129. package/src/dragdrop/DragDropRuntime.tsx +8 -2
  130. package/src/dragdrop/dragDrop.test.tsx +29 -2
  131. package/src/index.test.ts +23 -0
  132. package/src/index.ts +93 -4
  133. package/src/instrument/SineWave.smoke.test.tsx +1 -0
  134. package/src/instrument/types.ts +24 -1
  135. package/src/lab/Lab.surface.test.tsx +162 -0
  136. package/src/lab/Lab.tsx +102 -17
  137. package/src/lab/Lab.undock.test.tsx +85 -0
  138. package/src/lab/LabFullChrome.stories.tsx +20 -0
  139. package/src/lab/LabShell.less +11 -0
  140. package/src/lab/Workspace.less +73 -0
  141. package/src/lab/Workspace.surface.test.tsx +2 -0
  142. package/src/lab/Workspace.tsx +97 -8
  143. package/src/lab/index.ts +1 -1
  144. package/src/lab/panelHost.ts +37 -0
  145. package/src/loupe/AGENTS.md +49 -0
  146. package/src/loupe/CanvasLoupe.tsx +83 -0
  147. package/src/loupe/DomLoupe.tsx +62 -0
  148. package/src/loupe/Loupe.less +38 -0
  149. package/src/loupe/LoupeBubble.tsx +32 -0
  150. package/src/loupe/TrialLoupe.tsx +93 -0
  151. package/src/loupe/canvasLens.test.ts +205 -0
  152. package/src/loupe/canvasLens.ts +141 -0
  153. package/src/loupe/index.ts +19 -0
  154. package/src/loupe/types.test.ts +29 -0
  155. package/src/loupe/types.ts +90 -0
  156. package/src/loupe/useHostSize.ts +30 -0
  157. package/src/loupe/useLoupe.test.tsx +197 -0
  158. package/src/loupe/useLoupe.ts +189 -0
  159. package/src/passthrough/weasel-ui.ts +3 -0
  160. package/src/primitives/FloatingPanel.test.tsx +76 -1
  161. package/src/primitives/FloatingPanel.tsx +19 -3
  162. package/src/primitives/ScaleIndicator.test.tsx +7 -2
  163. package/src/primitives/Sidebar.less +18 -0
  164. package/src/primitives/Toolbar.less +8 -0
  165. package/src/primitives/Toolbar.tsx +5 -0
  166. package/src/primitives/useRovingTabIndex.test.ts +21 -0
  167. package/src/primitives/useRovingTabIndex.ts +26 -17
  168. package/src/state/document.test.ts +23 -0
  169. package/src/state/document.ts +14 -2
  170. package/src/state/index.ts +2 -0
  171. package/src/state/store.test.ts +24 -0
  172. package/src/state/store.ts +35 -1
  173. package/src/state/types.ts +11 -0
  174. package/src/state/undock.test.ts +40 -0
  175. package/src/state/undock.ts +39 -0
  176. package/src/styles.less +2 -0
  177. package/src/surface/SurfaceContext.ts +4 -0
  178. package/src/surface/index.ts +8 -3
  179. package/src/surface/useSurfaceTile.test.tsx +2 -0
  180. package/src/surface/useSurfaceTile.ts +7 -1
  181. package/src/surface/useTiledSurface.test.tsx +58 -0
  182. package/src/surface/useTiledSurface.ts +38 -3
  183. package/src/trial/Trial.annotations.persist.test.tsx +166 -0
  184. package/src/trial/Trial.annotations.test.tsx +121 -0
  185. package/src/trial/Trial.canvas.test.tsx +76 -1
  186. package/src/trial/Trial.less +19 -0
  187. package/src/trial/Trial.loupe.test.tsx +128 -0
  188. package/src/trial/Trial.stories.tsx +1 -0
  189. package/src/trial/Trial.test.tsx +12 -0
  190. package/src/trial/Trial.trialId.test.tsx +36 -0
  191. package/src/trial/Trial.tsx +215 -40
  192. package/src/trial/TrialChrome.tsx +35 -0
  193. package/src/trial/UndockedSections.tsx +65 -0
  194. package/src/trial/trialOps.test.ts +21 -0
  195. package/src/trial/trialOps.ts +5 -3
  196. package/dist/_dts/types-BttTIent.d.ts +0 -314
  197. package/dist/chunk-3XXPU73K.js.map +0 -1
  198. package/dist/chunk-4TMMVIDM.js.map +0 -1
  199. package/dist/chunk-6X5RBC6G.js +0 -366
  200. package/dist/chunk-6X5RBC6G.js.map +0 -1
  201. package/dist/chunk-BDJXIBRZ.js.map +0 -1
  202. package/dist/chunk-CPUJ3QXL.js.map +0 -1
  203. package/dist/chunk-JDROYQM3.js.map +0 -1
  204. package/dist/chunk-LGKRIUSW.js.map +0 -1
  205. package/dist/chunk-QCICIHEO.js.map +0 -1
  206. package/dist/chunk-QRYPXSGP.js.map +0 -1
  207. package/dist/chunk-RGT4EJIB.js +0 -6961
  208. package/dist/chunk-RGT4EJIB.js.map +0 -1
  209. package/dist/chunk-T66HU3HX.js +0 -17
  210. package/dist/chunk-T66HU3HX.js.map +0 -1
  211. package/dist/chunk-WTI26YTM.js.map +0 -1
@@ -0,0 +1,458 @@
1
+ import { centerOn, zoomAt } from './chunk-CTRKTLYZ.js';
2
+ import { resolveFrame, screenToWorld, DEFAULT_FRAME } from './chunk-2AYGEN57.js';
3
+ import { CanvasStackContext } from './chunk-CQLQPQ4P.js';
4
+ import { useVisibleRaf } from './chunk-L5QJOOLV.js';
5
+ import { useRef, useEffect, useReducer, useCallback, useContext, useMemo, useState } from 'react';
6
+ import { jsx } from 'react/jsx-runtime';
7
+
8
+ // src/loupe/canvasLens.ts
9
+ function lensCamera(aim, outer, outerFrame, factor, diameter, worldSpec) {
10
+ const size = { width: diameter, height: diameter };
11
+ const frame = resolveFrame(worldSpec, size);
12
+ const world = screenToWorld(aim, outer, outerFrame);
13
+ return { view: centerOn(world, outer.zoom * factor, size, frame), frame };
14
+ }
15
+ function lensSourceRect(aim, factor, diameter, dpr) {
16
+ const span = diameter / factor;
17
+ return {
18
+ sx: (aim.x - span / 2) * dpr,
19
+ sy: (aim.y - span / 2) * dpr,
20
+ sw: span * dpr,
21
+ sh: span * dpr
22
+ };
23
+ }
24
+ function sampleStack(layers, canvases, p, dpr) {
25
+ for (let i = layers.length - 1; i >= 0; i--) {
26
+ const layer = layers[i];
27
+ if (!layer.visible) continue;
28
+ const canvas = canvases.get(layer.id);
29
+ if (!canvas) continue;
30
+ const ctx = canvas.getContext("2d", { willReadFrequently: true });
31
+ if (!ctx) continue;
32
+ const x = Math.floor(p.x * dpr);
33
+ const y = Math.floor(p.y * dpr);
34
+ if (x < 0 || y < 0 || x >= canvas.width || y >= canvas.height) continue;
35
+ const [r, g, b, a] = ctx.getImageData(x, y, 1, 1).data;
36
+ if (a === 0) continue;
37
+ return `#${[r, g, b].map((c) => c.toString(16).padStart(2, "0")).join("")}`;
38
+ }
39
+ return null;
40
+ }
41
+ function drawCanvasLens(ctx, opts) {
42
+ const { aim, factor, diameter, dpr, mode, outer, layers, canvases } = opts;
43
+ ctx.save();
44
+ ctx.setTransform(dpr, 0, 0, dpr, 0, 0);
45
+ ctx.clearRect(0, 0, diameter, diameter);
46
+ if (mode === "pixel") {
47
+ const { sx, sy, sw, sh } = lensSourceRect(aim, factor, diameter, dpr);
48
+ ctx.imageSmoothingEnabled = false;
49
+ for (const layer of layers) {
50
+ if (!layer.visible) continue;
51
+ const canvas = canvases.get(layer.id);
52
+ if (!canvas || canvas.width === 0 || canvas.height === 0) continue;
53
+ ctx.drawImage(canvas, sx, sy, sw, sh, 0, 0, diameter, diameter);
54
+ }
55
+ ctx.restore();
56
+ return;
57
+ }
58
+ const lens = lensCamera(
59
+ aim,
60
+ outer,
61
+ opts.outerFrame ?? DEFAULT_FRAME,
62
+ factor,
63
+ diameter,
64
+ opts.worldSpec
65
+ );
66
+ for (const layer of layers) {
67
+ if (!layer.visible) continue;
68
+ ctx.save();
69
+ layer.render(ctx, lens.view, lens.frame);
70
+ ctx.restore();
71
+ }
72
+ ctx.restore();
73
+ }
74
+ function CanvasLoupe({
75
+ aim,
76
+ factor,
77
+ mode,
78
+ diameter,
79
+ surface,
80
+ view,
81
+ frame,
82
+ worldSpec
83
+ }) {
84
+ const canvasRef = useRef(null);
85
+ const dpr = surface.size.dpr;
86
+ const args = { aim, factor, mode, diameter, dpr, view, frame, worldSpec, surface };
87
+ const argsRef = useRef(args);
88
+ argsRef.current = args;
89
+ const loop = useVisibleRaf(
90
+ () => {
91
+ loop.request();
92
+ const canvas = canvasRef.current;
93
+ const ctx = canvas?.getContext("2d");
94
+ const canvases = argsRef.current.surface.canvases.current;
95
+ if (!ctx || !canvases) return;
96
+ const a = argsRef.current;
97
+ drawCanvasLens(ctx, {
98
+ aim: a.aim,
99
+ factor: a.factor,
100
+ diameter: a.diameter,
101
+ dpr: a.dpr,
102
+ mode: a.mode,
103
+ outer: a.view,
104
+ outerFrame: a.frame,
105
+ worldSpec: a.worldSpec,
106
+ layers: a.surface.layers,
107
+ canvases
108
+ });
109
+ },
110
+ { target: canvasRef }
111
+ );
112
+ useEffect(() => {
113
+ loop.request();
114
+ return () => loop.cancel();
115
+ }, [loop]);
116
+ return /* @__PURE__ */ jsx(
117
+ "canvas",
118
+ {
119
+ ref: canvasRef,
120
+ className: "lk-loupe__canvas",
121
+ width: Math.max(1, Math.round(diameter * dpr)),
122
+ height: Math.max(1, Math.round(diameter * dpr))
123
+ }
124
+ );
125
+ }
126
+ function DomLoupe({
127
+ aim,
128
+ factor,
129
+ mode,
130
+ diameter,
131
+ size,
132
+ view,
133
+ frame,
134
+ state,
135
+ config,
136
+ render
137
+ }) {
138
+ const style = {
139
+ width: `${size.width}px`,
140
+ height: `${size.height}px`,
141
+ transform: `translate(${diameter / 2 - aim.x}px, ${diameter / 2 - aim.y}px)`
142
+ };
143
+ return /* @__PURE__ */ jsx("div", { className: "lk-loupe__stage", style, children: render({
144
+ state,
145
+ config,
146
+ view: zoomAt(view, factor, aim, { frame }),
147
+ factor,
148
+ mode,
149
+ size
150
+ }) });
151
+ }
152
+ function LoupeBubble({ aim, diameter, hostRef, children }) {
153
+ const style = {
154
+ width: `${diameter}px`,
155
+ height: `${diameter}px`,
156
+ transform: `translate(${aim.x - diameter / 2}px, ${aim.y - diameter / 2}px)`
157
+ };
158
+ return /* @__PURE__ */ jsx("div", { ref: hostRef, className: "lk-loupe", style, "aria-hidden": "true", children });
159
+ }
160
+ var ZERO = { width: 0, height: 0 };
161
+ function useHostSize(ref) {
162
+ const [size, setSize] = useState(ZERO);
163
+ useEffect(() => {
164
+ const el = ref.current;
165
+ if (!el) return;
166
+ const update = () => {
167
+ const rect = el.getBoundingClientRect();
168
+ setSize(
169
+ (prev) => prev.width === rect.width && prev.height === rect.height ? prev : { width: rect.width, height: rect.height }
170
+ );
171
+ };
172
+ update();
173
+ if (typeof ResizeObserver === "undefined") return;
174
+ const ro = new ResizeObserver(update);
175
+ ro.observe(el);
176
+ return () => ro.disconnect();
177
+ }, [ref]);
178
+ return size;
179
+ }
180
+
181
+ // ../loupe/dist/index.js
182
+ function loupeSourcePoint(p, rect, aim, factor) {
183
+ return {
184
+ x: aim.x + (p.x - (rect.x + rect.w / 2)) / factor,
185
+ y: aim.y + (p.y - (rect.y + rect.h / 2)) / factor
186
+ };
187
+ }
188
+ function createLoupeModel(opts) {
189
+ const { surface } = opts;
190
+ let mode = opts.mode ?? "vector";
191
+ let factor = opts.factor ?? 8;
192
+ let aim = { x: 0, y: 0 };
193
+ let color = null;
194
+ let disposed = false;
195
+ const clamp = (n) => Math.min(
196
+ opts.maxFactor ?? Number.POSITIVE_INFINITY,
197
+ Math.max(opts.minFactor ?? Number.NEGATIVE_INFINITY, n)
198
+ );
199
+ const teardown = () => {
200
+ if (disposed) return;
201
+ disposed = true;
202
+ opts.onDispose?.();
203
+ };
204
+ const sampleColor = () => {
205
+ const hex = surface.sample(aim);
206
+ if (hex === null || hex === color) return;
207
+ color = hex;
208
+ opts.onColorChange?.(hex);
209
+ };
210
+ return {
211
+ get mode() {
212
+ return mode;
213
+ },
214
+ get factor() {
215
+ return factor;
216
+ },
217
+ get aim() {
218
+ return aim;
219
+ },
220
+ get color() {
221
+ return color;
222
+ },
223
+ setMode(next) {
224
+ if (disposed || next === mode) return;
225
+ mode = next;
226
+ surface.changed();
227
+ },
228
+ setFactor(next) {
229
+ if (disposed) return;
230
+ factor = clamp(next);
231
+ surface.changed();
232
+ },
233
+ aimAt(p) {
234
+ if (disposed) return;
235
+ if (surface.gone()) {
236
+ teardown();
237
+ return;
238
+ }
239
+ if (surface.hidden()) return;
240
+ if (surface.covers(p)) return;
241
+ aim = p;
242
+ sampleColor();
243
+ surface.changed();
244
+ },
245
+ pick(p) {
246
+ if (disposed) return null;
247
+ let at = aim;
248
+ if (p) {
249
+ const rect = surface.lens();
250
+ if (!rect) return null;
251
+ at = loupeSourcePoint(p, rect, aim, factor);
252
+ if (surface.covers(at)) return null;
253
+ }
254
+ const hex = surface.sample(at);
255
+ if (hex !== null) opts.onPick?.(hex);
256
+ return hex;
257
+ },
258
+ dispose: teardown
259
+ };
260
+ }
261
+ var WHEEL_RATE = 2e-3;
262
+ function useLoupe({ capability, hostRef, enabled, sample }) {
263
+ const [, bump] = useReducer((n) => n + 1, 0);
264
+ const overRef = useRef(false);
265
+ const peekingRef = useRef(false);
266
+ const enabledRef = useRef(enabled);
267
+ enabledRef.current = enabled;
268
+ const sampleRef = useRef(sample);
269
+ sampleRef.current = sample;
270
+ const onColorChangeRef = useRef(capability.onColorChange);
271
+ onColorChangeRef.current = capability.onColorChange;
272
+ const shown = useCallback(
273
+ () => overRef.current && (enabledRef.current || peekingRef.current),
274
+ []
275
+ );
276
+ const modelRef = useRef(null);
277
+ if (modelRef.current === null) {
278
+ modelRef.current = createLoupeModel({
279
+ mode: capability.mode,
280
+ factor: capability.factor,
281
+ minFactor: capability.minFactor,
282
+ maxFactor: capability.maxFactor,
283
+ onColorChange: (hex) => onColorChangeRef.current?.(hex),
284
+ surface: {
285
+ lens: () => {
286
+ const d = diameterRef.current;
287
+ const { x, y } = modelRef.current?.aim ?? { x: 0, y: 0 };
288
+ return { x: x - d / 2, y: y - d / 2, w: d, h: d };
289
+ },
290
+ // The lens follows the pointer and is painted over the host rather than
291
+ // into it, so it is never part of the picture being magnified.
292
+ covers: () => false,
293
+ sample: (p) => sampleRef.current?.(p) ?? null,
294
+ hidden: () => !shown(),
295
+ gone: () => goneRef.current,
296
+ changed: bump
297
+ }
298
+ });
299
+ }
300
+ const model = modelRef.current;
301
+ const diameterRef = useRef(capability.diameter);
302
+ diameterRef.current = capability.diameter;
303
+ const goneRef = useRef(false);
304
+ useEffect(() => {
305
+ goneRef.current = false;
306
+ return () => {
307
+ goneRef.current = true;
308
+ };
309
+ }, []);
310
+ useEffect(() => {
311
+ if (!enabled && !peekingRef.current) bump();
312
+ }, [enabled]);
313
+ useEffect(() => {
314
+ const host = hostRef.current;
315
+ if (!host) return;
316
+ const onPointerMove = (e) => {
317
+ overRef.current = true;
318
+ const rect = host.getBoundingClientRect();
319
+ model.aimAt({ x: e.clientX - rect.left, y: e.clientY - rect.top });
320
+ if (!shown()) return;
321
+ bump();
322
+ };
323
+ const onPointerLeave = () => {
324
+ overRef.current = false;
325
+ bump();
326
+ };
327
+ const onWheel = (e) => {
328
+ if (!shown()) return;
329
+ e.preventDefault();
330
+ e.stopPropagation();
331
+ model.setFactor(model.factor * Math.exp(-e.deltaY * WHEEL_RATE));
332
+ };
333
+ host.addEventListener("pointermove", onPointerMove);
334
+ host.addEventListener("pointerleave", onPointerLeave);
335
+ host.addEventListener("wheel", onWheel, { capture: true, passive: false });
336
+ return () => {
337
+ host.removeEventListener("pointermove", onPointerMove);
338
+ host.removeEventListener("pointerleave", onPointerLeave);
339
+ host.removeEventListener("wheel", onWheel, { capture: true });
340
+ };
341
+ }, [hostRef, model, shown]);
342
+ const peekKey = capability.peekKey;
343
+ useEffect(() => {
344
+ if (peekKey == null) return;
345
+ const setPeeking = (next) => {
346
+ if (peekingRef.current === next) return;
347
+ peekingRef.current = next;
348
+ bump();
349
+ };
350
+ const onKeyDown = (e) => {
351
+ if (e.key === peekKey) setPeeking(true);
352
+ };
353
+ const onKeyUp = (e) => {
354
+ if (e.key === peekKey) setPeeking(false);
355
+ };
356
+ const onBlur = () => setPeeking(false);
357
+ window.addEventListener("keydown", onKeyDown);
358
+ window.addEventListener("keyup", onKeyUp);
359
+ window.addEventListener("blur", onBlur);
360
+ return () => {
361
+ window.removeEventListener("keydown", onKeyDown);
362
+ window.removeEventListener("keyup", onKeyUp);
363
+ window.removeEventListener("blur", onBlur);
364
+ };
365
+ }, [peekKey]);
366
+ return {
367
+ visible: shown(),
368
+ aim: model.aim,
369
+ factor: model.factor,
370
+ mode: model.mode,
371
+ color: model.color,
372
+ setMode: model.setMode,
373
+ setFactor: model.setFactor,
374
+ pick: model.pick
375
+ };
376
+ }
377
+ var NO_HOST = { current: null };
378
+ function TrialLoupe({
379
+ capability,
380
+ enabled,
381
+ state,
382
+ config,
383
+ view,
384
+ worldSpec,
385
+ hostRef
386
+ }) {
387
+ const stack = useContext(CanvasStackContext);
388
+ const surface = capability.render ? void 0 : stack?.surface;
389
+ const host = surface?.element ?? hostRef ?? NO_HOST;
390
+ const sample = useMemo(() => {
391
+ if (!surface) return void 0;
392
+ return (p) => {
393
+ const canvases = surface.canvases.current;
394
+ return canvases ? sampleStack(surface.layers, canvases, p, surface.size.dpr) : null;
395
+ };
396
+ }, [surface]);
397
+ const loupe = useLoupe({ capability, hostRef: host, enabled, sample });
398
+ const measured = useHostSize(host);
399
+ const size = surface?.size ?? measured;
400
+ if (!loupe.visible) return null;
401
+ return /* @__PURE__ */ jsx(LoupeBubble, { aim: loupe.aim, diameter: capability.diameter, children: capability.render ? /* @__PURE__ */ jsx(
402
+ DomLoupe,
403
+ {
404
+ aim: loupe.aim,
405
+ factor: loupe.factor,
406
+ mode: loupe.mode,
407
+ diameter: capability.diameter,
408
+ size,
409
+ view,
410
+ frame: stack?.frame,
411
+ state,
412
+ config,
413
+ render: capability.render
414
+ }
415
+ ) : surface && stack ? /* @__PURE__ */ jsx(
416
+ CanvasLoupe,
417
+ {
418
+ aim: loupe.aim,
419
+ factor: loupe.factor,
420
+ mode: loupe.mode,
421
+ diameter: capability.diameter,
422
+ surface,
423
+ view: stack.view,
424
+ frame: stack.frame,
425
+ worldSpec
426
+ }
427
+ ) : null });
428
+ }
429
+
430
+ // src/loupe/types.ts
431
+ var LOUPE_DEFAULTS = {
432
+ factor: 6,
433
+ minFactor: 2,
434
+ maxFactor: 32,
435
+ mode: "vector",
436
+ diameter: 200,
437
+ peekKey: "Alt"
438
+ };
439
+ function resolveLoupe(declared) {
440
+ const cap = declared === true ? {} : declared;
441
+ const minFactor = cap.minFactor ?? LOUPE_DEFAULTS.minFactor;
442
+ const maxFactor = cap.maxFactor ?? LOUPE_DEFAULTS.maxFactor;
443
+ return {
444
+ render: cap.render,
445
+ onColorChange: cap.onColorChange,
446
+ minFactor,
447
+ maxFactor,
448
+ factor: Math.min(maxFactor, Math.max(minFactor, cap.factor ?? LOUPE_DEFAULTS.factor)),
449
+ diameter: cap.diameter ?? LOUPE_DEFAULTS.diameter,
450
+ peekKey: cap.peekKey === void 0 ? LOUPE_DEFAULTS.peekKey : cap.peekKey,
451
+ // A DOM loupe has no framebuffer, so `pixel` would have nothing to enlarge.
452
+ mode: cap.render ? "vector" : cap.mode ?? LOUPE_DEFAULTS.mode
453
+ };
454
+ }
455
+
456
+ export { CanvasLoupe, DomLoupe, LOUPE_DEFAULTS, LoupeBubble, TrialLoupe, drawCanvasLens, lensCamera, lensSourceRect, resolveLoupe, sampleStack, useLoupe };
457
+ //# sourceMappingURL=chunk-E2UQVZ44.js.map
458
+ //# sourceMappingURL=chunk-E2UQVZ44.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/loupe/canvasLens.ts","../src/loupe/CanvasLoupe.tsx","../src/loupe/DomLoupe.tsx","../src/loupe/LoupeBubble.tsx","../src/loupe/useHostSize.ts","../../loupe/src/geometry.ts","../../loupe/src/model.ts","../src/loupe/useLoupe.ts","../src/loupe/TrialLoupe.tsx","../src/loupe/types.ts"],"names":["jsx","useEffect","useRef"],"mappings":";;;;;;;;AAqBO,SAAS,WACd,GAAA,EACA,KAAA,EACA,UAAA,EACA,MAAA,EACA,UACA,SAAA,EACY;AACZ,EAAA,MAAM,IAAA,GAAO,EAAE,KAAA,EAAO,QAAA,EAAU,QAAQ,QAAA,EAAS;AACjD,EAAA,MAAM,KAAA,GAAQ,YAAA,CAAa,SAAA,EAAW,IAAI,CAAA;AAC1C,EAAA,MAAM,KAAA,GAAQ,aAAA,CAAc,GAAA,EAAK,KAAA,EAAO,UAAU,CAAA;AAClD,EAAA,OAAO,EAAE,IAAA,EAAM,QAAA,CAAS,KAAA,EAAO,KAAA,CAAM,OAAO,MAAA,EAAQ,IAAA,EAAM,KAAK,CAAA,EAAG,KAAA,EAAM;AAC1E;AAcO,SAAS,cAAA,CACd,GAAA,EACA,MAAA,EACA,QAAA,EACA,GAAA,EACY;AACZ,EAAA,MAAM,OAAO,QAAA,GAAW,MAAA;AACxB,EAAA,OAAO;AAAA,IACL,EAAA,EAAA,CAAK,GAAA,CAAI,CAAA,GAAI,IAAA,GAAO,CAAA,IAAK,GAAA;AAAA,IACzB,EAAA,EAAA,CAAK,GAAA,CAAI,CAAA,GAAI,IAAA,GAAO,CAAA,IAAK,GAAA;AAAA,IACzB,IAAI,IAAA,GAAO,GAAA;AAAA,IACX,IAAI,IAAA,GAAO;AAAA,GAC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AAa,IAAA,GAAO,EAAC,GAAI,QAAA;AACrC,EAAA,MAAM,SAAA,GAAY,GAAA,CAAI,SAAA,IAAa,cAAA,CAAe,SAAA;AAClD,EAAA,MAAM,SAAA,GAAY,GAAA,CAAI,SAAA,IAAa,cAAA,CAAe,SAAA;AAClD,EAAA,OAAO;AAAA,IACL,QAAQ,GAAA,CAAI,MAAA;AAAA,IACZ,eAAe,GAAA,CAAI,aAAA;AAAA,IACnB,SAAA;AAAA,IACA,SAAA;AAAA,IACA,MAAA,EAAQ,IAAA,CAAK,GAAA,CAAI,SAAA,EAAW,IAAA,CAAK,GAAA,CAAI,SAAA,EAAW,GAAA,CAAI,MAAA,IAAU,cAAA,CAAe,MAAM,CAAC,CAAA;AAAA,IACpF,QAAA,EAAU,GAAA,CAAI,QAAA,IAAY,cAAA,CAAe,QAAA;AAAA,IACzC,SAAS,GAAA,CAAI,OAAA,KAAY,MAAA,GAAY,cAAA,CAAe,UAAU,GAAA,CAAI,OAAA;AAAA;AAAA,IAElE,MAAM,GAAA,CAAI,MAAA,GAAS,QAAA,GAAY,GAAA,CAAI,QAAQ,cAAA,CAAe;AAAA,GAC5D;AACF","file":"chunk-E2UQVZ44.js","sourcesContent":["import type { LoupePoint } from '@weasel-js/loupe';\nimport { centerOn } from '../canvas/camera';\nimport { screenToWorld } from '../canvas/canvasCoords';\nimport type { CanvasLayerDescriptor } from '../canvas/useLayerScheduler';\nimport { DEFAULT_FRAME, resolveFrame, type WorldFrame, type WorldSpec } from '../canvas/worldSpec';\nimport type { ViewTransform } from '../instrument/types';\n\n/** The camera a round lens of `diameter` shows a magnified region through, and\n * the coordinate system it is read in — the instrument's own `WorldSpec`,\n * resolved against the lens rather than the stack. */\nexport interface LensCamera {\n view: ViewTransform;\n frame: WorldFrame;\n}\n\n/**\n * What a lens aimed at `aim` renders the stack's layers through.\n *\n * The lens is its own viewport, so the world spec resolves against its box: an\n * instrument centred on its viewport is centred in the lens too.\n */\nexport function lensCamera(\n aim: LoupePoint,\n outer: ViewTransform,\n outerFrame: WorldFrame,\n factor: number,\n diameter: number,\n worldSpec?: WorldSpec,\n): LensCamera {\n const size = { width: diameter, height: diameter };\n const frame = resolveFrame(worldSpec, size);\n const world = screenToWorld(aim, outer, outerFrame);\n return { view: centerOn(world, outer.zoom * factor, size, frame), frame };\n}\n\n/** A rectangle in a backing store's own device pixels. */\nexport interface SourceRect {\n sx: number;\n sy: number;\n sw: number;\n sh: number;\n}\n\n/**\n * The region a pixel-mode lens copies out of a presented canvas: the `diameter\n * / factor` CSS px around `aim`, in that canvas' backing-store pixels.\n */\nexport function lensSourceRect(\n aim: LoupePoint,\n factor: number,\n diameter: number,\n dpr: number,\n): SourceRect {\n const span = diameter / factor;\n return {\n sx: (aim.x - span / 2) * dpr,\n sy: (aim.y - span / 2) * dpr,\n sw: span * dpr,\n sh: span * dpr,\n };\n}\n\n/**\n * The colour a stack presents at a point: the topmost visible layer with\n * anything opaque there, or `null` when every layer is transparent — which is\n * the honest answer for a point showing nothing but the workspace behind.\n */\nexport function sampleStack(\n layers: readonly CanvasLayerDescriptor[],\n canvases: Map<string, HTMLCanvasElement>,\n p: LoupePoint,\n dpr: number,\n): string | null {\n for (let i = layers.length - 1; i >= 0; i--) {\n const layer = layers[i];\n if (!layer.visible) continue;\n const canvas = canvases.get(layer.id);\n if (!canvas) continue;\n const ctx = canvas.getContext('2d', { willReadFrequently: true });\n if (!ctx) continue;\n const x = Math.floor(p.x * dpr);\n const y = Math.floor(p.y * dpr);\n if (x < 0 || y < 0 || x >= canvas.width || y >= canvas.height) continue;\n const [r, g, b, a] = ctx.getImageData(x, y, 1, 1).data;\n if (a === 0) continue;\n return `#${[r, g, b].map((c) => c.toString(16).padStart(2, '0')).join('')}`;\n }\n return null;\n}\n\n/** Redraw the stack into a lens' own canvas. `mode` decides whether the layers\n * are re-run at the lens camera or their presented pixels are enlarged. */\nexport function drawCanvasLens(\n ctx: CanvasRenderingContext2D,\n opts: {\n aim: LoupePoint;\n factor: number;\n diameter: number;\n dpr: number;\n mode: 'vector' | 'pixel';\n outer: ViewTransform;\n outerFrame?: WorldFrame;\n worldSpec?: WorldSpec;\n layers: readonly CanvasLayerDescriptor[];\n canvases: Map<string, HTMLCanvasElement>;\n },\n): void {\n const { aim, factor, diameter, dpr, mode, outer, layers, canvases } = opts;\n ctx.save();\n ctx.setTransform(dpr, 0, 0, dpr, 0, 0);\n ctx.clearRect(0, 0, diameter, diameter);\n\n if (mode === 'pixel') {\n const { sx, sy, sw, sh } = lensSourceRect(aim, factor, diameter, dpr);\n ctx.imageSmoothingEnabled = false;\n for (const layer of layers) {\n if (!layer.visible) continue;\n const canvas = canvases.get(layer.id);\n if (!canvas || canvas.width === 0 || canvas.height === 0) continue;\n ctx.drawImage(canvas, sx, sy, sw, sh, 0, 0, diameter, diameter);\n }\n ctx.restore();\n return;\n }\n\n const lens = lensCamera(\n aim,\n outer,\n opts.outerFrame ?? DEFAULT_FRAME,\n factor,\n diameter,\n opts.worldSpec,\n );\n for (const layer of layers) {\n if (!layer.visible) continue;\n ctx.save();\n layer.render(ctx, lens.view, lens.frame);\n ctx.restore();\n }\n ctx.restore();\n}\n","import { useVisibleRaf } from '@weasel-js/core';\nimport type { LoupeMode, LoupePoint } from '@weasel-js/loupe';\nimport { useEffect, useRef } from 'react';\nimport type { CanvasStackSurface } from '../canvas/CanvasStackContext';\nimport type { WorldFrame, WorldSpec } from '../canvas/worldSpec';\nimport type { ViewTransform } from '../instrument/types';\nimport { drawCanvasLens } from './canvasLens';\n\n/** Props for `<CanvasLoupe>`. */\nexport interface CanvasLoupeProps {\n aim: LoupePoint;\n factor: number;\n mode: LoupeMode;\n diameter: number;\n /** The stack being magnified: its layers, its pixels, and its measured box. */\n surface: CanvasStackSurface;\n view: ViewTransform;\n frame: WorldFrame;\n worldSpec?: WorldSpec;\n}\n\n/**\n * Paints a lens over a `<CanvasStack>` by re-drawing the stack's layers through\n * a zoomed camera — so the magnified content is as sharp as the original, at\n * any factor. `pixel` mode enlarges what the stack presented instead.\n */\nexport function CanvasLoupe({\n aim,\n factor,\n mode,\n diameter,\n surface,\n view,\n frame,\n worldSpec,\n}: CanvasLoupeProps) {\n const canvasRef = useRef<HTMLCanvasElement | null>(null);\n const dpr = surface.size.dpr;\n\n // Whatever the stack redraws every frame, the lens has to redraw too — an\n // instrument that animates from its own loop never re-renders this component.\n const args = { aim, factor, mode, diameter, dpr, view, frame, worldSpec, surface };\n const argsRef = useRef(args);\n argsRef.current = args;\n\n const loop = useVisibleRaf(\n () => {\n loop.request();\n const canvas = canvasRef.current;\n const ctx = canvas?.getContext('2d');\n const canvases = argsRef.current.surface.canvases.current;\n if (!ctx || !canvases) return;\n const a = argsRef.current;\n drawCanvasLens(ctx, {\n aim: a.aim,\n factor: a.factor,\n diameter: a.diameter,\n dpr: a.dpr,\n mode: a.mode,\n outer: a.view,\n outerFrame: a.frame,\n worldSpec: a.worldSpec,\n layers: a.surface.layers,\n canvases,\n });\n },\n { target: canvasRef },\n );\n\n useEffect(() => {\n loop.request();\n return () => loop.cancel();\n }, [loop]);\n\n return (\n <canvas\n ref={canvasRef}\n className=\"lk-loupe__canvas\"\n width={Math.max(1, Math.round(diameter * dpr))}\n height={Math.max(1, Math.round(diameter * dpr))}\n />\n );\n}\n","import type { LoupeMode, LoupePoint } from '@weasel-js/loupe';\nimport type { CSSProperties } from 'react';\nimport { zoomAt } from '../canvas/camera';\nimport type { ViewportSize, WorldFrame } from '../canvas/worldSpec';\nimport type { ViewTransform } from '../instrument/types';\nimport type { LoupeRenderArgs } from './types';\n\n/** Props for `<DomLoupe>`. */\nexport interface DomLoupeProps {\n aim: LoupePoint;\n factor: number;\n mode: LoupeMode;\n diameter: number;\n /** The host the instrument draws into, which the stage reproduces. */\n size: ViewportSize;\n /** The trial's own camera. */\n view: ViewTransform;\n frame?: WorldFrame;\n state: unknown;\n config: unknown;\n render: (args: LoupeRenderArgs) => React.ReactNode;\n}\n\n/**\n * Paints a lens over DOM content by asking the instrument to draw itself again\n * at a magnified camera.\n *\n * The stage is a full copy of the host, so the instrument's own layout still\n * holds; the camera is composed about the aimed point, which keeps that point\n * where it already was, and the stage is then slid by `diameter / 2 - aim` to\n * bring it to the middle of the lens.\n */\nexport function DomLoupe({\n aim,\n factor,\n mode,\n diameter,\n size,\n view,\n frame,\n state,\n config,\n render,\n}: DomLoupeProps) {\n const style: CSSProperties = {\n width: `${size.width}px`,\n height: `${size.height}px`,\n transform: `translate(${diameter / 2 - aim.x}px, ${diameter / 2 - aim.y}px)`,\n };\n return (\n <div className=\"lk-loupe__stage\" style={style}>\n {render({\n state,\n config,\n view: zoomAt(view, factor, aim, { frame }),\n factor,\n mode,\n size,\n })}\n </div>\n );\n}\n","import type { LoupePoint } from '@weasel-js/loupe';\nimport type { CSSProperties, ReactNode, RefObject } from 'react';\n\n/** Props for `<LoupeBubble>`. */\nexport interface LoupeBubbleProps {\n /** Where the lens is aimed, in its container's own pixels. */\n aim: LoupePoint;\n diameter: number;\n hostRef?: RefObject<HTMLDivElement | null>;\n children: ReactNode;\n}\n\n/**\n * The lens itself: a circle centred on the aimed point, clipping whatever a\n * painter draws into it.\n *\n * It takes no pointer events, so the pan, the wheel and anything underneath\n * keep working while it is up — and it is `aria-hidden`, since it magnifies\n * content already on the page rather than adding any.\n */\nexport function LoupeBubble({ aim, diameter, hostRef, children }: LoupeBubbleProps) {\n const style: CSSProperties = {\n width: `${diameter}px`,\n height: `${diameter}px`,\n transform: `translate(${aim.x - diameter / 2}px, ${aim.y - diameter / 2}px)`,\n };\n return (\n <div ref={hostRef} className=\"lk-loupe\" style={style} aria-hidden=\"true\">\n {children}\n </div>\n );\n}\n","import { type RefObject, useEffect, useState } from 'react';\nimport type { ViewportSize } from '../canvas/worldSpec';\n\nconst ZERO: ViewportSize = { width: 0, height: 0 };\n\n/** An element's CSS-pixel box, kept current. `{0, 0}` until it is measured, and\n * wherever `ResizeObserver` is absent. */\nexport function useHostSize(ref: RefObject<HTMLElement | null>): ViewportSize {\n const [size, setSize] = useState<ViewportSize>(ZERO);\n\n useEffect(() => {\n const el = ref.current;\n if (!el) return;\n const update = (): void => {\n const rect = el.getBoundingClientRect();\n setSize((prev) =>\n prev.width === rect.width && prev.height === rect.height\n ? prev\n : { width: rect.width, height: rect.height },\n );\n };\n update();\n if (typeof ResizeObserver === 'undefined') return;\n const ro = new ResizeObserver(update);\n ro.observe(el);\n return () => ro.disconnect();\n }, [ref]);\n\n return size;\n}\n","import type { View } from '@weasel-js/core';\n\n/** A point on the magnified surface, in its own CSS pixels. */\nexport interface LoupePoint {\n x: number;\n y: number;\n}\n\n/** The lens' rectangle on that surface. */\nexport interface LoupeRect extends LoupePoint {\n w: number;\n h: number;\n}\n\n/**\n * The inner `View` a loupe renders its content through: the outer view's\n * magnification times `factor`, positioned so `target` sits at the center of\n * a viewport the size of `rect`.\n */\nexport function loupeInnerView(\n target: LoupePoint,\n outer: View,\n rect: LoupeRect,\n factor: number,\n): View {\n const scale = { x: outer.scale.x * factor, y: outer.scale.y * factor };\n return {\n x: target.x - rect.w / 2 / scale.x,\n y: target.y - rect.h / 2 / scale.y,\n scale,\n };\n}\n\n/**\n * Where on the outer surface a point inside the lens is looking, in\n * screen-space CSS px.\n *\n * The inverse of {@link loupeInnerView}: the lens centers `aim` in `rect` and\n * magnifies by `factor`, and the outer view's own scale cancels out of the\n * round trip — so this needs no `View`.\n */\nexport function loupeSourcePoint(\n p: LoupePoint,\n rect: LoupeRect,\n aim: LoupePoint,\n factor: number,\n): LoupePoint {\n return {\n x: aim.x + (p.x - (rect.x + rect.w / 2)) / factor,\n y: aim.y + (p.y - (rect.y + rect.h / 2)) / factor,\n };\n}\n","import { type LoupePoint, type LoupeRect, loupeSourcePoint } from './geometry';\n\n/** How a loupe magnifies. `'vector'` re-renders the source through a zoomed-in\n * view, so content stays sharp at any factor; `'pixel'` blows up the actual\n * pixels the surface presented. */\nexport type LoupeMode = 'vector' | 'pixel';\n\n/**\n * What a loupe needs from the surface it magnifies. A painter implements it,\n * and the model asks nothing about how the lens is drawn — only where it is,\n * what colour is under a point, and whether anyone can still see it.\n */\nexport interface LoupeSurface {\n /** The lens' rectangle, or `null` while it has none. */\n lens(): LoupeRect | null;\n /**\n * Does the lens itself cover this point? A stationary lens does over its own\n * frame, which is what the freeze rule below is for; a lens that follows the\n * pointer never covers it and answers `false`.\n */\n covers(p: LoupePoint): boolean;\n /** Hex colour at a surface point, or `null` when the surface cannot say. */\n sample(p: LoupePoint): string | null;\n /** Is the lens off screen? Aims are ignored while it is. */\n hidden(): boolean;\n /** Is the lens gone for good? The model tears itself down when it is. */\n gone(): boolean;\n /** Something the painter must redraw for has changed. */\n changed(): void;\n}\n\n/** Options for {@link createLoupeModel}. */\nexport interface LoupeModelOptions {\n surface: LoupeSurface;\n mode?: LoupeMode;\n /** Magnification. Default 8. */\n factor?: number;\n /** Bounds `setFactor` clamps to. Unset means unclamped. */\n minFactor?: number;\n maxFactor?: number;\n /** Called with the hex colour under the aim point whenever it changes. */\n onColorChange?: (hex: string) => void;\n /** Called when a pick lands — an eyedropper on the magnified surface. Where\n * the colour goes is the consumer's business. */\n onPick?: (hex: string) => void;\n /** Called when the model tears itself down because the surface reported the\n * lens gone, so the painter can drop what it was holding. */\n onDispose?: () => void;\n}\n\n/** Where a loupe is aimed, how far it magnifies, and what colour it is over. */\nexport interface LoupeModel {\n readonly mode: LoupeMode;\n readonly factor: number;\n /** Last aimed point, in the surface's CSS px. */\n readonly aim: LoupePoint;\n /** Hex colour under the aim point. `null` until something answers. */\n readonly color: string | null;\n setMode(mode: LoupeMode): void;\n setFactor(factor: number): void;\n /** Aim at a surface point. Ignored while the lens covers it, is hidden, or\n * the model is disposed. */\n aimAt(p: LoupePoint): void;\n /** Sample what the lens shows at `p`, a point inside it, and report that to\n * `onPick`. Omit `p` to pick at the aim point. Leaves the aim, and `color`,\n * alone. */\n pick(p?: LoupePoint): string | null;\n dispose(): void;\n}\n\n/**\n * Build the loupe's model over a surface. It holds the state a magnifier has —\n * aim, factor, mode, colour — and none of the drawing.\n */\nexport function createLoupeModel(opts: LoupeModelOptions): LoupeModel {\n const { surface } = opts;\n let mode: LoupeMode = opts.mode ?? 'vector';\n let factor = opts.factor ?? 8;\n let aim: LoupePoint = { x: 0, y: 0 };\n let color: string | null = null;\n let disposed = false;\n\n const clamp = (n: number): number =>\n Math.min(opts.maxFactor ?? Number.POSITIVE_INFINITY,\n Math.max(opts.minFactor ?? Number.NEGATIVE_INFINITY, n));\n\n const teardown = () => {\n if (disposed) return;\n disposed = true;\n opts.onDispose?.();\n };\n\n const sampleColor = () => {\n const hex = surface.sample(aim);\n if (hex === null || hex === color) return;\n color = hex;\n opts.onColorChange?.(hex);\n };\n\n return {\n get mode() { return mode; },\n get factor() { return factor; },\n get aim() { return aim; },\n get color() { return color; },\n\n setMode(next) {\n if (disposed || next === mode) return;\n mode = next;\n surface.changed();\n },\n\n setFactor(next) {\n if (disposed) return;\n factor = clamp(next);\n surface.changed();\n },\n\n aimAt(p) {\n if (disposed) return;\n // A lens can be taken away by whatever owns it, without coming back\n // through `dispose`. Follow it down rather than keep sampling for a\n // magnifier nobody can see.\n if (surface.gone()) { teardown(); return; }\n if (surface.hidden()) return;\n if (surface.covers(p)) return;\n aim = p;\n sampleColor();\n surface.changed();\n },\n\n pick(p) {\n if (disposed) return null;\n let at = aim;\n if (p) {\n const rect = surface.lens();\n if (!rect) return null;\n at = loupeSourcePoint(p, rect, aim, factor);\n // The lens is part of the picture the surface presents. A point near\n // its frame maps back underneath it, and sampling there reports the\n // lens' own chrome as artwork.\n if (surface.covers(at)) return null;\n }\n const hex = surface.sample(at);\n if (hex !== null) opts.onPick?.(hex);\n return hex;\n },\n\n dispose: teardown,\n };\n}\n","import {\n createLoupeModel,\n type LoupeMode,\n type LoupeModel,\n type LoupePoint,\n} from '@weasel-js/loupe';\nimport { type RefObject, useCallback, useEffect, useReducer, useRef } from 'react';\nimport type { ResolvedLoupe } from './types';\n\n/** Options for {@link useLoupe}. */\nexport interface UseLoupeOptions {\n capability: ResolvedLoupe;\n /** The element the lens tracks the pointer across. */\n hostRef: RefObject<HTMLElement | null>;\n /** Whether the loupe is turned on. Hold-to-peek shows it regardless. */\n enabled: boolean;\n /** Hex colour at a host point. Omitted, the loupe reports no colour — which\n * is the honest answer for a surface with no pixels to read. */\n sample?: (p: LoupePoint) => string | null;\n}\n\n/** A loupe as a React view reads it. */\nexport interface LoupeState {\n /** Whether the lens should be drawn. */\n visible: boolean;\n /** Where it is aimed, in the host's own pixels. */\n aim: LoupePoint;\n factor: number;\n mode: LoupeMode;\n color: string | null;\n setMode: (mode: LoupeMode) => void;\n setFactor: (factor: number) => void;\n /** Sample what the lens shows at a point inside it. */\n pick: (p?: LoupePoint) => string | null;\n}\n\n/** How fast the wheel walks the magnification. Gentler than the pan-zoom\n * wheel: the lens' whole range is one order of magnitude. */\nconst WHEEL_RATE = 0.002;\n\n/**\n * Binds `@weasel-js/loupe`'s model to a host element and drives it from the\n * pointer, the peek key and the wheel.\n *\n * These are plain listeners rather than gesture bindings because labkit trials\n * do not route input through the dispatcher — see the loupe entry under\n * \"Selection, actions & UI panels\" in `docs/TODO.md`. They are all in this one\n * hook so that when trial input does go through it, this is the only file to\n * rewrite.\n */\nexport function useLoupe({ capability, hostRef, enabled, sample }: UseLoupeOptions): LoupeState {\n const [, bump] = useReducer((n: number) => n + 1, 0);\n\n const overRef = useRef(false);\n const peekingRef = useRef(false);\n const enabledRef = useRef(enabled);\n enabledRef.current = enabled;\n const sampleRef = useRef(sample);\n sampleRef.current = sample;\n const onColorChangeRef = useRef(capability.onColorChange);\n onColorChangeRef.current = capability.onColorChange;\n\n // Reads nothing but refs, so it is stable and an effect may depend on it.\n const shown = useCallback(\n (): boolean => overRef.current && (enabledRef.current || peekingRef.current),\n [],\n );\n\n const modelRef = useRef<LoupeModel | null>(null);\n if (modelRef.current === null) {\n modelRef.current = createLoupeModel({\n mode: capability.mode,\n factor: capability.factor,\n minFactor: capability.minFactor,\n maxFactor: capability.maxFactor,\n onColorChange: (hex) => onColorChangeRef.current?.(hex),\n surface: {\n lens: () => {\n const d = diameterRef.current;\n const { x, y } = modelRef.current?.aim ?? { x: 0, y: 0 };\n return { x: x - d / 2, y: y - d / 2, w: d, h: d };\n },\n // The lens follows the pointer and is painted over the host rather than\n // into it, so it is never part of the picture being magnified.\n covers: () => false,\n sample: (p) => sampleRef.current?.(p) ?? null,\n hidden: () => !shown(),\n gone: () => goneRef.current,\n changed: bump,\n },\n });\n }\n const model = modelRef.current;\n\n const diameterRef = useRef(capability.diameter);\n diameterRef.current = capability.diameter;\n const goneRef = useRef(false);\n\n // The model holds no resources, and `dispose` is one-way — so unmounting only\n // reports the lens gone. Disposing here instead leaves React's mount / unmount\n // / remount in StrictMode with a permanently dead model that silently ignores\n // every aim.\n useEffect(() => {\n goneRef.current = false;\n return () => {\n goneRef.current = true;\n };\n }, []);\n\n // Turning the loupe off with the pointer still inside must put the lens away,\n // and the model only reconsiders on an aim.\n useEffect(() => {\n if (!enabled && !peekingRef.current) bump();\n }, [enabled]);\n\n useEffect(() => {\n const host = hostRef.current;\n if (!host) return;\n\n const onPointerMove = (e: PointerEvent): void => {\n overRef.current = true;\n const rect = host.getBoundingClientRect();\n model.aimAt({ x: e.clientX - rect.left, y: e.clientY - rect.top });\n if (!shown()) return;\n bump();\n };\n const onPointerLeave = (): void => {\n overRef.current = false;\n bump();\n };\n const onWheel = (e: WheelEvent): void => {\n if (!shown()) return;\n // Ahead of the stack's own pan-zoom, which is a React handler on this same\n // element: capture runs first, and stopping propagation keeps the event\n // from reaching React's root listener at all.\n e.preventDefault();\n e.stopPropagation();\n model.setFactor(model.factor * Math.exp(-e.deltaY * WHEEL_RATE));\n };\n\n host.addEventListener('pointermove', onPointerMove);\n host.addEventListener('pointerleave', onPointerLeave);\n host.addEventListener('wheel', onWheel, { capture: true, passive: false });\n return () => {\n host.removeEventListener('pointermove', onPointerMove);\n host.removeEventListener('pointerleave', onPointerLeave);\n host.removeEventListener('wheel', onWheel, { capture: true });\n };\n }, [hostRef, model, shown]);\n\n const peekKey = capability.peekKey;\n useEffect(() => {\n if (peekKey == null) return;\n const setPeeking = (next: boolean): void => {\n if (peekingRef.current === next) return;\n peekingRef.current = next;\n bump();\n };\n const onKeyDown = (e: KeyboardEvent): void => {\n if (e.key === peekKey) setPeeking(true);\n };\n const onKeyUp = (e: KeyboardEvent): void => {\n if (e.key === peekKey) setPeeking(false);\n };\n // A key held while the window loses focus never sends its keyup, so the\n // peek would stay on until the key was pressed and released again.\n const onBlur = (): void => setPeeking(false);\n\n window.addEventListener('keydown', onKeyDown);\n window.addEventListener('keyup', onKeyUp);\n window.addEventListener('blur', onBlur);\n return () => {\n window.removeEventListener('keydown', onKeyDown);\n window.removeEventListener('keyup', onKeyUp);\n window.removeEventListener('blur', onBlur);\n };\n }, [peekKey]);\n\n return {\n visible: shown(),\n aim: model.aim,\n factor: model.factor,\n mode: model.mode,\n color: model.color,\n setMode: model.setMode,\n setFactor: model.setFactor,\n pick: model.pick,\n };\n}\n","import { type RefObject, useContext, useMemo } from 'react';\nimport { CanvasStackContext } from '../canvas/CanvasStackContext';\nimport type { WorldSpec } from '../canvas/worldSpec';\nimport type { ViewTransform } from '../instrument/types';\nimport { CanvasLoupe } from './CanvasLoupe';\nimport { sampleStack } from './canvasLens';\nimport { DomLoupe } from './DomLoupe';\nimport { LoupeBubble } from './LoupeBubble';\nimport type { ResolvedLoupe } from './types';\nimport { useHostSize } from './useHostSize';\nimport { useLoupe } from './useLoupe';\n\n/** Props for `<TrialLoupe>`. */\nexport interface TrialLoupeProps {\n capability: ResolvedLoupe;\n /** Whether the trial's loupe is turned on. Hold-to-peek shows it anyway. */\n enabled: boolean;\n state: unknown;\n config: unknown;\n /** The trial's own camera. */\n view: ViewTransform;\n worldSpec?: WorldSpec;\n /** The element the lens tracks. A loupe mounted inside a `<CanvasStack>`\n * takes the stack's own element instead, and this goes unused. */\n hostRef?: RefObject<HTMLElement | null>;\n}\n\n/** Stable stand-in, so a loupe with nothing to track does not re-bind its\n * listeners on every render. */\nconst NO_HOST: RefObject<HTMLElement | null> = { current: null };\n\n/**\n * A trial's loupe, painted by whichever painter suits its content: the canvas\n * stack it is mounted inside, or the instrument's own `render` at a magnified\n * camera.\n */\nexport function TrialLoupe({\n capability,\n enabled,\n state,\n config,\n view,\n worldSpec,\n hostRef,\n}: TrialLoupeProps) {\n const stack = useContext(CanvasStackContext);\n const surface = capability.render ? undefined : stack?.surface;\n const host = surface?.element ?? hostRef ?? NO_HOST;\n\n const sample = useMemo(() => {\n if (!surface) return undefined;\n return (p: { x: number; y: number }): string | null => {\n const canvases = surface.canvases.current;\n return canvases ? sampleStack(surface.layers, canvases, p, surface.size.dpr) : null;\n };\n }, [surface]);\n\n const loupe = useLoupe({ capability, hostRef: host, enabled, sample });\n const measured = useHostSize(host);\n const size = surface?.size ?? measured;\n\n if (!loupe.visible) return null;\n\n return (\n <LoupeBubble aim={loupe.aim} diameter={capability.diameter}>\n {capability.render ? (\n <DomLoupe\n aim={loupe.aim}\n factor={loupe.factor}\n mode={loupe.mode}\n diameter={capability.diameter}\n size={size}\n view={view}\n frame={stack?.frame}\n state={state}\n config={config}\n render={capability.render}\n />\n ) : surface && stack ? (\n <CanvasLoupe\n aim={loupe.aim}\n factor={loupe.factor}\n mode={loupe.mode}\n diameter={capability.diameter}\n surface={surface}\n view={stack.view}\n frame={stack.frame}\n worldSpec={worldSpec}\n />\n ) : null}\n </LoupeBubble>\n );\n}\n","import type { LoupeMode } from '@weasel-js/loupe';\nimport type { ReactNode } from 'react';\nimport type { ViewportSize } from '../canvas/worldSpec';\nimport type { ViewTransform } from '../instrument/types';\n\n/** What a DOM instrument's loupe `render` is handed: the same data its own\n * `render` gets, and the camera to draw it through. */\nexport interface LoupeRenderArgs<TS = unknown, TC = unknown> {\n state: TS;\n config: TC;\n /** The trial's own camera composed with the magnification, about the aimed\n * point — so drawing the same content through it magnifies in place. */\n view: ViewTransform;\n /** The magnification on its own, for whatever must not scale with the\n * camera. */\n factor: number;\n mode: LoupeMode;\n /** The size of the viewport `view` is written for: the trial's content well,\n * not the lens. The lens shows a circle cut out of it. */\n size: ViewportSize;\n}\n\n/**\n * Declares that an instrument can be magnified.\n *\n * With no `render`, the loupe re-draws the instrument's canvas layers through a\n * zoomed camera, so it stays sharp at any factor. An instrument whose content is\n * DOM supplies `render` instead: given a camera, draw me again.\n */\nexport interface LoupeCapability<TS = unknown, TC = unknown> {\n render?: (args: LoupeRenderArgs<TS, TC>) => ReactNode;\n /** Opening magnification, clamped to the bounds below. Default 6. */\n factor?: number;\n /** What the wheel clamps to. Defaults 2 and 32. */\n minFactor?: number;\n maxFactor?: number;\n /** `'vector'` (default) re-renders the content magnified; `'pixel'` blows up\n * the pixels the instrument presented. A `render` loupe is always vector —\n * DOM has no framebuffer to enlarge. */\n mode?: LoupeMode;\n /** Lens diameter in CSS px. Default 200. */\n diameter?: number;\n /** Held for a momentary peek while the loupe is off. Default `'Alt'`; `null`\n * turns hold-to-peek off. Matched against `KeyboardEvent.key`. */\n peekKey?: string | null;\n /** Called with the colour under the aim, wherever the surface can say. The\n * canvas painter reads it back; a DOM loupe has no pixels to sample. */\n onColorChange?: (hex: string) => void;\n}\n\n/** An instrument's loupe declaration. `true` takes every default. */\nexport type LoupeDeclaration<TS = unknown, TC = unknown> = true | LoupeCapability<TS, TC>;\n\n/** A {@link LoupeCapability} with every default filled in. */\nexport interface ResolvedLoupe<TS = unknown, TC = unknown> {\n render?: (args: LoupeRenderArgs<TS, TC>) => ReactNode;\n onColorChange?: (hex: string) => void;\n factor: number;\n minFactor: number;\n maxFactor: number;\n mode: LoupeMode;\n diameter: number;\n peekKey: string | null;\n}\n\nexport const LOUPE_DEFAULTS = {\n factor: 6,\n minFactor: 2,\n maxFactor: 32,\n mode: 'vector',\n diameter: 200,\n peekKey: 'Alt',\n} as const satisfies Omit<ResolvedLoupe, 'render' | 'onColorChange'>;\n\nexport function resolveLoupe<TS, TC>(declared: LoupeDeclaration<TS, TC>): ResolvedLoupe<TS, TC> {\n const cap = declared === true ? {} : declared;\n const minFactor = cap.minFactor ?? LOUPE_DEFAULTS.minFactor;\n const maxFactor = cap.maxFactor ?? LOUPE_DEFAULTS.maxFactor;\n return {\n render: cap.render,\n onColorChange: cap.onColorChange,\n minFactor,\n maxFactor,\n factor: Math.min(maxFactor, Math.max(minFactor, cap.factor ?? LOUPE_DEFAULTS.factor)),\n diameter: cap.diameter ?? LOUPE_DEFAULTS.diameter,\n peekKey: cap.peekKey === undefined ? LOUPE_DEFAULTS.peekKey : cap.peekKey,\n // A DOM loupe has no framebuffer, so `pixel` would have nothing to enlarge.\n mode: cap.render ? 'vector' : (cap.mode ?? LOUPE_DEFAULTS.mode),\n };\n}\n"]}
@@ -1,4 +1,4 @@
1
- import { useVisibleRaf } from './chunk-QCICIHEO.js';
1
+ import { useVisibleRaf } from './chunk-L5QJOOLV.js';
2
2
  import { createContext, useContext, useCallback, useRef, useEffect } from 'react';
3
3
 
4
4
  // src/surface/composeRects.ts
@@ -31,6 +31,10 @@ function toDeviceRect(rect, surfaceHeight, dpr) {
31
31
  };
32
32
  }
33
33
  var SurfaceContext = createContext(null);
34
+ var SurfaceCanvasContext = createContext(null);
35
+ function useSurfaceCanvas() {
36
+ return useContext(SurfaceCanvasContext);
37
+ }
34
38
  function useSurfaceOptional() {
35
39
  return useContext(SurfaceContext);
36
40
  }
@@ -53,6 +57,7 @@ function useTiledSurface({ onFrame }) {
53
57
  const tiles = useRef(/* @__PURE__ */ new Map());
54
58
  const rects = useRef(/* @__PURE__ */ new Map());
55
59
  const dirty = useRef(/* @__PURE__ */ new Set());
60
+ const painters = useRef(/* @__PURE__ */ new Map());
56
61
  const needsMeasure = useRef(true);
57
62
  const observer = useRef(null);
58
63
  const lastDpr = useRef(0);
@@ -89,12 +94,17 @@ function useTiledSurface({ onFrame }) {
89
94
  }
90
95
  if (dirty.current.size === 0) return;
91
96
  const box = el.getBoundingClientRect();
92
- onFrameRef.current({
97
+ const frame = {
93
98
  dirty: new Set(dirty.current),
94
99
  rects: new Map(rects.current),
95
100
  dpr,
96
101
  size: { width: box.width, height: box.height }
97
- });
102
+ };
103
+ onFrameRef.current(frame);
104
+ for (const id of frame.dirty) {
105
+ const rect = frame.rects.get(id);
106
+ if (rect) painters.current.get(id)?.(rect, frame);
107
+ }
98
108
  dirty.current.clear();
99
109
  },
100
110
  // The host attaches its container through `containerRef`, which may land
@@ -137,6 +147,13 @@ function useTiledSurface({ onFrame }) {
137
147
  },
138
148
  [schedule]
139
149
  );
150
+ const registerPainter = useCallback((id, paint) => {
151
+ painters.current.set(id, paint);
152
+ return () => {
153
+ if (painters.current.get(id) === paint) painters.current.delete(id);
154
+ };
155
+ }, []);
156
+ const getContainer = useCallback(() => container.current, []);
140
157
  const containerRef = useCallback(
141
158
  (el) => {
142
159
  if (container.current === el) return;
@@ -162,9 +179,17 @@ function useTiledSurface({ onFrame }) {
162
179
  frameLoop.cancel();
163
180
  };
164
181
  }, [schedule, frameLoop]);
165
- return { invalidate, invalidateAll, invalidateRects, registerTile, containerRef };
182
+ return {
183
+ invalidate,
184
+ invalidateAll,
185
+ invalidateRects,
186
+ registerTile,
187
+ registerPainter,
188
+ containerRef,
189
+ getContainer
190
+ };
166
191
  }
167
192
 
168
- export { SurfaceContext, composeRects, rectsEqual, toDeviceRect, useSurface, useSurfaceOptional, useSurfaceTile, useTiledSurface };
169
- //# sourceMappingURL=chunk-LGKRIUSW.js.map
170
- //# sourceMappingURL=chunk-LGKRIUSW.js.map
193
+ export { SurfaceCanvasContext, SurfaceContext, composeRects, rectsEqual, toDeviceRect, useSurface, useSurfaceCanvas, useSurfaceOptional, useSurfaceTile, useTiledSurface };
194
+ //# sourceMappingURL=chunk-ESIQNQ6K.js.map
195
+ //# sourceMappingURL=chunk-ESIQNQ6K.js.map