@gramlot/gramlot 0.3.1 → 0.3.3

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.
@@ -53,6 +53,241 @@ function readParameterEnums(node) {
53
53
  return Object.fromEntries(Object.entries(node._getMeta("gramlot_component")?.parameters ?? {}).filter(([, spec]) => spec.enum).map(([name, spec]) => [name, spec.enum]));
54
54
  }
55
55
 
56
+ // ../tools/gramlot-tools-inspector/src/inspector-history.js
57
+ var InspectorHistory = class {
58
+ #undo = [];
59
+ #redo = [];
60
+ #references = /* @__PURE__ */ new WeakMap();
61
+ constructor(gramlot) {
62
+ this.gramlot = gramlot;
63
+ this.Source = gramlot.src.source.constructor;
64
+ this.Bag = gramlot.data.constructor;
65
+ }
66
+ get state() {
67
+ return { undo: this.#undo.at(-1)?.label ?? "", redo: this.#redo.at(-1)?.label ?? "" };
68
+ }
69
+ clear() {
70
+ this.#undo = [];
71
+ this.#redo = [];
72
+ }
73
+ #ref(node) {
74
+ if (!this.#references.has(node)) this.#references.set(node, { node });
75
+ return this.#references.get(node);
76
+ }
77
+ #bind(ref, node) {
78
+ ref.node = node;
79
+ this.#references.set(node, ref);
80
+ }
81
+ #capture(value) {
82
+ if (!(value instanceof this.Bag)) return value;
83
+ return value.getNodes().map((node) => [
84
+ this.#ref(node),
85
+ node.nodeTag,
86
+ node.resolver,
87
+ Object.entries(node.getAttr()).sort(([a], [b]) => a.localeCompare(b)),
88
+ this.#capture(node.getValue(true))
89
+ ]);
90
+ }
91
+ #equal(a, b) {
92
+ return Object.is(a, b) || Array.isArray(a) && Array.isArray(b) && a.length === b.length && a.every((v, i) => this.#equal(v, b[i]));
93
+ }
94
+ #commit(label, capture, redo, undo) {
95
+ const before = capture();
96
+ let error;
97
+ try {
98
+ redo();
99
+ } catch (caught) {
100
+ error = caught;
101
+ }
102
+ const after = capture();
103
+ if (!this.#equal(before, after)) {
104
+ this.#undo.push({ label, capture, before, after, undo, redo });
105
+ this.#redo = [];
106
+ }
107
+ if (error) throw error;
108
+ }
109
+ #step(from, to, direction, expected, result) {
110
+ const action = from.at(-1);
111
+ if (!action) return;
112
+ if (this.gramlot.state === "disposed") throw new Error("Inspected page disconnected");
113
+ if (!this.#equal(action.capture(), action[expected])) throw new Error("The target changed outside the inspector. Undo/Redo would overwrite those changes.");
114
+ let error;
115
+ try {
116
+ action[direction]();
117
+ } catch (caught) {
118
+ error = caught;
119
+ }
120
+ if (this.#equal(action.capture(), action[result])) {
121
+ from.pop();
122
+ to.push(action);
123
+ } else if (!error) error = new Error("The application changed the target during Undo/Redo.");
124
+ if (error) throw error;
125
+ }
126
+ undo() {
127
+ this.#step(this.#undo, this.#redo, "undo", "after", "before");
128
+ }
129
+ redo() {
130
+ this.#step(this.#redo, this.#undo, "redo", "before", "after");
131
+ }
132
+ edit(node, attrs, value, changeAttrs, changeValue) {
133
+ const ref = this.#ref(node), oldAttrs = { ...node.getAttr() }, oldValue = node.getValue(true);
134
+ const capture = () => [ref.node.parentBag, this.#captureNode(ref.node)];
135
+ const write = (attributes, content) => {
136
+ if (changeAttrs) ref.node.setAttr(attributes, true, false, false);
137
+ if (changeValue) ref.node.setValue(content);
138
+ };
139
+ this.#commit("Edit value or attributes", capture, () => write(attrs, value), () => write(oldAttrs, oldValue));
140
+ }
141
+ #captureNode(node) {
142
+ return [node.resolver, Object.entries(node.getAttr()).sort(([a], [b]) => a.localeCompare(b)), this.#capture(node.getValue(true))];
143
+ }
144
+ #sourceCapture = () => this.#capture(this.gramlot.src.source);
145
+ #node(path) {
146
+ if (typeof path !== "string" || !path) throw new Error("Select a Source node");
147
+ const node = this.gramlot.src.source.getNode(path);
148
+ if (!node || node.resolver) throw new Error("Source node is missing or deferred");
149
+ return node;
150
+ }
151
+ #removable(node) {
152
+ if (node.parentBag === this.gramlot.src.source) throw new Error("The page Source root cannot be removed or moved");
153
+ }
154
+ #saved(node) {
155
+ return { ref: this.#ref(node), label: node.label, tag: node.nodeTag, value: node.getValue(true), attrs: { ...node.getAttr() } };
156
+ }
157
+ #restore(saved, bag, index) {
158
+ if (bag.getNode(saved.label)) throw new Error(`Source label already exists: ${saved.label}`);
159
+ try {
160
+ bag.setItem(saved.label, saved.value, saved.attrs, index, false, false, "inspector", false, true, null, saved.tag);
161
+ } finally {
162
+ const inserted = bag.getNode(saved.label);
163
+ if (inserted) this.#bind(saved.ref, inserted);
164
+ }
165
+ }
166
+ #parentState(parent) {
167
+ return { value: parent.getValue(true), attrs: { ...parent.getAttr() } };
168
+ }
169
+ #setParent(parent, state) {
170
+ parent.setValue(state.value, true, state.attrs, false);
171
+ parent.setAttr(state.attrs, true, false, false);
172
+ }
173
+ childFields(parent) {
174
+ const specifications = parent._getMeta("gramlot_component")?.childParameters ?? {};
175
+ return Object.entries(specifications).filter(([, spec]) => spec.required).map(([name, spec]) => {
176
+ const children = parent.getValue(true);
177
+ const used = spec.unique && children instanceof this.Source ? children.getNodes().map((child) => child.getAttr(name)) : [];
178
+ return { name, choices: spec.enum.filter((value) => !used.includes(value)) };
179
+ });
180
+ }
181
+ validateChild(parent, attrs, moving = null) {
182
+ const specifications = parent._getMeta("gramlot_component")?.childParameters ?? {};
183
+ for (const [name, spec] of Object.entries(specifications)) {
184
+ const value = attrs[name];
185
+ if (spec.required && value == null) throw new Error(`Choose ${name} before inserting the child`);
186
+ if (spec.enum && value != null && !spec.enum.includes(value)) throw new Error(`Invalid ${name}: ${value}`);
187
+ const children = parent.getValue(true);
188
+ if (spec.unique && children instanceof this.Source && children.getNodes().some((child) => child !== moving && child.getAttr(name) === value)) {
189
+ throw new Error(`${name} is already in use: ${value}`);
190
+ }
191
+ }
192
+ }
193
+ catalog(path) {
194
+ const node = this.#node(path), builder = node.builder;
195
+ return Object.entries(builder.schema).filter(([, info]) => !info._meta?.data_element && !info._meta?.subbuilder && !info._meta?.container).map(([tag, info]) => ({ tag, kind: info._meta?.gramlot_component ? "component" : "html", fields: this.childFields(node) }));
196
+ }
197
+ insert(path, requestedTag, attrs = {}) {
198
+ const parent = this.#node(path), builder = parent.builder;
199
+ const tag = builder.schemaTag(requestedTag);
200
+ if (!this.catalog(path).some((entry) => entry.tag === tag)) throw new Error("Choose an available component or HTML tag");
201
+ this.validateChild(parent, attrs);
202
+ const previous = this.#parentState(parent), parentRef = this.#ref(parent);
203
+ let saved, promoted;
204
+ const redo = () => {
205
+ if (saved) {
206
+ if (!(previous.value instanceof this.Source)) this.#setParent(parentRef.node, promoted);
207
+ this.#restore(saved, parentRef.node.getValue(true), parentRef.node.getValue(true).getNodes().length);
208
+ return;
209
+ }
210
+ const existing = previous.value instanceof this.Source ? new Set(previous.value.getNodes()) : /* @__PURE__ */ new Set();
211
+ try {
212
+ builder.commandOnNode(parentRef.node, tag, null, attrs);
213
+ } finally {
214
+ const body = parentRef.node.getValue(true);
215
+ if (body instanceof this.Source) {
216
+ const added = body.getNodes().find((node) => !existing.has(node));
217
+ if (added) {
218
+ saved = this.#saved(added);
219
+ promoted = this.#parentState(parentRef.node);
220
+ }
221
+ }
222
+ }
223
+ };
224
+ const undo = () => {
225
+ if (saved) saved.ref.node.parentBag.popNode(saved.ref.node.label, "inspector");
226
+ if (!(previous.value instanceof this.Source)) this.#setParent(parentRef.node, previous);
227
+ };
228
+ this.#commit("Add element", this.#sourceCapture, redo, undo);
229
+ return `${path}.${saved.label}`;
230
+ }
231
+ remove(path) {
232
+ const node = this.#node(path);
233
+ this.#removable(node);
234
+ const saved = this.#saved(node), bag = node.parentBag, index = bag.getNodes().indexOf(node);
235
+ this.#commit(
236
+ "Remove branch",
237
+ this.#sourceCapture,
238
+ () => saved.ref.node.parentBag.popNode(saved.label, "inspector"),
239
+ () => this.#restore(saved, bag, index)
240
+ );
241
+ return path.slice(0, path.lastIndexOf("."));
242
+ }
243
+ move(path, destination, placement) {
244
+ if (!["before", "inside", "after"].includes(placement)) throw new Error("Invalid drop position");
245
+ const node = this.#node(path), target = this.#node(destination);
246
+ this.#removable(node);
247
+ if (path === destination || destination.startsWith(path + ".")) throw new Error("A branch cannot be moved into itself");
248
+ const parent = placement === "inside" ? target : target.parentBag.parentNode;
249
+ if (!parent) throw new Error("Drop inside the page Source root");
250
+ const builder = parent.builder;
251
+ if (node.builder !== builder) throw new Error("Branches cannot cross builder dialects");
252
+ this.validateChild(parent, node.getAttr(), node);
253
+ const parentRef = this.#ref(parent);
254
+ const previous = this.#parentState(parent), origin = node.parentBag, oldIndex = origin.getNodes().indexOf(node);
255
+ const body = previous.value;
256
+ if (body instanceof this.Source && body !== origin && body.getNode(node.label)) throw new Error(`Source label already exists: ${node.label}`);
257
+ const children = body instanceof this.Source ? body.getNodes().filter((child) => child !== node) : [];
258
+ if (parent.nodeTag) {
259
+ builder.validateParent(node.nodeTag, parent.nodeTag);
260
+ builder.validateChildren(parent.nodeTag, [...children.map((child) => child.nodeTag), node.nodeTag]);
261
+ }
262
+ let index = placement === "inside" ? children.length : children.indexOf(target) + (placement === "after" ? 1 : 0);
263
+ if (index < 0) throw new Error("Drop target changed");
264
+ if (body === origin) {
265
+ const ref = this.#ref(node);
266
+ this.#commit("Move branch", this.#sourceCapture, () => origin.move(origin.getNodes().indexOf(ref.node), index), () => origin.move(origin.getNodes().indexOf(ref.node), oldIndex));
267
+ } else {
268
+ const saved = this.#saved(node);
269
+ const destinationBag = body instanceof this.Source ? body : new this.Source(null, builder);
270
+ let promoted;
271
+ this.#commit("Move branch", this.#sourceCapture, () => {
272
+ if (!(body instanceof this.Source)) {
273
+ if (promoted) this.#setParent(parentRef.node, promoted);
274
+ else {
275
+ builder.promoteNodeValue(parentRef.node, body, destinationBag);
276
+ promoted = this.#parentState(parentRef.node);
277
+ }
278
+ }
279
+ saved.ref.node.parentBag.popNode(saved.label, "inspector");
280
+ this.#restore(saved, destinationBag, index);
281
+ }, () => {
282
+ saved.ref.node.parentBag.popNode(saved.label, "inspector");
283
+ this.#restore(saved, origin, oldIndex);
284
+ if (!(body instanceof this.Source)) this.#setParent(parentRef.node, previous);
285
+ });
286
+ }
287
+ return `${placement === "inside" ? destination : destination.slice(0, destination.lastIndexOf("."))}.${node.label}`;
288
+ }
289
+ };
290
+
56
291
  // ../tools/gramlot-tools-inspector/src/inspector-styles.js
57
292
  var InspectorStyles = class {
58
293
  #document;
@@ -131,6 +366,7 @@ function attachInspectorAgent(gramlot, port) {
131
366
  const document = gramlot.src.renderer.destination.ownerDocument;
132
367
  const window = document.defaultView;
133
368
  const styles = new InspectorStyles(document);
369
+ const history = new InspectorHistory(gramlot);
134
370
  const subscriber = `inspector-bridge-${window.crypto.getRandomValues(new Uint32Array(4)).join("-")}`;
135
371
  const identities = /* @__PURE__ */ new WeakMap();
136
372
  const versions = /* @__PURE__ */ new WeakMap();
@@ -165,7 +401,8 @@ function attachInspectorAgent(gramlot, port) {
165
401
  const snapshot = () => ({
166
402
  data: describe(bag("data"), "data"),
167
403
  source: describe(bag("source"), "source"),
168
- breakpoints: [...breakpoints.values()].map(({ test, ...record }) => record)
404
+ breakpoints: [...breakpoints.values()].map(({ test, ...record }) => record),
405
+ history: history.state
169
406
  });
170
407
  const send = (data) => {
171
408
  if (!stopped) port.postMessage(data);
@@ -238,8 +475,7 @@ function attachInspectorAgent(gramlot, port) {
238
475
  if (node.resolver || !scalarType(node.getValue(true))) throw new Error("This value is read-only");
239
476
  value = decodeScalar2(command.value);
240
477
  }
241
- if (command.set.length || command.remove.length) node.setAttr(attrs, true, false, false);
242
- if (Object.hasOwn(command, "value")) node.setValue(value);
478
+ history.edit(node, attrs, value, !!(command.set.length || command.remove.length), Object.hasOwn(command, "value"));
243
479
  };
244
480
  const setBreakpoint = (command) => {
245
481
  bag(command.root);
@@ -265,10 +501,27 @@ function attachInspectorAgent(gramlot, port) {
265
501
  send({ type: "result", id: message.id, ok: true, styles: styles.snapshot() });
266
502
  return;
267
503
  }
268
- if (message.type === "edit") edit(message.command);
504
+ let selection;
505
+ if (message.type === "catalog") {
506
+ send({ type: "result", id: message.id, ok: true, catalog: history.catalog(message.command.path) });
507
+ return;
508
+ }
509
+ if (["insert", "remove", "move"].includes(message.type)) {
510
+ const command = message.command, node = bag("source").getNode(command.path);
511
+ if (!node || identity(node) !== command.nodeId || (versions.get(node) ?? 0) !== command.revision) throw new Error("Source node changed. Select it again.");
512
+ if (message.type === "insert") selection = history.insert(command.path, command.tag, command.attrs);
513
+ if (message.type === "remove") selection = history.remove(command.path);
514
+ if (message.type === "move") {
515
+ const target = bag("source").getNode(command.destination);
516
+ if (!target || identity(target) !== command.targetId || (versions.get(target) ?? 0) !== command.targetRevision) throw new Error("Drop target changed. Try again.");
517
+ selection = history.move(command.path, command.destination, command.placement);
518
+ }
519
+ } else if (message.type === "undo") history.undo();
520
+ else if (message.type === "redo") history.redo();
521
+ else if (message.type === "edit") edit(message.command);
269
522
  else if (message.type === "breakpoint") setBreakpoint(message.command);
270
523
  else if (message.type !== "refresh") throw new Error(`Unknown inspector command: ${message.type}`);
271
- send({ type: "result", id: message.id, ok: true, snapshot: snapshot() });
524
+ send({ type: "result", id: message.id, ok: true, selection, snapshot: snapshot() });
272
525
  } catch (error) {
273
526
  send({ type: "result", id: message.id, ok: false, error: error.message });
274
527
  }
@@ -279,6 +532,7 @@ function attachInspectorAgent(gramlot, port) {
279
532
  stopped = true;
280
533
  for (const root of roots) bag(root).unsubscribe(subscriber, { any: true });
281
534
  breakpoints.clear();
535
+ history.clear();
282
536
  styles.dispose();
283
537
  port.removeEventListener("message", receive);
284
538
  window.removeEventListener("pagehide", dispose);
@@ -312,6 +566,7 @@ var InspectorConnection = class {
312
566
  error = "";
313
567
  breakpoints = [];
314
568
  styles = null;
569
+ history = { undo: "", redo: "" };
315
570
  #info = /* @__PURE__ */ new WeakMap();
316
571
  #listeners = /* @__PURE__ */ new Set();
317
572
  #pending = /* @__PURE__ */ new Map();
@@ -343,6 +598,7 @@ var InspectorConnection = class {
343
598
  if (message.snapshot) {
344
599
  for (const root of roots2) this.#reconcile(this[root], message.snapshot[root]);
345
600
  this.breakpoints = message.snapshot.breakpoints;
601
+ this.history = message.snapshot.history;
346
602
  this.state = "started";
347
603
  this.error = "";
348
604
  } else if (message.type === "error") this.error = message.error;
@@ -353,7 +609,7 @@ var InspectorConnection = class {
353
609
  const pending = this.#pending.get(message.id);
354
610
  if (pending) {
355
611
  this.#pending.delete(message.id);
356
- if (message.ok && !this.error) pending.resolve();
612
+ if (message.ok && !this.error) pending.resolve(message);
357
613
  else pending.reject(new Error(message.error || this.error));
358
614
  }
359
615
  }
@@ -418,13 +674,13 @@ var InspectorConnection = class {
418
674
  };
419
675
 
420
676
  // ../tools/gramlot-tools-inspector/src/gramlot-tools-inspector.css
421
- 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 { display: inline-flex; align-items: center; justify-content: center; 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\n.property-value select.enum-value { flex: 1; min-width: 0; max-width: none; width: 100%; border: 0; background: white; font: inherit; padding: 1px 5px; }\n\n/* Presentation changes preserve the inspector runtime and selection. */\n:host([hidden]) { display: none; }\n:host([docked]) { display: block; height: 100%; min-height: 0; }\n:host([docked][hidden]) { display: none; }\n:host([docked]) > div { height: 100%; }\n:host([docked]) .targetbar select { min-height: 40px; font-size: 16px; }\n:host([docked]) .property-row { min-height: 36px; font-size: 13px; }\n:host([docked]) .property-row input { min-height: 35px; font-size: 16px; }\n:host([docked]) .remove { opacity: 1; min-height: 35px; width: 32px; }\n\n:host([docked]) { --tree-action-size: 28px; --tree-font-size: 14px; --tree-row-height: 40px; --separator-size: 14px; }\n:host([docked]) .targetbar { height: 44px; }\n:host([docked]) gramlot-poc-tabs > button { height: 44px; font-size: 14px; }\n:host([docked]) .properties-heading { height: 40px; font-size: 13px; }\n:host([docked]) .properties-heading button { width: 36px; height: 36px; }\n:host([docked]) .property-value select { height: 35px; font-size: 14px; }\n";
677
+ 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 { display: inline-flex; align-items: center; justify-content: center; 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\n.property-value select.enum-value { flex: 1; min-width: 0; max-width: none; width: 100%; border: 0; background: white; font: inherit; padding: 1px 5px; }\n\n/* Presentation changes preserve the inspector runtime and selection. */\n:host([hidden]) { display: none; }\n:host([docked]) { display: block; height: 100%; min-height: 0; }\n:host([docked][hidden]) { display: none; }\n:host([docked]) > div { height: 100%; }\n:host([docked]) .targetbar select { min-height: 40px; font-size: 16px; }\n:host([docked]) .property-row { min-height: 36px; font-size: 13px; }\n:host([docked]) .property-row input { min-height: 35px; font-size: 16px; }\n:host([docked]) .remove { opacity: 1; min-height: 35px; width: 32px; }\n\n:host([docked]) { --tree-action-size: 28px; --tree-font-size: 14px; --tree-row-height: 40px; --separator-size: 14px; }\n:host([docked]) .targetbar { height: 44px; }\n:host([docked]) gramlot-poc-tabs > button { height: 44px; font-size: 14px; }\n:host([docked]) .properties-heading { height: 40px; font-size: 13px; }\n:host([docked]) .properties-heading button { width: 36px; height: 36px; }\n:host([docked]) .property-value select { height: 35px; font-size: 14px; }\n\n.history-toolbar { flex:none; display:flex; align-items:center; gap:6px; padding:4px 8px; border-bottom:1px solid #d1d7df; background:#f6f8fb; font:11px/1.4 system-ui,sans-serif; }\n.history-toolbar button { flex:none; padding:3px 6px; border:1px solid #c5cdda; border-radius:3px; background:white; cursor:pointer; }\n.history-toolbar button:disabled { opacity:.45; cursor:default; }\n.history-toolbar span { min-width:0; color:#a12121; overflow-wrap:anywhere; }\n";
422
678
 
423
679
  // ../tools/poc/gramlot-poc-tree.js
424
680
  import { Bag as Bag2 } from "@gramlot/gramlot";
425
681
 
426
682
  // ../tools/poc/gramlot-poc-tree.css
427
- 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:var(--tree-font-size,11px)/var(--tree-row-height,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:calc(var(--tree-action-size,14px) + 2px); }\n.tree-root > ul { padding-left:2px; }\nsummary, .leaf { position:relative; }\nli { line-height:var(--tree-row-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:calc(-1 * var(--tree-action-size,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:var(--tree-row-height,18px); width:var(--tree-action-size,14px); height:var(--tree-row-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\n@media (hover: none) { .actions { opacity:1; pointer-events:auto; } }\n";
683
+ 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:var(--tree-font-size,11px)/var(--tree-row-height,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:calc(var(--tree-action-size,14px) + 2px); }\n.tree-root > ul { padding-left:2px; }\nsummary, .leaf { position:relative; }\nli { line-height:var(--tree-row-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:calc(-1 * var(--tree-action-size,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:var(--tree-row-height,18px); width:var(--tree-action-size,14px); height:var(--tree-row-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\n@media (hover: none) { .actions { opacity:1; pointer-events:auto; } }\n\n.actions-end { position:static; margin-left:auto; flex:none; }\n[data-drop=inside] { outline:2px solid #356f9f; outline-offset:-2px; background:#e7f0fc; }\n[data-drop=before] { box-shadow:inset 0 2px #356f9f; }\n[data-drop=after] { box-shadow:inset 0 -2px #356f9f; }\n";
428
684
 
429
685
  // ../tools/poc/gramlot-poc-tree.js
430
686
  var sequence = 0;
@@ -442,6 +698,9 @@ var GramlotPocTree = class extends HTMLElement {
442
698
  this._expanded = /* @__PURE__ */ new Set();
443
699
  this._nodes = /* @__PURE__ */ new Map();
444
700
  this._rowActions = [];
701
+ this.dragEnabled = false;
702
+ this.fixedPaths = [];
703
+ this._drag = null;
445
704
  this._pendingRender = null;
446
705
  this._subId = `gramlot-poc-tree-${++sequence}`;
447
706
  }
@@ -469,6 +728,12 @@ var GramlotPocTree = class extends HTMLElement {
469
728
  this._selectedPath = path;
470
729
  if (this.isConnected) this._markSelection();
471
730
  }
731
+ revealPath(path) {
732
+ for (const details of this._root.querySelectorAll("details")) {
733
+ if (path.startsWith(details.dataset.path + ".")) details.open = true;
734
+ }
735
+ this.selectedPath = path;
736
+ }
472
737
  get rowActions() {
473
738
  return this._rowActions;
474
739
  }
@@ -498,12 +763,22 @@ var GramlotPocTree = class extends HTMLElement {
498
763
  }
499
764
  _render() {
500
765
  this._pendingRender = null;
766
+ const focused = this.shadowRoot.activeElement;
767
+ const focusedPath = focused?.closest("[data-node-path]")?.dataset.nodePath;
768
+ const focusedAction = focused?.dataset.action;
501
769
  for (const details of this._root.querySelectorAll("details")) {
502
770
  if (details.open) this._expanded.add(details.dataset.path);
503
771
  else this._expanded.delete(details.dataset.path);
504
772
  }
505
773
  this._nodes.clear();
506
774
  this._root.replaceChildren(...this._store ? [this._buildLevel(this._store, "", /* @__PURE__ */ new Set())] : []);
775
+ if (focusedPath) {
776
+ let path = focusedPath;
777
+ while (path && !this._nodes.has(path)) path = path.includes(".") ? path.slice(0, path.lastIndexOf(".")) : "";
778
+ const row = [...this._root.querySelectorAll("[data-node-path]")].find((item) => item.dataset.nodePath === path);
779
+ const action = focusedAction && row && [...row.querySelectorAll("button")].find((button) => button.dataset.action === focusedAction);
780
+ (action || row)?.focus({ preventScroll: true });
781
+ }
507
782
  if (this._selectedPath !== null && !this._nodes.has(this._selectedPath)) {
508
783
  this._selectedPath = null;
509
784
  this._notifySelection();
@@ -546,35 +821,91 @@ var GramlotPocTree = class extends HTMLElement {
546
821
  row.append(caption);
547
822
  row.addEventListener("click", () => this._select(path));
548
823
  this._actions(row, path, node);
824
+ this._dragEvents(row, path, node);
549
825
  list.append(item);
550
826
  }
551
827
  return list;
552
828
  }
553
829
  _actions(row, path, node) {
554
830
  if (!this._rowActions.length) return;
555
- const actions = this.ownerDocument.createElement("span");
556
- actions.className = "actions";
557
- for (const action of this._rowActions) {
558
- const button = this.ownerDocument.createElement("button");
559
- button.type = "button";
560
- button.textContent = action.icon ?? action.label;
561
- button.title = action.label;
562
- button.setAttribute("aria-label", action.label);
563
- button.dataset.action = action.id;
564
- button.classList.toggle("active", action.activePaths?.includes(path) ?? false);
565
- button.classList.toggle("conditional", action.conditionalPaths?.includes(path) ?? false);
566
- button.addEventListener("click", (event) => {
567
- event.preventDefault();
568
- event.stopPropagation();
569
- this.dispatchEvent(new this.ownerDocument.defaultView.CustomEvent("tree-action", {
570
- bubbles: true,
571
- composed: true,
572
- detail: { action: action.id, path, node }
573
- }));
574
- });
575
- actions.append(button);
831
+ for (const position of ["start", "end"]) {
832
+ const actions = this.ownerDocument.createElement("span");
833
+ actions.className = `actions actions-${position}`;
834
+ for (const action of this._rowActions) {
835
+ if ((action.position ?? "start") !== position || action.hiddenPaths?.includes(path)) continue;
836
+ const button = this.ownerDocument.createElement("button");
837
+ button.type = "button";
838
+ button.textContent = action.icon ?? action.label;
839
+ button.title = action.label;
840
+ button.setAttribute("aria-label", action.label);
841
+ button.dataset.action = action.id;
842
+ button.classList.toggle("active", action.activePaths?.includes(path) ?? false);
843
+ button.classList.toggle("conditional", action.conditionalPaths?.includes(path) ?? false);
844
+ button.addEventListener("click", (event) => {
845
+ event.preventDefault();
846
+ event.stopPropagation();
847
+ this.dispatchEvent(new this.ownerDocument.defaultView.CustomEvent("tree-action", {
848
+ bubbles: true,
849
+ composed: true,
850
+ detail: { action: action.id, path, node }
851
+ }));
852
+ });
853
+ actions.append(button);
854
+ }
855
+ if (actions.childElementCount) row.append(actions);
576
856
  }
577
- row.append(actions);
857
+ }
858
+ _clearDrop() {
859
+ for (const row of this._root.querySelectorAll("[data-drop]")) delete row.dataset.drop;
860
+ }
861
+ _dragEvents(row, path, node) {
862
+ if (!this.dragEnabled) return;
863
+ row.draggable = !this.fixedPaths.includes(path);
864
+ row.addEventListener("dragstart", (event) => {
865
+ if (!row.draggable || event.target.closest("button")) {
866
+ event.preventDefault();
867
+ return;
868
+ }
869
+ event.stopPropagation();
870
+ this._drag = { path, node };
871
+ event.dataTransfer.effectAllowed = "move";
872
+ event.dataTransfer.setData("application/x-gramlot-source", this._subId);
873
+ });
874
+ const placement = (event) => {
875
+ const rect = row.getBoundingClientRect();
876
+ const fraction = (event.clientY - rect.top) / rect.height;
877
+ return this.fixedPaths.includes(path) ? "inside" : fraction < 0.25 ? "before" : fraction > 0.75 ? "after" : "inside";
878
+ };
879
+ const valid = () => this._drag && this._drag.path !== path && !path.startsWith(this._drag.path + ".");
880
+ row.addEventListener("dragover", (event) => {
881
+ if (!valid()) return;
882
+ event.preventDefault();
883
+ event.stopPropagation();
884
+ event.dataTransfer.dropEffect = "move";
885
+ this._clearDrop();
886
+ row.dataset.drop = placement(event);
887
+ });
888
+ row.addEventListener("dragleave", () => {
889
+ delete row.dataset.drop;
890
+ });
891
+ row.addEventListener("drop", (event) => {
892
+ event.preventDefault();
893
+ event.stopPropagation();
894
+ this._clearDrop();
895
+ if (!valid() || event.dataTransfer.getData("application/x-gramlot-source") !== this._subId) return;
896
+ const source = this._drag;
897
+ this._drag = null;
898
+ if (this._store.getNode(source.path) !== source.node) return;
899
+ this.dispatchEvent(new this.ownerDocument.defaultView.CustomEvent("tree-drop", {
900
+ bubbles: true,
901
+ composed: true,
902
+ detail: { path: source.path, destination: path, placement: placement(event) }
903
+ }));
904
+ });
905
+ row.addEventListener("dragend", () => {
906
+ this._drag = null;
907
+ this._clearDrop();
908
+ });
578
909
  }
579
910
  _markSelection() {
580
911
  for (const row of this._root.querySelectorAll("[data-node-path]")) {
@@ -759,6 +1090,9 @@ var GramlotToolsInspector = class extends HTMLElement {
759
1090
  }
760
1091
  }
761
1092
  #target = null;
1093
+ #history = null;
1094
+ #insertTarget = null;
1095
+ #catalog = [];
762
1096
  #connection = null;
763
1097
  #connectionRelease = null;
764
1098
  #targets = [];
@@ -1002,6 +1336,67 @@ var GramlotToolsInspector = class extends HTMLElement {
1002
1336
  inspector.#applyEdit(choose);
1003
1337
  } else choose();
1004
1338
  }
1339
+ sourceAction(node, event) {
1340
+ if (event.detail.action === "breakpoint") {
1341
+ this.openBreakpoint(node, event);
1342
+ return;
1343
+ }
1344
+ const run = () => {
1345
+ if (event.detail.action === "insert") inspector.#openInsert(event.detail.path);
1346
+ if (event.detail.action === "remove") {
1347
+ try {
1348
+ inspector.#runCommand("remove", inspector.#sourceCommand(event.detail.path));
1349
+ } catch (error) {
1350
+ ui.data.setItem("status", error.message);
1351
+ }
1352
+ }
1353
+ };
1354
+ if (ui.data.getItem("editor.dirty")) inspector.#applyEdit(run);
1355
+ else run();
1356
+ }
1357
+ sourceDrop(node, event) {
1358
+ const run = () => {
1359
+ try {
1360
+ const { path, destination, placement } = event.detail;
1361
+ const command = inspector.#sourceCommand(path);
1362
+ const target = inspector.#sourceCommand(destination);
1363
+ inspector.#runCommand("move", { ...command, destination, placement, targetId: target.nodeId, targetRevision: target.revision });
1364
+ } catch (error) {
1365
+ ui.data.setItem("status", error.message);
1366
+ }
1367
+ };
1368
+ if (ui.data.getItem("editor.dirty")) inspector.#applyEdit(run);
1369
+ else run();
1370
+ }
1371
+ insertKind() {
1372
+ inspector.#insertChoices();
1373
+ }
1374
+ addElement() {
1375
+ inspector.#runCommand("insert", { ...inspector.#insertTarget, tag: ui.data.getItem("insert.tag"), attrs: Object.fromEntries((inspector.#catalog[0]?.fields ?? []).map(({ name }) => [name, ui.data.getItem(`insert.attrs.${name}`)])) });
1376
+ }
1377
+ closeInsert() {
1378
+ inspector.#dialog("insert").close();
1379
+ inspector.#insertTarget = null;
1380
+ }
1381
+ undo() {
1382
+ if (ui.data.getItem("editor.dirty")) inspector.#applyEdit(() => inspector.#runCommand("undo"));
1383
+ else inspector.#runCommand("undo");
1384
+ }
1385
+ redo() {
1386
+ if (ui.data.getItem("editor.dirty")) inspector.#applyEdit(() => inspector.#runCommand("redo"));
1387
+ else inspector.#runCommand("redo");
1388
+ }
1389
+ historyKey(node, event) {
1390
+ if (!(event.metaKey || event.ctrlKey) || event.altKey || event.target.closest("input,textarea,select,[contenteditable]")) return;
1391
+ if (event.key.toLowerCase() === "z") {
1392
+ event.preventDefault();
1393
+ event.shiftKey ? this.redo() : this.undo();
1394
+ }
1395
+ if (event.key.toLowerCase() === "y") {
1396
+ event.preventDefault();
1397
+ this.redo();
1398
+ }
1399
+ }
1005
1400
  openBreakpoint(node, event) {
1006
1401
  if (event.detail.action !== "breakpoint") return;
1007
1402
  ui.data.setItem("breakpointDirty", false);
@@ -1071,6 +1466,8 @@ var GramlotToolsInspector = class extends HTMLElement {
1071
1466
  breakpointCount: "\u25CF 0",
1072
1467
  noBreakpoints: true
1073
1468
  })) ui.data.setItem(key, value);
1469
+ ui.data.setItem("history", new Bag3({ noUndo: true, noRedo: true, undo: "", redo: "" }));
1470
+ ui.data.setItem("insert", new Bag3({ kind: "html", tag: "", html: true, component: false, status: "" }));
1074
1471
  ui.data.setItem("editor", new Bag3({ dirty: false, disabled: true, status: "", mode: "", condition: "" }));
1075
1472
  ui.data.setItem("breakpoints", new Bag3());
1076
1473
  ui.data.setItem("breakpointDirty", false);
@@ -1078,7 +1475,7 @@ var GramlotToolsInspector = class extends HTMLElement {
1078
1475
  const builder = new GramlotBuilder();
1079
1476
  const root = builder.root;
1080
1477
  root.style(gramlot_tools_inspector_default);
1081
- 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", docked: this.hasAttribute("docked") ? "" : null, connect_onclose: "inspector.close" });
1478
+ const palette = this.hasAttribute("embedded") ? root.section({ node_label: "palette", class: "embedded-panel", "aria-label": "Gramlot inspector", connect_onkeydown: "inspector.historyKey" }) : root.webcomponent({ code: "gramlot-poc-palette", node_label: "palette", title: "Gramlot inspector", docked: this.hasAttribute("docked") ? "" : null, connect_onclose: "inspector.close", connect_onkeydown: "inspector.historyKey" });
1082
1479
  const targetbar = palette.div({ node_label: "targetbar", class: "targetbar", hidden: "^singleTarget" });
1083
1480
  targetbar.select({ node_label: "targets", value: "^targetId", disabled: "^busy", "aria-label": "Inspected page", connect_onchange: "inspector.selectTarget" });
1084
1481
  const toolbar = palette.webcomponent({ code: "gramlot-poc-tabs", node_label: "tabs", role: "tablist", "aria-label": "Inspected root" });
@@ -1090,6 +1487,10 @@ var GramlotToolsInspector = class extends HTMLElement {
1090
1487
  "aria-selected": "false",
1091
1488
  connect_onclick: "inspector.selectRoot"
1092
1489
  });
1490
+ const historybar = palette.div({ class: "history-toolbar", hidden: "^stylesActive" });
1491
+ historybar.html_button("\u21B6 Undo", { type: "button", disabled: "^history.noUndo", title: "^history.undo", connect_onclick: "inspector.undo" });
1492
+ historybar.html_button("\u21B7 Redo", { type: "button", disabled: "^history.noRedo", title: "^history.redo", connect_onclick: "inspector.redo" });
1493
+ historybar.span("^status", { role: "status" });
1093
1494
  palette.p("^error", { role: "alert", class: "error" });
1094
1495
  const content = palette.webcomponent({ code: "gramlot-poc-splitter", node_label: "content", hidden: "^stylesActive" });
1095
1496
  const trees = content.div({ node_label: "tree", slot: "top", class: "tree" });
@@ -1099,7 +1500,8 @@ var GramlotToolsInspector = class extends HTMLElement {
1099
1500
  "data-root": name,
1100
1501
  hidden: name === "data" ? "^sourceActive" : "^dataActive",
1101
1502
  connect_onchange: "inspector.selectNode",
1102
- "connect_ontree-action": "inspector.openBreakpoint"
1503
+ "connect_ontree-action": "inspector.sourceAction",
1504
+ "connect_ontree-drop": "inspector.sourceDrop"
1103
1505
  });
1104
1506
  const properties = content.section({ node_label: "properties", slot: "bottom", class: "properties", "aria-label": "Selected node properties" });
1105
1507
  const heading = properties.div({ class: "properties-heading" });
@@ -1151,6 +1553,19 @@ var GramlotToolsInspector = class extends HTMLElement {
1151
1553
  list.h2("Active breakpoints");
1152
1554
  list.div({ node_label: "entries" });
1153
1555
  list.div({ class: "dialog-actions" }).html_button("Close", { type: "button", connect_onclick: "inspector.closeBreakpoints" });
1556
+ const insert = palette.dialog({ node_label: "insert", "aria-label": "Add Source element", class: "breakpoint-dialog", connect_oncancel: "inspector.closeInsert" });
1557
+ insert.h2("Add a child element");
1558
+ const kind = insert.select({ value: "^insert.kind", "aria-label": "Element kind", connect_onchange: "inspector.insertKind" });
1559
+ kind.option("HTML tag", { value: "html" });
1560
+ kind.option("Widget or container", { value: "component" });
1561
+ insert.select({ node_label: "components", value: "^insert.tag", hidden: "^insert.html", "aria-label": "Widget or container" });
1562
+ insert.input({ value: "^insert.tag", live: true, hidden: "^insert.component", list: `${this.#subscriber}-tags`, "aria-label": "HTML tag", placeholder: "div, span\u2026" });
1563
+ insert.datalist({ node_label: "tags", id: `${this.#subscriber}-tags` });
1564
+ insert.div({ node_label: "fields" });
1565
+ insert.p("^insert.status", { role: "status" });
1566
+ const insertActions = insert.div({ class: "dialog-actions" });
1567
+ insertActions.html_button("Cancel", { type: "button", connect_onclick: "inspector.closeInsert" });
1568
+ insertActions.html_button("Add", { type: "button", disabled: "^busy", connect_onclick: "inspector.addElement" });
1154
1569
  ui.src.startSource(builder.source);
1155
1570
  for (const name of ["data", "source"]) this.#trees[name] = ui.dom.getDomNode(ui.src.source.getNode(`main.palette.content.tree.${name}`));
1156
1571
  this.#renderTargets();
@@ -1176,6 +1591,7 @@ var GramlotToolsInspector = class extends HTMLElement {
1176
1591
  return;
1177
1592
  }
1178
1593
  if (!this.#target) return;
1594
+ this.#history = new InspectorHistory(this.#target);
1179
1595
  this.#subscriptions = [this.#target.data, this.#target.src.source];
1180
1596
  for (const [index, bag] of this.#subscriptions.entries()) bag.subscribe(this.#subscriber, { any: (event) => {
1181
1597
  this.#checkBreakpoints(index === 0 ? "data" : "source", event);
@@ -1183,6 +1599,10 @@ var GramlotToolsInspector = class extends HTMLElement {
1183
1599
  } });
1184
1600
  }
1185
1601
  #unsubscribe() {
1602
+ this.#history?.clear();
1603
+ this.#history = null;
1604
+ this.#insertTarget = null;
1605
+ if (this.#ui) this.#dialog("insert").close();
1186
1606
  ++this.#styleRequest;
1187
1607
  this.#styles?.dispose();
1188
1608
  this.#styles = null;
@@ -1229,6 +1649,11 @@ var GramlotToolsInspector = class extends HTMLElement {
1229
1649
  ui.data.setItem("breakpoints", configurations);
1230
1650
  ui.data.setItem("breakpointError", this.#connection.breakpoints.filter((record) => record.error).map((record) => `${record.root}: ${record.path}: ${record.error}`).join("; "));
1231
1651
  }
1652
+ const history = this.#connection ? this.#connection.history : this.#history?.state;
1653
+ for (const action of ["undo", "redo"]) {
1654
+ ui.data.setItem(`history.${action}`, history?.[action] ?? "");
1655
+ ui.data.setItem(`history.no${action === "undo" ? "Undo" : "Redo"}`, !history?.[action] || ui.data.getItem("busy") || this.#state() !== "started");
1656
+ }
1232
1657
  const count = ui.data.getItem("breakpoints").getNodes().length;
1233
1658
  ui.data.setItem("breakpointCount", `\u25CF ${count}`);
1234
1659
  ui.data.setItem("noBreakpoints", count === 0);
@@ -1238,13 +1663,18 @@ var GramlotToolsInspector = class extends HTMLElement {
1238
1663
  const tree = this.#trees[name];
1239
1664
  tree.storeBag = this.#connection || this.#target ? this.#targetBag(name) : null;
1240
1665
  const breakpoints = ui.data.getItem("breakpoints").getNodes().filter((node) => node.value.getItem("root") === name);
1666
+ tree.dragEnabled = name === "source";
1667
+ tree.fixedPaths = tree.storeBag?.getNodes().map((node) => node.label) ?? [];
1241
1668
  tree.rowActions = [{
1242
1669
  id: "breakpoint",
1243
1670
  icon: "\u25CB",
1244
1671
  label: "Breakpoint",
1245
1672
  activePaths: breakpoints.map((node) => node.value.getItem("path")),
1246
1673
  conditionalPaths: breakpoints.filter((node) => node.value.getItem("condition").trim()).map((node) => node.value.getItem("path"))
1247
- }];
1674
+ }, ...name === "source" ? [
1675
+ { id: "insert", icon: "+", label: "Add child element", position: "end" },
1676
+ { id: "remove", icon: "\u2212", label: "Remove branch", position: "end", hiddenPaths: tree.fixedPaths }
1677
+ ] : []];
1248
1678
  }
1249
1679
  if (this.#target || this.#connection) this.#index(this.#bag(), []);
1250
1680
  this.#trees[root].selectedPath = this.#selected()?.path.join(".") ?? null;
@@ -1614,8 +2044,7 @@ ${declarations}
1614
2044
  });
1615
2045
  return false;
1616
2046
  }
1617
- if (changed) target.setAttr(attrs, true, false, false);
1618
- if (!Object.is(value, this.#snapshot.value)) target.setValue(value);
2047
+ this.#history.edit(target, attrs, value, changed, !Object.is(value, this.#snapshot.value));
1619
2048
  this.#acceptEdit(target);
1620
2049
  this.#refresh();
1621
2050
  this.#ui.data.setItem("editor.status", "");
@@ -1626,6 +2055,91 @@ ${declarations}
1626
2055
  return false;
1627
2056
  }
1628
2057
  }
2058
+ #sourceCommand(path) {
2059
+ const node = this.#targetBag("source").getNode(path);
2060
+ if (!node) throw new Error("Source node no longer exists");
2061
+ const info = this.#connection?.nodeInfo(node);
2062
+ return { path, nodeId: info?.id, revision: info?.revision, ...this.#connection ? {} : { node } };
2063
+ }
2064
+ async #openInsert(path) {
2065
+ if (this.#ui.data.getItem("busy")) return;
2066
+ const generation = this.#generation;
2067
+ try {
2068
+ this.#insertTarget = this.#sourceCommand(path);
2069
+ this.#catalog = this.#connection ? (await this.#connection.request("catalog", { path })).catalog : this.#history.catalog(path);
2070
+ if (!this.#ui || generation !== this.#generation) return;
2071
+ this.#ui.data.setItem("insert.kind", "html");
2072
+ this.#ui.data.setItem("insert.status", "");
2073
+ this.#insertChoices();
2074
+ const builder = new GramlotBuilder();
2075
+ this.#ui.data.setItem("insert.attrs", new Bag3());
2076
+ for (const { name, choices } of this.#catalog[0]?.fields ?? []) {
2077
+ builder.root.html_label(name);
2078
+ const field = builder.root.select({ value: `^insert.attrs.${name}`, "aria-label": name });
2079
+ field.option(`Choose ${name}\u2026`, { value: "", disabled: true });
2080
+ for (const value of choices) field.option(value, { value });
2081
+ this.#ui.data.setItem(`insert.attrs.${name}`, "");
2082
+ }
2083
+ this.#ui.src.source.getNode("main.palette.insert.fields").setValue(this.#ui.src.prepareSource(builder.source));
2084
+ this.#dialog("insert").showModal();
2085
+ } catch (error) {
2086
+ if (this.#ui && generation === this.#generation) this.#ui.data.setItem("status", error.message);
2087
+ }
2088
+ }
2089
+ #insertChoices() {
2090
+ const html = this.#ui.data.getItem("insert.kind") === "html";
2091
+ this.#ui.data.setItem("insert.html", html);
2092
+ this.#ui.data.setItem("insert.component", !html);
2093
+ const choices = this.#catalog.filter((item) => item.kind === (html ? "html" : "component"));
2094
+ const builder = new GramlotBuilder();
2095
+ for (const item of choices) builder.root.option(item.tag, { value: item.tag });
2096
+ this.#ui.src.source.getNode(`main.palette.insert.${html ? "tags" : "components"}`).setValue(this.#ui.src.prepareSource(builder.source));
2097
+ this.#ui.data.setItem("insert.tag", html ? "" : choices[0]?.tag ?? "");
2098
+ }
2099
+ async #runCommand(type, command = {}) {
2100
+ const ui = this.#ui;
2101
+ if (ui.data.getItem("busy")) return;
2102
+ const generation = this.#generation;
2103
+ ui.data.setItem("busy", true);
2104
+ ui.data.setItem("status", "");
2105
+ ui.data.setItem("insert.status", "");
2106
+ try {
2107
+ let selection;
2108
+ if (this.#connection) ({ selection } = await this.#connection.request(type, command));
2109
+ else {
2110
+ if (command.path && this.#targetBag("source").getNode(command.path) !== command.node) throw new Error("Source node changed. Select it again.");
2111
+ if (type === "insert") selection = this.#history.insert(command.path, command.tag, command.attrs);
2112
+ if (type === "remove") selection = this.#history.remove(command.path);
2113
+ if (type === "move") selection = this.#history.move(command.path, command.destination, command.placement);
2114
+ if (type === "undo") this.#history.undo();
2115
+ if (type === "redo") this.#history.redo();
2116
+ }
2117
+ if (!this.#ui || generation !== this.#generation) return;
2118
+ if (type === "insert") this.#dialog("insert").close();
2119
+ this.#snapshot = null;
2120
+ ui.data.setItem("editor.dirty", false);
2121
+ this.#refresh();
2122
+ if (selection) {
2123
+ ui.data.setItem("root", "source");
2124
+ ui.data.setItem("tab", "source");
2125
+ this.#refresh();
2126
+ const entry = this.#nodes.find((item) => item.path.join(".") === selection);
2127
+ ui.data.setItem("selection", entry?.id ?? null);
2128
+ this.#trees.source.revealPath(selection);
2129
+ this.#refresh();
2130
+ }
2131
+ } catch (error) {
2132
+ if (this.#ui && generation === this.#generation) {
2133
+ ui.data.setItem("status", error.message);
2134
+ ui.data.setItem("insert.status", error.message);
2135
+ }
2136
+ } finally {
2137
+ if (this.#ui && generation === this.#generation) {
2138
+ ui.data.setItem("busy", false);
2139
+ this.#refresh();
2140
+ }
2141
+ }
2142
+ }
1629
2143
  #findBreakpoint(root, path) {
1630
2144
  return this.#ui.data.getItem("breakpoints").getNodes().find((node) => {
1631
2145
  const value = node.getValue(true);