@daasboard/embed 0.0.0-stage → 0.1.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.
@@ -0,0 +1,90 @@
1
+ import type { AssetHash } from "@daasboard/doc";
2
+ import type { Point, Rect } from "@daasboard/render-core";
3
+ export type Rung = 256 | 512 | 1024 | 2048 | 4096 | "full";
4
+ export interface StoredImage {
5
+ hash: AssetHash;
6
+ mime: string;
7
+ w: number;
8
+ h: number;
9
+ bytes: number;
10
+ thumb?: string;
11
+ }
12
+ export type ImageSource = {
13
+ url: string;
14
+ } | {
15
+ bytes: Blob | Uint8Array;
16
+ };
17
+ export interface TouchResult {
18
+ stored: ReadonlySet<AssetHash>;
19
+ missing: ReadonlySet<AssetHash>;
20
+ denied: ReadonlySet<AssetHash>;
21
+ pending: ReadonlySet<AssetHash>;
22
+ }
23
+ export interface TouchOptions {
24
+ until?: "attempt" | "answered";
25
+ signal?: AbortSignal;
26
+ }
27
+ export interface ImageStore {
28
+ readonly renders?: readonly string[];
29
+ put(image: {
30
+ hash: AssetHash;
31
+ bytes: Blob;
32
+ mime: string;
33
+ w: number;
34
+ h: number;
35
+ }, options: {
36
+ room?: string;
37
+ signal?: AbortSignal;
38
+ onProgress?(sent: number, total: number): void;
39
+ }): Promise<StoredImage | void>;
40
+ get(hash: AssetHash, rung: Rung, options?: {
41
+ room?: string;
42
+ signal?: AbortSignal;
43
+ }): Promise<ImageSource | null>;
44
+ refreshUrl?(hash: AssetHash, rung: Rung, options: {
45
+ url: string;
46
+ room?: string;
47
+ signal?: AbortSignal;
48
+ }): Promise<ImageSource | null>;
49
+ touch?(room: string, hashes: readonly AssetHash[], options?: TouchOptions): Promise<TouchResult>;
50
+ flush?(room: string): Promise<TouchResult>;
51
+ pending?(): readonly {
52
+ room: string;
53
+ hash: AssetHash;
54
+ }[];
55
+ dispose?(): readonly {
56
+ room: string;
57
+ hash: AssetHash;
58
+ }[];
59
+ share?(hash: AssetHash): {
60
+ url: string;
61
+ expires: number;
62
+ } | null;
63
+ import?(hash: AssetHash, from: {
64
+ url: string;
65
+ }, options: {
66
+ room?: string;
67
+ signal?: AbortSignal;
68
+ }): Promise<StoredImage | void>;
69
+ }
70
+ export interface BoardImageOptions {
71
+ store: ImageStore;
72
+ room?: string;
73
+ }
74
+ export type ImageErrorReason = "missing" | "denied" | "bad_image" | "too_large" | "unsupported" | "worker_unavailable" | "network" | "store";
75
+ export type OnImageError = (hash: AssetHash, rung: Rung, reason: ImageErrorReason) => void;
76
+ export interface ImagePlacement {
77
+ at: Point;
78
+ visible: Rect;
79
+ parent?: string | null;
80
+ label?: string;
81
+ }
82
+ export interface BoardImageHandle {
83
+ flush(): Promise<TouchResult>;
84
+ register(hashes: readonly AssetHash[], options?: {
85
+ signal?: AbortSignal;
86
+ }): Promise<TouchResult>;
87
+ pick(): void;
88
+ insert(files: readonly Blob[], at?: Point): Promise<readonly string[]>;
89
+ cancel(): void;
90
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,9 @@
1
+ export { createBoard } from "./createBoard.js";
2
+ export type { BoardAssets, BoardFont, BoardHandle, BoardUi, CreateBoardOptions } from "./types.js";
3
+ export { ContextPress, clipboardProxy, clipboardCommand, readSystemClipboard, clipboardDataOf, type SystemClipboardContent } from "./contextMenu.js";
4
+ export { BoardImages } from "./images/BoardImages.js";
5
+ export { assetServiceStore, AssetServiceError } from "./assets/assetServiceStore.js";
6
+ export type { AssetServiceStore, AssetServiceStoreOptions } from "./assets/assetServiceStore.js";
7
+ export type { ImageStore, ImageSource, StoredImage, Rung, TouchResult, TouchOptions, BoardImageOptions, BoardImageHandle, ImageErrorReason, OnImageError, ImagePlacement } from "./images/types.js";
8
+ export type { BoardBuiltin } from "./builtins.js";
9
+ export type { ObjectClipboard, ObjectClipboardContent, ObjectPasteResult, ClipboardSource } from "@daasboard/editor";
package/dist/index.js ADDED
@@ -0,0 +1,4 @@
1
+ export { createBoard } from "./createBoard.js";
2
+ export { ContextPress, clipboardProxy, clipboardCommand, readSystemClipboard, clipboardDataOf } from "./contextMenu.js";
3
+ export { BoardImages } from "./images/BoardImages.js";
4
+ export { assetServiceStore, AssetServiceError } from "./assets/assetServiceStore.js";
@@ -0,0 +1,49 @@
1
+ import type { BoardImageOptions, BoardImageHandle, OnImageError } from "./images/types.js";
2
+ import type { DocumentAdapter } from "@daasboard/doc";
3
+ import type { ObjectClipboard, TouchOptions } from "@daasboard/editor";
4
+ import type { FontFamilyInfo, UiController, UiHost, UiOptions, UiTools } from "@daasboard/ui";
5
+ import type { BoardBuiltin } from "./builtins.js";
6
+ export interface BoardAssets {
7
+ canvasKitWasm: string;
8
+ fontUrl(file: string, sha256: string): string;
9
+ }
10
+ export interface BoardFont extends FontFamilyInfo {
11
+ faces: readonly (string | Uint8Array)[];
12
+ }
13
+ export interface CreateBoardOptions {
14
+ container: HTMLElement;
15
+ document: DocumentAdapter;
16
+ assets: BoardAssets;
17
+ signal?: AbortSignal;
18
+ fonts?: readonly BoardFont[];
19
+ readOnly?: boolean;
20
+ disableBuiltins?: readonly BoardBuiltin[];
21
+ touch?: TouchOptions;
22
+ ui?: boolean | UiOptions;
23
+ lookupLink?: UiHost["lookupLink"];
24
+ backend?: "gpu" | "cpu";
25
+ images?: BoardImageOptions;
26
+ memory?: {
27
+ images?: number;
28
+ };
29
+ onImageError?: OnImageError;
30
+ onImageScaled?: (facts: {
31
+ originalWidth: number;
32
+ originalHeight: number;
33
+ width: number;
34
+ height: number;
35
+ }) => void;
36
+ }
37
+ export interface BoardUi {
38
+ configure(partial: UiOptions): void;
39
+ }
40
+ export interface BoardHandle {
41
+ readonly controller: UiController;
42
+ readonly tools: UiTools;
43
+ readonly camera: UiHost["camera"];
44
+ readonly clipboard: ObjectClipboard;
45
+ readonly ui: BoardUi | null;
46
+ readonly images: BoardImageHandle;
47
+ focus(): void;
48
+ dispose(): void;
49
+ }
package/dist/types.js ADDED
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,42 @@
1
+ import type { Camera, SceneStore } from "@daasboard/render-core";
2
+ import { type SelectionModel, type SelectTool } from "@daasboard/editor";
3
+ import type { FontFamilyInfo, UiController, UiHost, UiTools } from "@daasboard/ui";
4
+ export type GestureSource = "tool" | "pan" | "fling" | "scroll" | "wheel" | "zoom";
5
+ export interface EmbedUiHost extends UiHost {
6
+ syncCamera(): void;
7
+ gesture(source: GestureSource, active: boolean): void;
8
+ onGestureSources(listener: (sources: ReadonlySet<GestureSource>) => void): () => void;
9
+ wheel(kind?: "pan" | "zoom"): void;
10
+ dispose(): void;
11
+ }
12
+ export interface EmbedUiHostOptions {
13
+ allowsKey?: UiHost["allowsKey"];
14
+ handleClipboard?: UiHost["handleClipboard"];
15
+ allows?: UiHost["allows"];
16
+ clipboard?: UiHost["clipboard"];
17
+ ctrl: UiController;
18
+ tools: UiTools;
19
+ selection: Pick<SelectionModel, "ids">;
20
+ store: Pick<SceneStore, "boundsOf">;
21
+ cam: Camera;
22
+ canvas: Pick<HTMLCanvasElement, "focus">;
23
+ size(): {
24
+ width: number;
25
+ height: number;
26
+ };
27
+ ids(): Iterable<string>;
28
+ sink(): {
29
+ focus(): void;
30
+ } | null;
31
+ invalidate(): void;
32
+ readOnly?: boolean;
33
+ fonts?: readonly FontFamilyInfo[];
34
+ lookupLink?: UiHost["lookupLink"];
35
+ insertImages?: () => void;
36
+ gradientHandles?: {
37
+ show: SelectTool["showGradientHandles"];
38
+ hits(e: PointerEvent): boolean;
39
+ };
40
+ }
41
+ export declare function declaredFonts(extra?: readonly FontFamilyInfo[]): readonly FontFamilyInfo[];
42
+ export declare function createEmbedUiHost(o: EmbedUiHostOptions): EmbedUiHost;
package/dist/uiHost.js ADDED
@@ -0,0 +1,273 @@
1
+ import { MAX_ZOOM, MIN_ZOOM, clampZoom } from "@daasboard/editor";
2
+ const ZOOM_STEPS = [0.02, 0.05, 0.1, 0.25, 0.5, 0.75, 1, 1.5, 2, 3, 4, 8, 16, 32, 64];
3
+ const FIT_PADDING_PX = 48;
4
+ const SELECTION_MAX_ZOOM = 4;
5
+ const WHEEL_REST_MS = 150;
6
+ const nonnegative = (n) => Number.isFinite(n) ? Math.max(0, n) : 0;
7
+ export function declaredFonts(extra = []) {
8
+ const families = new Map([["Inter", { family: "Inter", weights: [400, 700], italic: true }]]);
9
+ for (const font of extra) {
10
+ const previous = families.get(font.family);
11
+ const label = font.label ?? previous?.label;
12
+ families.set(font.family, {
13
+ family: font.family,
14
+ ...(label === undefined ? {} : { label }),
15
+ weights: [...new Set([...(previous?.weights ?? []), ...font.weights])].sort((a, b) => a - b),
16
+ italic: font.italic || previous?.italic === true,
17
+ });
18
+ }
19
+ return Object.freeze([...families.values()].map((font) => Object.freeze({ ...font, weights: Object.freeze([...font.weights]) })));
20
+ }
21
+ function union(rects) {
22
+ let x0 = Infinity, y0 = Infinity, x1 = -Infinity, y1 = -Infinity;
23
+ for (const r of rects) {
24
+ if (!r)
25
+ continue;
26
+ x0 = Math.min(x0, r.x);
27
+ y0 = Math.min(y0, r.y);
28
+ x1 = Math.max(x1, r.x + r.w);
29
+ y1 = Math.max(y1, r.y + r.h);
30
+ }
31
+ return x0 <= x1 ? { x: x0, y: y0, w: x1 - x0, h: y1 - y0 } : null;
32
+ }
33
+ export function createEmbedUiHost(o) {
34
+ const { cam, ctrl, tools } = o;
35
+ const cameraListeners = new Set();
36
+ const gestureListeners = new Set();
37
+ const sourceListeners = new Set();
38
+ const subscriptions = new Set();
39
+ const sources = new Set();
40
+ const fonts = declaredFonts(o.fonts);
41
+ const readOnly = o.readOnly ?? false;
42
+ const lookupLink = o.lookupLink;
43
+ const handles = o.gradientHandles;
44
+ let disposed = false;
45
+ const wheelTimers = new Map();
46
+ let insets = { top: 0, right: 0, bottom: 0, left: 0 };
47
+ let seen = { x: cam.x, y: cam.y, zoom: cam.zoom };
48
+ const writable = () => !readOnly && !disposed;
49
+ const listen = (set, listener) => {
50
+ if (disposed)
51
+ return () => { };
52
+ set.add(listener);
53
+ return () => { set.delete(listener); };
54
+ };
55
+ const subscribe = (register) => {
56
+ if (disposed)
57
+ return () => { };
58
+ const remove = register();
59
+ const unsubscribe = () => { if (subscriptions.delete(unsubscribe))
60
+ remove(); };
61
+ subscriptions.add(unsubscribe);
62
+ return unsubscribe;
63
+ };
64
+ const gesture = (source, active) => {
65
+ if (disposed || sources.has(source) === active)
66
+ return;
67
+ const was = sources.size > 0;
68
+ if (active)
69
+ sources.add(source);
70
+ else
71
+ sources.delete(source);
72
+ for (const l of sourceListeners)
73
+ l(sources);
74
+ if ((sources.size > 0) !== was)
75
+ for (const l of gestureListeners)
76
+ l(sources.size > 0);
77
+ };
78
+ const syncCamera = () => {
79
+ if (disposed || (cam.x === seen.x && cam.y === seen.y && cam.zoom === seen.zoom))
80
+ return;
81
+ seen = { x: cam.x, y: cam.y, zoom: cam.zoom };
82
+ for (const l of cameraListeners)
83
+ l();
84
+ };
85
+ const uncovered = () => {
86
+ const { width, height } = o.size();
87
+ return {
88
+ x: insets.left,
89
+ y: insets.top,
90
+ w: Math.max(1, width - insets.left - insets.right),
91
+ h: Math.max(1, height - insets.top - insets.bottom),
92
+ };
93
+ };
94
+ const zoomCentered = (z) => {
95
+ const v = uncovered();
96
+ const cx = v.x + v.w / 2, cy = v.y + v.h / 2;
97
+ const wx = cam.x + cx / cam.zoom, wy = cam.y + cy / cam.zoom;
98
+ cam.zoom = clampZoom(z);
99
+ cam.x = wx - cx / cam.zoom;
100
+ cam.y = wy - cy / cam.zoom;
101
+ };
102
+ const fitRect = (r, maxZoom) => {
103
+ const v = uncovered();
104
+ const z = Math.min((v.w - 2 * FIT_PADDING_PX) / Math.max(r.w, 1e-6), (v.h - 2 * FIT_PADDING_PX) / Math.max(r.h, 1e-6), maxZoom);
105
+ cam.zoom = clampZoom(z > 0 ? z : MIN_ZOOM);
106
+ cam.x = r.x + r.w / 2 - (v.x + v.w / 2) / cam.zoom;
107
+ cam.y = r.y + r.h / 2 - (v.y + v.h / 2) / cam.zoom;
108
+ };
109
+ const step = (dir) => {
110
+ const next = dir > 0 ? ZOOM_STEPS.find((s) => s > cam.zoom * 1.01) : [...ZOOM_STEPS].reverse().find((s) => s < cam.zoom / 1.01);
111
+ return next ?? (dir > 0 ? MAX_ZOOM : MIN_ZOOM);
112
+ };
113
+ const selectionBounds = () => union([...o.selection.ids].map((id) => o.store.boundsOf(id)));
114
+ const commandAllowed = (name) => !disposed && (!readOnly || name === "selectAll");
115
+ const commands = Object.fromEntries(Object.entries(ctrl.commands).map(([name, command]) => [name, Object.freeze({
116
+ label: command.label,
117
+ canRun: () => commandAllowed(name) && command.canRun(),
118
+ run: () => { if (commandAllowed(name))
119
+ command.run(); },
120
+ })]));
121
+ const textTargets = new WeakMap();
122
+ const textTarget = () => {
123
+ const target = writable() ? ctrl.textTarget() : null;
124
+ if (!target)
125
+ return null;
126
+ let guarded = textTargets.get(target);
127
+ if (!guarded) {
128
+ guarded = Object.freeze({
129
+ objectId: target.objectId,
130
+ readMark: (name) => target.readMark(name),
131
+ writeMark: (name, value) => { if (writable())
132
+ target.writeMark(name, value); },
133
+ });
134
+ textTargets.set(target, guarded);
135
+ }
136
+ return guarded;
137
+ };
138
+ const controller = Object.freeze({
139
+ read: (...args) => ctrl.read(...args),
140
+ set: (...args) => { if (writable())
141
+ ctrl.set(...args); },
142
+ preview: (...args) => { if (writable())
143
+ ctrl.preview(...args); },
144
+ commitPreview: (...args) => { if (writable())
145
+ ctrl.commitPreview(...args); },
146
+ cancelPreview: () => { if (writable())
147
+ ctrl.cancelPreview(); },
148
+ hasPreview: () => writable() && ctrl.hasPreview(),
149
+ previewOverlay: () => writable() ? ctrl.previewOverlay() : new Map(),
150
+ commands: Object.freeze(commands),
151
+ subscribe: (l) => subscribe(() => ctrl.subscribe(l)),
152
+ targets: () => ctrl.targets(),
153
+ textTarget,
154
+ create: (...args) => {
155
+ if (!writable())
156
+ throw new Error(disposed ? "Board is disposed" : "Board is read-only");
157
+ return ctrl.create(...args);
158
+ },
159
+ });
160
+ const uiTools = Object.freeze({
161
+ get active() { return tools.active; },
162
+ setTool: (name) => {
163
+ if (!disposed && (!readOnly || name === "select" || name === "hand"))
164
+ tools.setTool(name);
165
+ },
166
+ setCreationStyle: (...args) => { if (writable())
167
+ tools.setCreationStyle(...args); },
168
+ creationStyle: (kind) => tools.creationStyle(kind),
169
+ onToolChange: (l) => subscribe(() => tools.onToolChange(l)),
170
+ });
171
+ return {
172
+ ...(o.allowsKey ? { allowsKey: o.allowsKey } : {}),
173
+ ...(o.handleClipboard ? { handleClipboard: o.handleClipboard } : {}),
174
+ ...(o.allows ? { allows: o.allows } : {}),
175
+ ...(o.clipboard ? { clipboard: o.clipboard } : {}),
176
+ controller,
177
+ tools: uiTools,
178
+ readOnly: () => readOnly,
179
+ camera: {
180
+ zoom: () => cam.zoom,
181
+ zoomTo(level) {
182
+ if (disposed || (typeof level === "number" && !Number.isFinite(level)))
183
+ return;
184
+ if (typeof level === "number")
185
+ zoomCentered(level);
186
+ else if (level === "in" || level === "out")
187
+ zoomCentered(step(level === "in" ? 1 : -1));
188
+ else {
189
+ const r = level === "fit" ? union([...o.ids()].map((id) => o.store.boundsOf(id))) : selectionBounds();
190
+ if (!r)
191
+ return;
192
+ fitRect(r, level === "fit" ? MAX_ZOOM : SELECTION_MAX_ZOOM);
193
+ }
194
+ gesture("zoom", true);
195
+ syncCamera();
196
+ o.invalidate();
197
+ gesture("zoom", false);
198
+ },
199
+ onChange: (l) => listen(cameraListeners, l),
200
+ },
201
+ onGesture: (l) => listen(gestureListeners, l),
202
+ onGestureSources: (l) => listen(sourceListeners, l),
203
+ selectionRect() {
204
+ const r = selectionBounds();
205
+ if (!r)
206
+ return null;
207
+ const { width, height } = o.size();
208
+ const rect = { x: (r.x - cam.x) * cam.zoom, y: (r.y - cam.y) * cam.zoom, w: r.w * cam.zoom, h: r.h * cam.zoom };
209
+ const off = rect.x + rect.w < 0 || rect.y + rect.h < 0 || rect.x > width || rect.y > height;
210
+ return off ? null : rect;
211
+ },
212
+ fonts: () => fonts,
213
+ ...(lookupLink ? { lookupLink: (url) => disposed ? Promise.reject(new Error("Board is disposed")) : lookupLink(url) } : {}),
214
+ ...(handles && !readOnly ? {
215
+ gradientHandles(target, events) {
216
+ if (disposed)
217
+ return null;
218
+ const session = handles.show(target, events);
219
+ o.invalidate();
220
+ return {
221
+ hits: (e) => !disposed && handles.hits(e),
222
+ cancelDrag() { session.cancelDrag(); o.invalidate(); },
223
+ hide() { session.hide(); o.invalidate(); },
224
+ };
225
+ },
226
+ } : {}),
227
+ ...(o.insertImages && !readOnly ? { insertImages: () => { if (writable())
228
+ o.insertImages(); } } : {}),
229
+ returnFocus() {
230
+ if (disposed)
231
+ return;
232
+ const sink = !readOnly && o.sink();
233
+ if (sink)
234
+ sink.focus();
235
+ else
236
+ o.canvas.focus({ preventScroll: true });
237
+ },
238
+ setInsets(next) {
239
+ if (disposed)
240
+ return;
241
+ insets = { top: nonnegative(next.top), right: nonnegative(next.right), bottom: nonnegative(next.bottom), left: nonnegative(next.left) };
242
+ },
243
+ onChange: () => () => { },
244
+ syncCamera,
245
+ gesture,
246
+ wheel(kind = "zoom") {
247
+ if (disposed)
248
+ return;
249
+ const source = kind === "pan" ? "scroll" : "wheel";
250
+ const previous = kind === "pan" ? "wheel" : "scroll";
251
+ gesture(source, true);
252
+ clearTimeout(wheelTimers.get(previous));
253
+ wheelTimers.delete(previous);
254
+ gesture(previous, false);
255
+ clearTimeout(wheelTimers.get(source));
256
+ wheelTimers.set(source, setTimeout(() => { wheelTimers.delete(source); gesture(source, false); }, WHEEL_REST_MS));
257
+ },
258
+ dispose() {
259
+ if (disposed)
260
+ return;
261
+ disposed = true;
262
+ for (const timer of wheelTimers.values())
263
+ clearTimeout(timer);
264
+ wheelTimers.clear();
265
+ sources.clear();
266
+ cameraListeners.clear();
267
+ gestureListeners.clear();
268
+ sourceListeners.clear();
269
+ for (const unsubscribe of subscriptions)
270
+ unsubscribe();
271
+ },
272
+ };
273
+ }
package/package.json CHANGED
@@ -1,6 +1,36 @@
1
1
  {
2
2
  "name": "@daasboard/embed",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
3
+ "version": "0.1.0",
4
+ "type": "module",
5
+ "main": "./dist/index.js",
6
+ "types": "./dist/index.d.ts",
7
+ "dependencies": {
8
+ "@daasboard/doc": "0.1.0",
9
+ "@daasboard/render-core": "0.1.0",
10
+ "@daasboard/editor": "0.1.0",
11
+ "@daasboard/ui": "0.1.0"
12
+ },
13
+ "license": "UNLICENSED",
14
+ "description": "Embeddable collaborative diagram editor.",
15
+ "exports": {
16
+ ".": {
17
+ "types": "./dist/index.d.ts",
18
+ "import": "./dist/index.js",
19
+ "default": "./dist/index.js"
20
+ }
21
+ },
22
+ "files": [
23
+ "dist",
24
+ "README.md",
25
+ "LICENSE",
26
+ "THIRD_PARTY_NOTICES.md"
27
+ ],
28
+ "sideEffects": false,
29
+ "publishConfig": {
30
+ "access": "public"
31
+ },
32
+ "engines": {
33
+ "node": ">=22.3.0"
34
+ },
35
+ "daasboardCommit": "ec08eb540f53c278ff9aa3d7c23a51407c429133"
6
36
  }