@gramlot/gramlot 0.2.17 → 0.2.18
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/gramlot.js +536 -22
- package/dist/gramlot.min.js +21 -21
- package/package.json +3 -2
- package/src/binding/ask-dialog.js +108 -0
- package/src/binding/providers.js +41 -6
- package/src/binding/runtime.js +36 -1
- package/src/builder/gramlot-builder.js +9 -2
- package/src/builder/source.js +18 -5
- package/src/frame-messages.js +177 -0
- package/src/gramlot.d.ts +14 -0
- package/src/gramlot.js +9 -1
- package/src/handlers/rpc-handler.js +2 -0
- package/src/handlers/source-handler.js +1 -0
- package/src/handlers/subscriptions-handler.js +103 -0
- package/src/index.d.ts +1 -1
- package/src/references.d.ts +5 -0
- package/src/references.js +23 -1
- package/src/renderer/attributes.js +3 -3
- package/src/renderer/gramlot-renderer.js +3 -0
- package/src/view/button.js +17 -3
- package/src/view/html.js +2 -2
- package/tools/gramlot-tools-inspector.js +317 -44
- package/tools/inspector-agent.js +252 -0
|
@@ -0,0 +1,252 @@
|
|
|
1
|
+
// ../tools/gramlot-tools-inspector/src/editor-values.js
|
|
2
|
+
function scalarType(value) {
|
|
3
|
+
if (value === null) return "null";
|
|
4
|
+
if (["string", "boolean"].includes(typeof value)) return typeof value;
|
|
5
|
+
if (typeof value === "number" && Number.isFinite(value)) return Number.isSafeInteger(value) ? "integer" : "number";
|
|
6
|
+
if (value instanceof Date && Number.isFinite(value.getTime())) {
|
|
7
|
+
const iso = value.toISOString();
|
|
8
|
+
return iso.slice(0, 10) === "1970-01-01" ? "time" : iso.endsWith("T00:00:00.000Z") ? "date" : "datetime";
|
|
9
|
+
}
|
|
10
|
+
return null;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
// ../tools/gramlot-tools-inspector/src/inspector-styles.js
|
|
14
|
+
var InspectorStyles = class {
|
|
15
|
+
#document;
|
|
16
|
+
#element = null;
|
|
17
|
+
#rule = null;
|
|
18
|
+
constructor(document) {
|
|
19
|
+
this.#document = document;
|
|
20
|
+
}
|
|
21
|
+
snapshot() {
|
|
22
|
+
const computed = this.#document.defaultView.getComputedStyle(this.#document.documentElement);
|
|
23
|
+
const names = /* @__PURE__ */ new Set();
|
|
24
|
+
for (let index = 0; index < computed.length; index++) {
|
|
25
|
+
const name = computed.item(index);
|
|
26
|
+
if (name.startsWith("--gramlot-")) names.add(name);
|
|
27
|
+
}
|
|
28
|
+
for (let index = 0; index < (this.#rule?.style.length ?? 0); index++) names.add(this.#rule.style.item(index));
|
|
29
|
+
return [...names].sort((left, right) => left.localeCompare(right, "en", { numeric: true })).map((name) => ({
|
|
30
|
+
name,
|
|
31
|
+
value: computed.getPropertyValue(name).trim(),
|
|
32
|
+
override: this.#rule?.style.getPropertyValue(name) ?? ""
|
|
33
|
+
}));
|
|
34
|
+
}
|
|
35
|
+
set(name, value) {
|
|
36
|
+
if (typeof name !== "string" || !this.snapshot().some((row) => row.name === name)) {
|
|
37
|
+
throw new Error("Choose an existing --gramlot-* variable on the document root");
|
|
38
|
+
}
|
|
39
|
+
if (typeof value !== "string" || !value.trim() || !this.#document.defaultView.CSS.supports(name, value)) {
|
|
40
|
+
throw new Error("Enter a valid CSS variable value");
|
|
41
|
+
}
|
|
42
|
+
if (!this.#element) {
|
|
43
|
+
const element = this.#document.createElement("style");
|
|
44
|
+
this.#document.head.append(element);
|
|
45
|
+
if (!element.sheet) {
|
|
46
|
+
element.remove();
|
|
47
|
+
throw new Error("The page does not allow the inspector preview stylesheet");
|
|
48
|
+
}
|
|
49
|
+
element.sheet.insertRule(":root {}", 0);
|
|
50
|
+
this.#element = element;
|
|
51
|
+
this.#rule = element.sheet.cssRules[0];
|
|
52
|
+
}
|
|
53
|
+
this.#rule.style.setProperty(name, value, "important");
|
|
54
|
+
}
|
|
55
|
+
reset(name) {
|
|
56
|
+
if (typeof name !== "string" || !name.startsWith("--gramlot-")) throw new Error("Invalid CSS variable name");
|
|
57
|
+
this.#rule?.style.removeProperty(name);
|
|
58
|
+
if (this.#rule?.style.length === 0) this.dispose();
|
|
59
|
+
}
|
|
60
|
+
dispose() {
|
|
61
|
+
this.#element?.remove();
|
|
62
|
+
this.#element = null;
|
|
63
|
+
this.#rule = null;
|
|
64
|
+
}
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
// ../tools/gramlot-tools-inspector/src/inspector-agent.js
|
|
68
|
+
var roots = ["data", "source"];
|
|
69
|
+
var forbidden = /* @__PURE__ */ new Set(["_meta", "__proto__", "prototype", "constructor"]);
|
|
70
|
+
function attachInspectorAgent(gramlot, port) {
|
|
71
|
+
if (!gramlot || gramlot.state === "disposed") throw new TypeError("Expected a live Gramlot instance");
|
|
72
|
+
const Bag = gramlot.data.constructor;
|
|
73
|
+
const encodeScalar = (value) => {
|
|
74
|
+
const wrapper = new Bag();
|
|
75
|
+
wrapper.setItem("value", value);
|
|
76
|
+
return wrapper.toTytx("json");
|
|
77
|
+
};
|
|
78
|
+
const decodeScalar = (text) => {
|
|
79
|
+
const value = Bag.fromTytx(text, "json").getItem("value");
|
|
80
|
+
if (!scalarType(value)) throw new TypeError("The inspector edits scalar values only");
|
|
81
|
+
return value;
|
|
82
|
+
};
|
|
83
|
+
function cell(value) {
|
|
84
|
+
if (scalarType(value)) return { encoded: encodeScalar(value) };
|
|
85
|
+
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);
|
|
86
|
+
return { display, readonly: true };
|
|
87
|
+
}
|
|
88
|
+
const document = gramlot.src.renderer.destination.ownerDocument;
|
|
89
|
+
const window = document.defaultView;
|
|
90
|
+
const styles = new InspectorStyles(document);
|
|
91
|
+
const subscriber = `inspector-bridge-${window.crypto.getRandomValues(new Uint32Array(4)).join("-")}`;
|
|
92
|
+
const identities = /* @__PURE__ */ new WeakMap();
|
|
93
|
+
const versions = /* @__PURE__ */ new WeakMap();
|
|
94
|
+
let nextNode = 0, stopped = false, scheduled = false;
|
|
95
|
+
const breakpoints = /* @__PURE__ */ new Map();
|
|
96
|
+
const bag = (root) => {
|
|
97
|
+
if (!roots.includes(root)) throw new Error(`Unknown root: ${root}`);
|
|
98
|
+
return root === "data" ? gramlot.data : gramlot.src.source;
|
|
99
|
+
};
|
|
100
|
+
const identity = (node) => {
|
|
101
|
+
if (!identities.has(node)) identities.set(node, ++nextNode);
|
|
102
|
+
return identities.get(node);
|
|
103
|
+
};
|
|
104
|
+
const describe = (store, ancestors = /* @__PURE__ */ new Set()) => {
|
|
105
|
+
if (ancestors.has(store)) throw new Error("Inspector snapshots require an acyclic Bag");
|
|
106
|
+
const next = /* @__PURE__ */ new Set([...ancestors, store]);
|
|
107
|
+
return store.getNodes().map((node) => {
|
|
108
|
+
const value = node.getValue(true);
|
|
109
|
+
const branch = !node.resolver && value instanceof Bag;
|
|
110
|
+
return {
|
|
111
|
+
id: identity(node),
|
|
112
|
+
label: node.label,
|
|
113
|
+
revision: versions.get(node) ?? 0,
|
|
114
|
+
value: branch ? null : cell(value),
|
|
115
|
+
children: branch ? describe(value, next) : null,
|
|
116
|
+
readonly: !!node.resolver,
|
|
117
|
+
attrs: Object.entries(node.getAttr()).filter(([name]) => name !== "_meta").map(([name, value2]) => [name, cell(value2)])
|
|
118
|
+
};
|
|
119
|
+
});
|
|
120
|
+
};
|
|
121
|
+
const snapshot = () => ({
|
|
122
|
+
data: describe(bag("data")),
|
|
123
|
+
source: describe(bag("source")),
|
|
124
|
+
breakpoints: [...breakpoints.values()].map(({ test, ...record }) => record)
|
|
125
|
+
});
|
|
126
|
+
const send = (data) => {
|
|
127
|
+
if (!stopped) port.postMessage(data);
|
|
128
|
+
};
|
|
129
|
+
const flush = () => {
|
|
130
|
+
if (scheduled || stopped) return;
|
|
131
|
+
scheduled = true;
|
|
132
|
+
queueMicrotask(() => {
|
|
133
|
+
scheduled = false;
|
|
134
|
+
if (stopped) return;
|
|
135
|
+
try {
|
|
136
|
+
send({ type: "snapshot", snapshot: snapshot() });
|
|
137
|
+
} catch (error) {
|
|
138
|
+
send({ type: "error", error: error.message });
|
|
139
|
+
}
|
|
140
|
+
});
|
|
141
|
+
};
|
|
142
|
+
const hit = (config, root, fields) => {
|
|
143
|
+
config.hits++;
|
|
144
|
+
if (config.mode === "debugger") {
|
|
145
|
+
debugger;
|
|
146
|
+
} else window.console.log("** Gramlot breakpoint **", root, fields);
|
|
147
|
+
};
|
|
148
|
+
const changed = (root, event) => {
|
|
149
|
+
const structural = event.evt === "ins" || event.evt === "del";
|
|
150
|
+
const labels = event.pathlist === null ? [] : event.pathlist;
|
|
151
|
+
for (const node of Array.isArray(event.node) ? event.node : [event.node]) {
|
|
152
|
+
versions.set(node, (versions.get(node) ?? 0) + 1);
|
|
153
|
+
const path = (structural ? [...labels, node.label] : labels).join(".");
|
|
154
|
+
const config = breakpoints.get(`${root}:${path}`);
|
|
155
|
+
if (!config) continue;
|
|
156
|
+
const fields = {
|
|
157
|
+
evt: event.evt,
|
|
158
|
+
node,
|
|
159
|
+
value: node.getValue(true),
|
|
160
|
+
oldvalue: event.oldvalue,
|
|
161
|
+
attrs_diff: event.attrs_diff,
|
|
162
|
+
reason: event.reason,
|
|
163
|
+
path
|
|
164
|
+
};
|
|
165
|
+
if (config.test) {
|
|
166
|
+
try {
|
|
167
|
+
if (!config.test(fields.evt, node, fields.value, fields.oldvalue, fields.attrs_diff, fields.reason, path)) continue;
|
|
168
|
+
config.error = "";
|
|
169
|
+
} catch (error) {
|
|
170
|
+
config.error = error.message;
|
|
171
|
+
continue;
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
hit(config, root, fields);
|
|
175
|
+
}
|
|
176
|
+
flush();
|
|
177
|
+
};
|
|
178
|
+
const edit = (command) => {
|
|
179
|
+
const node = bag(command.root).getNode(command.path);
|
|
180
|
+
if (!node || identity(node) !== command.nodeId || (versions.get(node) ?? 0) !== command.revision) {
|
|
181
|
+
throw new Error("Node changed or was removed. Press Escape to reload before saving.");
|
|
182
|
+
}
|
|
183
|
+
const attrs = { ...node.getAttr() };
|
|
184
|
+
for (const name of command.remove) {
|
|
185
|
+
if (forbidden.has(name) || !Object.hasOwn(attrs, name) || !scalarType(attrs[name])) throw new Error(`Cannot remove attribute: ${name}`);
|
|
186
|
+
delete attrs[name];
|
|
187
|
+
}
|
|
188
|
+
for (const [name, encoded] of command.set) {
|
|
189
|
+
if (!name.trim() || forbidden.has(name) || Object.hasOwn(attrs, name) && !scalarType(attrs[name])) throw new Error(`Cannot edit attribute: ${name}`);
|
|
190
|
+
attrs[name] = decodeScalar(encoded);
|
|
191
|
+
}
|
|
192
|
+
let value;
|
|
193
|
+
if (Object.hasOwn(command, "value")) {
|
|
194
|
+
if (node.resolver || !scalarType(node.getValue(true))) throw new Error("This value is read-only");
|
|
195
|
+
value = decodeScalar(command.value);
|
|
196
|
+
}
|
|
197
|
+
if (command.set.length || command.remove.length) node.setAttr(attrs, true, false, false);
|
|
198
|
+
if (Object.hasOwn(command, "value")) node.setValue(value);
|
|
199
|
+
};
|
|
200
|
+
const setBreakpoint = (command) => {
|
|
201
|
+
bag(command.root);
|
|
202
|
+
if (typeof command.path !== "string" || !command.path || !["", "console", "debugger"].includes(command.mode)) throw new Error("Invalid breakpoint");
|
|
203
|
+
const condition = command.condition.trim();
|
|
204
|
+
const test = command.mode && condition ? new window.Function("evt", "node", "value", "oldvalue", "attrs_diff", "reason", "path", `"use strict"; return (${condition});`) : null;
|
|
205
|
+
const key = `${command.root}:${command.path}`;
|
|
206
|
+
if (command.mode) breakpoints.set(key, { root: command.root, path: command.path, mode: command.mode, condition, hits: 0, error: "", test });
|
|
207
|
+
else breakpoints.delete(key);
|
|
208
|
+
};
|
|
209
|
+
const receive = (event) => {
|
|
210
|
+
const message = event.data;
|
|
211
|
+
if (message.type === "disconnect") {
|
|
212
|
+
dispose();
|
|
213
|
+
return;
|
|
214
|
+
}
|
|
215
|
+
try {
|
|
216
|
+
if (["styles", "setStyle", "resetStyle", "resetStyles"].includes(message.type)) {
|
|
217
|
+
if (gramlot.state === "disposed") throw new Error("Inspected page disconnected");
|
|
218
|
+
if (message.type === "setStyle") styles.set(message.command.name, message.command.value);
|
|
219
|
+
if (message.type === "resetStyle") styles.reset(message.command.name);
|
|
220
|
+
if (message.type === "resetStyles") styles.dispose();
|
|
221
|
+
send({ type: "result", id: message.id, ok: true, styles: styles.snapshot() });
|
|
222
|
+
return;
|
|
223
|
+
}
|
|
224
|
+
if (message.type === "edit") edit(message.command);
|
|
225
|
+
else if (message.type === "breakpoint") setBreakpoint(message.command);
|
|
226
|
+
else if (message.type !== "refresh") throw new Error(`Unknown inspector command: ${message.type}`);
|
|
227
|
+
send({ type: "result", id: message.id, ok: true, snapshot: snapshot() });
|
|
228
|
+
} catch (error) {
|
|
229
|
+
send({ type: "result", id: message.id, ok: false, error: error.message });
|
|
230
|
+
}
|
|
231
|
+
};
|
|
232
|
+
function dispose() {
|
|
233
|
+
if (stopped) return;
|
|
234
|
+
send({ type: "closed" });
|
|
235
|
+
stopped = true;
|
|
236
|
+
for (const root of roots) bag(root).unsubscribe(subscriber, { any: true });
|
|
237
|
+
breakpoints.clear();
|
|
238
|
+
styles.dispose();
|
|
239
|
+
port.removeEventListener("message", receive);
|
|
240
|
+
window.removeEventListener("pagehide", dispose);
|
|
241
|
+
port.close();
|
|
242
|
+
}
|
|
243
|
+
for (const root of roots) bag(root).subscribe(subscriber, { any: (event) => changed(root, event) });
|
|
244
|
+
port.addEventListener("message", receive);
|
|
245
|
+
window.addEventListener("pagehide", dispose);
|
|
246
|
+
port.start();
|
|
247
|
+
flush();
|
|
248
|
+
return { dispose };
|
|
249
|
+
}
|
|
250
|
+
export {
|
|
251
|
+
attachInspectorAgent
|
|
252
|
+
};
|