@margoengine/ui 0.0.0-stage → 1.0.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.
Files changed (61) hide show
  1. package/LICENSE +201 -0
  2. package/NOTICE +4 -0
  3. package/README.md +1224 -2
  4. package/dist/basic.d.ts +87 -0
  5. package/dist/collections.d.ts +180 -0
  6. package/dist/color.d.ts +24 -0
  7. package/dist/controls.d.ts +130 -0
  8. package/dist/curved.d.ts +41 -0
  9. package/dist/curved.js +105 -0
  10. package/dist/dom-CWJhpotq.js +340 -0
  11. package/dist/dom-host.d.ts +67 -0
  12. package/dist/dom-host.js +563 -0
  13. package/dist/dom-spec.d.ts +21 -0
  14. package/dist/dom.d.ts +63 -0
  15. package/dist/draw.d.ts +107 -0
  16. package/dist/editor.d.ts +83 -0
  17. package/dist/editor.js +323 -0
  18. package/dist/errors-CDgOgzCd.js +7 -0
  19. package/dist/focus.d.ts +12 -0
  20. package/dist/forms.d.ts +114 -0
  21. package/dist/forms.js +354 -0
  22. package/dist/gl-bb768Mje.js +165 -0
  23. package/dist/gl-load-wwKmojhb.js +5 -0
  24. package/dist/gl-load.d.ts +3 -0
  25. package/dist/gl.d.ts +30 -0
  26. package/dist/i18n.d.ts +69 -0
  27. package/dist/i18n.js +117 -0
  28. package/dist/image.d.ts +74 -0
  29. package/dist/image.js +295 -0
  30. package/dist/index.d.ts +36 -0
  31. package/dist/index.js +4 -0
  32. package/dist/input-dom.d.ts +14 -0
  33. package/dist/input.d.ts +92 -0
  34. package/dist/input.js +315 -0
  35. package/dist/interaction.d.ts +106 -0
  36. package/dist/keyboard.d.ts +40 -0
  37. package/dist/keyboard.js +132 -0
  38. package/dist/layout.d.ts +116 -0
  39. package/dist/markup.d.ts +89 -0
  40. package/dist/markup.js +369 -0
  41. package/dist/meters-yaZRtwsX.js +1401 -0
  42. package/dist/meters.d.ts +72 -0
  43. package/dist/mirror.d.ts +30 -0
  44. package/dist/overlays.d.ts +89 -0
  45. package/dist/painter.d.ts +115 -0
  46. package/dist/pass-CAn29pqc.js +15 -0
  47. package/dist/react.d.ts +99 -0
  48. package/dist/react.js +246 -0
  49. package/dist/renderer-Vs-YtiWv.js +1741 -0
  50. package/dist/renderer.d.ts +138 -0
  51. package/dist/rsc/react.js +3 -0
  52. package/dist/shader.d.ts +6 -0
  53. package/dist/theme.d.ts +104 -0
  54. package/dist/ui.d.ts +328 -0
  55. package/dist/virtual.d.ts +81 -0
  56. package/dist/widget.d.ts +196 -0
  57. package/dist/world-gl.d.ts +32 -0
  58. package/dist/world-gl.js +195 -0
  59. package/dist/world.d.ts +216 -0
  60. package/dist/world.js +561 -0
  61. package/package.json +117 -4
package/dist/draw.d.ts ADDED
@@ -0,0 +1,107 @@
1
+ import type { Font, TextLayout } from '@margoengine/text';
2
+ import type { Rgba } from './color.js';
3
+ /** A filled and outlined rounded rectangle, in device pixels. */
4
+ export interface RectCommand {
5
+ type: 'rect';
6
+ x: number;
7
+ y: number;
8
+ width: number;
9
+ height: number;
10
+ /** Corner radius in pixels. */
11
+ radius: number;
12
+ fill: Rgba;
13
+ border: Rgba;
14
+ /** Outline width in pixels, drawn inside the rectangle. 0 for none. */
15
+ borderWidth: number;
16
+ /** Edge softness in pixels; above 0 makes a soft shadow instead of a crisp shape. */
17
+ blur: number;
18
+ }
19
+ /** A laid-out block of text. `layout` is in UI units, y up from the block's top-left; `scale` turns it into pixels. */
20
+ export interface TextCommand {
21
+ type: 'text';
22
+ /** Top-left of the block in pixels. */
23
+ x: number;
24
+ y: number;
25
+ layout: TextLayout;
26
+ scale: number;
27
+ font: Font;
28
+ color: Rgba;
29
+ /** Outline width in pixels around each glyph; 0 for none. */
30
+ outlineWidth: number;
31
+ outlineColor: Rgba;
32
+ }
33
+ /** A premultiplied RGBA tile from the optional image widget, in device pixels. */
34
+ export interface ImageCommand {
35
+ type: 'image';
36
+ bitmap: import('@margoengine/text').GlyphBitmap;
37
+ x: number;
38
+ y: number;
39
+ width: number;
40
+ height: number;
41
+ /** Source texel rectangle within the bitmap, including its filter gutter. */
42
+ source: {
43
+ x: number;
44
+ y: number;
45
+ width: number;
46
+ height: number;
47
+ };
48
+ color: Rgba;
49
+ }
50
+ /** Start clipping to a rectangle in pixels, inside any clip already active. Matched by an {@link UnclipCommand}. */
51
+ export interface ClipCommand {
52
+ type: 'clip';
53
+ x: number;
54
+ y: number;
55
+ width: number;
56
+ height: number;
57
+ }
58
+ /** End the innermost {@link ClipCommand}. */
59
+ export interface UnclipCommand {
60
+ type: 'unclip';
61
+ }
62
+ /** One step of a {@link DrawList}. */
63
+ export type DrawCommand = RectCommand | TextCommand | ClipCommand | UnclipCommand | ImageCommand;
64
+ /** Options for {@link DrawList.rect}; colours default to transparent. */
65
+ export interface RectStyle {
66
+ fill?: Rgba;
67
+ border?: Rgba;
68
+ /** UI units. */
69
+ borderWidth?: number;
70
+ /** UI units. */
71
+ radius?: number;
72
+ /** UI units; makes the rectangle a soft shadow. */
73
+ blur?: number;
74
+ }
75
+ /**
76
+ * What a {@link Ui} draws, as plain data in back-to-front order: rounded rectangles, text blocks and clip regions in
77
+ * device pixels. A renderer ({@link UiRenderer}) turns it into GPU draws; tests can read `commands` directly. The
78
+ * methods take UI units and apply the list's `scale` and pixel snapping.
79
+ */
80
+ export declare class DrawList {
81
+ /** The commands in draw order. Reused: do not keep it across a `clear()`. */
82
+ readonly commands: DrawCommand[];
83
+ /** Device pixels per UI unit. */
84
+ scale: number;
85
+ /** Multiplies the alpha of everything added; set by the UI while it draws a fading widget. */
86
+ alpha: number;
87
+ /** Changes whenever the list is rebuilt, so a renderer can skip uploading an unchanged one. */
88
+ version: number;
89
+ private clips;
90
+ /** Empty the list and start a new version at `scale` pixels per UI unit. */
91
+ clear(scale: number): void;
92
+ private fade;
93
+ /** A rounded rectangle with its edges snapped to whole pixels. */
94
+ rect(x: number, y: number, width: number, height: number, style: RectStyle): void;
95
+ /** A text block whose top-left is at (x, y) UI units. Empty layouts add nothing. */
96
+ text(x: number, y: number, layout: TextLayout, font: Font, color: Rgba, outline?: {
97
+ width: number;
98
+ color: Rgba;
99
+ }): void;
100
+ /** Clip what follows to a rectangle in UI units until the matching {@link DrawList.popClip}. */
101
+ pushClip(x: number, y: number, width: number, height: number): void;
102
+ /**
103
+ * End the innermost clip.
104
+ * @throws RangeError when no clip is open: a stray popClip would end a clip its caller did not make.
105
+ */
106
+ popClip(): void;
107
+ }
@@ -0,0 +1,83 @@
1
+ /// <reference lib="esnext.disposable" preserve="true" />
2
+ import { type Ui } from '@margoengine/ui';
3
+ import { type MarkupElement, type ScreenOptions, type Screen } from '@margoengine/ui/markup';
4
+ /** Editable, portable layout source. Text remains markup and style sheets, never executable JavaScript. */
5
+ export interface LayoutSource {
6
+ markup: string;
7
+ styles: string;
8
+ }
9
+ /** Layout source with its version for disk persistence. */
10
+ export interface LayoutProject extends LayoutSource {
11
+ version: 1;
12
+ }
13
+ /** Validated source document with visual edits, bounded undo/redo and portable JSON. No DOM or GPU is needed. */
14
+ export declare class LayoutDocument {
15
+ private readonly options;
16
+ private states;
17
+ private cursor;
18
+ private readonly listeners;
19
+ /** Maximum retained edits, default 100. */
20
+ readonly historyLimit: number;
21
+ constructor(source: LayoutSource, options?: ScreenOptions & {
22
+ historyLimit?: number;
23
+ });
24
+ /** Defensive snapshot of the current valid source. */
25
+ get source(): LayoutSource;
26
+ get canUndo(): boolean;
27
+ get canRedo(): boolean;
28
+ private validate;
29
+ /** Validate both sources before committing. Invalid edits retain the previous valid document and preview. */
30
+ apply(patch: Partial<LayoutSource>): void;
31
+ /** Build fresh widgets from the current document. Caller owns these widgets. */
32
+ build(): Screen;
33
+ /** The current markup tree, detached from document state. */
34
+ get tree(): MarkupElement[];
35
+ /** Edit an element attribute, or remove it with undefined. */
36
+ setAttribute(id: string, name: string, value: string | undefined): void;
37
+ /** Replace the text of an element. The markup builder validates whether the tag accepts it. */
38
+ setText(id: string, text: string): void;
39
+ /** Insert a widget into a container, or the top level; returns its generated stable id. */
40
+ insert(tag: string, parentId?: string): string;
41
+ /** Delete a widget and its subtree, undoably. */
42
+ remove(id: string): void;
43
+ /** Reparent/reorder an element. Descendant cycles and invalid container tags reject atomically. */
44
+ move(id: string, parentId: string | undefined, index?: number): void;
45
+ undo(): boolean;
46
+ redo(): boolean;
47
+ /** Save exact validated sources, without handler functions or runtime state. */
48
+ save(): string;
49
+ /** Load a project string as one undoable edit; unsupported versions reject without changing the document. */
50
+ load(json: string): void;
51
+ /** Subscribe to committed edits, undo and redo. */
52
+ onChange(listener: () => void): () => void;
53
+ private changed;
54
+ dispose(): void;
55
+ [Symbol.dispose]: () => void;
56
+ }
57
+ /** Options for the browser layout workspace. Rendering stays with the application's existing UiRenderer. */
58
+ export interface LayoutEditorOptions extends LayoutSource {
59
+ ui: Ui;
60
+ /** Engine canvas. The editor moves it into the preview until disposed, then restores its original place. */
61
+ canvas: HTMLCanvasElement;
62
+ /** Explicit action handlers allowed in markup, default none. Never loaded from project JSON. */
63
+ actions?: ScreenOptions['actions'];
64
+ signal?: AbortSignal;
65
+ }
66
+ /** Browser source/visual editor with selection, properties, hierarchy, preview, undo/redo and save/open. */
67
+ export declare class LayoutEditor {
68
+ private readonly options;
69
+ readonly document: LayoutDocument;
70
+ readonly element: HTMLElement;
71
+ private readonly preview;
72
+ private readonly abort;
73
+ private readonly cleanup;
74
+ private selected;
75
+ constructor(host: HTMLElement, options: LayoutEditorOptions);
76
+ /** Load a saved project from disk, respecting editor lifetime and caller cancellation. */
77
+ open(file: Blob, options?: {
78
+ signal?: AbortSignal;
79
+ }): Promise<void>;
80
+ /** Detach listeners and preview widgets. The application retains its Ui, canvas, renderer and engine. */
81
+ dispose(): void;
82
+ [Symbol.dispose]: () => void;
83
+ }
package/dist/editor.js ADDED
@@ -0,0 +1,323 @@
1
+ import { n as e, r as t } from "./errors-CDgOgzCd.js";
2
+ import { Panel as n } from "@margoengine/ui";
3
+ import { buildScreen as r, parseMarkup as i } from "@margoengine/ui/markup";
4
+ //#region src/editor.ts
5
+ var a = (e) => e.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;").replace(/"/g, "&quot;");
6
+ function o(e, t = 0) {
7
+ return e.map((e) => {
8
+ let n = `${" ".repeat(t)}<${e.tag}${Object.entries(e.attributes).map(([e, t]) => ` ${e}="${a(t)}"`).join("")}>`;
9
+ return e.children.length ? `${n}\n${o(e.children, t + 1)}\n${" ".repeat(t)}</${e.tag}>` : `${n}${a(e.text)}</${e.tag}>`;
10
+ }).join("\n");
11
+ }
12
+ function s(e, t) {
13
+ for (let n = 0; n < e.length; n++) {
14
+ let r = e[n];
15
+ if (r.attributes.id === t) return {
16
+ element: r,
17
+ siblings: e,
18
+ index: n
19
+ };
20
+ let i = s(r.children, t);
21
+ if (i) return i;
22
+ }
23
+ return null;
24
+ }
25
+ function c(e, t, n) {
26
+ if (!e.visible) return null;
27
+ let r = e.rect, i = t >= r.x && t <= r.x + r.width && n >= r.y && n <= r.y + r.height;
28
+ if (!i && (e.style.overflow === "hidden" || e.style.overflow === "scroll")) return null;
29
+ for (let r of [...e.children].reverse()) {
30
+ let e = c(r, t, n);
31
+ if (e) return e;
32
+ }
33
+ return i ? e : null;
34
+ }
35
+ var l = class {
36
+ constructor(t, n = {}) {
37
+ if (this.options = n, this.states = [], this.cursor = 0, this.listeners = /* @__PURE__ */ new Set(), this.historyLimit = n.historyLimit ?? 100, !Number.isInteger(this.historyLimit) || this.historyLimit < 1) throw e("historyLimit must be a positive integer; use 100", "ui");
38
+ this.validate(t), this.states = [{
39
+ markup: t.markup,
40
+ styles: t.styles
41
+ }];
42
+ }
43
+ get source() {
44
+ return { ...this.states[this.cursor] };
45
+ }
46
+ get canUndo() {
47
+ return this.cursor > 0;
48
+ }
49
+ get canRedo() {
50
+ return this.cursor < this.states.length - 1;
51
+ }
52
+ validate(e) {
53
+ if (!e || typeof e.markup != "string" || typeof e.styles != "string") throw t("Layout needs markup and styles strings; supply both source fields", "ui");
54
+ let n = r(e.markup, {
55
+ ...this.options,
56
+ styles: e.styles
57
+ });
58
+ for (let e of n.widgets) e.dispose();
59
+ }
60
+ apply(e) {
61
+ let t = {
62
+ ...this.source,
63
+ ...e
64
+ };
65
+ this.validate(t), (t.markup !== this.source.markup || t.styles !== this.source.styles) && (this.states = this.states.slice(0, this.cursor + 1), this.states.push(t), this.states.length > this.historyLimit + 1 && this.states.shift(), this.cursor = this.states.length - 1, this.changed());
66
+ }
67
+ build() {
68
+ return r(this.source.markup, {
69
+ ...this.options,
70
+ styles: this.source.styles
71
+ });
72
+ }
73
+ get tree() {
74
+ return i(this.source.markup);
75
+ }
76
+ setAttribute(n, r, i) {
77
+ if (!/^[A-Za-z_][\w.-]*$/.test(r)) throw t("Attribute name must be an XML name; use width, padding or another widget property", "ui");
78
+ let a = this.tree, c = s(a, n);
79
+ if (!c) throw e(`No layout element ${n}; select an existing id`, "ui");
80
+ i === void 0 ? delete c.element.attributes[r] : c.element.attributes[r] = i, this.apply({ markup: o(a) });
81
+ }
82
+ setText(t, n) {
83
+ let r = this.tree, i = s(r, t);
84
+ if (!i) throw e(`No layout element ${t}; select an existing id`, "ui");
85
+ i.element.text = n, this.apply({ markup: o(r) });
86
+ }
87
+ insert(t, n) {
88
+ let r = this.tree, i = 1;
89
+ for (; s(r, `element-${i}`);) i++;
90
+ let a = `element-${i}`, c = {
91
+ tag: t,
92
+ attributes: { id: a },
93
+ children: [],
94
+ text: [
95
+ "label",
96
+ "button",
97
+ "toggle"
98
+ ].includes(t) ? t : "",
99
+ line: 1,
100
+ column: 1
101
+ }, l = n ? s(r, n) : null;
102
+ if (n && !l) throw e(`No parent ${n}; select an existing container`, "ui");
103
+ return (l ? l.element.children : r).push(c), this.apply({ markup: o(r) }), a;
104
+ }
105
+ remove(t) {
106
+ let n = this.tree, r = s(n, t);
107
+ if (!r) throw e(`No layout element ${t}; select an existing id`, "ui");
108
+ r.siblings.splice(r.index, 1), this.apply({ markup: o(n) });
109
+ }
110
+ move(t, n, r = 0) {
111
+ let i = this.tree, a = s(i, t), c = n ? s(i, n) : null;
112
+ if (!a || n && !c || !Number.isInteger(r) || r < 0) throw e("Move needs existing ids and a non-negative index; select valid elements", "ui");
113
+ if (c && (c.element === a.element || s(a.element.children, n))) throw e("Cannot move an element into its descendant; select another parent", "ui");
114
+ a.siblings.splice(a.index, 1);
115
+ let l = c ? c.element.children : i;
116
+ l.splice(Math.min(r, l.length), 0, a.element), this.apply({ markup: o(i) });
117
+ }
118
+ undo() {
119
+ return this.canUndo ? (this.cursor--, this.changed(), !0) : !1;
120
+ }
121
+ redo() {
122
+ return this.canRedo ? (this.cursor++, this.changed(), !0) : !1;
123
+ }
124
+ save() {
125
+ return JSON.stringify({
126
+ version: 1,
127
+ ...this.source
128
+ }, null, 2);
129
+ }
130
+ load(n) {
131
+ let r;
132
+ try {
133
+ r = JSON.parse(n);
134
+ } catch {
135
+ throw t("Layout project is not JSON; open a saved layout project", "ui");
136
+ }
137
+ if (r?.version !== 1) throw e("Unsupported layout project version; open a version 1 project", "ui");
138
+ this.apply({
139
+ markup: r.markup,
140
+ styles: r.styles
141
+ });
142
+ }
143
+ onChange(e) {
144
+ return this.listeners.add(e), () => this.listeners.delete(e);
145
+ }
146
+ changed() {
147
+ for (let e of this.listeners) e();
148
+ }
149
+ dispose() {
150
+ this.listeners.clear(), this.states = [this.source], this.cursor = 0;
151
+ }
152
+ [Symbol.dispose ?? Symbol.for("Symbol.dispose")]() {
153
+ this.dispose();
154
+ }
155
+ }, u = class {
156
+ constructor(e, t) {
157
+ this.options = t, this.abort = new AbortController(), t.signal?.throwIfAborted(), this.document = new l(t, { actions: t.actions }), this.preview = new n({
158
+ direction: "stack",
159
+ anchor: "stretch",
160
+ pointerEvents: "none"
161
+ }), t.ui.add(this.preview);
162
+ let r = e.ownerDocument, i = r.createElement("section");
163
+ this.element = i, e.append(i);
164
+ let a = i.attachShadow({ mode: "open" }), o = r.createElement("style");
165
+ o.textContent = d, a.append(o);
166
+ let u = (e, t, n) => {
167
+ let i = r.createElement(e);
168
+ return n && (i.textContent = n), t.appendChild(i), i;
169
+ }, f = u("div", a);
170
+ f.className = "workspace";
171
+ let p = u("header", f);
172
+ u("strong", p, "Layout studio");
173
+ let m = u("output", p, "Ready");
174
+ m.setAttribute("aria-live", "polite");
175
+ let h = (e) => {
176
+ try {
177
+ e(), m.value = "Ready";
178
+ } catch (e) {
179
+ m.value = e instanceof Error ? e.message : String(e);
180
+ }
181
+ }, g = (e, t, n, r = this.abort.signal) => {
182
+ let i = u("button", t, e);
183
+ return i.type = "button", i.addEventListener("click", () => h(n), { signal: r }), i;
184
+ };
185
+ g("Undo", p, () => {
186
+ this.document.undo();
187
+ }), g("Redo", p, () => {
188
+ this.document.redo();
189
+ });
190
+ let _ = (e, t, n) => {
191
+ let r = URL.createObjectURL(new Blob([t], { type: n })), i = u("a", a);
192
+ i.href = r, i.download = e, i.click(), i.remove(), setTimeout(() => URL.revokeObjectURL(r), 1e3);
193
+ }, v = () => this.document.apply({
194
+ markup: I.value,
195
+ styles: L.value
196
+ }), y = () => {
197
+ v(), _("layout.json", this.document.save(), "application/json");
198
+ };
199
+ g("Save project", p, y), g("Export markup", p, () => {
200
+ v(), _("layout.xml", this.document.source.markup, "application/xml");
201
+ }), g("Export styles", p, () => {
202
+ v(), _("layout.css", this.document.source.styles, "text/css");
203
+ });
204
+ let b = u("input", p);
205
+ b.type = "file", b.accept = ".json,application/json", b.setAttribute("aria-label", "Open layout project"), b.addEventListener("change", () => {
206
+ let e = b.files?.[0];
207
+ e && this.open(e).then(() => {
208
+ m.value = "Opened project";
209
+ }, (e) => {
210
+ m.value = String(e);
211
+ });
212
+ }, { signal: this.abort.signal });
213
+ let x = u("div", f);
214
+ x.className = "body";
215
+ let S = u("aside", x);
216
+ u("h2", S, "Elements");
217
+ let C = u("div", S);
218
+ C.className = "palette";
219
+ for (let e of [
220
+ "panel",
221
+ "label",
222
+ "button",
223
+ "slider",
224
+ "toggle"
225
+ ]) g(`Add ${e}`, C, () => {
226
+ this.selected = this.document.insert(e, this.selected), B();
227
+ });
228
+ let w = u("div", S);
229
+ w.setAttribute("role", "tree"), w.setAttribute("aria-label", "Layout hierarchy"), g("Delete selected", S, () => {
230
+ if (this.selected) {
231
+ let e = this.selected;
232
+ this.selected = void 0, this.document.remove(e);
233
+ }
234
+ });
235
+ let T = u("main", x);
236
+ u("h2", T, "Preview");
237
+ let E = u("div", T);
238
+ E.className = "viewport";
239
+ let D = t.canvas, O = D.parentNode, k = r.createComment("UI editor canvas location"), A = D.nextElementSibling?.matches("[data-margo-ui=\"mirror\"]") ? D.nextElementSibling : null;
240
+ O?.insertBefore(k, D), E.append(D), A && E.append(A);
241
+ let j = u("aside", x);
242
+ u("h2", j, "Properties");
243
+ let M = u("p", j, "Select an element"), N = /* @__PURE__ */ new Map();
244
+ for (let e of [
245
+ "text",
246
+ "width",
247
+ "height",
248
+ "padding",
249
+ "gap",
250
+ "direction",
251
+ "background",
252
+ "anchor"
253
+ ]) {
254
+ let t = u("input", u("label", j, e));
255
+ t.setAttribute("aria-label", `Selected ${e}`), N.set(e, t), t.addEventListener("change", () => h(() => {
256
+ this.selected && (e === "text" ? this.document.setText(this.selected, t.value) : this.document.setAttribute(this.selected, e, t.value || void 0));
257
+ }), { signal: this.abort.signal });
258
+ }
259
+ let P = u("div", f);
260
+ P.className = "sources";
261
+ let F = (e) => {
262
+ let t = u("label", P, e), n = u("textarea", t);
263
+ return n.setAttribute("aria-label", e), n.spellcheck = !1, n;
264
+ }, I = F("Markup"), L = F("Style sheet"), R = (e) => {
265
+ this.selected = e;
266
+ let t = e ? s(this.document.tree, e)?.element : void 0;
267
+ M.textContent = t ? `${t.tag} #${e}` : "Select an element";
268
+ for (let [e, n] of N) n.value = t ? e === "text" ? t.text : t.attributes[e] ?? "" : "";
269
+ }, z = new AbortController(), B = () => {
270
+ z.abort(), z = new AbortController();
271
+ let e = this.document.build();
272
+ for (let e of [...this.preview.children]) e.dispose();
273
+ this.preview.add(...e.widgets), t.ui.update(0), I.value = this.document.source.markup, L.value = this.document.source.styles, w.replaceChildren();
274
+ let n = (e, t = 1) => {
275
+ for (let r of e) {
276
+ let e = r.attributes.id, i = g(`${r.tag}${e ? " #" + e : ""}`, w, () => R(e), z.signal);
277
+ i.setAttribute("role", "treeitem"), i.setAttribute("aria-level", String(t)), i.style.paddingInlineStart = `${t * 12}px`, i.disabled = !e, i.draggable = !!e, i.addEventListener("dragstart", (t) => t.dataTransfer?.setData("text/plain", e), { signal: z.signal }), i.addEventListener("dragover", (e) => e.preventDefault(), { signal: z.signal }), i.addEventListener("drop", (t) => {
278
+ t.preventDefault();
279
+ let n = t.dataTransfer?.getData("text/plain");
280
+ n && h(() => this.document.move(n, e, r.children.length));
281
+ }, { signal: z.signal }), n(r.children, t + 1);
282
+ }
283
+ };
284
+ n(this.document.tree), R(this.selected);
285
+ }, V = () => h(() => this.document.apply({
286
+ markup: I.value,
287
+ styles: L.value
288
+ }));
289
+ I.addEventListener("input", V, { signal: this.abort.signal }), L.addEventListener("input", V, { signal: this.abort.signal }), E.addEventListener("pointerdown", (e) => {
290
+ let n = D.getBoundingClientRect(), r = c(this.preview, (e.clientX - n.left) / t.ui.cssScale, (e.clientY - n.top) / t.ui.cssScale);
291
+ r && R(r.id);
292
+ }, {
293
+ signal: this.abort.signal,
294
+ capture: !0
295
+ }), i.addEventListener("keydown", (e) => {
296
+ let t = e;
297
+ (t.ctrlKey || t.metaKey) && t.key.toLowerCase() === "s" ? (t.preventDefault(), h(y)) : (t.ctrlKey || t.metaKey) && t.key.toLowerCase() === "z" && t.composedPath()[0]?.tagName !== "TEXTAREA" && (t.preventDefault(), h(() => {
298
+ t.shiftKey ? this.document.redo() : this.document.undo();
299
+ }));
300
+ }, { signal: this.abort.signal });
301
+ let H = this.document.onChange(B);
302
+ B();
303
+ let U = () => this.dispose();
304
+ t.signal?.addEventListener("abort", U, { once: !0 }), this.cleanup = () => {
305
+ this.abort.abort(), z.abort(), H(), t.signal?.removeEventListener("abort", U), this.preview.dispose(), this.document.dispose();
306
+ let e = D.nextElementSibling?.matches("[data-margo-ui=\"mirror\"]") ? D.nextElementSibling : A;
307
+ O ? O.insertBefore(D, k.parentNode === O ? k : null) : D.remove(), e && D.after(e), k.remove(), i.remove();
308
+ };
309
+ }
310
+ async open(e, t = {}) {
311
+ this.abort.signal.throwIfAborted(), t.signal?.throwIfAborted();
312
+ let n = await e.text();
313
+ this.abort.signal.throwIfAborted(), t.signal?.throwIfAborted(), this.document.load(n);
314
+ }
315
+ dispose() {
316
+ this.abort.signal.aborted || this.cleanup();
317
+ }
318
+ [Symbol.dispose ?? Symbol.for("Symbol.dispose")]() {
319
+ this.dispose();
320
+ }
321
+ }, d = ":host{display:block;color:#dce3e8;font:13px system-ui;background:#13181d;min-height:100vh}*{box-sizing:border-box}.workspace{padding:20px;max-width:1600px;margin:auto}header{display:flex;gap:8px;align-items:center;flex-wrap:wrap;border-bottom:1px solid #34414b;padding-bottom:16px}strong{font-size:20px;margin-right:20px}output{flex:1;min-width:100px;color:#98b4c5}button,input,textarea{font:inherit;color:inherit;background:#222b33;border:1px solid #43525e;border-radius:4px;padding:8px}button{cursor:pointer;min-height:40px;transition:background .12s}button:active{transform:translateY(1px)}button:hover{background:#354754}button:focus-visible,input:focus-visible,textarea:focus-visible{outline:2px solid #8fd3ff;outline-offset:2px}.body{display:grid;grid-template-columns:200px minmax(300px,1fr) 200px;gap:20px;padding:16px 0}h2{font-size:12px;text-transform:uppercase;letter-spacing:.08em;color:#9cabb5}aside button{display:block;width:100%;text-align:left;margin-bottom:5px}.palette{display:grid;grid-template-columns:1fr 1fr;gap:4px;margin-bottom:16px}.palette button{font-size:11px}label{display:block;margin-bottom:12px}label input{display:block;width:100%;margin-top:4px}.viewport{position:relative;overflow:auto;background:#080b0e;border:1px solid #34414b;min-height:360px}canvas{display:block;width:640px;height:auto;aspect-ratio:16/9;max-width:100%;margin-inline:auto}.sources{display:grid;grid-template-columns:1fr 1fr;gap:20px}textarea{display:block;margin-top:8px;width:100%;min-height:280px;resize:vertical;font:12px/1.6 Consolas,monospace;tab-size:2}@media(max-width:900px){.body{grid-template-columns:160px minmax(0,1fr)}.body>aside:last-child{grid-column:1/-1;display:flex;gap:10px;flex-wrap:wrap}.sources{grid-template-columns:1fr}header input{max-width:220px}}@media(max-width:650px){.workspace{padding:12px}.body{grid-template-columns:minmax(0,1fr)}.body>aside:last-child{display:grid;grid-template-columns:1fr 1fr}.palette{grid-template-columns:repeat(3,1fr)}[role=tree]{max-height:180px;overflow:auto}.sources{grid-template-columns:minmax(0,1fr)}button{min-height:44px}.viewport{min-height:200px}header strong{width:100%}}";
322
+ //#endregion
323
+ export { l as LayoutDocument, u as LayoutEditor };
@@ -0,0 +1,7 @@
1
+ //#region ../shared/errors.js
2
+ var e = (e, t) => Object.assign(Error(t), {
3
+ name: "MargoError",
4
+ code: e
5
+ }), t = (e, t) => `${e}; see https://margoengine.com/errors#${t}`, n = (n, r, i = n) => e(n, t(r, i)), r = (e, n) => TypeError(t(e, n)), i = (e, n) => RangeError(t(e, n));
6
+ //#endregion
7
+ export { i as n, r, n as t };
@@ -0,0 +1,12 @@
1
+ import type { Rect } from './layout.js';
2
+ /** A direction of spatial navigation. */
3
+ export type Direction = 'up' | 'down' | 'left' | 'right';
4
+ /**
5
+ * Pick where focus moves from `from` in `direction` among `candidates` (rectangles in UI units, in document order).
6
+ * A candidate qualifies when its centre lies beyond the centre of `from` in that direction. Among those, ones that overlap
7
+ * `from` on the other axis (a neighbour in the same row or column) beat ones that do not; then the nearest edge wins,
8
+ * with sideways misalignment counting twice. Ties go to the earlier candidate. Returns an index into `candidates`, or -1.
9
+ * With `wrap`, when nothing lies ahead, the farthest candidate in the opposite direction that best lines up is chosen
10
+ * (down from the last row goes to the first).
11
+ */
12
+ export declare function moveFocus(from: Rect, candidates: readonly Rect[], direction: Direction, wrap?: boolean): number;
@@ -0,0 +1,114 @@
1
+ import { Button, Panel, Toggle, Widget, type ButtonOptions, type PanelOptions, type ToggleOptions, type UiCommand, type UiEvent, type WidgetOptions, type Detach, type Ui } from '@margoengine/ui';
2
+ import { TextInput } from '@margoengine/ui/input';
3
+ /** One named, uniquely valued choice. */
4
+ export interface FormOption {
5
+ label: string;
6
+ value: string;
7
+ disabled?: boolean;
8
+ }
9
+ /** A change handler receives the chosen string as value and index. */
10
+ export interface ChoiceOptions extends WidgetOptions {
11
+ options: readonly FormOption[];
12
+ value?: string;
13
+ onChange?: (event: UiEvent) => void;
14
+ }
15
+ /** Select control with a modal listbox, keyboard and gamepad arrows and cancellation. */
16
+ export declare class Select extends Button {
17
+ readonly options: readonly FormOption[];
18
+ private _value;
19
+ private popup?;
20
+ constructor({ options, value, onChange, ...rest }: ChoiceOptions);
21
+ private validate;
22
+ private refresh;
23
+ /** Selected option value. Programmatic writes do not emit change. */
24
+ get value(): string;
25
+ set value(v: string);
26
+ /** Whether the choice dialog is open. */
27
+ get expanded(): boolean;
28
+ /** ID of the choice dialog while expanded, for accessible relationships. */
29
+ get popupId(): string | undefined;
30
+ /** Select by value, optionally announcing a user change. */
31
+ select(value: string, emit?: boolean): void;
32
+ /** Close the owned popup and release widget listeners. */
33
+ dispose(): void;
34
+ command(command: UiCommand): boolean;
35
+ activate(): void;
36
+ }
37
+ /** Checkbox appearance backed by the existing complete toggle input and mirror. */
38
+ export declare class Checkbox extends Toggle {
39
+ constructor(options?: Omit<ToggleOptions, 'variant'>);
40
+ }
41
+ /** Options for a radio group. */
42
+ export interface RadioGroupOptions extends ChoiceOptions {
43
+ /** Default vertical. */ orientation?: 'horizontal' | 'vertical';
44
+ }
45
+ declare class ChoiceButton extends Button {
46
+ readonly group: RadioGroup | Tabs;
47
+ readonly index: number;
48
+ constructor(group: RadioGroup | Tabs, index: number, options: ButtonOptions);
49
+ activate(): void;
50
+ command(c: UiCommand): boolean;
51
+ }
52
+ /** Single selection, one tab stop, wrapping arrows, disabled-option exclusion. */
53
+ export declare class RadioGroup extends Panel {
54
+ readonly options: readonly FormOption[];
55
+ readonly orientation: 'horizontal' | 'vertical';
56
+ protected _value: string;
57
+ protected readonly choiceButtons: ChoiceButton[];
58
+ /** The controls in option order. */
59
+ get buttons(): readonly Button[];
60
+ constructor({ options, value, onChange, orientation, ...rest }: RadioGroupOptions);
61
+ /** Selected value. Programmatic writes do not emit change. */
62
+ get value(): string;
63
+ set value(v: string);
64
+ protected refresh(): void;
65
+ /** Choose an enabled option by index. */
66
+ selectIndex(i: number, emit?: boolean): void;
67
+ /** @internal Arrow navigation shares the same path for keyboard and gamepad. */
68
+ move(at: number, c: UiCommand): boolean;
69
+ }
70
+ /** A tab and its associated content panel. */
71
+ export interface TabOption extends FormOption {
72
+ panel: Widget;
73
+ }
74
+ /** Options for Tabs. */
75
+ export interface TabsOptions extends Omit<RadioGroupOptions, 'options'> {
76
+ options: readonly TabOption[];
77
+ }
78
+ /** Tablist with automatic activation, one tab stop and an associated visible panel. */
79
+ export declare class Tabs extends RadioGroup {
80
+ readonly panels: readonly Widget[];
81
+ constructor({ options, ...rest }: TabsOptions);
82
+ protected refresh(): void;
83
+ }
84
+ /** A colour picker with HSV, RGB and alpha sliders plus a hex field. */
85
+ export interface ColorPickerOptions extends PanelOptions {
86
+ /** Initial #rrggbb or #rrggbbaa. Default #ffffff. */ value?: string;
87
+ onChange?: (event: UiEvent) => void;
88
+ }
89
+ /** Pointer saturation/value square and precise keyboard, gamepad and accessible channel controls. */
90
+ export declare class ColorPicker extends Panel {
91
+ private rgba;
92
+ private hueValue;
93
+ private saturationValue;
94
+ private readonly channels;
95
+ readonly hexInput: TextInput;
96
+ constructor({ value, onChange, ...rest }?: ColorPickerOptions);
97
+ /** Normalized #rrggbbaa value, including alpha. */
98
+ get value(): string;
99
+ set value(v: string);
100
+ /** Current hue in [0,1]. */
101
+ get hue(): number;
102
+ /** Current saturation in [0,1], kept while Value is 0. */
103
+ get saturation(): number;
104
+ /** Set HSV from a pointer or application, emitting a change. */
105
+ setHSV(h: number, s: number, v: number): void;
106
+ private change;
107
+ private keep;
108
+ private refresh;
109
+ }
110
+ /** Add radio, tab and select ARIA states to the ordinary UiRenderer mirror. Attach after UiRenderer. */
111
+ export declare function attachFormsMirror(ui: Ui, target: HTMLElement, options?: {
112
+ signal?: AbortSignal;
113
+ }): Detach;
114
+ export {};