@daasboard/ui 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.
- package/LICENSE +5 -0
- package/README.md +6 -2
- package/dist/LICENSE-lucide.txt +43 -0
- package/dist/color/color.d.ts +34 -0
- package/dist/color/color.js +106 -0
- package/dist/color/edit.d.ts +34 -0
- package/dist/color/edit.js +40 -0
- package/dist/color/gradient.d.ts +39 -0
- package/dist/color/gradient.js +129 -0
- package/dist/color/index.d.ts +3 -0
- package/dist/color/index.js +3 -0
- package/dist/controls/buttons.d.ts +54 -0
- package/dist/controls/buttons.js +95 -0
- package/dist/controls/font-picker.d.ts +35 -0
- package/dist/controls/font-picker.js +178 -0
- package/dist/controls/index.d.ts +43 -0
- package/dist/controls/index.js +32 -0
- package/dist/controls/number-field.d.ts +60 -0
- package/dist/controls/number-field.js +190 -0
- package/dist/controls/place.d.ts +10 -0
- package/dist/controls/place.js +77 -0
- package/dist/controls/popover.d.ts +84 -0
- package/dist/controls/popover.js +366 -0
- package/dist/controls/shared.d.ts +30 -0
- package/dist/controls/shared.js +134 -0
- package/dist/controls/slider.d.ts +82 -0
- package/dist/controls/slider.js +241 -0
- package/dist/controls/swatch.d.ts +25 -0
- package/dist/controls/swatch.js +38 -0
- package/dist/controls/tooltip.d.ts +19 -0
- package/dist/controls/tooltip.js +84 -0
- package/dist/controls/typed.d.ts +28 -0
- package/dist/controls/typed.js +126 -0
- package/dist/element.d.ts +40 -0
- package/dist/element.js +78 -0
- package/dist/host.d.ts +99 -0
- package/dist/host.js +1 -0
- package/dist/icons/index.d.ts +7 -0
- package/dist/icons/index.js +31 -0
- package/dist/icons/lucide.d.ts +851 -0
- package/dist/icons/lucide.js +90 -0
- package/dist/index.d.ts +18 -0
- package/dist/index.js +16 -0
- package/dist/keys.d.ts +30 -0
- package/dist/keys.js +122 -0
- package/dist/mount.d.ts +11 -0
- package/dist/mount.js +96 -0
- package/dist/options.d.ts +152 -0
- package/dist/options.js +78 -0
- package/dist/palette.d.ts +6 -0
- package/dist/palette.js +9 -0
- package/dist/picker/handles.d.ts +7 -0
- package/dist/picker/handles.js +8 -0
- package/dist/picker/index.d.ts +21 -0
- package/dist/picker/index.js +20 -0
- package/dist/picker/palette.d.ts +8 -0
- package/dist/picker/palette.js +14 -0
- package/dist/picker/picker.d.ts +63 -0
- package/dist/picker/picker.js +561 -0
- package/dist/picker/solid-editor.d.ts +60 -0
- package/dist/picker/solid-editor.js +330 -0
- package/dist/picker/stop-bar.d.ts +27 -0
- package/dist/picker/stop-bar.js +140 -0
- package/dist/prefs.d.ts +6 -0
- package/dist/prefs.js +34 -0
- package/dist/recents.d.ts +9 -0
- package/dist/recents.js +140 -0
- package/dist/root.d.ts +31 -0
- package/dist/root.js +526 -0
- package/dist/scheduler.d.ts +21 -0
- package/dist/scheduler.js +132 -0
- package/dist/strings.d.ts +284 -0
- package/dist/strings.js +287 -0
- package/dist/surface.d.ts +27 -0
- package/dist/surface.js +8 -0
- package/dist/surfaces/context-menu/index.d.ts +2 -0
- package/dist/surfaces/context-menu/index.js +2 -0
- package/dist/surfaces/context-menu/items.d.ts +14 -0
- package/dist/surfaces/context-menu/items.js +38 -0
- package/dist/surfaces/context-menu/menu.d.ts +16 -0
- package/dist/surfaces/context-menu/menu.js +135 -0
- package/dist/surfaces/pane/index.d.ts +16 -0
- package/dist/surfaces/pane/index.js +19 -0
- package/dist/surfaces/pane/model.d.ts +41 -0
- package/dist/surfaces/pane/model.js +139 -0
- package/dist/surfaces/pane/outlet.d.ts +16 -0
- package/dist/surfaces/pane/outlet.js +20 -0
- package/dist/surfaces/pane/pane.d.ts +40 -0
- package/dist/surfaces/pane/pane.js +1429 -0
- package/dist/surfaces/pane/placement.d.ts +11 -0
- package/dist/surfaces/pane/placement.js +62 -0
- package/dist/surfaces/pane/text-field.d.ts +16 -0
- package/dist/surfaces/pane/text-field.js +20 -0
- package/dist/surfaces/selection-toolbar/index.d.ts +2 -0
- package/dist/surfaces/selection-toolbar/index.js +2 -0
- package/dist/surfaces/selection-toolbar/items.d.ts +37 -0
- package/dist/surfaces/selection-toolbar/items.js +183 -0
- package/dist/surfaces/selection-toolbar/toolbar.d.ts +27 -0
- package/dist/surfaces/selection-toolbar/toolbar.js +956 -0
- package/dist/surfaces/tools/index.d.ts +2 -0
- package/dist/surfaces/tools/index.js +2 -0
- package/dist/surfaces/tools/keys.d.ts +11 -0
- package/dist/surfaces/tools/keys.js +21 -0
- package/dist/surfaces/tools/model.d.ts +48 -0
- package/dist/surfaces/tools/model.js +155 -0
- package/dist/surfaces/tools/toolbar.d.ts +27 -0
- package/dist/surfaces/tools/toolbar.js +502 -0
- package/dist/theme.d.ts +26 -0
- package/dist/theme.js +47 -0
- package/package.json +31 -3
package/dist/recents.js
ADDED
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
import { normalizeHex, parseHex } from "./color/color.js";
|
|
2
|
+
export const DEFAULT_RECENTS = 16;
|
|
3
|
+
export const RECENTS_PREF = "picker.recents";
|
|
4
|
+
export function paintKey(p) {
|
|
5
|
+
if (typeof p === "string")
|
|
6
|
+
return normalizeHex(p) ?? p;
|
|
7
|
+
if (p.type === "radial")
|
|
8
|
+
p = { ...p, cx: p.cx ?? 0.5, cy: p.cy ?? 0.5, r: p.r ?? 0.5 };
|
|
9
|
+
const canonical = (v) => {
|
|
10
|
+
if (Array.isArray(v))
|
|
11
|
+
return v.map(canonical);
|
|
12
|
+
if (v && typeof v === "object")
|
|
13
|
+
return Object.fromEntries(Object.entries(v)
|
|
14
|
+
.filter(([, x]) => x !== undefined)
|
|
15
|
+
.sort(([a], [b]) => (a < b ? -1 : a > b ? 1 : 0))
|
|
16
|
+
.map(([k, x]) => [k, k === "color" && typeof x === "string" ? (normalizeHex(x) ?? x) : canonical(x)]));
|
|
17
|
+
return v;
|
|
18
|
+
};
|
|
19
|
+
return JSON.stringify(canonical(p));
|
|
20
|
+
}
|
|
21
|
+
const finite = Number.isFinite;
|
|
22
|
+
const isHex = (v) => typeof v === "string" && v[0] === "#" && !/\s/.test(v) && !!parseHex(v);
|
|
23
|
+
const isPoint = (v) => Array.isArray(v) && v.length === 2 && finite(v[0]) && finite(v[1]);
|
|
24
|
+
export function storedPaint(v) {
|
|
25
|
+
if (typeof v !== "object" || v === null)
|
|
26
|
+
return isHex(v) ? v : null;
|
|
27
|
+
const g = v;
|
|
28
|
+
const stops = [];
|
|
29
|
+
for (const s of Array.isArray(g.stops) ? g.stops : []) {
|
|
30
|
+
if (!finite(s?.at) || !isHex(s.color))
|
|
31
|
+
return null;
|
|
32
|
+
stops.push({ at: s.at, color: s.color });
|
|
33
|
+
}
|
|
34
|
+
if (!stops.length)
|
|
35
|
+
return null;
|
|
36
|
+
if (g.type === "linear")
|
|
37
|
+
return isPoint(g.start) && isPoint(g.end) ? { type: "linear", start: [...g.start], end: [...g.end], stops } : null;
|
|
38
|
+
if (g.type !== "radial")
|
|
39
|
+
return null;
|
|
40
|
+
const p = { type: "radial", stops };
|
|
41
|
+
for (const k of ["cx", "cy", "r"]) {
|
|
42
|
+
const x = g[k];
|
|
43
|
+
if (x == null)
|
|
44
|
+
continue;
|
|
45
|
+
if (!finite(x))
|
|
46
|
+
return null;
|
|
47
|
+
p[k] = x;
|
|
48
|
+
}
|
|
49
|
+
return (p.r ?? 0.5) > 0 ? p : null;
|
|
50
|
+
}
|
|
51
|
+
function storedList(text, max) {
|
|
52
|
+
let v;
|
|
53
|
+
try {
|
|
54
|
+
v = JSON.parse(text);
|
|
55
|
+
}
|
|
56
|
+
catch {
|
|
57
|
+
return [];
|
|
58
|
+
}
|
|
59
|
+
return Array.isArray(v) ? newest(v, max, storedPaint) : [];
|
|
60
|
+
}
|
|
61
|
+
function newest(list, max, clean = (v) => v) {
|
|
62
|
+
const seen = new Set();
|
|
63
|
+
const out = [];
|
|
64
|
+
max = Math.trunc(max);
|
|
65
|
+
for (const v of list) {
|
|
66
|
+
if (!(out.length < max))
|
|
67
|
+
break;
|
|
68
|
+
const p = clean(v);
|
|
69
|
+
if (p && seen.size < seen.add(paintKey(p)).size)
|
|
70
|
+
out.push(p);
|
|
71
|
+
}
|
|
72
|
+
return out;
|
|
73
|
+
}
|
|
74
|
+
function recentsStore(load, save, max, clean) {
|
|
75
|
+
const listeners = new Set();
|
|
76
|
+
return {
|
|
77
|
+
list: load,
|
|
78
|
+
add(paint) {
|
|
79
|
+
const p = clean(paint);
|
|
80
|
+
if (p)
|
|
81
|
+
save(p, newest([p, ...load()], max));
|
|
82
|
+
for (const l of listeners)
|
|
83
|
+
l();
|
|
84
|
+
},
|
|
85
|
+
subscribe(l) {
|
|
86
|
+
listeners.add(l);
|
|
87
|
+
return () => void listeners.delete(l);
|
|
88
|
+
},
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
export function memoryRecents(max = DEFAULT_RECENTS) {
|
|
92
|
+
let list = [];
|
|
93
|
+
return recentsStore(() => list, (_, next) => void (list = next), max, (p) => p);
|
|
94
|
+
}
|
|
95
|
+
export function prefsRecents(prefs, max = DEFAULT_RECENTS) {
|
|
96
|
+
let at;
|
|
97
|
+
let text;
|
|
98
|
+
let stored = [];
|
|
99
|
+
let pending = [];
|
|
100
|
+
let readable = true;
|
|
101
|
+
const load = () => {
|
|
102
|
+
const store = prefs();
|
|
103
|
+
if (store !== at) {
|
|
104
|
+
at = store;
|
|
105
|
+
text = undefined;
|
|
106
|
+
stored = [];
|
|
107
|
+
pending = [];
|
|
108
|
+
}
|
|
109
|
+
try {
|
|
110
|
+
const now = store.get(RECENTS_PREF);
|
|
111
|
+
if (now !== text)
|
|
112
|
+
stored = storedList(now, max);
|
|
113
|
+
text = now;
|
|
114
|
+
readable = true;
|
|
115
|
+
}
|
|
116
|
+
catch {
|
|
117
|
+
readable = false;
|
|
118
|
+
}
|
|
119
|
+
if (pending.length) {
|
|
120
|
+
const keys = stored.map(paintKey);
|
|
121
|
+
pending = pending.filter(([p, ahead]) => {
|
|
122
|
+
const i = keys.indexOf(paintKey(p));
|
|
123
|
+
return i < 0 || keys.slice(0, i).some((k) => ahead.includes(k));
|
|
124
|
+
});
|
|
125
|
+
}
|
|
126
|
+
return pending.length ? newest([...pending.map(([p]) => p), ...stored], max) : stored;
|
|
127
|
+
};
|
|
128
|
+
const save = (paint, next) => {
|
|
129
|
+
const key = paintKey(paint);
|
|
130
|
+
pending = [[paint, next.slice(1).map(paintKey)], ...pending.filter(([p]) => paintKey(p) !== key)].slice(0, max);
|
|
131
|
+
try {
|
|
132
|
+
if (readable)
|
|
133
|
+
prefs().set(RECENTS_PREF, JSON.stringify(next));
|
|
134
|
+
}
|
|
135
|
+
catch {
|
|
136
|
+
}
|
|
137
|
+
load();
|
|
138
|
+
};
|
|
139
|
+
return recentsStore(load, save, max, storedPaint);
|
|
140
|
+
}
|
package/dist/root.d.ts
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { CONTEXT, UiElement, type ContextOwner, type UiContext } from "./element.js";
|
|
2
|
+
import type { UiHost, UiPoint } from "./host.js";
|
|
3
|
+
import { type ResolvedOptions, type UiOptions } from "./options.js";
|
|
4
|
+
import type { UiScheduler } from "./scheduler.js";
|
|
5
|
+
import { type Dock, type UiSurface } from "./surface.js";
|
|
6
|
+
import { type ContextMenu } from "./surfaces/context-menu/index.js";
|
|
7
|
+
export declare const ROOT_TAG = "daas-ui";
|
|
8
|
+
export declare class UiRoot extends UiElement implements ContextOwner {
|
|
9
|
+
#private;
|
|
10
|
+
static styles: import("lit").CSSResult[];
|
|
11
|
+
[CONTEXT]: UiContext;
|
|
12
|
+
init(host: UiHost, options: UiOptions, scheduler: UiScheduler, mac: boolean): void;
|
|
13
|
+
disconnectedCallback(): void;
|
|
14
|
+
configure(partial: UiOptions): void;
|
|
15
|
+
openContextMenu(at: UiPoint | null): boolean;
|
|
16
|
+
handleKey(e: KeyboardEvent): boolean;
|
|
17
|
+
owns(target: EventTarget | null): boolean;
|
|
18
|
+
dispose(): void;
|
|
19
|
+
adopt(el: HTMLElement): void;
|
|
20
|
+
release(el: HTMLElement): void;
|
|
21
|
+
addSurface(el: UiSurface, dock: Dock): void;
|
|
22
|
+
relocate(el: UiSurface): void;
|
|
23
|
+
removeSurface(el: UiSurface): void;
|
|
24
|
+
get resolved(): ResolvedOptions;
|
|
25
|
+
get renders(): number;
|
|
26
|
+
protected willUpdate(): void;
|
|
27
|
+
protected updated(): void;
|
|
28
|
+
protected render(): (import("lit").TemplateResult<1> | ContextMenu | null)[];
|
|
29
|
+
protected createRenderRoot(): HTMLElement | DocumentFragment;
|
|
30
|
+
protected firstUpdated(): void;
|
|
31
|
+
}
|
package/dist/root.js
ADDED
|
@@ -0,0 +1,526 @@
|
|
|
1
|
+
import { css, html, nothing } from "lit";
|
|
2
|
+
import { TOOL_NAMES } from "@daasboard/editor";
|
|
3
|
+
import { focused, focusWithin, within } from "./controls/shared.js";
|
|
4
|
+
import { CONTEXT, UiElement } from "./element.js";
|
|
5
|
+
import { bindingAllowed, matchesChord, toolBindings } from "./keys.js";
|
|
6
|
+
import { memoryPrefs } from "./prefs.js";
|
|
7
|
+
import { prefsRecents } from "./recents.js";
|
|
8
|
+
import { mergeOptions, resolveOptions } from "./options.js";
|
|
9
|
+
import { ALL_KEYS } from "./surface.js";
|
|
10
|
+
import { CONTEXT_MENU_TAG } from "./surfaces/context-menu/index.js";
|
|
11
|
+
import { commandBindings, SELECTION_TOOLBAR_TAG } from "./surfaces/selection-toolbar/index.js";
|
|
12
|
+
import { TOOLS_TAG } from "./surfaces/tools/index.js";
|
|
13
|
+
import { handleZoomKey } from "./surfaces/tools/keys.js";
|
|
14
|
+
import { rootReset, TOKEN_NAMES, themeStyles, tokenVar } from "./theme.js";
|
|
15
|
+
export const ROOT_TAG = "daas-ui";
|
|
16
|
+
const DOCKS = ["top", "bottom", "left", "right", "float"];
|
|
17
|
+
const NO_VALUES = Object.freeze({});
|
|
18
|
+
function takesFocus(path, root) {
|
|
19
|
+
for (const t of path) {
|
|
20
|
+
if (t === root)
|
|
21
|
+
return false;
|
|
22
|
+
if (t instanceof HTMLElement && t.getAttribute("role") === "toolbar" && t.getAttribute("tabindex") === "-1")
|
|
23
|
+
continue;
|
|
24
|
+
if (t instanceof HTMLElement && (t.tabIndex >= 0 || t.hasAttribute("tabindex") || t.isContentEditable))
|
|
25
|
+
return true;
|
|
26
|
+
}
|
|
27
|
+
return false;
|
|
28
|
+
}
|
|
29
|
+
const rootStyles = css `
|
|
30
|
+
:host{position: absolute;inset: 0;display: block;pointer-events: none;font-family: var(--daas-ui-font);font-size: 13px;line-height: 1.4;color: var(--daas-ui-fg);}:host([hidden]){display: none;}.dock{position: absolute;display: flex;gap: 8px;pointer-events: none;}.dock > *{pointer-events: auto;}.dock:not(:has(> :not([hidden]))){display: none;}.top{top: 12px;left: 12px;right: 12px;flex-direction: column;align-items: center;}.bottom{bottom: 12px;left: 50%;transform: translateX(-50%);flex-direction: column-reverse;align-items: center;}.left{top: 0;bottom: 0;left: 0;align-items: center;}.right{top: 0;bottom: 0;right: 0;align-items: center;}.float{inset: 0;}`;
|
|
31
|
+
export class UiRoot extends UiElement {
|
|
32
|
+
static styles = [rootReset, rootStyles, themeStyles];
|
|
33
|
+
#scheduler;
|
|
34
|
+
#elements = new Set();
|
|
35
|
+
#drag = null;
|
|
36
|
+
#scopeIds = [];
|
|
37
|
+
#scopeSession = false;
|
|
38
|
+
#scope = 1;
|
|
39
|
+
#host;
|
|
40
|
+
#options = {};
|
|
41
|
+
#resolved;
|
|
42
|
+
#prefs = memoryPrefs();
|
|
43
|
+
#mac = false;
|
|
44
|
+
#bindings = [];
|
|
45
|
+
#surfaces = [];
|
|
46
|
+
#unsubs = [];
|
|
47
|
+
#gesture = false;
|
|
48
|
+
#revision = 0;
|
|
49
|
+
#valuesStale = true;
|
|
50
|
+
#values = NO_VALUES;
|
|
51
|
+
#targets = [];
|
|
52
|
+
#state = null;
|
|
53
|
+
#renders = 0;
|
|
54
|
+
#insets = null;
|
|
55
|
+
#resize = null;
|
|
56
|
+
#disposed = false;
|
|
57
|
+
#toolbar = null;
|
|
58
|
+
#toolbarAt = null;
|
|
59
|
+
#selection = null;
|
|
60
|
+
#selectionAt = null;
|
|
61
|
+
#contextMenu = null;
|
|
62
|
+
#roots = new Set();
|
|
63
|
+
#held = null;
|
|
64
|
+
init(host, options, scheduler, mac) {
|
|
65
|
+
this.#scheduler = scheduler;
|
|
66
|
+
scheduler.lead(this);
|
|
67
|
+
const root = this;
|
|
68
|
+
this[CONTEXT] = {
|
|
69
|
+
scheduler,
|
|
70
|
+
get options() {
|
|
71
|
+
return root.#resolved;
|
|
72
|
+
},
|
|
73
|
+
mac,
|
|
74
|
+
get container() {
|
|
75
|
+
return root.parentElement;
|
|
76
|
+
},
|
|
77
|
+
recents: prefsRecents(() => root.#resolved.prefs),
|
|
78
|
+
attach: (el) => void root.#elements.add(el),
|
|
79
|
+
detach: (el) => void root.#elements.delete(el),
|
|
80
|
+
beginDrag: (cancel) => {
|
|
81
|
+
root.#drag = cancel;
|
|
82
|
+
return () => {
|
|
83
|
+
if (root.#drag === cancel)
|
|
84
|
+
root.#drag = null;
|
|
85
|
+
};
|
|
86
|
+
},
|
|
87
|
+
get scope() {
|
|
88
|
+
return root.#scope;
|
|
89
|
+
},
|
|
90
|
+
returnFocus: () => root.#host.returnFocus(),
|
|
91
|
+
};
|
|
92
|
+
this.#host = host;
|
|
93
|
+
this.#mac = mac;
|
|
94
|
+
this.#options = options;
|
|
95
|
+
const menu = (this.#contextMenu = this.ownerDocument.createElement(CONTEXT_MENU_TAG));
|
|
96
|
+
menu.host = host;
|
|
97
|
+
menu.gesturing = () => this.#gesture;
|
|
98
|
+
this.#guard(this, true);
|
|
99
|
+
this.#applyOptions();
|
|
100
|
+
this.#rescope();
|
|
101
|
+
const stale = () => {
|
|
102
|
+
this.#valuesStale = true;
|
|
103
|
+
const change = this.#rescope();
|
|
104
|
+
if (change) {
|
|
105
|
+
this.#scope++;
|
|
106
|
+
const told = new Set(this.#elements);
|
|
107
|
+
for (const s of this.#surfaces)
|
|
108
|
+
told.add(s.el);
|
|
109
|
+
for (const el of told)
|
|
110
|
+
el.scopeChanged(change);
|
|
111
|
+
this.#cancelDrag();
|
|
112
|
+
}
|
|
113
|
+
this.requestUpdate();
|
|
114
|
+
};
|
|
115
|
+
const render = () => this.requestUpdate();
|
|
116
|
+
this.addEventListener("blur", () => {
|
|
117
|
+
this.#selection?.release(this);
|
|
118
|
+
if (!this.#held)
|
|
119
|
+
this.removeAttribute("tabindex");
|
|
120
|
+
});
|
|
121
|
+
this.#unsubs.push(host.controller.subscribe(stale), host.tools.onToolChange(render), host.camera.onChange(render), host.onChange(() => {
|
|
122
|
+
this.#revision++;
|
|
123
|
+
stale();
|
|
124
|
+
if (host.readOnly())
|
|
125
|
+
this.#cancelDrag();
|
|
126
|
+
}), host.onGesture((active) => {
|
|
127
|
+
if (active)
|
|
128
|
+
this.#contextMenu?.hide();
|
|
129
|
+
if (active === this.#gesture)
|
|
130
|
+
return;
|
|
131
|
+
this.#gesture = active;
|
|
132
|
+
this.requestUpdate();
|
|
133
|
+
}));
|
|
134
|
+
}
|
|
135
|
+
#onRootKey = (e) => {
|
|
136
|
+
if (this.#host.allowsKey?.(e) === false)
|
|
137
|
+
return;
|
|
138
|
+
if (e.key !== "Escape" || e.isComposing || e.keyCode === 229 || !this.#cancelDrag())
|
|
139
|
+
return;
|
|
140
|
+
e.preventDefault();
|
|
141
|
+
e.stopPropagation();
|
|
142
|
+
};
|
|
143
|
+
#onClipboard = (e) => {
|
|
144
|
+
if (e.defaultPrevented)
|
|
145
|
+
return;
|
|
146
|
+
if (e.composedPath().some((node) => node instanceof HTMLElement && (node.isContentEditable || /^(INPUT|TEXTAREA)$/.test(node.tagName))))
|
|
147
|
+
return;
|
|
148
|
+
if (this.#host.handleClipboard?.(e))
|
|
149
|
+
e.stopPropagation();
|
|
150
|
+
};
|
|
151
|
+
#onRootPress = (e) => {
|
|
152
|
+
if (e.defaultPrevented || !this.#host.controller.textTarget() || takesFocus(e.composedPath(), e.currentTarget))
|
|
153
|
+
return;
|
|
154
|
+
e.preventDefault();
|
|
155
|
+
};
|
|
156
|
+
#guard(el, on) {
|
|
157
|
+
const method = on ? "addEventListener" : "removeEventListener";
|
|
158
|
+
el[method]("keydown", this.#onRootKey, true);
|
|
159
|
+
el[method]("mousedown", this.#onRootPress);
|
|
160
|
+
for (const type of ["copy", "cut", "paste"])
|
|
161
|
+
el[method](type, this.#onClipboard);
|
|
162
|
+
this.#roots[on ? "add" : "delete"](el);
|
|
163
|
+
}
|
|
164
|
+
disconnectedCallback() {
|
|
165
|
+
super.disconnectedCallback();
|
|
166
|
+
this.#resize?.disconnect();
|
|
167
|
+
this.#resize = null;
|
|
168
|
+
}
|
|
169
|
+
#rescope() {
|
|
170
|
+
const c = this.#host.controller;
|
|
171
|
+
const session = c.textTarget() !== null;
|
|
172
|
+
const targets = c.targets();
|
|
173
|
+
const ids = this.#scopeIds;
|
|
174
|
+
let same = targets.length === ids.length;
|
|
175
|
+
for (let i = 0; same && i < ids.length; i++)
|
|
176
|
+
same = targets[i].id === ids[i];
|
|
177
|
+
if (!same && targets.length === ids.length) {
|
|
178
|
+
const kept = new Set(ids);
|
|
179
|
+
same = targets.every((o) => kept.has(o.id));
|
|
180
|
+
if (same)
|
|
181
|
+
this.#scopeIds = targets.map((o) => o.id);
|
|
182
|
+
}
|
|
183
|
+
const change = { targets: !same, session: session !== this.#scopeSession };
|
|
184
|
+
if (!change.targets && !change.session)
|
|
185
|
+
return null;
|
|
186
|
+
this.#scopeSession = session;
|
|
187
|
+
if (change.targets)
|
|
188
|
+
this.#scopeIds = targets.map((o) => o.id);
|
|
189
|
+
return change;
|
|
190
|
+
}
|
|
191
|
+
#cancelDrag() {
|
|
192
|
+
const cancel = this.#drag;
|
|
193
|
+
if (!cancel)
|
|
194
|
+
return false;
|
|
195
|
+
this.#drag = null;
|
|
196
|
+
cancel();
|
|
197
|
+
return true;
|
|
198
|
+
}
|
|
199
|
+
configure(partial) {
|
|
200
|
+
if (this.#disposed)
|
|
201
|
+
return;
|
|
202
|
+
const prefs = this.#resolved.prefs;
|
|
203
|
+
this.#options = mergeOptions(this.#options, partial);
|
|
204
|
+
if ((this.#options.prefs ?? this.#prefs) !== prefs)
|
|
205
|
+
for (const el of this.#elements)
|
|
206
|
+
el.settleRecent?.();
|
|
207
|
+
this.#applyOptions();
|
|
208
|
+
if (!this.#resolved.contextMenu)
|
|
209
|
+
this.#contextMenu?.hide();
|
|
210
|
+
this.#valuesStale = true;
|
|
211
|
+
for (const el of this.#elements)
|
|
212
|
+
el.requestUpdate();
|
|
213
|
+
this.requestUpdate();
|
|
214
|
+
}
|
|
215
|
+
openContextMenu(at) {
|
|
216
|
+
if (this.#disposed || !this.#resolved.contextMenu)
|
|
217
|
+
return false;
|
|
218
|
+
return this.#contextMenu?.show(at) ?? false;
|
|
219
|
+
}
|
|
220
|
+
handleKey(e) {
|
|
221
|
+
if (this.#host.allowsKey?.(e) === false)
|
|
222
|
+
return false;
|
|
223
|
+
if (this.#disposed || e.defaultPrevented || e.isComposing || e.keyCode === 229 || e.key === "Process")
|
|
224
|
+
return false;
|
|
225
|
+
if (e.key === "Escape" && this.#cancelDrag()) {
|
|
226
|
+
e.preventDefault();
|
|
227
|
+
return true;
|
|
228
|
+
}
|
|
229
|
+
const menuKey = e.key === "ContextMenu" ? !e.shiftKey : e.key === "F10" && e.shiftKey;
|
|
230
|
+
if (menuKey && !e.ctrlKey && !e.metaKey && !e.altKey && this.openContextMenu(null)) {
|
|
231
|
+
e.preventDefault();
|
|
232
|
+
return true;
|
|
233
|
+
}
|
|
234
|
+
const readOnly = this.#host.readOnly();
|
|
235
|
+
for (const b of this.#bindings) {
|
|
236
|
+
if (!matchesChord(b.chord, e) || !bindingAllowed(b, readOnly))
|
|
237
|
+
continue;
|
|
238
|
+
if ("tool" in b) {
|
|
239
|
+
this.#host.tools.setTool(b.tool);
|
|
240
|
+
this.#toolbar?.recordToolUse(b.tool);
|
|
241
|
+
}
|
|
242
|
+
else if ("custom" in b) {
|
|
243
|
+
b.custom.activate(this.#host);
|
|
244
|
+
this.#toolbar?.recordToolUse(b.custom);
|
|
245
|
+
}
|
|
246
|
+
else if (!this.#selection?.runCommand(b.command))
|
|
247
|
+
continue;
|
|
248
|
+
e.preventDefault();
|
|
249
|
+
return true;
|
|
250
|
+
}
|
|
251
|
+
return handleZoomKey(e, this.#resolved, this.#host, this.#mac);
|
|
252
|
+
}
|
|
253
|
+
owns(target) {
|
|
254
|
+
if (this.#disposed || !target || typeof target.nodeType !== "number")
|
|
255
|
+
return false;
|
|
256
|
+
const ctx = this[CONTEXT];
|
|
257
|
+
for (let n = target; n; n = n.parentNode ?? n.host ?? null)
|
|
258
|
+
if (n === this || n[CONTEXT] === ctx)
|
|
259
|
+
return true;
|
|
260
|
+
return false;
|
|
261
|
+
}
|
|
262
|
+
dispose() {
|
|
263
|
+
if (this.#disposed)
|
|
264
|
+
return;
|
|
265
|
+
this.#disposed = true;
|
|
266
|
+
for (const u of this.#unsubs.splice(0))
|
|
267
|
+
u();
|
|
268
|
+
for (const el of [...this.#roots])
|
|
269
|
+
this.#guard(el, false);
|
|
270
|
+
this.#held = null;
|
|
271
|
+
this.#contextMenu?.hide();
|
|
272
|
+
this.#toolbar?.remove();
|
|
273
|
+
this.#selection?.remove();
|
|
274
|
+
this.#toolbar = this.#selection = null;
|
|
275
|
+
this.#scheduler.dispose();
|
|
276
|
+
this.remove();
|
|
277
|
+
const last = this.#insets;
|
|
278
|
+
if (last && (last.top || last.right || last.bottom || last.left))
|
|
279
|
+
this.#host.setInsets({ top: 0, right: 0, bottom: 0, left: 0 });
|
|
280
|
+
}
|
|
281
|
+
adopt(el) {
|
|
282
|
+
el[CONTEXT] = this[CONTEXT];
|
|
283
|
+
this.#guard(el, true);
|
|
284
|
+
this.#theme(el);
|
|
285
|
+
}
|
|
286
|
+
release(el) {
|
|
287
|
+
this.#guard(el, false);
|
|
288
|
+
delete el[CONTEXT];
|
|
289
|
+
}
|
|
290
|
+
addSurface(el, dock) {
|
|
291
|
+
const at = this.#surfaces.findIndex((s) => s.el === el);
|
|
292
|
+
if (at >= 0) {
|
|
293
|
+
if (this.#surfaces[at].dock === dock)
|
|
294
|
+
return;
|
|
295
|
+
this.#holdFocus(el, true);
|
|
296
|
+
this.#surfaces.splice(at, 1);
|
|
297
|
+
}
|
|
298
|
+
this.#surfaces.push({ el, dock });
|
|
299
|
+
this.#valuesStale = true;
|
|
300
|
+
this.requestUpdate();
|
|
301
|
+
}
|
|
302
|
+
relocate(el) {
|
|
303
|
+
if (!this.#surfaces.some((s) => s.el === el))
|
|
304
|
+
return;
|
|
305
|
+
this.#holdFocus(el, true);
|
|
306
|
+
this.requestUpdate();
|
|
307
|
+
}
|
|
308
|
+
removeSurface(el) {
|
|
309
|
+
const i = this.#surfaces.findIndex((s) => s.el === el);
|
|
310
|
+
if (i < 0)
|
|
311
|
+
return;
|
|
312
|
+
this.#letGo(el);
|
|
313
|
+
this.#surfaces.splice(i, 1);
|
|
314
|
+
this.requestUpdate();
|
|
315
|
+
}
|
|
316
|
+
#letGo(el) {
|
|
317
|
+
if (!focusWithin(el))
|
|
318
|
+
return;
|
|
319
|
+
this.#host.returnFocus();
|
|
320
|
+
this.#holdFocus(el, false);
|
|
321
|
+
}
|
|
322
|
+
#holdFocus(el, restore) {
|
|
323
|
+
const active = focused(this.ownerDocument);
|
|
324
|
+
if (!active || !within(el, active))
|
|
325
|
+
return;
|
|
326
|
+
if (restore)
|
|
327
|
+
this.#held = { el, control: active };
|
|
328
|
+
this.tabIndex = -1;
|
|
329
|
+
this.focus({ preventScroll: true });
|
|
330
|
+
}
|
|
331
|
+
get resolved() {
|
|
332
|
+
return this.#resolved;
|
|
333
|
+
}
|
|
334
|
+
get renders() {
|
|
335
|
+
return this.#renders;
|
|
336
|
+
}
|
|
337
|
+
#applyOptions() {
|
|
338
|
+
const r = (this.#resolved = resolveOptions(this.#options, this.#prefs));
|
|
339
|
+
for (const el of this.#roots)
|
|
340
|
+
this.#theme(el);
|
|
341
|
+
this.#bindings = [...toolBindings(r.toolbar, r.shortcuts, TOOL_NAMES, this.#mac), ...commandBindings(r.selectionToolbar, r.shortcuts, this.#mac)];
|
|
342
|
+
}
|
|
343
|
+
#theme(el) {
|
|
344
|
+
const r = this.#resolved;
|
|
345
|
+
el.dataset.theme = r.theme;
|
|
346
|
+
el.dataset.density = r.density;
|
|
347
|
+
for (const name of TOKEN_NAMES) {
|
|
348
|
+
const value = r.tokens?.[name];
|
|
349
|
+
if (value === undefined)
|
|
350
|
+
el.style.removeProperty(tokenVar(name));
|
|
351
|
+
else
|
|
352
|
+
el.style.setProperty(tokenVar(name), value);
|
|
353
|
+
}
|
|
354
|
+
}
|
|
355
|
+
#adopt(tag) {
|
|
356
|
+
const el = this.ownerDocument.createElement(tag);
|
|
357
|
+
this.adopt(el);
|
|
358
|
+
return el;
|
|
359
|
+
}
|
|
360
|
+
#move(el, at) {
|
|
361
|
+
if (!at) {
|
|
362
|
+
if (el === this.#selection && focused(this.ownerDocument) === this && this.#selection.release(this))
|
|
363
|
+
this.#host.returnFocus();
|
|
364
|
+
else
|
|
365
|
+
this.#letGo(el);
|
|
366
|
+
}
|
|
367
|
+
else if (el === this.#selection) {
|
|
368
|
+
if (focusWithin(el)) {
|
|
369
|
+
this.tabIndex = -1;
|
|
370
|
+
this.focus({ preventScroll: true });
|
|
371
|
+
}
|
|
372
|
+
}
|
|
373
|
+
else
|
|
374
|
+
this.#holdFocus(el, true);
|
|
375
|
+
this.#surfaces = this.#surfaces.filter((s) => s.el !== el);
|
|
376
|
+
el.remove();
|
|
377
|
+
if (!at)
|
|
378
|
+
this.release(el);
|
|
379
|
+
else if (typeof at === "string")
|
|
380
|
+
this.#surfaces.push({ el, dock: at });
|
|
381
|
+
else {
|
|
382
|
+
at.append(el);
|
|
383
|
+
this.#surfaces.push({ el, dock: "container" });
|
|
384
|
+
}
|
|
385
|
+
this.#valuesStale = true;
|
|
386
|
+
}
|
|
387
|
+
#placeToolbar() {
|
|
388
|
+
const options = this.#resolved.toolbar;
|
|
389
|
+
const placement = options && (options.placement ?? "bottom");
|
|
390
|
+
const at = !placement ? null : typeof placement === "string" ? placement : placement.container;
|
|
391
|
+
if (at === this.#toolbarAt)
|
|
392
|
+
return;
|
|
393
|
+
let el = this.#toolbar;
|
|
394
|
+
if (!el) {
|
|
395
|
+
el = this.#toolbar = this.#adopt(TOOLS_TAG);
|
|
396
|
+
el.keyHandler = (e) => this.handleKey(e);
|
|
397
|
+
}
|
|
398
|
+
this.#move(el, at);
|
|
399
|
+
this.#toolbarAt = at;
|
|
400
|
+
if (!at)
|
|
401
|
+
this.#toolbar = null;
|
|
402
|
+
}
|
|
403
|
+
#placeSelectionToolbar() {
|
|
404
|
+
const o = this.#resolved.selectionToolbar;
|
|
405
|
+
const p = o && (o.placement ?? "top");
|
|
406
|
+
const at = !p ? null : p === "floating" ? "float" : typeof p === "object" && p.container instanceof HTMLElement ? p.container : "top";
|
|
407
|
+
if (at === this.#selectionAt)
|
|
408
|
+
return;
|
|
409
|
+
let el = this.#selection;
|
|
410
|
+
if (!el)
|
|
411
|
+
el = this.#selection = this.#adopt(SELECTION_TOOLBAR_TAG);
|
|
412
|
+
else if (at)
|
|
413
|
+
el.startMove(this);
|
|
414
|
+
this.#move(el, at);
|
|
415
|
+
this.#selectionAt = at;
|
|
416
|
+
if (!at)
|
|
417
|
+
this.#selection = null;
|
|
418
|
+
}
|
|
419
|
+
#read() {
|
|
420
|
+
if (!this.#valuesStale)
|
|
421
|
+
return;
|
|
422
|
+
this.#valuesStale = false;
|
|
423
|
+
const ctrl = this.#host.controller;
|
|
424
|
+
const lists = this.#surfaces.map((s) => s.el.keys());
|
|
425
|
+
if (lists.includes(ALL_KEYS))
|
|
426
|
+
this.#values = this.#scheduler.read(() => ctrl.read());
|
|
427
|
+
else {
|
|
428
|
+
const keys = lists.length === 1 ? lists[0] : [...new Set(lists.flat())];
|
|
429
|
+
this.#values = keys.length > 0 ? this.#scheduler.read(() => ctrl.read(keys)) : NO_VALUES;
|
|
430
|
+
}
|
|
431
|
+
this.#targets = ctrl.targets();
|
|
432
|
+
}
|
|
433
|
+
willUpdate() {
|
|
434
|
+
this.#placeToolbar();
|
|
435
|
+
this.#placeSelectionToolbar();
|
|
436
|
+
this.#read();
|
|
437
|
+
const host = this.#host;
|
|
438
|
+
this.#state = {
|
|
439
|
+
host,
|
|
440
|
+
options: this.#resolved,
|
|
441
|
+
values: this.#values,
|
|
442
|
+
targets: this.#targets,
|
|
443
|
+
tool: host.tools.active,
|
|
444
|
+
readOnly: host.readOnly(),
|
|
445
|
+
textSession: host.controller.textTarget() !== null,
|
|
446
|
+
gesture: this.#gesture,
|
|
447
|
+
revision: this.#revision,
|
|
448
|
+
zoom: host.camera.zoom(),
|
|
449
|
+
};
|
|
450
|
+
for (const s of this.#surfaces)
|
|
451
|
+
s.el.state = this.#state;
|
|
452
|
+
}
|
|
453
|
+
updated() {
|
|
454
|
+
const held = this.#held;
|
|
455
|
+
if (held) {
|
|
456
|
+
this.#held = null;
|
|
457
|
+
const back = () => {
|
|
458
|
+
if (focused(this.ownerDocument) !== this) {
|
|
459
|
+
if (!within(this, focused(this.ownerDocument)))
|
|
460
|
+
this.removeAttribute("tabindex");
|
|
461
|
+
return;
|
|
462
|
+
}
|
|
463
|
+
if (held.control.isConnected && within(held.el, held.control))
|
|
464
|
+
held.control.focus({ preventScroll: true });
|
|
465
|
+
if (focused(this.ownerDocument) !== this)
|
|
466
|
+
return;
|
|
467
|
+
const stop = held.el === this.#toolbar ? held.el.shadowRoot?.querySelector('button[tabindex="0"]') : null;
|
|
468
|
+
if (stop)
|
|
469
|
+
stop.focus({ preventScroll: true });
|
|
470
|
+
else
|
|
471
|
+
this.#host.returnFocus();
|
|
472
|
+
};
|
|
473
|
+
if (held.control.isConnected)
|
|
474
|
+
back();
|
|
475
|
+
else
|
|
476
|
+
queueMicrotask(back);
|
|
477
|
+
}
|
|
478
|
+
const sel = this.#selection;
|
|
479
|
+
if (sel?.moving)
|
|
480
|
+
queueMicrotask(() => sel.moved());
|
|
481
|
+
}
|
|
482
|
+
render() {
|
|
483
|
+
this.#renders++;
|
|
484
|
+
return [
|
|
485
|
+
...DOCKS.map((dock) => {
|
|
486
|
+
const els = this.#surfaces.filter((s) => s.dock === dock).map((s) => s.el);
|
|
487
|
+
return html `<div class="dock ${dock}" part="dock dock-${dock}">${els.length > 0 ? els : nothing}</div>`;
|
|
488
|
+
}),
|
|
489
|
+
this.#contextMenu,
|
|
490
|
+
];
|
|
491
|
+
}
|
|
492
|
+
createRenderRoot() {
|
|
493
|
+
const root = super.createRenderRoot();
|
|
494
|
+
root.addEventListener("focusin", () => this.#selection?.release(this));
|
|
495
|
+
return root;
|
|
496
|
+
}
|
|
497
|
+
firstUpdated() {
|
|
498
|
+
const RO = this.ownerDocument.defaultView?.ResizeObserver;
|
|
499
|
+
if (!RO || this.#resize || this.#disposed || !this.isConnected)
|
|
500
|
+
return;
|
|
501
|
+
this.#resize = new RO(() => this.#measureInsets());
|
|
502
|
+
this.#resize.observe(this);
|
|
503
|
+
for (const dock of this.renderRoot.querySelectorAll(".dock"))
|
|
504
|
+
this.#resize.observe(dock);
|
|
505
|
+
}
|
|
506
|
+
#measureInsets() {
|
|
507
|
+
if (this.#disposed || !this.isConnected)
|
|
508
|
+
return;
|
|
509
|
+
const covered = (dock) => this.renderRoot.querySelector(`.dock.${dock} > :not([hidden])`)?.parentElement ?? null;
|
|
510
|
+
const top = covered("top");
|
|
511
|
+
const bottom = covered("bottom");
|
|
512
|
+
const left = covered("left");
|
|
513
|
+
const right = covered("right");
|
|
514
|
+
const insets = {
|
|
515
|
+
top: top ? Math.max(0, top.offsetTop + top.offsetHeight) : 0,
|
|
516
|
+
right: right ? Math.max(0, this.offsetWidth - right.offsetLeft) : 0,
|
|
517
|
+
bottom: bottom ? Math.max(0, this.offsetHeight - bottom.offsetTop) : 0,
|
|
518
|
+
left: left ? Math.max(0, left.offsetLeft + left.offsetWidth) : 0,
|
|
519
|
+
};
|
|
520
|
+
const last = this.#insets;
|
|
521
|
+
if (last && last.top === insets.top && last.right === insets.right && last.bottom === insets.bottom && last.left === insets.left)
|
|
522
|
+
return;
|
|
523
|
+
this.#insets = insets;
|
|
524
|
+
this.#host.setInsets(insets);
|
|
525
|
+
}
|
|
526
|
+
}
|