@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,34 @@
1
+ import { type UiHandle, type UiOptions } from "@daasboard/ui";
2
+ import type { BoardUi } from "./types.js";
3
+ export declare function mountOptions(ui: boolean | UiOptions | undefined): UiOptions | null;
4
+ export declare function hostContainers(o: UiOptions, isElement: (v: unknown) => v is HTMLElement): HTMLElement[];
5
+ export declare class BoardUiWiring {
6
+ #private;
7
+ private readonly handle;
8
+ private readonly watch;
9
+ private readonly isElement;
10
+ readonly ui: BoardUi;
11
+ constructor(handle: Pick<UiHandle, "configure">, options: UiOptions, watch: (container: HTMLElement) => () => void, isElement: (v: unknown) => v is HTMLElement);
12
+ get options(): UiOptions;
13
+ get containers(): HTMLElement[];
14
+ configure(partial: UiOptions): void;
15
+ dispose(): void;
16
+ }
17
+ export declare function hides(to: EventTarget, node: Node): boolean;
18
+ export interface LandingTimers {
19
+ setTimeout(fn: () => void, ms: number): number;
20
+ clearTimeout(id: number): void;
21
+ }
22
+ export declare class FocusLanding {
23
+ #private;
24
+ private readonly o;
25
+ constructor(o: {
26
+ places: () => readonly Node[];
27
+ inside: (focused: Element) => boolean;
28
+ end: () => void;
29
+ timers: LandingTimers;
30
+ });
31
+ waits(to: EventTarget | null): boolean;
32
+ focused(): Element | null;
33
+ dispose(): void;
34
+ }
@@ -0,0 +1,122 @@
1
+ import { mergeOptions } from "@daasboard/ui";
2
+ export function mountOptions(ui) {
3
+ if (ui === false)
4
+ return null;
5
+ return typeof ui === "object" && ui !== null ? ui : {};
6
+ }
7
+ export function hostContainers(o, isElement) {
8
+ const out = [];
9
+ const add = (v) => {
10
+ if (isElement(v) && !out.includes(v))
11
+ out.push(v);
12
+ };
13
+ if (typeof o.pane === "object" && o.pane !== null)
14
+ add(o.pane.container);
15
+ const tools = o.toolbar === false ? undefined : o.toolbar?.placement;
16
+ if (typeof tools === "object" && tools !== null)
17
+ add(tools.container);
18
+ const selection = o.selectionToolbar === false ? undefined : o.selectionToolbar?.placement;
19
+ if (typeof selection === "object" && selection !== null)
20
+ add(selection.container);
21
+ return out;
22
+ }
23
+ export class BoardUiWiring {
24
+ handle;
25
+ watch;
26
+ isElement;
27
+ #options;
28
+ #watched = new Map();
29
+ #disposed = false;
30
+ ui;
31
+ constructor(handle, options, watch, isElement) {
32
+ this.handle = handle;
33
+ this.watch = watch;
34
+ this.isElement = isElement;
35
+ this.#options = options;
36
+ this.#sync();
37
+ this.ui = Object.freeze({ configure: (partial) => this.configure(partial) });
38
+ }
39
+ get options() {
40
+ return this.#options;
41
+ }
42
+ get containers() {
43
+ return [...this.#watched.keys()];
44
+ }
45
+ configure(partial) {
46
+ if (this.#disposed)
47
+ return;
48
+ this.#options = mergeOptions(this.#options, partial);
49
+ try {
50
+ this.handle.configure(partial);
51
+ }
52
+ finally {
53
+ this.#sync();
54
+ }
55
+ }
56
+ #sync() {
57
+ const next = hostContainers(this.#options, this.isElement);
58
+ for (const [container, stop] of [...this.#watched]) {
59
+ if (next.includes(container))
60
+ continue;
61
+ this.#watched.delete(container);
62
+ stop();
63
+ }
64
+ for (const container of next)
65
+ if (!this.#watched.has(container))
66
+ this.#watched.set(container, this.watch(container));
67
+ }
68
+ dispose() {
69
+ if (this.#disposed)
70
+ return;
71
+ this.#disposed = true;
72
+ const stops = [...this.#watched.values()];
73
+ this.#watched.clear();
74
+ for (const stop of stops)
75
+ stop();
76
+ }
77
+ }
78
+ export function hides(to, node) {
79
+ for (let n = node; n;) {
80
+ if (n.parentNode)
81
+ n = n.parentNode;
82
+ else if ((n = n.host ?? null) === to)
83
+ return true;
84
+ }
85
+ return false;
86
+ }
87
+ export class FocusLanding {
88
+ o;
89
+ #timer;
90
+ #disposed = false;
91
+ constructor(o) {
92
+ this.o = o;
93
+ }
94
+ waits(to) {
95
+ if (this.#disposed || !to || !this.o.places().some((place) => hides(to, place)))
96
+ return false;
97
+ this.#timer ??= this.o.timers.setTimeout(() => this.#landed(), 0);
98
+ return true;
99
+ }
100
+ focused() {
101
+ const roots = new Set();
102
+ for (const place of this.o.places())
103
+ roots.add(place.getRootNode());
104
+ for (const root of roots) {
105
+ const at = root.activeElement;
106
+ if (at && this.o.inside(at))
107
+ return at;
108
+ }
109
+ return null;
110
+ }
111
+ #landed() {
112
+ this.#timer = undefined;
113
+ if (!this.#disposed && !this.focused())
114
+ this.o.end();
115
+ }
116
+ dispose() {
117
+ this.#disposed = true;
118
+ if (this.#timer !== undefined)
119
+ this.o.timers.clearTimeout(this.#timer);
120
+ this.#timer = undefined;
121
+ }
122
+ }
@@ -0,0 +1,19 @@
1
+ import { type ToolName } from "@daasboard/editor";
2
+ import { type CommandName, type UiOptions } from "@daasboard/ui";
3
+ export type BoardCommand = CommandName | "cancel" | "nudge" | "bold" | "italic" | "underline" | "strike";
4
+ export type BoardBuiltin = "clipboard" | "copy" | "cut" | "paste" | "shortcuts" | "pan" | "zoom" | "textEditing" | "textInput" | "contextMenu" | `command:${BoardCommand}` | `tool:${ToolName}` | `shortcut:${string}`;
5
+ export interface BuiltinEvent {
6
+ readonly type: string;
7
+ readonly target?: EventTarget | null;
8
+ readonly key?: string;
9
+ readonly code?: string;
10
+ readonly ctrlKey?: boolean;
11
+ readonly metaKey?: boolean;
12
+ readonly altKey?: boolean;
13
+ readonly shiftKey?: boolean;
14
+ readonly inputType?: string;
15
+ }
16
+ export declare function createBuiltinPolicy(disabled: readonly BoardBuiltin[] | undefined, ui: () => UiOptions | null, textEditing: (event: BuiltinEvent) => boolean, mac: boolean): {
17
+ allows(event: BuiltinEvent): boolean;
18
+ enabled(name: BoardBuiltin): boolean;
19
+ };
@@ -0,0 +1,198 @@
1
+ import { TOOL_NAMES } from "@daasboard/editor";
2
+ import { DEFAULT_TOOL_SHORTCUTS, DEFAULT_SELECTION_ITEMS, SELECTION_SHORTCUTS } from "@daasboard/ui";
3
+ const ZOOM_KEYS = {
4
+ zoomIn: "Mod+=", zoomOut: "Mod+-", zoomReset: "Mod+0", zoomFit: "Shift+1", zoomSelection: "Shift+2",
5
+ };
6
+ function chord(text, mac) {
7
+ const parts = text.split("+");
8
+ if (parts.length > 1 && parts.at(-1) === "" && parts.at(-2) === "")
9
+ parts.splice(-2, 2, "+");
10
+ const raw = parts.pop()?.trim();
11
+ if (!raw)
12
+ return null;
13
+ const key = raw === "Space" ? " " : raw.length === 1 ? raw.toLowerCase() : raw;
14
+ const out = { key, ctrl: false, meta: false, alt: false, shift: false };
15
+ for (const part of parts) {
16
+ switch (part.trim().toLowerCase()) {
17
+ case "mod":
18
+ if (mac)
19
+ out.meta = true;
20
+ else
21
+ out.ctrl = true;
22
+ break;
23
+ case "ctrl":
24
+ case "control":
25
+ out.ctrl = true;
26
+ break;
27
+ case "cmd":
28
+ case "meta":
29
+ out.meta = true;
30
+ break;
31
+ case "alt":
32
+ case "option":
33
+ out.alt = true;
34
+ break;
35
+ case "shift":
36
+ out.shift = true;
37
+ break;
38
+ default: return null;
39
+ }
40
+ }
41
+ return out;
42
+ }
43
+ function matches(c, e) {
44
+ if (!!e.ctrlKey !== c.ctrl || !!e.metaKey !== c.meta || !!e.altKey !== c.alt || !!e.shiftKey !== c.shift)
45
+ return false;
46
+ const key = e.key?.length === 1 ? e.key.toLowerCase() : e.key;
47
+ if (key === c.key)
48
+ return true;
49
+ if (key && /^[a-z0-9]$/.test(key))
50
+ return false;
51
+ if (/^[a-z]$/.test(c.key))
52
+ return e.code === `Key${c.key.toUpperCase()}`;
53
+ if (/^[0-9]$/.test(c.key))
54
+ return e.code === `Digit${c.key}`;
55
+ return false;
56
+ }
57
+ export function createBuiltinPolicy(disabled, ui, textEditing, mac) {
58
+ const off = new Set(disabled);
59
+ const literals = [...off].filter((n) => n.startsWith("shortcut:")).map((n) => chord(n.slice(9), mac)).filter((c) => c !== null);
60
+ const clipboardKeys = { copy: "c", cut: "x", paste: "v" };
61
+ const clipboardKeysOff = new Set();
62
+ for (const name of Object.keys(clipboardKeys)) {
63
+ const nativeKey = { type: "keydown", key: clipboardKeys[name], ctrlKey: !mac, metaKey: mac };
64
+ if (off.has("shortcuts") || literals.some((c) => matches(c, nativeKey)))
65
+ clipboardKeysOff.add(name);
66
+ }
67
+ const enabled = (name) => {
68
+ if (off.has(name))
69
+ return false;
70
+ if ((name === "copy" || name === "cut" || name === "paste") && (off.has("clipboard") || clipboardKeysOff.has(name)))
71
+ return false;
72
+ if (name.startsWith("command:zoom") && off.has("zoom"))
73
+ return false;
74
+ return true;
75
+ };
76
+ const namedChord = (name, text, e) => {
77
+ if (enabled(name) || !text)
78
+ return true;
79
+ const c = chord(text, mac);
80
+ return !c || !matches(c, e);
81
+ };
82
+ const uiAllows = (e) => {
83
+ const options = ui();
84
+ if (!options)
85
+ return true;
86
+ const overrides = options.shortcuts ?? {};
87
+ if (options.toolbar !== false) {
88
+ const entries = options.toolbar?.tools;
89
+ const tools = entries ? entries.flatMap((t) => typeof t === "object" && "tools" in t ? t.tools : [t]) : TOOL_NAMES;
90
+ for (const tool of tools) {
91
+ if (typeof tool !== "string" || !TOOL_NAMES.includes(tool))
92
+ continue;
93
+ if (!namedChord(`tool:${tool}`, tool in overrides ? overrides[tool] : DEFAULT_TOOL_SHORTCUTS[tool], e))
94
+ return false;
95
+ }
96
+ if (options.toolbar?.zoom !== false)
97
+ for (const [name, text] of Object.entries(ZOOM_KEYS)) {
98
+ if (!namedChord(`command:${name}`, name in overrides ? overrides[name] : text, e))
99
+ return false;
100
+ }
101
+ }
102
+ const selection = options.selectionToolbar;
103
+ if (selection !== false) {
104
+ const items = typeof selection?.items === "function" ? selection.items(DEFAULT_SELECTION_ITEMS) : selection?.items ?? DEFAULT_SELECTION_ITEMS;
105
+ if (items.includes("more"))
106
+ for (const [name, text] of Object.entries(SELECTION_SHORTCUTS)) {
107
+ if (!namedChord(`command:${name}`, name in overrides ? overrides[name] : text, e))
108
+ return false;
109
+ }
110
+ }
111
+ return true;
112
+ };
113
+ const allows = (e) => {
114
+ if (!off.size)
115
+ return true;
116
+ if (e.type === "copy" || e.type === "cut" || e.type === "paste")
117
+ return enabled(e.type);
118
+ if (e.type === "wheel")
119
+ return enabled(e.ctrlKey || e.metaKey ? "zoom" : "pan");
120
+ if (e.type === "dblclick")
121
+ return enabled("textEditing");
122
+ if (e.type === "contextmenu")
123
+ return enabled("contextMenu");
124
+ if (e.type === "beforeinput" || e.type === "input") {
125
+ if (!enabled("textInput"))
126
+ return false;
127
+ if (e.inputType === "insertFromPaste" || e.inputType === "insertFromPasteAsQuotation")
128
+ return enabled("paste");
129
+ if (e.inputType === "deleteByCut")
130
+ return enabled("cut");
131
+ if (e.inputType === "historyUndo")
132
+ return enabled("command:undo");
133
+ if (e.inputType === "historyRedo")
134
+ return enabled("command:redo");
135
+ return true;
136
+ }
137
+ if (e.type.startsWith("composition"))
138
+ return enabled("textInput");
139
+ if (e.type !== "keydown" && e.type !== "keyup" && e.type !== "keypress")
140
+ return true;
141
+ if (!enabled("shortcuts") || literals.some((c) => matches(c, e)))
142
+ return false;
143
+ const key = e.key ?? "";
144
+ const lower = key.toLowerCase();
145
+ const mod = !!(e.ctrlKey || e.metaKey);
146
+ if (mod && !e.altKey && !e.shiftKey) {
147
+ const operation = lower === "c" ? "copy" : lower === "x" ? "cut" : lower === "v" ? "paste" : null;
148
+ if (operation && !enabled(operation))
149
+ return false;
150
+ }
151
+ const inText = textEditing(e);
152
+ if (inText && !enabled("textInput"))
153
+ return false;
154
+ if (key === "ContextMenu" || (key === "F10" && e.shiftKey))
155
+ return enabled("contextMenu");
156
+ if (key === "Escape")
157
+ return enabled("command:cancel");
158
+ if (inText) {
159
+ if (mod && !e.altKey) {
160
+ if (lower === "z")
161
+ return enabled(e.shiftKey ? "command:redo" : "command:undo");
162
+ if (lower === "y")
163
+ return enabled("command:redo");
164
+ if (lower === "a")
165
+ return enabled("command:selectAll");
166
+ if (lower === "b" || lower === "i" || lower === "u")
167
+ return enabled(`command:${lower === "b" ? "bold" : lower === "i" ? "italic" : "underline"}`);
168
+ if (lower === "x" && e.shiftKey)
169
+ return enabled("command:strike");
170
+ }
171
+ return true;
172
+ }
173
+ if (!uiAllows(e))
174
+ return false;
175
+ if (key === " " || e.code === "Space")
176
+ return enabled("pan");
177
+ if (key === "Enter" || key === "F2")
178
+ return enabled("textEditing");
179
+ if (key === "Delete" || key === "Backspace")
180
+ return enabled("command:delete");
181
+ if (key.startsWith("Arrow"))
182
+ return enabled("command:nudge");
183
+ if (mod) {
184
+ if (lower === "z")
185
+ return enabled(e.shiftKey ? "command:redo" : "command:undo");
186
+ if (lower === "y")
187
+ return enabled("command:redo");
188
+ if (lower === "a")
189
+ return enabled("command:selectAll");
190
+ if (key === "]" || key === "}")
191
+ return enabled(e.shiftKey || key === "}" ? "command:bringToFront" : "command:bringForward");
192
+ if (key === "[" || key === "{")
193
+ return enabled(e.shiftKey || key === "{" ? "command:sendToBack" : "command:sendBackward");
194
+ }
195
+ return true;
196
+ };
197
+ return { allows, enabled };
198
+ }
@@ -0,0 +1,24 @@
1
+ import type { UiPoint } from "@daasboard/ui";
2
+ export declare class ContextPress {
3
+ #private;
4
+ private readonly open;
5
+ constructor(open: (at: UiPoint | null) => boolean | void);
6
+ down(pointerId: number): void;
7
+ up(pointerId: number): boolean;
8
+ event(at: UiPoint, pointer: boolean): boolean;
9
+ cancel(): void;
10
+ openAt(at: UiPoint): boolean;
11
+ get held(): boolean;
12
+ get pointer(): number | null;
13
+ }
14
+ export declare function clipboardProxy(doc: Document): HTMLElement;
15
+ export declare function clipboardCommand(doc: Document, op: "copy" | "cut", at: {
16
+ focus(options?: FocusOptions): void;
17
+ }, release?: () => void): void;
18
+ export interface SystemClipboardContent {
19
+ html: string;
20
+ text: string;
21
+ images: File[];
22
+ }
23
+ export declare function readSystemClipboard(nav: Navigator): Promise<SystemClipboardContent | null>;
24
+ export declare function clipboardDataOf(content: SystemClipboardContent): Pick<DataTransfer, "types" | "files" | "getData">;
@@ -0,0 +1,114 @@
1
+ import { MAX_CLIPBOARD_BYTES } from "@daasboard/editor";
2
+ export class ContextPress {
3
+ open;
4
+ #press = null;
5
+ constructor(open) {
6
+ this.open = open;
7
+ }
8
+ down(pointerId) {
9
+ this.#press = { id: pointerId, released: false, at: null };
10
+ }
11
+ up(pointerId) {
12
+ const press = this.#press;
13
+ if (!press || press.id !== pointerId || press.released)
14
+ return false;
15
+ if (press.at) {
16
+ this.#press = null;
17
+ this.open(press.at);
18
+ }
19
+ else
20
+ press.released = true;
21
+ return true;
22
+ }
23
+ event(at, pointer) {
24
+ const press = this.#press;
25
+ if (press && !press.released) {
26
+ press.at = at;
27
+ return false;
28
+ }
29
+ this.#press = null;
30
+ const keys = !press && !pointer;
31
+ return this.open(keys ? null : at) === true && keys;
32
+ }
33
+ cancel() {
34
+ this.#press = null;
35
+ }
36
+ openAt(at) {
37
+ this.#press = null;
38
+ return this.open(at) === true;
39
+ }
40
+ get held() {
41
+ return !!this.#press && !this.#press.released;
42
+ }
43
+ get pointer() {
44
+ return this.held ? this.#press.id : null;
45
+ }
46
+ }
47
+ export function clipboardProxy(doc) {
48
+ const el = doc.createElement("div");
49
+ el.contentEditable = "true";
50
+ el.tabIndex = -1;
51
+ el.setAttribute("aria-hidden", "true");
52
+ el.setAttribute("data-daas-clipboard", "");
53
+ el.style.cssText = "position:absolute;left:0;top:0;width:1px;height:1px;overflow:hidden;opacity:0;pointer-events:none;white-space:pre";
54
+ return el;
55
+ }
56
+ function focusedElement(doc) {
57
+ let el = doc.activeElement;
58
+ while (el?.shadowRoot?.activeElement)
59
+ el = el.shadowRoot.activeElement;
60
+ return el === doc.body || el === doc.documentElement ? null : el;
61
+ }
62
+ export function clipboardCommand(doc, op, at, release) {
63
+ const was = focusedElement(doc);
64
+ at.focus({ preventScroll: true });
65
+ try {
66
+ doc.execCommand(op);
67
+ }
68
+ finally {
69
+ if (was && typeof was.focus === "function")
70
+ was.focus({ preventScroll: true });
71
+ release?.();
72
+ }
73
+ }
74
+ export async function readSystemClipboard(nav) {
75
+ const clipboard = nav.clipboard;
76
+ if (typeof clipboard?.read !== "function")
77
+ return null;
78
+ try {
79
+ const items = await clipboard.read({ unsanitized: ["text/html"] });
80
+ const out = { html: "", text: "", images: [] };
81
+ for (const item of items) {
82
+ for (const [type, key] of [["text/html", "html"], ["text/plain", "text"]]) {
83
+ if (out[key] || !item.types.includes(type))
84
+ continue;
85
+ const blob = await item.getType(type);
86
+ if (blob.size > MAX_CLIPBOARD_BYTES)
87
+ return null;
88
+ out[key] = await blob.text();
89
+ }
90
+ for (const type of item.types) {
91
+ if (!type.startsWith("image/"))
92
+ continue;
93
+ const blob = await item.getType(type);
94
+ out.images.push(new File([blob], `image.${type.slice(6).replace(/\W.*$/, "") || "png"}`, { type }));
95
+ }
96
+ }
97
+ return out;
98
+ }
99
+ catch {
100
+ return null;
101
+ }
102
+ }
103
+ export function clipboardDataOf(content) {
104
+ const formats = {};
105
+ if (content.html)
106
+ formats["text/html"] = content.html;
107
+ if (content.text)
108
+ formats["text/plain"] = content.text;
109
+ return {
110
+ types: [...Object.keys(formats), ...(content.images.length ? ["Files"] : [])],
111
+ files: content.images,
112
+ getData: (type) => formats[type] ?? "",
113
+ };
114
+ }
@@ -0,0 +1,11 @@
1
+ export declare function boardCoordinates(viewport: HTMLElement): {
2
+ axisAligned(): boolean;
3
+ scale(): number;
4
+ local(p: {
5
+ x: number;
6
+ y: number;
7
+ }): {
8
+ x: number;
9
+ y: number;
10
+ };
11
+ };
@@ -0,0 +1,30 @@
1
+ export function boardCoordinates(viewport) {
2
+ const probes = [[0, 0], [100, 0], [0, 100]].map(([x, y]) => {
3
+ const el = viewport.ownerDocument.createElement("span");
4
+ el.style.cssText = `position:absolute;left:${x}px;top:${y}px;width:0;height:0;pointer-events:none`;
5
+ viewport.append(el);
6
+ return el;
7
+ });
8
+ const matrix = () => {
9
+ const [o, x, y] = probes.map((p) => p.getBoundingClientRect());
10
+ return { x: o.left, y: o.top, a: (x.left - o.left) / 100, b: (x.top - o.top) / 100, c: (y.left - o.left) / 100, d: (y.top - o.top) / 100 };
11
+ };
12
+ return {
13
+ axisAligned() {
14
+ const m = matrix();
15
+ return Math.abs(m.b) < 1e-6 * Math.hypot(m.a, m.b) && Math.abs(m.c) < 1e-6 * Math.hypot(m.c, m.d);
16
+ },
17
+ scale() {
18
+ const m = matrix();
19
+ return Math.sqrt(Math.abs(m.a * m.d - m.b * m.c));
20
+ },
21
+ local(p) {
22
+ const m = matrix();
23
+ const det = m.a * m.d - m.b * m.c;
24
+ if (Math.abs(det) < 1e-12)
25
+ return { x: 0, y: 0 };
26
+ const x = p.x - m.x, y = p.y - m.y;
27
+ return { x: (m.d * x - m.c * y) / det, y: (m.a * y - m.b * x) / det };
28
+ },
29
+ };
30
+ }
@@ -0,0 +1,2 @@
1
+ import type { BoardHandle, CreateBoardOptions } from "./types.js";
2
+ export declare function createBoard(options: CreateBoardOptions): Promise<BoardHandle>;