@haklex/rich-ext-whiteboard 0.65.7 → 0.66.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.
- package/README.md +45 -5
- package/dist/InteractiveScene-CpoWGP_L.js +424 -0
- package/dist/WhiteboardEditor-cZ32fLqJ.js +4799 -0
- package/dist/WhiteboardExpandShell-BBjXd5qK.js +474 -0
- package/dist/editor/ContextMenu.d.ts +9 -0
- package/dist/editor/ContextMenu.d.ts.map +1 -0
- package/dist/editor/MobileDock.d.ts +2 -0
- package/dist/editor/MobileDock.d.ts.map +1 -1
- package/dist/editor/TextEditor.d.ts.map +1 -1
- package/dist/editor/Toolbar.d.ts +1 -0
- package/dist/editor/Toolbar.d.ts.map +1 -1
- package/dist/editor/WhiteboardCanvas.d.ts +2 -1
- package/dist/editor/WhiteboardCanvas.d.ts.map +1 -1
- package/dist/editor/WhiteboardEditor.d.ts +15 -0
- package/dist/editor/WhiteboardEditor.d.ts.map +1 -1
- package/dist/editor/card-actions.d.ts +8 -0
- package/dist/editor/card-actions.d.ts.map +1 -0
- package/dist/editor/chrome.css.d.ts +3 -0
- package/dist/editor/chrome.css.d.ts.map +1 -1
- package/dist/editor/context-menu.d.ts +27 -0
- package/dist/editor/context-menu.d.ts.map +1 -0
- package/dist/editor/context-menu.test.d.ts +2 -0
- package/dist/editor/context-menu.test.d.ts.map +1 -0
- package/dist/editor/embed.test.d.ts +2 -0
- package/dist/editor/embed.test.d.ts.map +1 -0
- package/dist/editor/focus.d.ts +11 -0
- package/dist/editor/focus.d.ts.map +1 -0
- package/dist/editor/remote-edit.test.d.ts +2 -0
- package/dist/editor/remote-edit.test.d.ts.map +1 -0
- package/dist/editor/shortcuts.d.ts +1 -1
- package/dist/editor/shortcuts.d.ts.map +1 -1
- package/dist/editor/store.d.ts +10 -1
- package/dist/editor/store.d.ts.map +1 -1
- package/dist/editor/tools/card.d.ts +0 -2
- package/dist/editor/tools/card.d.ts.map +1 -1
- package/dist/editor-entry.d.ts +28 -0
- package/dist/editor-entry.d.ts.map +1 -0
- package/dist/editor.mjs +186 -0
- package/dist/export/export.test.d.ts +2 -0
- package/dist/export/export.test.d.ts.map +1 -0
- package/dist/export/fonts.d.ts +22 -0
- package/dist/export/fonts.d.ts.map +1 -0
- package/dist/export/index.d.ts +7 -0
- package/dist/export/index.d.ts.map +1 -0
- package/dist/export/png.d.ts +10 -0
- package/dist/export/png.d.ts.map +1 -0
- package/dist/export/svg.d.ts +19 -0
- package/dist/export/svg.d.ts.map +1 -0
- package/dist/export-B_abF5zK.js +248 -0
- package/dist/index.mjs +6 -4408
- package/dist/model/reconcile.d.ts +14 -0
- package/dist/model/reconcile.d.ts.map +1 -0
- package/dist/model/reconcile.test.d.ts +2 -0
- package/dist/model/reconcile.test.d.ts.map +1 -0
- package/dist/model/restore-fonts.test.d.ts +2 -0
- package/dist/model/restore-fonts.test.d.ts.map +1 -0
- package/dist/model/restore.d.ts +5 -0
- package/dist/model/restore.d.ts.map +1 -1
- package/dist/model/richtext-layout.d.ts +2 -0
- package/dist/model/richtext-layout.d.ts.map +1 -1
- package/dist/model/skeleton.d.ts +21 -0
- package/dist/model/skeleton.d.ts.map +1 -0
- package/dist/model/skeleton.test.d.ts +2 -0
- package/dist/model/skeleton.test.d.ts.map +1 -0
- package/dist/model/supersede.d.ts +3 -0
- package/dist/model/supersede.d.ts.map +1 -0
- package/dist/model/viewport-fit.d.ts +24 -0
- package/dist/model/viewport-fit.d.ts.map +1 -0
- package/dist/reconcile-Of9Y1rFE.js +57 -0
- package/dist/rich-ext-whiteboard.css +1 -1
- package/dist/scene/InteractiveScene.d.ts +14 -1
- package/dist/scene/InteractiveScene.d.ts.map +1 -1
- package/dist/scene/Scene.d.ts +1 -0
- package/dist/scene/Scene.d.ts.map +1 -1
- package/dist/scene/SceneControls.d.ts +10 -0
- package/dist/scene/SceneControls.d.ts.map +1 -0
- package/dist/scene/context.d.ts +2 -0
- package/dist/scene/context.d.ts.map +1 -1
- package/dist/scene/elements.d.ts.map +1 -1
- package/dist/scene/fonts.d.ts +2 -0
- package/dist/scene/fonts.d.ts.map +1 -1
- package/dist/scene/fonts.test.d.ts +2 -0
- package/dist/scene/fonts.test.d.ts.map +1 -0
- package/dist/scene/interactive.test.d.ts +2 -0
- package/dist/scene/interactive.test.d.ts.map +1 -0
- package/dist/scene/richtext/RichTextSvgBody.d.ts +6 -0
- package/dist/scene/richtext/RichTextSvgBody.d.ts.map +1 -0
- package/dist/scene/transform.d.ts +18 -0
- package/dist/scene/transform.d.ts.map +1 -0
- package/dist/scene/useSceneFocus.d.ts +35 -0
- package/dist/scene/useSceneFocus.d.ts.map +1 -0
- package/dist/scene/useSceneGestures.d.ts +20 -0
- package/dist/scene/useSceneGestures.d.ts.map +1 -0
- package/dist/scene-entry.d.ts +16 -0
- package/dist/scene-entry.d.ts.map +1 -0
- package/dist/scene.mjs +5 -0
- package/dist/static.mjs +1 -1
- package/dist/{WhiteboardExpandShell-D1s7iDKh.js → viewport-fit-DzCmuJXY.js} +306 -814
- package/package.json +13 -5
package/README.md
CHANGED
|
@@ -85,6 +85,44 @@ import {
|
|
|
85
85
|
} from '@haklex/rich-ext-whiteboard/static';
|
|
86
86
|
```
|
|
87
87
|
|
|
88
|
+
### Embed the editor on its own
|
|
89
|
+
|
|
90
|
+
`@haklex/rich-ext-whiteboard/editor` is the whiteboard without the article block around it. The host owns the store and decides which built-in chrome shows.
|
|
91
|
+
|
|
92
|
+
```tsx
|
|
93
|
+
import {
|
|
94
|
+
convertSkeletons,
|
|
95
|
+
createWhiteboardStore,
|
|
96
|
+
exportToBlob,
|
|
97
|
+
focusElements,
|
|
98
|
+
WhiteboardEditor,
|
|
99
|
+
} from '@haklex/rich-ext-whiteboard/editor';
|
|
100
|
+
|
|
101
|
+
const store = createWhiteboardStore({ elements, files }, { sync: true });
|
|
102
|
+
|
|
103
|
+
<WhiteboardEditor
|
|
104
|
+
store={store}
|
|
105
|
+
theme="light"
|
|
106
|
+
chrome={{ toolbar: false, stylePanel: false, zoom: false, mobileDock: false }}
|
|
107
|
+
tools={['selection', 'rectangle', 'arrow', 'text', 'card']}
|
|
108
|
+
contextMenuItems={({ selected, scenePoint }) => [
|
|
109
|
+
{ key: 'comment', label: 'Comment', onSelect: () => openComment(selected, scenePoint) },
|
|
110
|
+
]}
|
|
111
|
+
/>;
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
- `chrome` switches the toolbar, style panel, zoom controls, mobile dock and context menu off one by one; all are on by default.
|
|
115
|
+
- `initialFit={false}` leaves the viewport where the store has it instead of fitting the content on mount.
|
|
116
|
+
- `tools` limits the tools the toolbar and keyboard shortcuts offer.
|
|
117
|
+
- `contextMenuItems` appends host items to the right-click menu.
|
|
118
|
+
- `handFontOverride` draws handwritten text with another font stack without changing the stored `fontFamily`.
|
|
119
|
+
- With `{ sync: true }` every local change, undo and redo included, raises the changed elements' `version`, deleted elements stay as `isDeleted` tombstones, and `store.applyRemote({ elements, files })` merges elements from elsewhere: the higher `version` wins. A change to an element the user is drawing or typing in waits until they finish: if they changed it, their version is kept and raised above the remote one; if they did not, the remote one is taken.
|
|
120
|
+
- `store.subscribe` reports changes; `store.getState()` holds `elements`, `files`, `selectedIds` and `viewport` (`screen = (scene + scroll) * zoom`).
|
|
121
|
+
- `convertSkeletons` completes short element descriptions, see [the scene format](./docs/scene-format.md#skeleton-shorthand). `focusElements` moves the viewport to elements or a box.
|
|
122
|
+
- `exportToSvg` and `exportToBlob` draw a scene without a mounted editor. `richtext` cards are drawn as plain SVG text, and the fonts in use are embedded.
|
|
123
|
+
|
|
124
|
+
`@haklex/rich-ext-whiteboard/scene` holds the read-only side: `Scene`, `SceneContent`, `InteractiveScene` (pan, zoom, `onViewportChange`, a `focus` handle, `preserveView` to keep the view while the scene updates live), the font stacks and the same export functions. It never loads the editor.
|
|
125
|
+
|
|
88
126
|
### Snapshot utilities
|
|
89
127
|
|
|
90
128
|
```ts
|
|
@@ -133,11 +171,13 @@ import '@haklex/rich-ext-whiteboard/style.css';
|
|
|
133
171
|
|
|
134
172
|
### Sub-path Exports
|
|
135
173
|
|
|
136
|
-
| Path | Description
|
|
137
|
-
| --------------------------------------- |
|
|
138
|
-
| `@haklex/rich-ext-whiteboard` | Full exports (edit + static)
|
|
139
|
-
| `@haklex/rich-ext-whiteboard/static` | Static-only (no Excalidraw editor deps)
|
|
140
|
-
| `@haklex/rich-ext-whiteboard/
|
|
174
|
+
| Path | Description |
|
|
175
|
+
| --------------------------------------- | -------------------------------------------- |
|
|
176
|
+
| `@haklex/rich-ext-whiteboard` | Full exports (edit + static) |
|
|
177
|
+
| `@haklex/rich-ext-whiteboard/static` | Static-only (no Excalidraw editor deps) |
|
|
178
|
+
| `@haklex/rich-ext-whiteboard/editor` | The editor on its own, without Lexical nodes |
|
|
179
|
+
| `@haklex/rich-ext-whiteboard/scene` | Read-only scene, viewport control and export |
|
|
180
|
+
| `@haklex/rich-ext-whiteboard/style.css` | Stylesheet |
|
|
141
181
|
|
|
142
182
|
## Part of Haklex
|
|
143
183
|
|
|
@@ -0,0 +1,424 @@
|
|
|
1
|
+
import { U as getCommonBounds, a as computeSceneBounds, i as SceneContent, n as DARK_SCENE_FILTER, t as viewportFor } from "./viewport-fit-DzCmuJXY.js";
|
|
2
|
+
import { useCallback, useEffect, useImperativeHandle, useLayoutEffect, useMemo, useRef, useState } from "react";
|
|
3
|
+
import { Fragment, jsx, jsxs } from "react/jsx-runtime";
|
|
4
|
+
import { Maximize2, ScanSearch, ZoomIn, ZoomOut } from "lucide-react";
|
|
5
|
+
//#region src/scene/styles.css.ts
|
|
6
|
+
var sceneRoot = "hfbknr0";
|
|
7
|
+
var sceneSvg = "hfbknr1";
|
|
8
|
+
var toolbar = "hfbknr2";
|
|
9
|
+
var toolButton = "hfbknr3";
|
|
10
|
+
var zoomLabel = "hfbknr4";
|
|
11
|
+
var toolSeparator = "hfbknr5";
|
|
12
|
+
//#endregion
|
|
13
|
+
//#region src/scene/SceneControls.tsx
|
|
14
|
+
var SceneControls = ({ onExpand, onFit, onZoomIn, onZoomOut, scale }) => /* @__PURE__ */ jsxs("div", {
|
|
15
|
+
className: toolbar,
|
|
16
|
+
children: [
|
|
17
|
+
/* @__PURE__ */ jsx("button", {
|
|
18
|
+
"aria-label": "Zoom out",
|
|
19
|
+
className: toolButton,
|
|
20
|
+
title: "Zoom out (⌘/Ctrl + scroll)",
|
|
21
|
+
type: "button",
|
|
22
|
+
onClick: onZoomOut,
|
|
23
|
+
children: /* @__PURE__ */ jsx(ZoomOut, {})
|
|
24
|
+
}),
|
|
25
|
+
/* @__PURE__ */ jsxs("span", {
|
|
26
|
+
className: zoomLabel,
|
|
27
|
+
children: [Math.round(scale * 100), "%"]
|
|
28
|
+
}),
|
|
29
|
+
/* @__PURE__ */ jsx("button", {
|
|
30
|
+
"aria-label": "Zoom in",
|
|
31
|
+
className: toolButton,
|
|
32
|
+
title: "Zoom in (⌘/Ctrl + scroll)",
|
|
33
|
+
type: "button",
|
|
34
|
+
onClick: onZoomIn,
|
|
35
|
+
children: /* @__PURE__ */ jsx(ZoomIn, {})
|
|
36
|
+
}),
|
|
37
|
+
/* @__PURE__ */ jsx("span", {
|
|
38
|
+
"aria-hidden": true,
|
|
39
|
+
className: toolSeparator
|
|
40
|
+
}),
|
|
41
|
+
/* @__PURE__ */ jsx("button", {
|
|
42
|
+
"aria-label": "Fit to content",
|
|
43
|
+
className: toolButton,
|
|
44
|
+
title: "Fit to content (double-click)",
|
|
45
|
+
type: "button",
|
|
46
|
+
onClick: onFit,
|
|
47
|
+
children: /* @__PURE__ */ jsx(ScanSearch, {})
|
|
48
|
+
}),
|
|
49
|
+
onExpand && /* @__PURE__ */ jsxs(Fragment, { children: [/* @__PURE__ */ jsx("span", {
|
|
50
|
+
"aria-hidden": true,
|
|
51
|
+
className: "hfbknr5"
|
|
52
|
+
}), /* @__PURE__ */ jsx("button", {
|
|
53
|
+
"aria-label": "Fullscreen",
|
|
54
|
+
className: "hfbknr3",
|
|
55
|
+
title: "Fullscreen",
|
|
56
|
+
type: "button",
|
|
57
|
+
onClick: (e) => onExpand(e.currentTarget),
|
|
58
|
+
children: /* @__PURE__ */ jsx(Maximize2, {})
|
|
59
|
+
})] })
|
|
60
|
+
]
|
|
61
|
+
});
|
|
62
|
+
//#endregion
|
|
63
|
+
//#region src/scene/transform.ts
|
|
64
|
+
var MIN_SCALE = .05;
|
|
65
|
+
var fromViewport = (v) => ({
|
|
66
|
+
s: v.zoom,
|
|
67
|
+
tx: v.scrollX * v.zoom,
|
|
68
|
+
ty: v.scrollY * v.zoom
|
|
69
|
+
});
|
|
70
|
+
var toViewport = (t) => ({
|
|
71
|
+
scrollX: t.tx / t.s,
|
|
72
|
+
scrollY: t.ty / t.s,
|
|
73
|
+
zoom: t.s
|
|
74
|
+
});
|
|
75
|
+
//#endregion
|
|
76
|
+
//#region src/scene/useSceneFocus.ts
|
|
77
|
+
var FOCUS_DURATION = 280;
|
|
78
|
+
var isBounds = (target) => !!target && target.length === 4 && target.every((value) => typeof value === "number");
|
|
79
|
+
function useSceneFocus(ref, { bounds, elements, setTransform, sizeRef, transformRef, userInteractedRef }) {
|
|
80
|
+
const frameRef = useRef(void 0);
|
|
81
|
+
useEffect(() => () => {
|
|
82
|
+
if (frameRef.current !== void 0) cancelAnimationFrame(frameRef.current);
|
|
83
|
+
}, []);
|
|
84
|
+
useImperativeHandle(ref, () => ({
|
|
85
|
+
getViewport: () => ({
|
|
86
|
+
...toViewport(transformRef.current),
|
|
87
|
+
width: sizeRef.current.w,
|
|
88
|
+
height: sizeRef.current.h
|
|
89
|
+
}),
|
|
90
|
+
focus: (target, options = {}) => {
|
|
91
|
+
const { w, h } = sizeRef.current;
|
|
92
|
+
let box = null;
|
|
93
|
+
if (isBounds(target)) box = target;
|
|
94
|
+
else if (target) {
|
|
95
|
+
const wanted = new Set(target);
|
|
96
|
+
const picked = elements.filter((el) => wanted.has(el.id));
|
|
97
|
+
if (picked.length > 0) box = getCommonBounds(picked);
|
|
98
|
+
} else if (elements.length > 0) box = [
|
|
99
|
+
bounds.minX,
|
|
100
|
+
bounds.minY,
|
|
101
|
+
bounds.minX + bounds.width,
|
|
102
|
+
bounds.minY + bounds.height
|
|
103
|
+
];
|
|
104
|
+
if (!box || !w || !h) return false;
|
|
105
|
+
const next = fromViewport(viewportFor(box, {
|
|
106
|
+
width: w,
|
|
107
|
+
height: h
|
|
108
|
+
}, {
|
|
109
|
+
...options,
|
|
110
|
+
minZoom: MIN_SCALE,
|
|
111
|
+
maxZoom: Math.min(options.maxZoom ?? 16, 16)
|
|
112
|
+
}));
|
|
113
|
+
userInteractedRef.current = true;
|
|
114
|
+
if (frameRef.current !== void 0) cancelAnimationFrame(frameRef.current);
|
|
115
|
+
frameRef.current = void 0;
|
|
116
|
+
if (!options.animate || typeof requestAnimationFrame !== "function") {
|
|
117
|
+
setTransform(next);
|
|
118
|
+
return true;
|
|
119
|
+
}
|
|
120
|
+
const from = transformRef.current;
|
|
121
|
+
const started = performance.now();
|
|
122
|
+
const duration = options.duration ?? FOCUS_DURATION;
|
|
123
|
+
const step = (now) => {
|
|
124
|
+
const t = Math.min(1, (now - started) / duration);
|
|
125
|
+
const eased = 1 - (1 - t) ** 3;
|
|
126
|
+
const mix = (a, b) => a + (b - a) * eased;
|
|
127
|
+
setTransform({
|
|
128
|
+
s: mix(from.s, next.s),
|
|
129
|
+
tx: mix(from.tx, next.tx),
|
|
130
|
+
ty: mix(from.ty, next.ty)
|
|
131
|
+
});
|
|
132
|
+
frameRef.current = t < 1 ? requestAnimationFrame(step) : void 0;
|
|
133
|
+
};
|
|
134
|
+
frameRef.current = requestAnimationFrame(step);
|
|
135
|
+
return true;
|
|
136
|
+
}
|
|
137
|
+
}), [
|
|
138
|
+
bounds,
|
|
139
|
+
elements,
|
|
140
|
+
setTransform,
|
|
141
|
+
sizeRef,
|
|
142
|
+
transformRef,
|
|
143
|
+
userInteractedRef
|
|
144
|
+
]);
|
|
145
|
+
}
|
|
146
|
+
//#endregion
|
|
147
|
+
//#region src/scene/useSceneGestures.ts
|
|
148
|
+
function useSceneGestures({ containerRef, mode, setTransform, transformRef, userInteractedRef, wheelPan, zoomAt }) {
|
|
149
|
+
const [panning, setPanning] = useState(false);
|
|
150
|
+
useEffect(() => {
|
|
151
|
+
const node = containerRef.current;
|
|
152
|
+
if (!node) return;
|
|
153
|
+
const onWheel = (e) => {
|
|
154
|
+
if (!e.ctrlKey && !e.metaKey) {
|
|
155
|
+
if (!wheelPan) return;
|
|
156
|
+
e.preventDefault();
|
|
157
|
+
userInteractedRef.current = true;
|
|
158
|
+
const dx = e.shiftKey && !e.deltaX ? e.deltaY : e.deltaX;
|
|
159
|
+
const dy = e.shiftKey && !e.deltaX ? 0 : e.deltaY;
|
|
160
|
+
setTransform((t) => ({
|
|
161
|
+
s: t.s,
|
|
162
|
+
tx: t.tx - dx,
|
|
163
|
+
ty: t.ty - dy
|
|
164
|
+
}));
|
|
165
|
+
return;
|
|
166
|
+
}
|
|
167
|
+
e.preventDefault();
|
|
168
|
+
const rect = node.getBoundingClientRect();
|
|
169
|
+
zoomAt(e.deltaY > 0 ? .9 : 1.1, e.clientX - rect.left, e.clientY - rect.top);
|
|
170
|
+
};
|
|
171
|
+
node.addEventListener("wheel", onWheel, { passive: false });
|
|
172
|
+
return () => node.removeEventListener("wheel", onWheel);
|
|
173
|
+
}, [
|
|
174
|
+
containerRef,
|
|
175
|
+
setTransform,
|
|
176
|
+
userInteractedRef,
|
|
177
|
+
wheelPan,
|
|
178
|
+
zoomAt
|
|
179
|
+
]);
|
|
180
|
+
const pointersRef = useRef(/* @__PURE__ */ new Map());
|
|
181
|
+
const panRef = useRef(null);
|
|
182
|
+
const pinchRef = useRef(null);
|
|
183
|
+
const ownsPointer = (e) => !(mode === "inline" && e.pointerType === "touch");
|
|
184
|
+
const startPinch = () => {
|
|
185
|
+
const pts = [...pointersRef.current.values()];
|
|
186
|
+
const node = containerRef.current;
|
|
187
|
+
if (pts.length < 2 || !node) return;
|
|
188
|
+
const rect = node.getBoundingClientRect();
|
|
189
|
+
pinchRef.current = {
|
|
190
|
+
distance: Math.hypot(pts[0].x - pts[1].x, pts[0].y - pts[1].y) || 1,
|
|
191
|
+
origScale: transformRef.current.s,
|
|
192
|
+
origTx: transformRef.current.tx,
|
|
193
|
+
origTy: transformRef.current.ty,
|
|
194
|
+
midX: (pts[0].x + pts[1].x) / 2 - rect.left,
|
|
195
|
+
midY: (pts[0].y + pts[1].y) / 2 - rect.top
|
|
196
|
+
};
|
|
197
|
+
};
|
|
198
|
+
const handlePointerDown = (e) => {
|
|
199
|
+
if (!ownsPointer(e)) return;
|
|
200
|
+
if (e.pointerType === "mouse" && e.button !== 0) return;
|
|
201
|
+
if (e.target.closest("button")) return;
|
|
202
|
+
e.currentTarget.setPointerCapture(e.pointerId);
|
|
203
|
+
pointersRef.current.set(e.pointerId, {
|
|
204
|
+
x: e.clientX,
|
|
205
|
+
y: e.clientY
|
|
206
|
+
});
|
|
207
|
+
if (pointersRef.current.size === 1) {
|
|
208
|
+
panRef.current = {
|
|
209
|
+
startX: e.clientX,
|
|
210
|
+
startY: e.clientY,
|
|
211
|
+
origTx: transformRef.current.tx,
|
|
212
|
+
origTy: transformRef.current.ty
|
|
213
|
+
};
|
|
214
|
+
setPanning(true);
|
|
215
|
+
} else if (pointersRef.current.size === 2) {
|
|
216
|
+
panRef.current = null;
|
|
217
|
+
startPinch();
|
|
218
|
+
}
|
|
219
|
+
};
|
|
220
|
+
const handlePointerMove = (e) => {
|
|
221
|
+
if (!pointersRef.current.has(e.pointerId)) return;
|
|
222
|
+
pointersRef.current.set(e.pointerId, {
|
|
223
|
+
x: e.clientX,
|
|
224
|
+
y: e.clientY
|
|
225
|
+
});
|
|
226
|
+
const pinch = pinchRef.current;
|
|
227
|
+
if (pinch && pointersRef.current.size === 2) {
|
|
228
|
+
userInteractedRef.current = true;
|
|
229
|
+
const pts = [...pointersRef.current.values()];
|
|
230
|
+
const ratio = (Math.hypot(pts[0].x - pts[1].x, pts[0].y - pts[1].y) || 1) / pinch.distance;
|
|
231
|
+
const newS = Math.max(MIN_SCALE, Math.min(16, pinch.origScale * ratio));
|
|
232
|
+
const k = newS / pinch.origScale;
|
|
233
|
+
setTransform({
|
|
234
|
+
s: newS,
|
|
235
|
+
tx: pinch.midX - (pinch.midX - pinch.origTx) * k,
|
|
236
|
+
ty: pinch.midY - (pinch.midY - pinch.origTy) * k
|
|
237
|
+
});
|
|
238
|
+
return;
|
|
239
|
+
}
|
|
240
|
+
const pan = panRef.current;
|
|
241
|
+
if (pan && pointersRef.current.size === 1) {
|
|
242
|
+
userInteractedRef.current = true;
|
|
243
|
+
setTransform((t) => ({
|
|
244
|
+
s: t.s,
|
|
245
|
+
tx: pan.origTx + e.clientX - pan.startX,
|
|
246
|
+
ty: pan.origTy + e.clientY - pan.startY
|
|
247
|
+
}));
|
|
248
|
+
}
|
|
249
|
+
};
|
|
250
|
+
const releasePointer = (e) => {
|
|
251
|
+
if (!pointersRef.current.delete(e.pointerId)) return;
|
|
252
|
+
if (pointersRef.current.size < 2) pinchRef.current = null;
|
|
253
|
+
if (pointersRef.current.size === 0) {
|
|
254
|
+
panRef.current = null;
|
|
255
|
+
setPanning(false);
|
|
256
|
+
}
|
|
257
|
+
if (e.currentTarget.hasPointerCapture(e.pointerId)) e.currentTarget.releasePointerCapture(e.pointerId);
|
|
258
|
+
};
|
|
259
|
+
return {
|
|
260
|
+
panning,
|
|
261
|
+
onPointerCancel: releasePointer,
|
|
262
|
+
onPointerDown: handlePointerDown,
|
|
263
|
+
onPointerMove: handlePointerMove,
|
|
264
|
+
onPointerUp: releasePointer
|
|
265
|
+
};
|
|
266
|
+
}
|
|
267
|
+
//#endregion
|
|
268
|
+
//#region src/scene/InteractiveScene.tsx
|
|
269
|
+
var ZOOM_STEP = 1.2;
|
|
270
|
+
var InteractiveScene = ({ controls = true, handFontOverride, initialViewport, mode = "inline", onExpand, onViewportChange, preserveView = false, ref, scene, theme = "light", wheelPan = false }) => {
|
|
271
|
+
const containerRef = useRef(null);
|
|
272
|
+
const [size, setSize] = useState({
|
|
273
|
+
w: 0,
|
|
274
|
+
h: 0
|
|
275
|
+
});
|
|
276
|
+
const [transform, setTransform] = useState(() => initialViewport ? fromViewport(initialViewport) : {
|
|
277
|
+
s: 1,
|
|
278
|
+
tx: 0,
|
|
279
|
+
ty: 0
|
|
280
|
+
});
|
|
281
|
+
const transformRef = useRef(transform);
|
|
282
|
+
const sizeRef = useRef(size);
|
|
283
|
+
const pinnedRef = useRef(!!initialViewport);
|
|
284
|
+
const userInteractedRef = useRef(!!initialViewport);
|
|
285
|
+
const viewportListenerRef = useRef(onViewportChange);
|
|
286
|
+
viewportListenerRef.current = onViewportChange;
|
|
287
|
+
const elements = useMemo(() => scene.elements.filter((el) => !el.isDeleted), [scene]);
|
|
288
|
+
const bounds = useMemo(() => computeSceneBounds(elements), [elements]);
|
|
289
|
+
useEffect(() => {
|
|
290
|
+
transformRef.current = transform;
|
|
291
|
+
sizeRef.current = size;
|
|
292
|
+
if (!size.w || !size.h) return;
|
|
293
|
+
viewportListenerRef.current?.({
|
|
294
|
+
...toViewport(transform),
|
|
295
|
+
width: size.w,
|
|
296
|
+
height: size.h
|
|
297
|
+
});
|
|
298
|
+
}, [transform, size]);
|
|
299
|
+
useSceneFocus(ref, {
|
|
300
|
+
bounds,
|
|
301
|
+
elements,
|
|
302
|
+
setTransform,
|
|
303
|
+
sizeRef,
|
|
304
|
+
transformRef,
|
|
305
|
+
userInteractedRef
|
|
306
|
+
});
|
|
307
|
+
useLayoutEffect(() => {
|
|
308
|
+
const node = containerRef.current;
|
|
309
|
+
if (!node) return;
|
|
310
|
+
const rect = node.getBoundingClientRect();
|
|
311
|
+
setSize({
|
|
312
|
+
w: rect.width,
|
|
313
|
+
h: rect.height
|
|
314
|
+
});
|
|
315
|
+
const ro = new ResizeObserver((entries) => {
|
|
316
|
+
const r = entries[0].contentRect;
|
|
317
|
+
setSize({
|
|
318
|
+
w: r.width,
|
|
319
|
+
h: r.height
|
|
320
|
+
});
|
|
321
|
+
});
|
|
322
|
+
ro.observe(node);
|
|
323
|
+
return () => ro.disconnect();
|
|
324
|
+
}, []);
|
|
325
|
+
const computeFit = useCallback(() => {
|
|
326
|
+
if (!size.w || !size.h) return {
|
|
327
|
+
s: 1,
|
|
328
|
+
tx: 0,
|
|
329
|
+
ty: 0
|
|
330
|
+
};
|
|
331
|
+
const sceneW = bounds.width || 1;
|
|
332
|
+
const sceneH = bounds.height || 1;
|
|
333
|
+
const s = Math.max(MIN_SCALE, Math.min(16, Math.min((size.w - 64) / sceneW, (size.h - 64) / sceneH)));
|
|
334
|
+
return {
|
|
335
|
+
s,
|
|
336
|
+
tx: size.w / 2 - (bounds.minX + sceneW / 2) * s,
|
|
337
|
+
ty: size.h / 2 - (bounds.minY + sceneH / 2) * s
|
|
338
|
+
};
|
|
339
|
+
}, [size, bounds]);
|
|
340
|
+
const fitToContent = useCallback(() => {
|
|
341
|
+
setTransform(computeFit());
|
|
342
|
+
userInteractedRef.current = false;
|
|
343
|
+
}, [computeFit]);
|
|
344
|
+
useLayoutEffect(() => {
|
|
345
|
+
if (userInteractedRef.current) return;
|
|
346
|
+
if (!size.w || !size.h) return;
|
|
347
|
+
setTransform(computeFit());
|
|
348
|
+
if (preserveView) userInteractedRef.current = true;
|
|
349
|
+
}, [
|
|
350
|
+
size,
|
|
351
|
+
computeFit,
|
|
352
|
+
preserveView
|
|
353
|
+
]);
|
|
354
|
+
useEffect(() => {
|
|
355
|
+
if (pinnedRef.current || preserveView) return;
|
|
356
|
+
userInteractedRef.current = false;
|
|
357
|
+
}, [scene, preserveView]);
|
|
358
|
+
const zoomAt = useCallback((factor, anchorX, anchorY) => {
|
|
359
|
+
const t = transformRef.current;
|
|
360
|
+
const newS = Math.max(MIN_SCALE, Math.min(16, t.s * factor));
|
|
361
|
+
const k = newS / t.s;
|
|
362
|
+
if (k === 1) return;
|
|
363
|
+
userInteractedRef.current = true;
|
|
364
|
+
setTransform({
|
|
365
|
+
s: newS,
|
|
366
|
+
tx: anchorX - (anchorX - t.tx) * k,
|
|
367
|
+
ty: anchorY - (anchorY - t.ty) * k
|
|
368
|
+
});
|
|
369
|
+
}, []);
|
|
370
|
+
const gestures = useSceneGestures({
|
|
371
|
+
containerRef,
|
|
372
|
+
mode,
|
|
373
|
+
setTransform,
|
|
374
|
+
transformRef,
|
|
375
|
+
userInteractedRef,
|
|
376
|
+
wheelPan,
|
|
377
|
+
zoomAt
|
|
378
|
+
});
|
|
379
|
+
const handleDoubleClick = (e) => {
|
|
380
|
+
if (e.target.closest("button")) return;
|
|
381
|
+
fitToContent();
|
|
382
|
+
};
|
|
383
|
+
const centerZoom = (factor) => zoomAt(factor, size.w / 2, size.h / 2);
|
|
384
|
+
const isDark = theme === "dark";
|
|
385
|
+
const label = `Whiteboard, ${elements.length} elements`;
|
|
386
|
+
return /* @__PURE__ */ jsxs("div", {
|
|
387
|
+
className: sceneRoot,
|
|
388
|
+
"data-color-scheme": theme,
|
|
389
|
+
"data-mode": mode,
|
|
390
|
+
"data-panning": gestures.panning,
|
|
391
|
+
ref: containerRef,
|
|
392
|
+
onDoubleClick: handleDoubleClick,
|
|
393
|
+
onPointerCancel: gestures.onPointerCancel,
|
|
394
|
+
onPointerDown: gestures.onPointerDown,
|
|
395
|
+
onPointerMove: gestures.onPointerMove,
|
|
396
|
+
onPointerUp: gestures.onPointerUp,
|
|
397
|
+
children: [/* @__PURE__ */ jsx("svg", {
|
|
398
|
+
"aria-label": label,
|
|
399
|
+
className: sceneSvg,
|
|
400
|
+
preserveAspectRatio: "xMinYMin meet",
|
|
401
|
+
role: "img",
|
|
402
|
+
style: isDark ? { filter: DARK_SCENE_FILTER } : void 0,
|
|
403
|
+
viewBox: `0 0 ${size.w || 1} ${size.h || 1}`,
|
|
404
|
+
xmlns: "http://www.w3.org/2000/svg",
|
|
405
|
+
children: /* @__PURE__ */ jsx("g", {
|
|
406
|
+
transform: `translate(${transform.tx} ${transform.ty}) scale(${transform.s})`,
|
|
407
|
+
children: /* @__PURE__ */ jsx(SceneContent, {
|
|
408
|
+
dark: isDark,
|
|
409
|
+
files: scene.files,
|
|
410
|
+
handFontOverride,
|
|
411
|
+
scene
|
|
412
|
+
})
|
|
413
|
+
})
|
|
414
|
+
}), controls && /* @__PURE__ */ jsx(SceneControls, {
|
|
415
|
+
scale: transform.s,
|
|
416
|
+
onExpand,
|
|
417
|
+
onFit: fitToContent,
|
|
418
|
+
onZoomIn: () => centerZoom(ZOOM_STEP),
|
|
419
|
+
onZoomOut: () => centerZoom(1 / ZOOM_STEP)
|
|
420
|
+
})]
|
|
421
|
+
});
|
|
422
|
+
};
|
|
423
|
+
//#endregion
|
|
424
|
+
export { InteractiveScene as t };
|