@gramlot/gramlot 0.2.17 → 0.2.19

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.
@@ -1,8 +1,8 @@
1
1
  // ../tools/gramlot-tools-inspector/src/gramlot-tools-inspector.js
2
- import { Bag as Bag3, Gramlot as Gramlot2, GramlotBuilder } from "@gramlot/gramlot";
2
+ import { Bag as Bag3, Gramlot, GramlotBuilder } from "@gramlot/gramlot";
3
3
 
4
4
  // ../tools/gramlot-tools-inspector/src/inspector-bridge.js
5
- import { Bag, Gramlot } from "@gramlot/gramlot";
5
+ import { Bag } from "@gramlot/gramlot";
6
6
 
7
7
  // ../tools/gramlot-tools-inspector/src/editor-values.js
8
8
  var editorTypes = ["string", "number", "integer", "boolean", "date", "time", "datetime", "null"];
@@ -50,31 +50,85 @@ function parseScalar(type, text) {
50
50
  throw new Error(`Invalid ${type} value: ${text || "(empty)"}`);
51
51
  }
52
52
 
53
- // ../tools/gramlot-tools-inspector/src/inspector-bridge.js
54
- var sequence = 0;
53
+ // ../tools/gramlot-tools-inspector/src/inspector-styles.js
54
+ var InspectorStyles = class {
55
+ #document;
56
+ #element = null;
57
+ #rule = null;
58
+ constructor(document) {
59
+ this.#document = document;
60
+ }
61
+ snapshot() {
62
+ const computed = this.#document.defaultView.getComputedStyle(this.#document.documentElement);
63
+ const names = /* @__PURE__ */ new Set();
64
+ for (let index = 0; index < computed.length; index++) {
65
+ const name = computed.item(index);
66
+ if (name.startsWith("--gramlot-")) names.add(name);
67
+ }
68
+ for (let index = 0; index < (this.#rule?.style.length ?? 0); index++) names.add(this.#rule.style.item(index));
69
+ return [...names].sort((left, right) => left.localeCompare(right, "en", { numeric: true })).map((name) => ({
70
+ name,
71
+ value: computed.getPropertyValue(name).trim(),
72
+ override: this.#rule?.style.getPropertyValue(name) ?? ""
73
+ }));
74
+ }
75
+ set(name, value) {
76
+ if (typeof name !== "string" || !this.snapshot().some((row) => row.name === name)) {
77
+ throw new Error("Choose an existing --gramlot-* variable on the document root");
78
+ }
79
+ if (typeof value !== "string" || !value.trim() || !this.#document.defaultView.CSS.supports(name, value)) {
80
+ throw new Error("Enter a valid CSS variable value");
81
+ }
82
+ if (!this.#element) {
83
+ const element = this.#document.createElement("style");
84
+ this.#document.head.append(element);
85
+ if (!element.sheet) {
86
+ element.remove();
87
+ throw new Error("The page does not allow the inspector preview stylesheet");
88
+ }
89
+ element.sheet.insertRule(":root {}", 0);
90
+ this.#element = element;
91
+ this.#rule = element.sheet.cssRules[0];
92
+ }
93
+ this.#rule.style.setProperty(name, value, "important");
94
+ }
95
+ reset(name) {
96
+ if (typeof name !== "string" || !name.startsWith("--gramlot-")) throw new Error("Invalid CSS variable name");
97
+ this.#rule?.style.removeProperty(name);
98
+ if (this.#rule?.style.length === 0) this.dispose();
99
+ }
100
+ dispose() {
101
+ this.#element?.remove();
102
+ this.#element = null;
103
+ this.#rule = null;
104
+ }
105
+ };
106
+
107
+ // ../tools/gramlot-tools-inspector/src/inspector-agent.js
55
108
  var roots = ["data", "source"];
56
109
  var forbidden = /* @__PURE__ */ new Set(["_meta", "__proto__", "prototype", "constructor"]);
57
- function encodeScalar(value) {
58
- if (!scalarType(value)) throw new TypeError("The inspector edits scalar values only");
59
- const wrapper = new Bag();
60
- wrapper.setItem("value", value);
61
- return wrapper.toTytx("json");
62
- }
63
- function decodeScalar(text) {
64
- const value = Bag.fromTytx(text, "json").getItem("value");
65
- if (!scalarType(value)) throw new TypeError("The inspector edits scalar values only");
66
- return value;
67
- }
68
- function cell(value) {
69
- if (scalarType(value)) return { encoded: encodeScalar(value) };
70
- const display = value instanceof Bag ? `Bag (${value.getNodes().length} nodes)` : value === void 0 ? "undefined" : typeof value === "function" ? "[Function]" : typeof value === "bigint" || typeof value === "symbol" ? String(value) : JSON.stringify(value);
71
- return { display, readonly: true };
72
- }
73
110
  function attachInspectorAgent(gramlot, port) {
74
- if (!(gramlot instanceof Gramlot) || gramlot.state === "disposed") throw new TypeError("Expected a live Gramlot instance");
111
+ if (!gramlot || gramlot.state === "disposed") throw new TypeError("Expected a live Gramlot instance");
112
+ const Bag4 = gramlot.data.constructor;
113
+ const encodeScalar2 = (value) => {
114
+ const wrapper = new Bag4();
115
+ wrapper.setItem("value", value);
116
+ return wrapper.toTytx("json");
117
+ };
118
+ const decodeScalar2 = (text) => {
119
+ const value = Bag4.fromTytx(text, "json").getItem("value");
120
+ if (!scalarType(value)) throw new TypeError("The inspector edits scalar values only");
121
+ return value;
122
+ };
123
+ function cell(value) {
124
+ if (scalarType(value)) return { encoded: encodeScalar2(value) };
125
+ const display = value instanceof Bag4 ? `Bag (${value.getNodes().length} nodes)` : value === void 0 ? "undefined" : typeof value === "function" ? "[Function]" : typeof value === "bigint" || typeof value === "symbol" ? String(value) : JSON.stringify(value);
126
+ return { display, readonly: true };
127
+ }
75
128
  const document = gramlot.src.renderer.destination.ownerDocument;
76
129
  const window = document.defaultView;
77
- const subscriber = `inspector-bridge-${++sequence}`;
130
+ const styles = new InspectorStyles(document);
131
+ const subscriber = `inspector-bridge-${window.crypto.getRandomValues(new Uint32Array(4)).join("-")}`;
78
132
  const identities = /* @__PURE__ */ new WeakMap();
79
133
  const versions = /* @__PURE__ */ new WeakMap();
80
134
  let nextNode = 0, stopped = false, scheduled = false;
@@ -92,7 +146,7 @@ function attachInspectorAgent(gramlot, port) {
92
146
  const next = /* @__PURE__ */ new Set([...ancestors, store]);
93
147
  return store.getNodes().map((node) => {
94
148
  const value = node.getValue(true);
95
- const branch = !node.resolver && value instanceof Bag;
149
+ const branch = !node.resolver && value instanceof Bag4;
96
150
  return {
97
151
  id: identity(node),
98
152
  label: node.label,
@@ -173,12 +227,12 @@ function attachInspectorAgent(gramlot, port) {
173
227
  }
174
228
  for (const [name, encoded] of command.set) {
175
229
  if (!name.trim() || forbidden.has(name) || Object.hasOwn(attrs, name) && !scalarType(attrs[name])) throw new Error(`Cannot edit attribute: ${name}`);
176
- attrs[name] = decodeScalar(encoded);
230
+ attrs[name] = decodeScalar2(encoded);
177
231
  }
178
232
  let value;
179
233
  if (Object.hasOwn(command, "value")) {
180
234
  if (node.resolver || !scalarType(node.getValue(true))) throw new Error("This value is read-only");
181
- value = decodeScalar(command.value);
235
+ value = decodeScalar2(command.value);
182
236
  }
183
237
  if (command.set.length || command.remove.length) node.setAttr(attrs, true, false, false);
184
238
  if (Object.hasOwn(command, "value")) node.setValue(value);
@@ -199,6 +253,14 @@ function attachInspectorAgent(gramlot, port) {
199
253
  return;
200
254
  }
201
255
  try {
256
+ if (["styles", "setStyle", "resetStyle", "resetStyles"].includes(message.type)) {
257
+ if (gramlot.state === "disposed") throw new Error("Inspected page disconnected");
258
+ if (message.type === "setStyle") styles.set(message.command.name, message.command.value);
259
+ if (message.type === "resetStyle") styles.reset(message.command.name);
260
+ if (message.type === "resetStyles") styles.dispose();
261
+ send({ type: "result", id: message.id, ok: true, styles: styles.snapshot() });
262
+ return;
263
+ }
202
264
  if (message.type === "edit") edit(message.command);
203
265
  else if (message.type === "breakpoint") setBreakpoint(message.command);
204
266
  else if (message.type !== "refresh") throw new Error(`Unknown inspector command: ${message.type}`);
@@ -213,6 +275,7 @@ function attachInspectorAgent(gramlot, port) {
213
275
  stopped = true;
214
276
  for (const root of roots) bag(root).unsubscribe(subscriber, { any: true });
215
277
  breakpoints.clear();
278
+ styles.dispose();
216
279
  port.removeEventListener("message", receive);
217
280
  window.removeEventListener("pagehide", dispose);
218
281
  port.close();
@@ -224,12 +287,27 @@ function attachInspectorAgent(gramlot, port) {
224
287
  flush();
225
288
  return { dispose };
226
289
  }
290
+
291
+ // ../tools/gramlot-tools-inspector/src/inspector-bridge.js
292
+ var roots2 = ["data", "source"];
293
+ function encodeScalar(value) {
294
+ if (!scalarType(value)) throw new TypeError("The inspector edits scalar values only");
295
+ const wrapper = new Bag();
296
+ wrapper.setItem("value", value);
297
+ return wrapper.toTytx("json");
298
+ }
299
+ function decodeScalar(text) {
300
+ const value = Bag.fromTytx(text, "json").getItem("value");
301
+ if (!scalarType(value)) throw new TypeError("The inspector edits scalar values only");
302
+ return value;
303
+ }
227
304
  var InspectorConnection = class {
228
305
  data = new Bag();
229
306
  source = new Bag();
230
307
  state = "connecting";
231
308
  error = "";
232
309
  breakpoints = [];
310
+ styles = null;
233
311
  #info = /* @__PURE__ */ new WeakMap();
234
312
  #listeners = /* @__PURE__ */ new Set();
235
313
  #pending = /* @__PURE__ */ new Map();
@@ -257,8 +335,9 @@ var InspectorConnection = class {
257
335
  return;
258
336
  }
259
337
  try {
338
+ if (message.styles) this.styles = message.styles;
260
339
  if (message.snapshot) {
261
- for (const root of roots) this.#reconcile(this[root], message.snapshot[root]);
340
+ for (const root of roots2) this.#reconcile(this[root], message.snapshot[root]);
262
341
  this.breakpoints = message.snapshot.breakpoints;
263
342
  this.state = "started";
264
343
  this.error = "";
@@ -279,7 +358,7 @@ var InspectorConnection = class {
279
358
  #reconcile(store, rows) {
280
359
  const labels = new Set(rows.map((row) => row.label));
281
360
  for (const node of store.getNodes()) if (!labels.has(node.label)) store.popNode(node.label);
282
- for (const row of rows) {
361
+ for (const [index, row] of rows.entries()) {
283
362
  let node = store.getNode(row.label);
284
363
  let previous = node ? this.#info.get(node) : null;
285
364
  if (node && previous?.id !== row.id) {
@@ -309,6 +388,8 @@ var InspectorConnection = class {
309
388
  attrsWire: new Map(row.attrs.map(([name, descriptor]) => [name, JSON.stringify(descriptor)]))
310
389
  });
311
390
  if (branch) this.#reconcile(value, row.children);
391
+ const position = store.getNodes().indexOf(node);
392
+ if (position !== index) store.move(position, index);
312
393
  }
313
394
  }
314
395
  request(type, command = {}) {
@@ -332,7 +413,7 @@ var InspectorConnection = class {
332
413
  };
333
414
 
334
415
  // ../tools/gramlot-tools-inspector/src/gramlot-tools-inspector.css
335
- var gramlot_tools_inspector_default = ":host { all: initial; color-scheme: light; font: 12px/1.4 system-ui, sans-serif; color: #38414b; }\n* { box-sizing: border-box; }\nbutton, input, select { font: inherit; color: inherit; }\nbutton { cursor: pointer; }\nbutton:focus-visible, input:focus-visible, select:focus-visible { outline: 1px solid #3574cb; outline-offset: -1px; }\n.tree { padding: 7px 3px; }\n.properties { background: white; }\n.properties-heading { font-size: 11px; display: flex; align-items: center; justify-content: space-between; height: 29px; padding: 0 8px; background: #f1f3f5; color: #6d7989; border-bottom: 1px solid #d1d7df; }\n.properties-heading button { background: white; border: 1px solid #c5cdda; border-radius: 2px; width: 23px; height: 23px; padding: 0; font-size: 18px; line-height: 20px; }\nfieldset { border: 0; margin: 0; padding: 0; min-width: 0; }\n.property-row { display: grid; grid-template-columns: minmax(95px, 30%) minmax(0, 1fr); min-height: 20px; border-bottom: 1px solid #d1d7df; font-size: 11px; }\n.property-name { display: flex; align-items: center; justify-content: flex-end; background: #888; color: white; font-weight: 700; padding: 1px 6px; border-right: 1px solid #d1d7df; overflow-wrap: anywhere; text-align: right; }\n.property-value { display: flex; align-items: center; min-width: 0; }\n.property-row input { border: 0; border-radius: 0; min-width: 0; width: 100%; height: 19px; padding: 1px 5px; background: transparent; }\n.property-name input { color: white; text-align: right; padding: 0; }\n.property-value input { flex: 1; }\n.property-value input[type=checkbox] { flex: none; width: 14px; height: 14px; margin: 2px 6px; }\n.property-value select { max-width: 82px; height: 20px; padding: 0; border: 0; border-left: 1px solid #d1d7df; background: #f1f3f5; font-size: 10px; }\n.remove { margin-left: auto; flex: none; width: 22px; height: 20px; padding: 3px; border: 0; background: transparent; color: #7c8797; opacity: 0; }\n.remove svg { width: 13px; height: 13px; }\n.property-row:hover .remove, .property-row:focus-within .remove { opacity: 1; }\n.remove:hover { color: #b82323; background: #fbe9e9; }\n:disabled { cursor: default; opacity: .65; }\n.path { flex: none; padding: 5px 9px; border-top: 1px solid #c9cfd6; background: #f5f7f9; color: #697687; font-size: 11px; min-height: 27px; overflow-wrap: anywhere; }\n.error, .edit-status, .breakpoint-status, .breakpoint-line { font-size: 11px; margin: 0; padding: 3px 8px; overflow-wrap: anywhere; }\n.error, .edit-status, .breakpoint-status { color: #a12121; }\n.breakpoint-line { background: #fff2ed; color: #854127; border-bottom: 1px solid #d1d7df; }\n.error:empty, .edit-status:empty, .breakpoint-status:empty, .breakpoint-line:empty { display: none; }\n.breakpoint-dialog { width: min(340px, 90vw); color: #38414b; border: 1px solid #bbc3cf; border-radius: 4px; padding: 12px; box-shadow: 0 8px 28px #0003; font: 12px/1.4 system-ui, sans-serif; }\n.breakpoint-dialog::backdrop { background: #0002; }\n.breakpoint-dialog h2 { font-size: 13px; margin: 0 0 10px; overflow-wrap: anywhere; }\n.breakpoint-dialog select, .breakpoint-dialog input { width: 100%; border: 1px solid #bbc3cf; padding: 4px; margin: 0 0 7px; background: white; }\n.dialog-actions { display: flex; gap: 6px; justify-content: flex-end; margin-top: 7px; }\n.dialog-actions button { padding: 3px 10px; border: 1px solid #bbc3cf; border-radius: 3px; background: #f1f3f5; }\n.heading-label { flex: 1; }\n.properties-heading .breakpoint-count { width: auto; padding: 0 5px; margin-right: 5px; font-size: 11px; color: #b04a35; }\n.breakpoint-entry { display: flex; align-items: center; gap: 6px; padding: 4px 0; border-bottom: 1px solid #d1d7df; }\n.breakpoint-entry span { flex: 1; overflow-wrap: anywhere; }\n.breakpoint-entry button { background: transparent; border: 0; font-size: 17px; }\n[hidden] { display: none !important; }\n.targetbar { flex: none; height: 28px; padding: 2px 6px; background: #edf2fa; border-bottom: 1px solid #dadce0; }\n.targetbar select { width: 100%; height: 23px; border: 0; background: transparent; font-size: 11px; }\n";
416
+ var gramlot_tools_inspector_default = ":host { all: initial; color-scheme: light; font: 12px/1.4 system-ui, sans-serif; color: #38414b; }\n* { box-sizing: border-box; }\nbutton, input, select { font: inherit; color: inherit; }\nbutton { cursor: pointer; }\nbutton:focus-visible, input:focus-visible, select:focus-visible { outline: 1px solid #3574cb; outline-offset: -1px; }\n.tree { padding: 7px 3px; }\n.properties { background: white; }\n.properties-heading { font-size: 11px; display: flex; align-items: center; justify-content: space-between; height: 29px; padding: 0 8px; background: #f1f3f5; color: #6d7989; border-bottom: 1px solid #d1d7df; }\n.properties-heading button { background: white; border: 1px solid #c5cdda; border-radius: 2px; width: 23px; height: 23px; padding: 0; font-size: 18px; line-height: 20px; }\nfieldset { border: 0; margin: 0; padding: 0; min-width: 0; }\n.property-row { display: grid; grid-template-columns: minmax(95px, 30%) minmax(0, 1fr); min-height: 20px; border-bottom: 1px solid #d1d7df; font-size: 11px; }\n.property-name { display: flex; align-items: center; justify-content: flex-end; background: #888; color: white; font-weight: 700; padding: 1px 6px; border-right: 1px solid #d1d7df; overflow-wrap: anywhere; text-align: right; }\n.property-value { display: flex; align-items: center; min-width: 0; }\n.property-row input { border: 0; border-radius: 0; min-width: 0; width: 100%; height: 19px; padding: 1px 5px; background: transparent; }\n.property-name input { color: white; text-align: right; padding: 0; }\n.property-value input { flex: 1; }\n.property-value input[type=checkbox] { flex: none; width: 14px; height: 14px; margin: 2px 6px; }\n.property-value select { max-width: 82px; height: 20px; padding: 0; border: 0; border-left: 1px solid #d1d7df; background: #f1f3f5; font-size: 10px; }\n.remove { margin-left: auto; flex: none; width: 22px; height: 20px; padding: 3px; border: 0; background: transparent; color: #7c8797; opacity: 0; }\n.remove svg { width: 13px; height: 13px; }\n.property-row:hover .remove, .property-row:focus-within .remove { opacity: 1; }\n.remove:hover { color: #b82323; background: #fbe9e9; }\n:disabled { cursor: default; opacity: .65; }\n.path { flex: none; padding: 5px 9px; border-top: 1px solid #c9cfd6; background: #f5f7f9; color: #697687; font-size: 11px; min-height: 27px; overflow-wrap: anywhere; }\n.error, .edit-status, .breakpoint-status, .breakpoint-line { font-size: 11px; margin: 0; padding: 3px 8px; overflow-wrap: anywhere; }\n.error, .edit-status, .breakpoint-status { color: #a12121; }\n.breakpoint-line { background: #fff2ed; color: #854127; border-bottom: 1px solid #d1d7df; }\n.error:empty, .edit-status:empty, .breakpoint-status:empty, .breakpoint-line:empty { display: none; }\n.breakpoint-dialog { width: min(340px, 90vw); color: #38414b; border: 1px solid #bbc3cf; border-radius: 4px; padding: 12px; box-shadow: 0 8px 28px #0003; font: 12px/1.4 system-ui, sans-serif; }\n.breakpoint-dialog::backdrop { background: #0002; }\n.breakpoint-dialog h2 { font-size: 13px; margin: 0 0 10px; overflow-wrap: anywhere; }\n.breakpoint-dialog select, .breakpoint-dialog input { width: 100%; border: 1px solid #bbc3cf; padding: 4px; margin: 0 0 7px; background: white; }\n.dialog-actions { display: flex; gap: 6px; justify-content: flex-end; margin-top: 7px; }\n.dialog-actions button { padding: 3px 10px; border: 1px solid #bbc3cf; border-radius: 3px; background: #f1f3f5; }\n.heading-label { flex: 1; }\n.properties-heading .breakpoint-count { width: auto; padding: 0 5px; margin-right: 5px; font-size: 11px; color: #b04a35; }\n.breakpoint-entry { display: flex; align-items: center; gap: 6px; padding: 4px 0; border-bottom: 1px solid #d1d7df; }\n.breakpoint-entry span { flex: 1; overflow-wrap: anywhere; }\n.breakpoint-entry button { background: transparent; border: 0; font-size: 17px; }\n[hidden] { display: none !important; }\n.targetbar { flex: none; height: 28px; padding: 2px 6px; background: #edf2fa; border-bottom: 1px solid #dadce0; }\n.targetbar select { width: 100%; height: 23px; border: 0; background: transparent; font-size: 11px; }\n.styles-panel { flex: 1; min-height: 0; overflow: auto; background: #fff; font-size: 11px; }\n.styles-toolbar { position: sticky; top: 0; z-index: 1; display: flex; flex-wrap: wrap; gap: 6px; align-items: center; padding: 6px 8px; background: #f1f3f5; border-bottom: 1px solid #d1d7df; }\n.styles-toolbar span { flex: 1; font-weight: 600; }\n.styles-toolbar button, .styles-toolbar a { padding: 3px 7px; border: 1px solid #c5cdda; border-radius: 3px; background: white; }\n.styles-toolbar a { color: inherit; text-decoration: none; }\n.styles-toolbar a[aria-disabled=true] { cursor: default; opacity: .65; }\n.styles-toolbar a:focus-visible { outline: 1px solid #3574cb; outline-offset: -1px; }\n.styles-help, .styles-empty, .styles-status { margin: 0; padding: 7px 9px; color: #697687; }\n.styles-status { color: #a12121; }\n.styles-status:empty { display: none; }\n.styles-group h3 { margin: 0; padding: 6px 9px; font-size: 11px; background: #edf2fa; border-block: 1px solid #d1d7df; }\n.style-row { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1fr) 25px 23px; align-items: center; gap: 8px; min-width: 0; padding: 3px 9px; border-bottom: 1px solid #e8ebef; }\n.style-row label { min-width: 0; font: 11px/1.4 ui-monospace, monospace; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }\n.style-row input { min-width: 0; width: 100%; height: 23px; padding: 2px 5px; border: 1px solid #c5cdda; border-radius: 3px; background: white; }\n.style-row input[type=color] { width: 25px; padding: 1px; cursor: pointer; }\n.style-row button { grid-column: 4; width: 23px; height: 23px; padding: 0; border: 1px solid #c5cdda; border-radius: 3px; background: #f5f7f9; font-size: 15px; }\n\n:host([embedded]) { display: block; height: 100%; min-height: 0; }\n:host([embedded]) > div { height: 100%; }\n.embedded-panel { display: flex; flex-direction: column; height: 100%; min-height: 0; overflow: hidden; }\n";
336
417
 
337
418
  // ../tools/poc/gramlot-poc-tree.js
338
419
  import { Bag as Bag2 } from "@gramlot/gramlot";
@@ -341,7 +422,7 @@ import { Bag as Bag2 } from "@gramlot/gramlot";
341
422
  var gramlot_poc_tree_default = "/* Copyright 2025-2026 Softwell S.r.l. - SPDX-License-Identifier: Apache-2.0\n * Inspector subset of the DevTools storeTree stylesheet and inspector.css variables. */\n:host { display:block; font:11px/18px -apple-system,BlinkMacSystemFont,'Segoe UI',sans-serif; color:#38414b; }\n:host([hidden]) { display:none; }\nul { list-style:none; margin:0; padding-left:17px; }\n.tree-root { padding-left:16px; }\n.tree-root > ul { padding-left:2px; }\nsummary, .leaf { position:relative; }\nli { line-height:18px; }\nsummary { display:flex; align-items:center; gap:7px; padding:0 5px; cursor:pointer; user-select:none; list-style:none; }\nsummary::-webkit-details-marker { display:none; }\nsummary::before { content:''; width:5px; height:5px; border-right:1.5px solid #78818a; border-bottom:1.5px solid #78818a; transform:rotate(-45deg); flex:none; margin:0 3px; }\ndetails[open] > summary::before { transform:rotate(45deg); }\n.leaf { display:flex; align-items:center; gap:7px; padding:0 5px 0 23px; cursor:pointer; }\nsummary:hover,.leaf:hover { background:#f0f3f6; }\n.selected,.selected:hover { background:transparent; color:#315dbd; font-weight:700; border-radius:2px; }\n.caption { flex:1; min-width:0; overflow:hidden; text-overflow:ellipsis; white-space:nowrap; }\n/* Reserve a left gutter; showing an action must not shift the node label. */\n.actions { position:absolute; left:-14px; top:0; display:inline-flex; opacity:0; pointer-events:none; }\nsummary:hover > .actions,.leaf:hover > .actions,summary:focus-visible > .actions,.leaf:focus-visible > .actions,\n.actions:has(button:focus-visible),.actions:has(button.active) { opacity:1; pointer-events:auto; }\n.actions button { border:0; background:transparent; color:#80868b; font:inherit; font-size:11px; line-height:18px; width:14px; height:18px; padding:0; cursor:pointer; border-radius:3px; }\n.actions button:hover { background:#e1e5ed; }\n.actions button[data-action=breakpoint] { font-size:0; }\n.actions button[data-action=breakpoint]::before { content:''; display:inline-block; box-sizing:border-box; width:8px; height:8px; border:1.5px solid currentColor; border-radius:50%; vertical-align:middle; }\n.actions button.active { color:#b3261e; }\n.actions button.active.conditional { color:#f9ab00; }\n.actions button.active[data-action=breakpoint]::before { background:currentColor; }\n.actions button:focus-visible,summary:focus-visible,.leaf:focus-visible { outline:2px solid #356f9f; outline-offset:-2px; }\n";
342
423
 
343
424
  // ../tools/poc/gramlot-poc-tree.js
344
- var sequence2 = 0;
425
+ var sequence = 0;
345
426
  var GramlotPocTree = class extends HTMLElement {
346
427
  constructor() {
347
428
  super();
@@ -357,7 +438,7 @@ var GramlotPocTree = class extends HTMLElement {
357
438
  this._nodes = /* @__PURE__ */ new Map();
358
439
  this._rowActions = [];
359
440
  this._pendingRender = null;
360
- this._subId = `gramlot-poc-tree-${++sequence2}`;
441
+ this._subId = `gramlot-poc-tree-${++sequence}`;
361
442
  }
362
443
  get storeBag() {
363
444
  return this._store;
@@ -683,6 +764,10 @@ var GramlotToolsInspector = class extends HTMLElement {
683
764
  #breakpointTests = /* @__PURE__ */ new Map();
684
765
  #trees = {};
685
766
  #breakpointTarget = null;
767
+ #styles = null;
768
+ #styleNames = "";
769
+ #styleRequest = 0;
770
+ #styleDownloadUrl = null;
686
771
  constructor() {
687
772
  super();
688
773
  this.#mount = this.ownerDocument.createElement("div");
@@ -692,7 +777,7 @@ var GramlotToolsInspector = class extends HTMLElement {
692
777
  return this.#target;
693
778
  }
694
779
  set gramlot(value) {
695
- if (value !== null && (!(value instanceof Gramlot2) || value.state === "disposed")) {
780
+ if (value !== null && (!(value instanceof Gramlot) || value.state === "disposed")) {
696
781
  throw new TypeError("gramlot must be a live Gramlot instance or null");
697
782
  }
698
783
  this.#unsubscribe();
@@ -716,6 +801,7 @@ var GramlotToolsInspector = class extends HTMLElement {
716
801
  this.#ui.data.setItem("breakpointStatus", "");
717
802
  this.#subscribe();
718
803
  this.#refresh();
804
+ if (this.#ui.data.getItem("tab") === "styles") this.#loadStyles();
719
805
  }
720
806
  }
721
807
  get targets() {
@@ -758,6 +844,7 @@ var GramlotToolsInspector = class extends HTMLElement {
758
844
  this.#ui.data.setItem("breakpointDirty", false);
759
845
  this.#subscribe();
760
846
  this.#refresh();
847
+ if (this.#ui.data.getItem("tab") === "styles") this.#loadStyles();
761
848
  }
762
849
  }
763
850
  #renderTargets() {
@@ -777,7 +864,7 @@ var GramlotToolsInspector = class extends HTMLElement {
777
864
  connectedCallback() {
778
865
  if (this.#ui) return;
779
866
  const inspector = this;
780
- const ui = new Gramlot2({ document: this.ownerDocument, element: this.#mount, transport: false });
867
+ const ui = new Gramlot({ document: this.ownerDocument, element: this.#mount, transport: false });
781
868
  this.#ui = ui;
782
869
  class Logic {
783
870
  close(node, event) {
@@ -844,14 +931,47 @@ var GramlotToolsInspector = class extends HTMLElement {
844
931
  }
845
932
  selectRoot(node) {
846
933
  const choose = () => {
847
- ui.data.setItem("root", node.getAttr("data-root"));
848
- ui.data.setItem("selection", null);
849
- inspector.#snapshot = null;
934
+ const tab = node.getAttr("data-root");
935
+ ui.data.setItem("tab", tab);
936
+ if (tab !== "styles") {
937
+ ui.data.setItem("root", tab);
938
+ ui.data.setItem("selection", null);
939
+ inspector.#snapshot = null;
940
+ }
850
941
  inspector.#refresh();
942
+ if (tab === "styles") inspector.#loadStyles();
851
943
  };
852
944
  if (ui.data.getItem("editor.dirty")) inspector.#applyEdit(choose);
853
945
  else choose();
854
946
  }
947
+ refreshStyles() {
948
+ inspector.#loadStyles();
949
+ }
950
+ styleChanged(node) {
951
+ ui.data.setItem(`styles.rows.${node.getAttr("data-row")}.dirty`, true);
952
+ }
953
+ setStyle(node) {
954
+ const row = ui.data.getItem(`styles.rows.${node.getAttr("data-row")}`);
955
+ if (node.getAttr("data-field") === "color") row.setItem("dirty", false);
956
+ else if (!row.getItem("dirty")) return;
957
+ inspector.#loadStyles("setStyle", { name: row.getItem("name"), value: row.getItem(node.getAttr("data-field")) });
958
+ }
959
+ styleKey(node, event) {
960
+ if (event.key === "Enter" && !event.isComposing) {
961
+ event.preventDefault();
962
+ this.setStyle(node);
963
+ }
964
+ if (event.key === "Escape") {
965
+ event.preventDefault();
966
+ inspector.#loadStyles();
967
+ }
968
+ }
969
+ resetStyle(node) {
970
+ inspector.#loadStyles("resetStyle", { name: ui.data.getItem(`styles.rows.${node.getAttr("data-row")}.name`) });
971
+ }
972
+ resetStyles() {
973
+ inspector.#loadStyles("resetStyles");
974
+ }
855
975
  selectNode(node, event) {
856
976
  const choose = () => {
857
977
  const entry = inspector.#nodes.find((item) => item.node === event.detail.node);
@@ -914,6 +1034,9 @@ var GramlotToolsInspector = class extends HTMLElement {
914
1034
  ui.src.logicRegistry.register(Logic, { group: "inspector", resource: "gramlot-tools-inspector" });
915
1035
  for (const [key, value] of Object.entries({
916
1036
  root: "data",
1037
+ tab: "data",
1038
+ stylesActive: false,
1039
+ treeActive: true,
917
1040
  dataActive: true,
918
1041
  sourceActive: false,
919
1042
  targetId: "",
@@ -933,14 +1056,15 @@ var GramlotToolsInspector = class extends HTMLElement {
933
1056
  ui.data.setItem("editor", new Bag3({ dirty: false, disabled: true, status: "", mode: "", condition: "" }));
934
1057
  ui.data.setItem("breakpoints", new Bag3());
935
1058
  ui.data.setItem("breakpointDirty", false);
1059
+ ui.data.setItem("styles", new Bag3({ status: "", busy: false, noChanges: true, noDownload: true, downloadUrl: null, rows: new Bag3() }));
936
1060
  const builder = new GramlotBuilder();
937
1061
  const root = builder.root;
938
1062
  root.style(gramlot_tools_inspector_default);
939
- const palette = root.webcomponent({ code: "gramlot-poc-palette", node_label: "palette", title: "Gramlot inspector", connect_onclose: "inspector.close" });
1063
+ const palette = this.hasAttribute("embedded") ? root.section({ node_label: "palette", class: "embedded-panel", "aria-label": "Gramlot inspector" }) : root.webcomponent({ code: "gramlot-poc-palette", node_label: "palette", title: "Gramlot inspector", connect_onclose: "inspector.close" });
940
1064
  const targetbar = palette.div({ node_label: "targetbar", class: "targetbar", hidden: "^singleTarget" });
941
1065
  targetbar.select({ node_label: "targets", value: "^targetId", disabled: "^busy", "aria-label": "Inspected page", connect_onchange: "inspector.selectTarget" });
942
1066
  const toolbar = palette.webcomponent({ code: "gramlot-poc-tabs", node_label: "tabs", role: "tablist", "aria-label": "Inspected root" });
943
- for (const name of ["Data", "Source"]) toolbar.button(name, {
1067
+ for (const name of ["Data", "Source", "Styles"]) toolbar.button(name, {
944
1068
  node_label: name.toLowerCase(),
945
1069
  type: "button",
946
1070
  role: "tab",
@@ -949,7 +1073,7 @@ var GramlotToolsInspector = class extends HTMLElement {
949
1073
  connect_onclick: "inspector.selectRoot"
950
1074
  });
951
1075
  palette.p("^error", { role: "alert", class: "error" });
952
- const content = palette.webcomponent({ code: "gramlot-poc-splitter", node_label: "content" });
1076
+ const content = palette.webcomponent({ code: "gramlot-poc-splitter", node_label: "content", hidden: "^stylesActive" });
953
1077
  const trees = content.div({ node_label: "tree", slot: "top", class: "tree" });
954
1078
  for (const name of ["data", "source"]) trees.webcomponent({
955
1079
  code: "gramlot-poc-tree",
@@ -983,7 +1107,17 @@ var GramlotToolsInspector = class extends HTMLElement {
983
1107
  const editor = properties.fieldset({ node_label: "editor", disabled: "^editor.disabled" });
984
1108
  editor.div({ node_label: "rows", class: "rows" });
985
1109
  properties.p("^editor.status", { role: "status", class: "edit-status" });
986
- const footer = palette.footer({ class: "path" });
1110
+ const styles = palette.section({ node_label: "styles", class: "styles-panel", hidden: "^treeActive", "aria-label": "CSS variables" });
1111
+ const stylebar = styles.div({ class: "styles-toolbar" });
1112
+ stylebar.span("Document variables");
1113
+ stylebar.button("Refresh", { type: "button", disabled: "^styles.busy", connect_onclick: "inspector.refreshStyles" });
1114
+ stylebar.button("Reset all", { type: "button", disabled: "^styles.noChanges", connect_onclick: "inspector.resetStyles" });
1115
+ stylebar.a("Download CSS", { href: "^styles.downloadUrl", download: "theme-overrides.css", "aria-disabled": "^styles.noDownload" });
1116
+ styles.p("Temporary preview \xB7 Enter or leave a field to apply. Closing the connection clears previews.", { class: "styles-help" });
1117
+ styles.p("^styles.status", { role: "status", class: "styles-status" });
1118
+ styles.p("No --gramlot-* variables on this document root. Load a Gramlot theme, then Refresh.", { class: "styles-empty", node_label: "empty" });
1119
+ styles.div({ node_label: "rows", class: "styles-rows" });
1120
+ const footer = palette.footer({ class: "path", hidden: "^stylesActive" });
987
1121
  footer.span("Path: ");
988
1122
  footer.em("^path");
989
1123
  const breakpoint = palette.dialog({ node_label: "breakpoint", "aria-label": "Breakpoint", class: "breakpoint-dialog", connect_oncancel: "inspector.closeBreakpoint" });
@@ -1031,6 +1165,14 @@ var GramlotToolsInspector = class extends HTMLElement {
1031
1165
  } });
1032
1166
  }
1033
1167
  #unsubscribe() {
1168
+ ++this.#styleRequest;
1169
+ this.#styles?.dispose();
1170
+ this.#styles = null;
1171
+ if (this.#ui) {
1172
+ this.#renderStyles([]);
1173
+ this.#ui.data.setItem("styles.busy", false);
1174
+ this.#ui.data.setItem("styles.status", "");
1175
+ }
1034
1176
  this.#connectionRelease?.();
1035
1177
  this.#connectionRelease = null;
1036
1178
  ++this.#generation;
@@ -1057,6 +1199,12 @@ var GramlotToolsInspector = class extends HTMLElement {
1057
1199
  try {
1058
1200
  this.#nodes = [];
1059
1201
  const root = ui.data.getItem("root");
1202
+ const tab = ui.data.getItem("tab");
1203
+ ui.data.setItem("stylesActive", tab === "styles");
1204
+ ui.data.setItem("treeActive", tab !== "styles");
1205
+ for (const name of ["data", "source", "styles"]) {
1206
+ ui.src.source.getNode(`main.palette.tabs.${name}`).setAttr({ "aria-selected": String(tab === name) });
1207
+ }
1060
1208
  if (this.#connection) {
1061
1209
  const configurations = new Bag3();
1062
1210
  for (const [index, record] of this.#connection.breakpoints.entries()) configurations.setItem(`b${index}`, new Bag3(record));
@@ -1069,7 +1217,6 @@ var GramlotToolsInspector = class extends HTMLElement {
1069
1217
  ui.data.setItem("dataActive", root === "data");
1070
1218
  ui.data.setItem("sourceActive", root === "source");
1071
1219
  for (const name of ["data", "source"]) {
1072
- ui.src.source.getNode(`main.palette.tabs.${name}`).setAttr({ "aria-selected": String(root === name) });
1073
1220
  const tree = this.#trees[name];
1074
1221
  tree.storeBag = this.#connection || this.#target ? this.#targetBag(name) : null;
1075
1222
  const breakpoints = ui.data.getItem("breakpoints").getNodes().filter((node) => node.value.getItem("root") === name);
@@ -1089,6 +1236,132 @@ var GramlotToolsInspector = class extends HTMLElement {
1089
1236
  ui.data.setItem("error", error.message);
1090
1237
  }
1091
1238
  }
1239
+ async #loadStyles(type = "styles", command = {}) {
1240
+ const ui = this.#ui;
1241
+ const request = ++this.#styleRequest;
1242
+ ui.data.setItem("styles.busy", true);
1243
+ ui.data.setItem("styles.downloadUrl", null);
1244
+ ui.data.setItem("styles.noDownload", true);
1245
+ ui.data.setItem("styles.status", "");
1246
+ try {
1247
+ if (!this.#state() || this.#state() === "disposed" || this.#connection && this.#state() !== "started") {
1248
+ throw new Error("Select a connected page before editing CSS variables");
1249
+ }
1250
+ let rows;
1251
+ if (this.#connection) {
1252
+ const connection = this.#connection;
1253
+ await connection.request(type, command);
1254
+ rows = connection.styles;
1255
+ } else {
1256
+ this.#styles ??= new InspectorStyles(this.#target.src.renderer.destination.ownerDocument);
1257
+ if (type === "setStyle") this.#styles.set(command.name, command.value);
1258
+ if (type === "resetStyle") this.#styles.reset(command.name);
1259
+ if (type === "resetStyles") this.#styles.dispose();
1260
+ rows = this.#styles.snapshot();
1261
+ }
1262
+ if (this.#ui !== ui || request !== this.#styleRequest) return;
1263
+ this.#renderStyles(rows, type, command);
1264
+ } catch (error) {
1265
+ if (this.#ui === ui && request === this.#styleRequest) ui.data.setItem("styles.status", error.message);
1266
+ } finally {
1267
+ if (this.#ui === ui && request === this.#styleRequest) {
1268
+ ui.data.setItem("styles.busy", false);
1269
+ ui.data.setItem("styles.downloadUrl", this.#styleDownloadUrl);
1270
+ ui.data.setItem("styles.noDownload", !this.#styleDownloadUrl);
1271
+ }
1272
+ }
1273
+ }
1274
+ #renderStyles(rows, type = "styles", command = {}) {
1275
+ const ui = this.#ui;
1276
+ const browser = this.ownerDocument.defaultView;
1277
+ if (this.#styleDownloadUrl) browser.URL.revokeObjectURL(this.#styleDownloadUrl);
1278
+ this.#styleDownloadUrl = null;
1279
+ const overrides = rows.filter((row) => row.override);
1280
+ if (overrides.length) {
1281
+ const declarations = overrides.map((row) => ` ${browser.CSS.escape(row.name)}: ${row.override} !important;`).join("\n");
1282
+ const css = `/* Gramlot overrides. Load after the theme stylesheet. */
1283
+ :root {
1284
+ ${declarations}
1285
+ }
1286
+ `;
1287
+ this.#styleDownloadUrl = browser.URL.createObjectURL(new browser.Blob([css], { type: "text/css;charset=utf-8" }));
1288
+ }
1289
+ ui.data.setItem("styles.downloadUrl", ui.data.getItem("styles.busy") ? null : this.#styleDownloadUrl);
1290
+ ui.data.setItem("styles.noDownload", ui.data.getItem("styles.busy") || !this.#styleDownloadUrl);
1291
+ const names = JSON.stringify(rows.map((row) => row.name));
1292
+ const rebuild = names !== this.#styleNames;
1293
+ this.#styleNames = names;
1294
+ const builder = rebuild ? new GramlotBuilder() : null;
1295
+ const groups = /* @__PURE__ */ new Map();
1296
+ if (rebuild) {
1297
+ ui.src.source.getNode("main.palette.styles.rows").setValue(ui.src.prepareSource(new GramlotBuilder().source));
1298
+ ui.data.setItem("styles.rows", new Bag3());
1299
+ }
1300
+ for (const [index, record] of rows.entries()) {
1301
+ const id = `v${index}`, path = `styles.rows.${id}`;
1302
+ const color = /^#[\da-f]{6}$/i.test(record.value) ? record.value : /^#[\da-f]{3}$/i.test(record.value) ? "#" + [...record.value.slice(1)].map((char) => char + char).join("") : null;
1303
+ const fields = {
1304
+ name: record.name,
1305
+ text: record.override || record.value,
1306
+ computed: record.value,
1307
+ dirty: false,
1308
+ color: color ?? "#000000",
1309
+ noColor: color === null,
1310
+ unchanged: !record.override
1311
+ };
1312
+ if (!rebuild && ui.data.getItem(`${path}.dirty`) && !["styles", "resetStyles"].includes(type)) {
1313
+ const acknowledged = record.name === command.name && (type === "resetStyle" || ui.data.getItem(`${path}.text`) === command.value);
1314
+ if (!acknowledged) {
1315
+ delete fields.text;
1316
+ delete fields.dirty;
1317
+ }
1318
+ }
1319
+ if (rebuild) ui.data.setItem(path, new Bag3(fields));
1320
+ else for (const [key, value] of Object.entries(fields)) ui.data.setItem(`${path}.${key}`, value);
1321
+ if (!rebuild) continue;
1322
+ const group = record.name.includes("-font-") ? "Typography" : record.name.includes("-space-") ? "Spacing" : /-(radius|page-width)/.test(record.name) ? "Dimensions" : record.name.endsWith("-shadow") ? "Effects" : color ? "Colors" : "Other";
1323
+ if (!groups.has(group)) {
1324
+ const section = builder.root.section({ class: "styles-group" });
1325
+ section.h3(group);
1326
+ groups.set(group, section);
1327
+ }
1328
+ const row = groups.get(group).div({ class: "style-row" });
1329
+ row.html_label(record.name, { for: `${this.#subscriber}-${id}`, title: record.name });
1330
+ row.input({
1331
+ id: `${this.#subscriber}-${id}`,
1332
+ value: `^${path}.text`,
1333
+ live: true,
1334
+ "aria-label": `${record.name} value`,
1335
+ title: `^${path}.computed`,
1336
+ "data-row": id,
1337
+ "data-field": "text",
1338
+ connect_oninput: "inspector.styleChanged",
1339
+ connect_onchange: "inspector.setStyle",
1340
+ connect_onkeydown: "inspector.styleKey",
1341
+ spellcheck: "false"
1342
+ });
1343
+ row.input({
1344
+ type: "color",
1345
+ value: `^${path}.color`,
1346
+ hidden: `^${path}.noColor`,
1347
+ "aria-label": `${record.name} color`,
1348
+ "data-row": id,
1349
+ "data-field": "color",
1350
+ connect_onchange: "inspector.setStyle"
1351
+ });
1352
+ row.button("\u21B6", {
1353
+ type: "button",
1354
+ "aria-label": `Reset ${record.name}`,
1355
+ title: "Reset variable",
1356
+ disabled: `^${path}.unchanged`,
1357
+ "data-row": id,
1358
+ connect_onclick: "inspector.resetStyle"
1359
+ });
1360
+ }
1361
+ if (rebuild) ui.src.source.getNode("main.palette.styles.rows").setValue(ui.src.prepareSource(builder.source));
1362
+ ui.data.setItem("styles.noChanges", !rows.some((row) => row.override));
1363
+ ui.src.source.getNode("main.palette.styles.empty").setAttr({ hidden: rows.length !== 0 });
1364
+ }
1092
1365
  #index(bag, labels) {
1093
1366
  for (const node of bag.getNodes()) {
1094
1367
  const path = [...labels, node.label];
@@ -1182,8 +1455,8 @@ var GramlotToolsInspector = class extends HTMLElement {
1182
1455
  const label = line.div({ class: "property-name" });
1183
1456
  if (fresh) label.input({ value: "^.name", live: true, "aria-label": "New attribute name", "data-row": row.label, connect_oninput: "inspector.editChanged" });
1184
1457
  else label.span(name);
1185
- const cell2 = line.div({ class: "property-value" });
1186
- cell2.input({
1458
+ const cell = line.div({ class: "property-value" });
1459
+ cell.input({
1187
1460
  value: "^.text",
1188
1461
  type: "^.inputType",
1189
1462
  live: true,
@@ -1196,11 +1469,11 @@ var GramlotToolsInspector = class extends HTMLElement {
1196
1469
  connect_onkeydown: "inspector.editKey"
1197
1470
  });
1198
1471
  if (fresh) {
1199
- const type = cell2.select({ value: "^.type", "aria-label": "New attribute type", "data-row": row.label, connect_onchange: "inspector.typeChanged" });
1472
+ const type = cell.select({ value: "^.type", "aria-label": "New attribute type", "data-row": row.label, connect_onchange: "inspector.typeChanged" });
1200
1473
  for (const value of editorTypes) type.option(value, { value });
1201
1474
  }
1202
1475
  if (!primary) {
1203
- const button = cell2.button({
1476
+ const button = cell.button({
1204
1477
  type: "button",
1205
1478
  disabled: locked,
1206
1479
  "aria-label": `Remove ${name || "new attribute"}`,