blueprintsjs 0.0.0-stage → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +18 -0
- package/LICENSE +21 -0
- package/README.md +165 -2
- package/dist/blueprintsjs.umd.cjs +325 -0
- package/dist/blueprintsjs.umd.cjs.map +1 -0
- package/dist/chunks/validate-DHOIGyop.js +860 -0
- package/dist/chunks/validate-DHOIGyop.js.map +1 -0
- package/dist/chunks/validate-Ddbqd8bD.cjs +859 -0
- package/dist/chunks/validate-Ddbqd8bD.cjs.map +1 -0
- package/dist/core/build.d.cts +33 -0
- package/dist/core/build.d.ts +33 -0
- package/dist/core/compile.d.cts +27 -0
- package/dist/core/compile.d.ts +27 -0
- package/dist/core/helpers.d.cts +13 -0
- package/dist/core/helpers.d.ts +13 -0
- package/dist/core/layout.d.cts +19 -0
- package/dist/core/layout.d.ts +19 -0
- package/dist/core/registry.d.cts +23 -0
- package/dist/core/registry.d.ts +23 -0
- package/dist/core/schema.d.cts +14 -0
- package/dist/core/schema.d.ts +14 -0
- package/dist/core/types.d.cts +215 -0
- package/dist/core/types.d.ts +215 -0
- package/dist/core/validate.d.cts +9 -0
- package/dist/core/validate.d.ts +9 -0
- package/dist/editor/GraphEditor.d.cts +181 -0
- package/dist/editor/GraphEditor.d.ts +181 -0
- package/dist/editor/details.d.cts +23 -0
- package/dist/editor/details.d.ts +23 -0
- package/dist/editor/dom.d.cts +20 -0
- package/dist/editor/dom.d.ts +20 -0
- package/dist/editor/highlight.d.cts +7 -0
- package/dist/editor/highlight.d.ts +7 -0
- package/dist/editor/index.d.cts +13 -0
- package/dist/editor/index.d.ts +13 -0
- package/dist/editor/state.d.cts +113 -0
- package/dist/editor/state.d.ts +113 -0
- package/dist/editor/styles.d.cts +7 -0
- package/dist/editor/styles.d.ts +7 -0
- package/dist/editor.cjs +2196 -0
- package/dist/editor.cjs.map +1 -0
- package/dist/editor.js +2196 -0
- package/dist/editor.js.map +1 -0
- package/dist/index.cjs +1251 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +21 -0
- package/dist/index.d.ts +21 -0
- package/dist/index.js +1252 -0
- package/dist/index.js.map +1 -0
- package/dist/locales/index.d.cts +15 -0
- package/dist/locales/index.d.ts +15 -0
- package/dist/locales/ru.d.cts +3 -0
- package/dist/locales/ru.d.ts +3 -0
- package/dist/locales/uk.d.cts +3 -0
- package/dist/locales/uk.d.ts +3 -0
- package/dist/locales.cjs +579 -0
- package/dist/locales.cjs.map +1 -0
- package/dist/locales.js +579 -0
- package/dist/locales.js.map +1 -0
- package/dist/nodes/data.d.cts +26 -0
- package/dist/nodes/data.d.ts +26 -0
- package/dist/nodes/dom.d.cts +27 -0
- package/dist/nodes/dom.d.ts +27 -0
- package/dist/nodes/events.d.cts +11 -0
- package/dist/nodes/events.d.ts +11 -0
- package/dist/nodes/flow.d.cts +10 -0
- package/dist/nodes/flow.d.ts +10 -0
- package/dist/nodes/index.d.cts +11 -0
- package/dist/nodes/index.d.ts +11 -0
- package/dist/nodes/lib.d.cts +7 -0
- package/dist/nodes/lib.d.ts +7 -0
- package/dist/umd.d.cts +5 -0
- package/dist/umd.d.ts +5 -0
- package/dist/version.d.cts +2 -0
- package/dist/version.d.ts +2 -0
- package/docs/compiler.md +149 -0
- package/docs/custom-nodes.md +119 -0
- package/docs/editor.md +166 -0
- package/docs/guide.md +79 -0
- package/docs/nodes.md +494 -0
- package/docs/nodes.ru.md +494 -0
- package/docs/schema.md +64 -0
- package/examples/index.html +110 -0
- package/package.json +107 -4
- package/schema/blueprint.schema.json +102 -0
|
@@ -0,0 +1,859 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
function autoLayout(bp, o = {}) {
|
|
3
|
+
var _a;
|
|
4
|
+
const gapX = o.gapX ?? 120, gapY = o.gapY ?? 28, W = o.width ?? (() => 220), H = o.height ?? (() => 80), [ox, oy] = o.origin ?? [0, 0];
|
|
5
|
+
const ids = bp.nodes.map((n) => n.id), index = new Map(ids.map((id, i) => [id, i]));
|
|
6
|
+
const out = new Map(ids.map((id) => [id, []])), inc = new Map(ids.map((id) => [id, []]));
|
|
7
|
+
for (const l of bp.links) {
|
|
8
|
+
const a = l.from[0], b = l.to[0];
|
|
9
|
+
if (a === b || !index.has(a) || !index.has(b)) continue;
|
|
10
|
+
out.get(a).push(b);
|
|
11
|
+
inc.get(b).push(a);
|
|
12
|
+
}
|
|
13
|
+
const state = /* @__PURE__ */ new Map(), back = /* @__PURE__ */ new Set();
|
|
14
|
+
const dfs = (v) => {
|
|
15
|
+
state.set(v, 1);
|
|
16
|
+
for (const w of out.get(v)) {
|
|
17
|
+
if (state.get(w) === 1) back.add(`${v}>${w}`);
|
|
18
|
+
else if (!state.get(w)) dfs(w);
|
|
19
|
+
}
|
|
20
|
+
state.set(v, 2);
|
|
21
|
+
};
|
|
22
|
+
for (const id of ids) if (!inc.get(id).length && !state.get(id)) dfs(id);
|
|
23
|
+
for (const id of ids) if (!state.get(id)) dfs(id);
|
|
24
|
+
const fwd = (v) => out.get(v).filter((w) => !back.has(`${v}>${w}`));
|
|
25
|
+
const preds = (v) => inc.get(v).filter((u) => !back.has(`${u}>${v}`));
|
|
26
|
+
const layer = /* @__PURE__ */ new Map();
|
|
27
|
+
const visit = (v) => {
|
|
28
|
+
const known = layer.get(v);
|
|
29
|
+
if (known !== void 0) return known;
|
|
30
|
+
layer.set(v, 0);
|
|
31
|
+
const l = Math.max(0, ...preds(v).map((u) => visit(u) + 1));
|
|
32
|
+
layer.set(v, l);
|
|
33
|
+
return l;
|
|
34
|
+
};
|
|
35
|
+
ids.forEach(visit);
|
|
36
|
+
const layers = [];
|
|
37
|
+
for (const id of ids) (layers[_a = layer.get(id)] ?? (layers[_a] = [])).push(id);
|
|
38
|
+
const pos = /* @__PURE__ */ new Map();
|
|
39
|
+
const setPos = () => layers.forEach((L) => L.forEach((id, i) => pos.set(id, i)));
|
|
40
|
+
setPos();
|
|
41
|
+
const bary = (v, ns) => ns.length ? ns.reduce((a, u) => a + pos.get(u), 0) / ns.length : pos.get(v);
|
|
42
|
+
for (let it = 0; it < 4; it++) {
|
|
43
|
+
for (let i = 1; i < layers.length; i++) layers[i].sort((a, b) => bary(a, preds(a)) - bary(b, preds(b)) || index.get(a) - index.get(b)), setPos();
|
|
44
|
+
for (let i = layers.length - 2; i >= 0; i--) layers[i].sort((a, b) => bary(a, fwd(a)) - bary(b, fwd(b)) || index.get(a) - index.get(b)), setPos();
|
|
45
|
+
}
|
|
46
|
+
const colH = layers.map((L) => L.reduce((a, id) => a + H(id) + gapY, -gapY));
|
|
47
|
+
const maxH = Math.max(0, ...colH);
|
|
48
|
+
let x = ox;
|
|
49
|
+
const byId = new Map(bp.nodes.map((n) => [n.id, n]));
|
|
50
|
+
layers.forEach((L, i) => {
|
|
51
|
+
let y = oy + (maxH - colH[i]) / 2;
|
|
52
|
+
for (const id of L) {
|
|
53
|
+
byId.get(id).pos = [Math.round(x), Math.round(y)];
|
|
54
|
+
y += H(id) + gapY;
|
|
55
|
+
}
|
|
56
|
+
x += Math.max(...L.map(W)) + gapX;
|
|
57
|
+
});
|
|
58
|
+
return bp;
|
|
59
|
+
}
|
|
60
|
+
const HELPER_PREFIX = "__bp_";
|
|
61
|
+
const HELPERS = {
|
|
62
|
+
list: {
|
|
63
|
+
code: `function __bp_list(x) {
|
|
64
|
+
if (x == null) return [];
|
|
65
|
+
if (Array.isArray(x)) return x;
|
|
66
|
+
if (typeof x.length === 'number' && typeof x !== 'string' && !x.nodeType) return Array.prototype.slice.call(x);
|
|
67
|
+
return [x];
|
|
68
|
+
}`
|
|
69
|
+
},
|
|
70
|
+
on: {
|
|
71
|
+
code: `function __bp_on(d, target, type, fn, opts) {
|
|
72
|
+
if (!target || !target.addEventListener) return;
|
|
73
|
+
target.addEventListener(type, fn, opts || false);
|
|
74
|
+
d.push(function () { target.removeEventListener(type, fn, opts || false); });
|
|
75
|
+
}`
|
|
76
|
+
},
|
|
77
|
+
delegate: {
|
|
78
|
+
deps: ["on"],
|
|
79
|
+
code: `function __bp_delegate(d, root, type, sel, fn) {
|
|
80
|
+
var map = { mouseenter: 'mouseover', mouseleave: 'mouseout', focus: 'focusin', blur: 'focusout' };
|
|
81
|
+
var hover = type === 'mouseenter' || type === 'mouseleave';
|
|
82
|
+
if (!sel) {
|
|
83
|
+
var self = root.nodeType === 9 ? root.documentElement : root;
|
|
84
|
+
__bp_on(d, root, type, function (e) { fn(e, self); });
|
|
85
|
+
return;
|
|
86
|
+
}
|
|
87
|
+
__bp_on(d, root, map[type] || type, function (e) {
|
|
88
|
+
var t = e.target;
|
|
89
|
+
if (t && t.nodeType !== 1) t = t.parentNode;
|
|
90
|
+
if (!t || !t.closest) return;
|
|
91
|
+
var el;
|
|
92
|
+
try { el = t.closest(sel); } catch (err) { return; }
|
|
93
|
+
if (!el || (root.nodeType === 1 && el !== root && !root.contains(el))) return;
|
|
94
|
+
if (hover && e.relatedTarget && el.contains(e.relatedTarget)) return;
|
|
95
|
+
fn(e, el);
|
|
96
|
+
});
|
|
97
|
+
}`
|
|
98
|
+
},
|
|
99
|
+
q: {
|
|
100
|
+
deps: ["list"],
|
|
101
|
+
code: `function __bp_q(root, sel, mode, from) {
|
|
102
|
+
var out = [];
|
|
103
|
+
if (!sel) return mode && from != null ? __bp_list(from) : out;
|
|
104
|
+
try {
|
|
105
|
+
if (mode === 'closest') {
|
|
106
|
+
__bp_list(from).forEach(function (f) { var c = f && f.closest && f.closest(sel); if (c && out.indexOf(c) < 0) out.push(c); });
|
|
107
|
+
return out;
|
|
108
|
+
}
|
|
109
|
+
var base = mode === 'within' && from != null ? __bp_list(from) : [root];
|
|
110
|
+
base.forEach(function (b) {
|
|
111
|
+
if (!b || !b.querySelectorAll) return;
|
|
112
|
+
var found = b.querySelectorAll(sel);
|
|
113
|
+
for (var i = 0; i < found.length; i++) if (out.indexOf(found[i]) < 0) out.push(found[i]);
|
|
114
|
+
});
|
|
115
|
+
} catch (err) {}
|
|
116
|
+
return out;
|
|
117
|
+
}`
|
|
118
|
+
},
|
|
119
|
+
els: {
|
|
120
|
+
deps: ["list", "q"],
|
|
121
|
+
code: `function __bp_els(root, x) {
|
|
122
|
+
return typeof x === 'string' ? __bp_q(root, x) : __bp_list(x).filter(function (e) { return e && e.nodeType === 1; });
|
|
123
|
+
}`
|
|
124
|
+
},
|
|
125
|
+
timeout: {
|
|
126
|
+
code: `function __bp_timeout(d, fn, ms) {
|
|
127
|
+
var t = setTimeout(fn, ms);
|
|
128
|
+
d.push(function () { clearTimeout(t); });
|
|
129
|
+
return t;
|
|
130
|
+
}`
|
|
131
|
+
},
|
|
132
|
+
interval: {
|
|
133
|
+
code: `function __bp_interval(d, fn, ms) {
|
|
134
|
+
var t = setInterval(fn, ms);
|
|
135
|
+
d.push(function () { clearInterval(t); });
|
|
136
|
+
return t;
|
|
137
|
+
}`
|
|
138
|
+
},
|
|
139
|
+
show: {
|
|
140
|
+
code: `function __bp_show(el) {
|
|
141
|
+
if (el.style.display === 'none') el.style.display = el.__bpDisplay || '';
|
|
142
|
+
el.removeAttribute('hidden');
|
|
143
|
+
var w = el.ownerDocument && el.ownerDocument.defaultView;
|
|
144
|
+
if (w && w.getComputedStyle(el).display === 'none') el.style.display = 'block';
|
|
145
|
+
}`
|
|
146
|
+
},
|
|
147
|
+
hide: {
|
|
148
|
+
code: `function __bp_hide(el) {
|
|
149
|
+
if (el.style.display && el.style.display !== 'none') el.__bpDisplay = el.style.display;
|
|
150
|
+
el.style.display = 'none';
|
|
151
|
+
}`
|
|
152
|
+
},
|
|
153
|
+
visible: {
|
|
154
|
+
code: `function __bp_visible(el) {
|
|
155
|
+
if (!el || !el.ownerDocument) return false;
|
|
156
|
+
var w = el.ownerDocument.defaultView;
|
|
157
|
+
return !!w && w.getComputedStyle(el).display !== 'none' && !el.hasAttribute('hidden');
|
|
158
|
+
}`
|
|
159
|
+
},
|
|
160
|
+
dispatch: {
|
|
161
|
+
code: `function __bp_dispatch(el, name, detail) {
|
|
162
|
+
var w = (el.ownerDocument || el).defaultView || window;
|
|
163
|
+
var ev;
|
|
164
|
+
if (typeof w.CustomEvent === 'function') ev = new w.CustomEvent(name, { bubbles: true, cancelable: true, detail: detail });
|
|
165
|
+
else { ev = (el.ownerDocument || el).createEvent('CustomEvent'); ev.initCustomEvent(name, true, true, detail); }
|
|
166
|
+
el.dispatchEvent(ev);
|
|
167
|
+
}`
|
|
168
|
+
},
|
|
169
|
+
lib: {
|
|
170
|
+
code: `var __bp_lib = {
|
|
171
|
+
resolve: function (w, path) {
|
|
172
|
+
var parts = String(path).split('.'), ctx = w, val = w;
|
|
173
|
+
for (var i = 0; i < parts.length; i++) {
|
|
174
|
+
ctx = val;
|
|
175
|
+
val = val == null ? undefined : val[parts[i]];
|
|
176
|
+
}
|
|
177
|
+
return { ctx: ctx, val: val };
|
|
178
|
+
},
|
|
179
|
+
get: function (w, path) { return this.resolve(w, path).val; },
|
|
180
|
+
call: function (w, path, args) {
|
|
181
|
+
var r = this.resolve(w, path);
|
|
182
|
+
if (typeof r.val !== 'function') { if (typeof console !== 'undefined') console.warn('[blueprint] not a function: ' + path); return undefined; }
|
|
183
|
+
return r.val.apply(r.ctx, args);
|
|
184
|
+
},
|
|
185
|
+
create: function (w, path, args) {
|
|
186
|
+
var C = this.resolve(w, path).val;
|
|
187
|
+
if (typeof C !== 'function') { if (typeof console !== 'undefined') console.warn('[blueprint] not a class: ' + path); return null; }
|
|
188
|
+
return new (Function.prototype.bind.apply(C, [null].concat(args)))();
|
|
189
|
+
},
|
|
190
|
+
method: function (obj, name, args) {
|
|
191
|
+
if (obj == null || typeof obj[name] !== 'function') { if (typeof console !== 'undefined') console.warn('[blueprint] no method ' + name); return undefined; }
|
|
192
|
+
return obj[name].apply(obj, args);
|
|
193
|
+
}
|
|
194
|
+
};`
|
|
195
|
+
},
|
|
196
|
+
store: {
|
|
197
|
+
code: `var __bp_store = {
|
|
198
|
+
get: function (k) { try { return window.localStorage.getItem(k); } catch (e) { return null; } },
|
|
199
|
+
set: function (k, v) { try { window.localStorage.setItem(k, v == null ? '' : String(v)); } catch (e) {} },
|
|
200
|
+
remove: function (k) { try { window.localStorage.removeItem(k); } catch (e) {} }
|
|
201
|
+
};`
|
|
202
|
+
}
|
|
203
|
+
};
|
|
204
|
+
function resolveHelpers(names) {
|
|
205
|
+
const out = [];
|
|
206
|
+
const visit = (n) => {
|
|
207
|
+
if (out.includes(n)) return;
|
|
208
|
+
const h = HELPERS[n];
|
|
209
|
+
if (!h) throw new Error(`[blueprint] unknown helper "${n}"`);
|
|
210
|
+
(h.deps || []).forEach(visit);
|
|
211
|
+
out.push(n);
|
|
212
|
+
};
|
|
213
|
+
for (const n of names) visit(n);
|
|
214
|
+
return out;
|
|
215
|
+
}
|
|
216
|
+
function defineNode(def) {
|
|
217
|
+
return def;
|
|
218
|
+
}
|
|
219
|
+
function canConnect(from, to) {
|
|
220
|
+
if (from === "exec" || to === "exec") return from === to;
|
|
221
|
+
if (from === to || from === "any" || to === "any") return true;
|
|
222
|
+
if (to === "elements" && from === "element") return true;
|
|
223
|
+
if (to === "string" && (from === "number" || from === "boolean")) return true;
|
|
224
|
+
if (to === "boolean") return true;
|
|
225
|
+
return false;
|
|
226
|
+
}
|
|
227
|
+
class NodeRegistry {
|
|
228
|
+
constructor(defs = []) {
|
|
229
|
+
this.defs = /* @__PURE__ */ new Map();
|
|
230
|
+
defs.forEach((d) => this.add(d));
|
|
231
|
+
}
|
|
232
|
+
add(def) {
|
|
233
|
+
if (!def || typeof def.type !== "string" || !def.type) throw new Error("[blueprint] node definition without type");
|
|
234
|
+
if (def.compile !== void 0 && typeof def.compile !== "function") throw new Error(`[blueprint] node "${def.type}": compile must be a function`);
|
|
235
|
+
this.defs.set(def.type, def);
|
|
236
|
+
return this;
|
|
237
|
+
}
|
|
238
|
+
remove(type) {
|
|
239
|
+
return this.defs.delete(type);
|
|
240
|
+
}
|
|
241
|
+
get(type) {
|
|
242
|
+
return this.defs.get(type);
|
|
243
|
+
}
|
|
244
|
+
has(type) {
|
|
245
|
+
return this.defs.has(type);
|
|
246
|
+
}
|
|
247
|
+
list() {
|
|
248
|
+
return Array.from(this.defs.values());
|
|
249
|
+
}
|
|
250
|
+
categories() {
|
|
251
|
+
return Array.from(new Set(this.list().map((d) => d.category)));
|
|
252
|
+
}
|
|
253
|
+
/** Pins of a node instance (dynamic pins are computed from its props). */
|
|
254
|
+
ports(node, props) {
|
|
255
|
+
const type = typeof node === "string" ? node : node.type;
|
|
256
|
+
const def = this.defs.get(type);
|
|
257
|
+
if (!def) return { inputs: [], outputs: [] };
|
|
258
|
+
const p = typeof node === "string" ? props || {} : { ...defaultProps(def), ...node.props || {} };
|
|
259
|
+
const ports = typeof def.ports === "function" ? def.ports(p) : def.ports;
|
|
260
|
+
return { inputs: ports.inputs || [], outputs: ports.outputs || [] };
|
|
261
|
+
}
|
|
262
|
+
pin(node, side, pinId) {
|
|
263
|
+
const ports = this.ports(node);
|
|
264
|
+
return (side === "in" ? ports.inputs : ports.outputs).find((p) => p.id === pinId);
|
|
265
|
+
}
|
|
266
|
+
/** Whether the node is pure (no exec pins at all). */
|
|
267
|
+
isPure(node) {
|
|
268
|
+
const def = this.defs.get(node.type);
|
|
269
|
+
if (!def || def.event) return false;
|
|
270
|
+
const { inputs, outputs } = this.ports(node);
|
|
271
|
+
return !inputs.some((p) => p.type === "exec") && !outputs.some((p) => p.type === "exec");
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
function defaultProps(def) {
|
|
275
|
+
const out = {};
|
|
276
|
+
for (const p of def.props || []) if (p.default !== void 0) out[p.id] = clone(p.default);
|
|
277
|
+
if (typeof def.ports !== "function") {
|
|
278
|
+
for (const pin of def.ports.inputs) if (pin.default !== void 0 && !(pin.id in out)) out[pin.id] = clone(pin.default);
|
|
279
|
+
}
|
|
280
|
+
return out;
|
|
281
|
+
}
|
|
282
|
+
function clone(v) {
|
|
283
|
+
return v && typeof v === "object" ? JSON.parse(JSON.stringify(v)) : v;
|
|
284
|
+
}
|
|
285
|
+
const ROOT = "root";
|
|
286
|
+
const DOC = "__doc";
|
|
287
|
+
const WIN = "__win";
|
|
288
|
+
const DISPOSERS = "__d";
|
|
289
|
+
function jsLiteral(value) {
|
|
290
|
+
const json = JSON.stringify(value === void 0 ? null : value);
|
|
291
|
+
return json.replace(/</g, "\\u003c").replace(/\u2028/g, "\\u2028").replace(/\u2029/g, "\\u2029");
|
|
292
|
+
}
|
|
293
|
+
function ident(id) {
|
|
294
|
+
const s = String(id).replace(/[^A-Za-z0-9_$]/g, "_");
|
|
295
|
+
return /^[0-9]/.test(s) ? `_${s}` : s;
|
|
296
|
+
}
|
|
297
|
+
function commentText(s) {
|
|
298
|
+
return s.replace(/\*\//g, "* /").replace(/[\r\n\u2028\u2029]+/g, " ").trim();
|
|
299
|
+
}
|
|
300
|
+
function indentBlock(code, unit) {
|
|
301
|
+
return code.split("\n").map((l) => l.trim() ? unit + l : l).join("\n");
|
|
302
|
+
}
|
|
303
|
+
const key = (nodeId, pinId) => `${nodeId}\0${pinId}`;
|
|
304
|
+
function compileBlueprint(bp, registry, options = {}) {
|
|
305
|
+
return new Compiler(bp, registry, options).run();
|
|
306
|
+
}
|
|
307
|
+
class Compiler {
|
|
308
|
+
constructor(bp, registry, options) {
|
|
309
|
+
this.bp = bp;
|
|
310
|
+
this.registry = registry;
|
|
311
|
+
this.options = options;
|
|
312
|
+
this.nodes = /* @__PURE__ */ new Map();
|
|
313
|
+
this.dataIn = /* @__PURE__ */ new Map();
|
|
314
|
+
this.execOut = /* @__PURE__ */ new Map();
|
|
315
|
+
this.execInCount = /* @__PURE__ */ new Map();
|
|
316
|
+
this.usedOut = /* @__PURE__ */ new Set();
|
|
317
|
+
this.helpers = /* @__PURE__ */ new Set();
|
|
318
|
+
this.diagnostics = [];
|
|
319
|
+
this.declared = /* @__PURE__ */ new Map();
|
|
320
|
+
this.functions = /* @__PURE__ */ new Map();
|
|
321
|
+
this.fnOrder = [];
|
|
322
|
+
this.reached = /* @__PURE__ */ new Set();
|
|
323
|
+
this.localCounter = 0;
|
|
324
|
+
this.pureStack = [];
|
|
325
|
+
this.unit = options.indent ?? " ";
|
|
326
|
+
}
|
|
327
|
+
run() {
|
|
328
|
+
const bp = this.bp;
|
|
329
|
+
for (const n of bp.nodes || []) {
|
|
330
|
+
if (this.nodes.has(n.id)) this.diag("error", "duplicate-node", `Duplicate node id "${n.id}"`, n.id, void 0, void 0, [n.id]);
|
|
331
|
+
this.nodes.set(n.id, n);
|
|
332
|
+
}
|
|
333
|
+
this.indexLinks();
|
|
334
|
+
const events = (bp.nodes || []).map((n) => ({ n, def: this.registry.get(n.type) })).filter((x) => {
|
|
335
|
+
if (!x.def) {
|
|
336
|
+
this.diag("error", "unknown-node", `Unknown node type "${x.n.type}"`, x.n.id, void 0, void 0, [x.n.type]);
|
|
337
|
+
return false;
|
|
338
|
+
}
|
|
339
|
+
return !!x.def.event && !x.n.disabled;
|
|
340
|
+
}).sort((a, b) => (a.def.order || 0) - (b.def.order || 0) || a.n.pos[1] - b.n.pos[1] || a.n.pos[0] - b.n.pos[0]);
|
|
341
|
+
const setup = [];
|
|
342
|
+
for (const { n } of events) {
|
|
343
|
+
this.reached.add(n.id);
|
|
344
|
+
const code = this.compileNode(n, "");
|
|
345
|
+
if (code.trim()) setup.push(code);
|
|
346
|
+
}
|
|
347
|
+
for (const n of bp.nodes || []) {
|
|
348
|
+
const def = this.registry.get(n.type);
|
|
349
|
+
if (!def || def.event || n.disabled) continue;
|
|
350
|
+
if (!this.reached.has(n.id) && !this.registry.isPure(n)) {
|
|
351
|
+
this.diag("warning", "unreachable", "Not connected to any event, so it never runs", n.id);
|
|
352
|
+
}
|
|
353
|
+
}
|
|
354
|
+
const u = this.unit;
|
|
355
|
+
const vars = (bp.variables || []).map((v) => `var ${this.variable(v.id)} = ${jsLiteral(v.default ?? defaultOf(v.type))};`);
|
|
356
|
+
const state = Array.from(this.declared.entries()).map(([name, init]) => `var ${name}${init ? ` = ${init}` : ""};`);
|
|
357
|
+
const fns = this.fnOrder.map((name) => this.functions.get(name));
|
|
358
|
+
const header = this.options.comments === false ? [] : [`// ${commentText(bp.name || bp.id)} (${bp.kind})`];
|
|
359
|
+
const rest = [...vars, ...state, ...fns, ...setup].join("\n");
|
|
360
|
+
const uses = (name) => new RegExp(`(^|[^\\w$])${name.replace(/\$/g, "\\$")}([^\\w$]|$)`).test(rest);
|
|
361
|
+
const usesWin = uses(WIN);
|
|
362
|
+
const body = [
|
|
363
|
+
...header,
|
|
364
|
+
`var ${DISPOSERS} = [];`,
|
|
365
|
+
...uses(DOC) || usesWin ? [`var ${DOC} = ${ROOT}.nodeType === 9 ? ${ROOT} : ${ROOT}.ownerDocument;`] : [],
|
|
366
|
+
...usesWin ? [`var ${WIN} = ${DOC}.defaultView || window;`] : [],
|
|
367
|
+
...uses("params") ? [`var params = (ctx && ctx.params) || {};`] : [],
|
|
368
|
+
rest,
|
|
369
|
+
`return function () {
|
|
370
|
+
${u}${DISPOSERS}.splice(0).forEach(function (f) { try { f(); } catch (e) {} });
|
|
371
|
+
};`
|
|
372
|
+
].filter((l) => l.trim()).join("\n");
|
|
373
|
+
return {
|
|
374
|
+
id: bp.id,
|
|
375
|
+
name: bp.name,
|
|
376
|
+
code: `function (${ROOT}, ctx) {
|
|
377
|
+
${indentBlock(body, u)}
|
|
378
|
+
}`,
|
|
379
|
+
helpers: Array.from(this.helpers),
|
|
380
|
+
diagnostics: this.diagnostics
|
|
381
|
+
};
|
|
382
|
+
}
|
|
383
|
+
// ---------------------------------------------------------------- links
|
|
384
|
+
indexLinks() {
|
|
385
|
+
var _a, _b;
|
|
386
|
+
const seen = /* @__PURE__ */ new Set();
|
|
387
|
+
for (const l of this.bp.links || []) {
|
|
388
|
+
const from = this.nodes.get((_a = l.from) == null ? void 0 : _a[0]);
|
|
389
|
+
const to = this.nodes.get((_b = l.to) == null ? void 0 : _b[0]);
|
|
390
|
+
if (!from || !to) {
|
|
391
|
+
this.diag("error", "dangling-link", "Wire to a missing node", (from == null ? void 0 : from.id) || (to == null ? void 0 : to.id), void 0, l);
|
|
392
|
+
continue;
|
|
393
|
+
}
|
|
394
|
+
const outPin = this.registry.pin(from, "out", l.from[1]);
|
|
395
|
+
const inPin = this.registry.pin(to, "in", l.to[1]);
|
|
396
|
+
if (!outPin || !inPin) {
|
|
397
|
+
this.diag("error", "dangling-pin", "Wire to a missing pin", (outPin ? to : from).id, void 0, l);
|
|
398
|
+
continue;
|
|
399
|
+
}
|
|
400
|
+
const sig = `${l.from.join(".")}>${l.to.join(".")}`;
|
|
401
|
+
if (seen.has(sig)) continue;
|
|
402
|
+
seen.add(sig);
|
|
403
|
+
if (inPin.type === "exec") {
|
|
404
|
+
const k = key(l.from[0], l.from[1]);
|
|
405
|
+
const arr = this.execOut.get(k) || [];
|
|
406
|
+
arr.push(l);
|
|
407
|
+
this.execOut.set(k, arr);
|
|
408
|
+
const ik = key(l.to[0], l.to[1]);
|
|
409
|
+
this.execInCount.set(ik, (this.execInCount.get(ik) || 0) + 1);
|
|
410
|
+
} else {
|
|
411
|
+
const k = key(l.to[0], l.to[1]);
|
|
412
|
+
if (this.dataIn.has(k)) {
|
|
413
|
+
this.diag("error", "multiple-inputs", `Input "${inPin.label || inPin.id}" has more than one wire`, to.id, inPin.id, l, [inPin.label || inPin.id]);
|
|
414
|
+
continue;
|
|
415
|
+
}
|
|
416
|
+
this.dataIn.set(k, l);
|
|
417
|
+
this.usedOut.add(key(l.from[0], l.from[1]));
|
|
418
|
+
}
|
|
419
|
+
}
|
|
420
|
+
for (const arr of this.execOut.values()) {
|
|
421
|
+
arr.sort((a, b) => {
|
|
422
|
+
const na = this.nodes.get(a.to[0]);
|
|
423
|
+
const nb = this.nodes.get(b.to[0]);
|
|
424
|
+
return na.pos[1] - nb.pos[1] || na.pos[0] - nb.pos[0];
|
|
425
|
+
});
|
|
426
|
+
}
|
|
427
|
+
}
|
|
428
|
+
// ---------------------------------------------------------------- nodes
|
|
429
|
+
title(n) {
|
|
430
|
+
const def = this.registry.get(n.type);
|
|
431
|
+
if (!def) return n.type;
|
|
432
|
+
return this.options.title ? this.options.title(def, n) : def.title;
|
|
433
|
+
}
|
|
434
|
+
/** Code that runs exec node `id` entered through `entry`. */
|
|
435
|
+
emitExec(id, entry, stack) {
|
|
436
|
+
const n = this.nodes.get(id);
|
|
437
|
+
if (!n) return "";
|
|
438
|
+
this.reached.add(id);
|
|
439
|
+
const shared = (this.execInCount.get(key(id, entry)) || 0) > 1;
|
|
440
|
+
const recursive = stack.includes(key(id, entry));
|
|
441
|
+
if (shared || recursive) {
|
|
442
|
+
const fn = `_${ident(id)}_${ident(entry)}`;
|
|
443
|
+
if (!this.functions.has(fn)) {
|
|
444
|
+
this.functions.set(fn, "");
|
|
445
|
+
const body = this.compileNode(n, entry, [...stack, key(id, entry)]);
|
|
446
|
+
this.functions.set(fn, `function ${fn}() {
|
|
447
|
+
${indentBlock(body, this.unit)}
|
|
448
|
+
}`);
|
|
449
|
+
this.fnOrder.push(fn);
|
|
450
|
+
}
|
|
451
|
+
return `${fn}();`;
|
|
452
|
+
}
|
|
453
|
+
return this.compileNode(n, entry, [...stack, key(id, entry)]);
|
|
454
|
+
}
|
|
455
|
+
compileNode(n, entry, stack = []) {
|
|
456
|
+
const def = this.registry.get(n.type);
|
|
457
|
+
if (!def) return "";
|
|
458
|
+
const ports = this.registry.ports(n);
|
|
459
|
+
const ctx = this.context(n, def, ports.inputs, entry, stack);
|
|
460
|
+
let code;
|
|
461
|
+
if (n.disabled) {
|
|
462
|
+
const firstExec = ports.outputs.find((p) => p.type === "exec");
|
|
463
|
+
code = firstExec ? ctx.next(firstExec.id) : "";
|
|
464
|
+
} else {
|
|
465
|
+
let res;
|
|
466
|
+
try {
|
|
467
|
+
res = compileNode(def, ctx);
|
|
468
|
+
} catch (e) {
|
|
469
|
+
this.diag("error", "compile-error", e.message, n.id);
|
|
470
|
+
return "";
|
|
471
|
+
}
|
|
472
|
+
code = typeof res === "string" ? res : res.code || "";
|
|
473
|
+
}
|
|
474
|
+
if (!code.trim()) return "";
|
|
475
|
+
if (this.options.comments === false) return code.trim();
|
|
476
|
+
return `// ${commentText(`${n.id} · ${this.title(n)}`)}
|
|
477
|
+
${code.trim()}`;
|
|
478
|
+
}
|
|
479
|
+
/** Expression of a pure node's output. */
|
|
480
|
+
pureOutput(n, pinId) {
|
|
481
|
+
var _a;
|
|
482
|
+
const def = this.registry.get(n.type);
|
|
483
|
+
if (!def) return "undefined";
|
|
484
|
+
if (this.pureStack.includes(n.id)) {
|
|
485
|
+
this.diag("error", "cycle", "Depends on itself (a loop of data wires)", n.id);
|
|
486
|
+
return "undefined";
|
|
487
|
+
}
|
|
488
|
+
this.pureStack.push(n.id);
|
|
489
|
+
try {
|
|
490
|
+
const ctx = this.context(n, def, this.registry.ports(n).inputs, "", []);
|
|
491
|
+
const res = compileNode(def, ctx);
|
|
492
|
+
const expr = typeof res === "string" ? res : (_a = res.outputs) == null ? void 0 : _a[pinId];
|
|
493
|
+
if (expr == null) {
|
|
494
|
+
this.diag("error", "compile-error", `No output "${pinId}"`, n.id, pinId);
|
|
495
|
+
return "undefined";
|
|
496
|
+
}
|
|
497
|
+
return `(${expr})`;
|
|
498
|
+
} catch (e) {
|
|
499
|
+
this.diag("error", "compile-error", e.message, n.id);
|
|
500
|
+
return "undefined";
|
|
501
|
+
} finally {
|
|
502
|
+
this.pureStack.pop();
|
|
503
|
+
}
|
|
504
|
+
}
|
|
505
|
+
context(n, def, inputs, entry, stack) {
|
|
506
|
+
const props = { ...defaultProps(def), ...n.props || {} };
|
|
507
|
+
const self = this;
|
|
508
|
+
return {
|
|
509
|
+
node: n,
|
|
510
|
+
root: ROOT,
|
|
511
|
+
doc: DOC,
|
|
512
|
+
win: WIN,
|
|
513
|
+
entry,
|
|
514
|
+
prop(id) {
|
|
515
|
+
return props[id];
|
|
516
|
+
},
|
|
517
|
+
linked(pinId) {
|
|
518
|
+
return self.dataIn.has(key(n.id, pinId));
|
|
519
|
+
},
|
|
520
|
+
in(pinId) {
|
|
521
|
+
const pin = inputs.find((p) => p.id === pinId);
|
|
522
|
+
const link = self.dataIn.get(key(n.id, pinId));
|
|
523
|
+
if (link) {
|
|
524
|
+
const src = self.nodes.get(link.from[0]);
|
|
525
|
+
if (self.registry.isPure(src)) return self.pureOutput(src, link.from[1]);
|
|
526
|
+
return self.outVar(src.id, link.from[1]);
|
|
527
|
+
}
|
|
528
|
+
const value = pinId in props ? props[pinId] : pin == null ? void 0 : pin.default;
|
|
529
|
+
return self.literal(pin, value);
|
|
530
|
+
},
|
|
531
|
+
next(pinId) {
|
|
532
|
+
const links = self.execOut.get(key(n.id, pinId)) || [];
|
|
533
|
+
return links.map((l) => self.emitExec(l.to[0], l.to[1], stack)).filter((c) => c.trim()).join("\n");
|
|
534
|
+
},
|
|
535
|
+
out(pinId) {
|
|
536
|
+
return self.outVar(n.id, pinId);
|
|
537
|
+
},
|
|
538
|
+
used(pinId) {
|
|
539
|
+
return self.usedOut.has(key(n.id, pinId));
|
|
540
|
+
},
|
|
541
|
+
set(pinId, expr) {
|
|
542
|
+
return self.usedOut.has(key(n.id, pinId)) ? `${self.outVar(n.id, pinId)} = ${expr};` : "";
|
|
543
|
+
},
|
|
544
|
+
h(name) {
|
|
545
|
+
self.helpers.add(name);
|
|
546
|
+
return HELPER_PREFIX + name;
|
|
547
|
+
},
|
|
548
|
+
lit: jsLiteral,
|
|
549
|
+
local(hint = "l") {
|
|
550
|
+
return `_${ident(hint)}${++self.localCounter}`;
|
|
551
|
+
},
|
|
552
|
+
variable(id) {
|
|
553
|
+
if (!(self.bp.variables || []).some((v) => v.id === id)) {
|
|
554
|
+
self.diag("error", "unknown-variable", `Unknown variable "${id}"`, n.id);
|
|
555
|
+
}
|
|
556
|
+
return self.variable(id);
|
|
557
|
+
},
|
|
558
|
+
warn(message) {
|
|
559
|
+
self.diag("warning", "node-warning", message, n.id);
|
|
560
|
+
},
|
|
561
|
+
state(hint, init) {
|
|
562
|
+
const name = `_${ident(n.id)}_${ident(hint)}`;
|
|
563
|
+
if (!self.declared.has(name)) self.declared.set(name, init || "");
|
|
564
|
+
return name;
|
|
565
|
+
}
|
|
566
|
+
};
|
|
567
|
+
}
|
|
568
|
+
outVar(nodeId, pinId) {
|
|
569
|
+
const name = `_${ident(nodeId)}_${ident(pinId)}`;
|
|
570
|
+
if (!this.declared.has(name)) this.declared.set(name, "");
|
|
571
|
+
return name;
|
|
572
|
+
}
|
|
573
|
+
variable(id) {
|
|
574
|
+
return `v_${ident(id)}`;
|
|
575
|
+
}
|
|
576
|
+
/** JS for an unconnected input's literal, converted to the pin type. */
|
|
577
|
+
literal(pin, value) {
|
|
578
|
+
const type = (pin == null ? void 0 : pin.type) || "any";
|
|
579
|
+
switch (type) {
|
|
580
|
+
case "number": {
|
|
581
|
+
const num2 = typeof value === "number" ? value : parseFloat(String(value ?? ""));
|
|
582
|
+
return Number.isFinite(num2) ? String(num2) : "0";
|
|
583
|
+
}
|
|
584
|
+
case "boolean":
|
|
585
|
+
return value === true || value === "true" ? "true" : "false";
|
|
586
|
+
case "string":
|
|
587
|
+
return jsLiteral(value == null ? "" : String(value));
|
|
588
|
+
case "element":
|
|
589
|
+
case "elements": {
|
|
590
|
+
if (typeof value === "string" && value.trim()) {
|
|
591
|
+
this.helpers.add("q");
|
|
592
|
+
const q = `${HELPER_PREFIX}q(${ROOT}, ${jsLiteral(value.trim())})`;
|
|
593
|
+
return type === "element" ? `(${q}[0] || null)` : q;
|
|
594
|
+
}
|
|
595
|
+
return "null";
|
|
596
|
+
}
|
|
597
|
+
case "event":
|
|
598
|
+
return "null";
|
|
599
|
+
default:
|
|
600
|
+
return value === void 0 ? "undefined" : jsLiteral(value);
|
|
601
|
+
}
|
|
602
|
+
}
|
|
603
|
+
diag(severity, code, message, nodeId, pinId, link, args) {
|
|
604
|
+
this.diagnostics.push({ severity, code, message, ...args ? { args } : {}, ...nodeId ? { nodeId } : {}, ...pinId ? { pinId } : {}, ...link ? { link } : {} });
|
|
605
|
+
}
|
|
606
|
+
}
|
|
607
|
+
function defaultOf(type) {
|
|
608
|
+
switch (type) {
|
|
609
|
+
case "number":
|
|
610
|
+
return 0;
|
|
611
|
+
case "boolean":
|
|
612
|
+
return false;
|
|
613
|
+
case "string":
|
|
614
|
+
return "";
|
|
615
|
+
default:
|
|
616
|
+
return null;
|
|
617
|
+
}
|
|
618
|
+
}
|
|
619
|
+
function compileNode(def, ctx) {
|
|
620
|
+
if (!def.compile) {
|
|
621
|
+
ctx.warn(`node "${def.type}" is display-only and cannot be compiled`);
|
|
622
|
+
return "";
|
|
623
|
+
}
|
|
624
|
+
return def.compile(ctx);
|
|
625
|
+
}
|
|
626
|
+
const DEFAULT_BANNER = "Generated by blueprintsjs. Edit the blueprints, not this file.";
|
|
627
|
+
function buildScript(blueprints, registry, options = {}) {
|
|
628
|
+
const u = options.indent ?? " ";
|
|
629
|
+
const list = blueprints.filter((b) => b && b.kind !== "component" && (options.includeDisabled || b.enabled !== false));
|
|
630
|
+
const compiled = list.map((b) => compileBlueprint(b, registry, options));
|
|
631
|
+
const helpers = resolveHelpers(new Set(compiled.flatMap((c) => c.helpers)));
|
|
632
|
+
const diagnostics = compiled.flatMap((c) => c.diagnostics.map((d) => ({ ...d, blueprintId: c.id })));
|
|
633
|
+
const entries = compiled.map((c, i) => {
|
|
634
|
+
var _a, _b;
|
|
635
|
+
const when = list[i].kind === "template" ? ((_b = (_a = list[i].target) == null ? void 0 : _a.when) == null ? void 0 : _b.trim()) || null : null;
|
|
636
|
+
return `{ id: ${jsLiteral(c.id)}, when: ${jsLiteral(when)}, run: ${indentRest(c.code, u)} }`;
|
|
637
|
+
});
|
|
638
|
+
const start = [
|
|
639
|
+
"function start(doc) {",
|
|
640
|
+
`${u}var stops = [];`,
|
|
641
|
+
`${u}blueprints.forEach(function (b) {`,
|
|
642
|
+
`${u}${u}try {`,
|
|
643
|
+
`${u}${u}${u}if (b.when && !doc.querySelector(b.when)) return;`,
|
|
644
|
+
`${u}${u}${u}var stop = b.run(doc, {});`,
|
|
645
|
+
`${u}${u}${u}if (typeof stop === 'function') stops.push(stop);`,
|
|
646
|
+
`${u}${u}} catch (e) {`,
|
|
647
|
+
`${u}${u}${u}if (typeof console !== 'undefined') console.error('[blueprint ' + b.id + ']', e);`,
|
|
648
|
+
`${u}${u}}`,
|
|
649
|
+
`${u}});`,
|
|
650
|
+
`${u}return function () { stops.splice(0).forEach(function (s) { try { s(); } catch (e) {} }); };`,
|
|
651
|
+
"}"
|
|
652
|
+
].join("\n");
|
|
653
|
+
const inner = [
|
|
654
|
+
"'use strict';",
|
|
655
|
+
entries.length ? `var blueprints = [
|
|
656
|
+
${entries.map((e) => u + e).join(",\n")}
|
|
657
|
+
];` : "var blueprints = [];",
|
|
658
|
+
start,
|
|
659
|
+
...helpers.length ? ["// Runtime helpers", ...helpers.map((h) => HELPERS[h].code)] : []
|
|
660
|
+
];
|
|
661
|
+
let code;
|
|
662
|
+
if (options.target === "preview") {
|
|
663
|
+
code = `(function () {
|
|
664
|
+
${indent([...inner, "return start;"].join("\n"), u)}
|
|
665
|
+
})()`;
|
|
666
|
+
} else {
|
|
667
|
+
inner.push(
|
|
668
|
+
"if (document.readyState === 'loading') document.addEventListener('DOMContentLoaded', function () { start(document); });",
|
|
669
|
+
"else start(document);"
|
|
670
|
+
);
|
|
671
|
+
code = `(function () {
|
|
672
|
+
${indent(inner.join("\n"), u)}
|
|
673
|
+
})();
|
|
674
|
+
`;
|
|
675
|
+
}
|
|
676
|
+
const banner = options.banner === false ? "" : `/* ${(options.banner ?? DEFAULT_BANNER).replace(/\*\//g, "* /")} */
|
|
677
|
+
`;
|
|
678
|
+
return {
|
|
679
|
+
code: banner + code,
|
|
680
|
+
blueprints: compiled,
|
|
681
|
+
diagnostics,
|
|
682
|
+
hasErrors: diagnostics.some((d) => d.severity === "error")
|
|
683
|
+
};
|
|
684
|
+
}
|
|
685
|
+
function indent(code, u) {
|
|
686
|
+
return code.split("\n").map((l) => l.trim() ? u + l : l).join("\n");
|
|
687
|
+
}
|
|
688
|
+
function indentRest(code, u) {
|
|
689
|
+
const [first, ...rest] = code.split("\n");
|
|
690
|
+
return [first, ...rest.map((l) => l.trim() ? u + l : l)].join("\n");
|
|
691
|
+
}
|
|
692
|
+
function buildStandalone(bp, registry, options = {}) {
|
|
693
|
+
const u = options.indent ?? " ";
|
|
694
|
+
const compiled = compileBlueprint(bp, registry, options);
|
|
695
|
+
const helpers = resolveHelpers(compiled.helpers);
|
|
696
|
+
const inner = [...helpers.map((h) => HELPERS[h].code), `return (${indentRest(compiled.code, "")})(root, ctx);`].join("\n");
|
|
697
|
+
return { ...compiled, code: `function (root, ctx) {
|
|
698
|
+
${indent(inner, u)}
|
|
699
|
+
}` };
|
|
700
|
+
}
|
|
701
|
+
const DATA_PIN_TYPES = [
|
|
702
|
+
"any",
|
|
703
|
+
"string",
|
|
704
|
+
"number",
|
|
705
|
+
"boolean",
|
|
706
|
+
"element",
|
|
707
|
+
"elements",
|
|
708
|
+
"event",
|
|
709
|
+
"object"
|
|
710
|
+
];
|
|
711
|
+
const SCHEMA_VERSION = 1;
|
|
712
|
+
let counter = 0;
|
|
713
|
+
function uid(prefix = "id") {
|
|
714
|
+
counter = (counter + 1) % 1296;
|
|
715
|
+
return `${prefix}_${Date.now().toString(36).slice(-4)}${Math.random().toString(36).slice(2, 6)}${counter.toString(36)}`;
|
|
716
|
+
}
|
|
717
|
+
function createBlueprint(init) {
|
|
718
|
+
return normalizeBlueprint({ ...init, id: init.id || uid("bp") });
|
|
719
|
+
}
|
|
720
|
+
const KINDS = ["global", "template", "component"];
|
|
721
|
+
function normalizeBlueprint(raw) {
|
|
722
|
+
const r = raw && typeof raw === "object" ? raw : {};
|
|
723
|
+
const nodes = (Array.isArray(r.nodes) ? r.nodes : []).filter((n) => n && typeof n.id === "string" && n.id && typeof n.type === "string").map((n) => {
|
|
724
|
+
var _a, _b;
|
|
725
|
+
const out = { id: n.id, type: n.type, pos: [num((_a = n.pos) == null ? void 0 : _a[0]), num((_b = n.pos) == null ? void 0 : _b[1])] };
|
|
726
|
+
if (n.props && typeof n.props === "object") out.props = JSON.parse(JSON.stringify(n.props));
|
|
727
|
+
if (n.collapsed) out.collapsed = true;
|
|
728
|
+
if (n.disabled) out.disabled = true;
|
|
729
|
+
if (typeof n.title === "string") out.title = n.title;
|
|
730
|
+
if (Array.isArray(n.lines)) out.lines = n.lines.map(String);
|
|
731
|
+
if (typeof n.color === "string") out.color = n.color;
|
|
732
|
+
return out;
|
|
733
|
+
});
|
|
734
|
+
const ids = new Set(nodes.map((n) => n.id));
|
|
735
|
+
const links = (Array.isArray(r.links) ? r.links : []).filter((l) => validRef(l == null ? void 0 : l.from) && validRef(l == null ? void 0 : l.to) && ids.has(l.from[0]) && ids.has(l.to[0])).map((l) => ({ from: [l.from[0], l.from[1]], to: [l.to[0], l.to[1]] }));
|
|
736
|
+
const variables = (Array.isArray(r.variables) ? r.variables : []).filter((v) => v && typeof v.id === "string" && v.id).map((v) => ({ id: v.id, name: String(v.name || v.id), type: v.type || "any", ...v.default !== void 0 ? { default: v.default } : {} }));
|
|
737
|
+
const bp = {
|
|
738
|
+
schema: SCHEMA_VERSION,
|
|
739
|
+
id: typeof r.id === "string" && r.id ? r.id : uid("bp"),
|
|
740
|
+
name: typeof r.name === "string" && r.name.trim() ? r.name.trim() : "Blueprint",
|
|
741
|
+
kind: KINDS.includes(r.kind) ? r.kind : "global",
|
|
742
|
+
enabled: r.enabled !== false,
|
|
743
|
+
variables,
|
|
744
|
+
nodes,
|
|
745
|
+
links
|
|
746
|
+
};
|
|
747
|
+
if (typeof r.description === "string" && r.description) bp.description = r.description;
|
|
748
|
+
if (r.target && typeof r.target === "object") {
|
|
749
|
+
bp.target = {};
|
|
750
|
+
if (Array.isArray(r.target.pages)) bp.target.pages = r.target.pages.filter((x) => typeof x === "string");
|
|
751
|
+
if (typeof r.target.when === "string" && r.target.when.trim()) bp.target.when = r.target.when.trim();
|
|
752
|
+
}
|
|
753
|
+
if (r.component && typeof r.component === "object") bp.component = JSON.parse(JSON.stringify(r.component));
|
|
754
|
+
if (Array.isArray(r.libraries)) bp.libraries = r.libraries.filter((x) => typeof x === "string");
|
|
755
|
+
if (Array.isArray(r.comments)) bp.comments = JSON.parse(JSON.stringify(r.comments));
|
|
756
|
+
if (r.editor && typeof r.editor === "object") bp.editor = JSON.parse(JSON.stringify(r.editor));
|
|
757
|
+
return bp;
|
|
758
|
+
}
|
|
759
|
+
function num(v) {
|
|
760
|
+
const n = Number(v);
|
|
761
|
+
return Number.isFinite(n) ? Math.round(n) : 0;
|
|
762
|
+
}
|
|
763
|
+
function validRef(r) {
|
|
764
|
+
return Array.isArray(r) && typeof r[0] === "string" && typeof r[1] === "string" && !!r[0] && !!r[1];
|
|
765
|
+
}
|
|
766
|
+
function validateBlueprint(bp, registry, options = {}) {
|
|
767
|
+
const out = [];
|
|
768
|
+
const check = options.checkSelector ?? defaultSelectorCheck();
|
|
769
|
+
const nodes = new Map(bp.nodes.map((n) => [n.id, n]));
|
|
770
|
+
const vars = new Set((bp.variables || []).map((v) => v.id));
|
|
771
|
+
const connectedIn = /* @__PURE__ */ new Set();
|
|
772
|
+
for (const l of bp.links) {
|
|
773
|
+
const a = nodes.get(l.from[0]);
|
|
774
|
+
const b = nodes.get(l.to[0]);
|
|
775
|
+
if (!a || !b) {
|
|
776
|
+
out.push({ severity: "error", code: "dangling-link", message: "Wire to a missing node", link: l });
|
|
777
|
+
continue;
|
|
778
|
+
}
|
|
779
|
+
const pa = registry.pin(a, "out", l.from[1]);
|
|
780
|
+
const pb = registry.pin(b, "in", l.to[1]);
|
|
781
|
+
if (!pa || !pb) {
|
|
782
|
+
out.push({ severity: "error", code: "dangling-pin", message: "Wire to a missing pin", link: l, nodeId: (pa ? b : a).id });
|
|
783
|
+
continue;
|
|
784
|
+
}
|
|
785
|
+
if (!canConnect(pa.type, pb.type)) {
|
|
786
|
+
out.push({ severity: "error", code: "type-mismatch", message: `Cannot connect ${pa.type} to ${pb.type}`, args: [pa.type, pb.type], link: l, nodeId: b.id, pinId: pb.id });
|
|
787
|
+
}
|
|
788
|
+
connectedIn.add(`${b.id}\0${pb.id}`);
|
|
789
|
+
}
|
|
790
|
+
let events = 0;
|
|
791
|
+
for (const n of bp.nodes) {
|
|
792
|
+
const def = registry.get(n.type);
|
|
793
|
+
if (!def) {
|
|
794
|
+
out.push({ severity: "error", code: "unknown-node", message: `Unknown node type "${n.type}"`, args: [n.type], nodeId: n.id });
|
|
795
|
+
continue;
|
|
796
|
+
}
|
|
797
|
+
if (def.event && !n.disabled) events++;
|
|
798
|
+
const props = { ...defaultProps(def), ...n.props || {} };
|
|
799
|
+
for (const p of def.props || []) {
|
|
800
|
+
const v = props[p.id];
|
|
801
|
+
if (p.required && (v == null || String(v).trim() === "")) {
|
|
802
|
+
out.push({ severity: "error", code: "required-prop", message: `"${p.label || p.id}" is required`, args: [p.label || p.id], nodeId: n.id, pinId: p.id });
|
|
803
|
+
}
|
|
804
|
+
if (p.type === "variable" && v && !vars.has(String(v))) {
|
|
805
|
+
out.push({ severity: "error", code: "unknown-variable", message: `Unknown variable "${v}"`, args: [String(v)], nodeId: n.id, pinId: p.id });
|
|
806
|
+
}
|
|
807
|
+
if (p.type === "selector" && typeof v === "string" && v.trim() && !check(v.trim())) {
|
|
808
|
+
out.push({ severity: "error", code: "bad-selector", message: `Invalid selector "${v}"`, args: [String(v)], nodeId: n.id, pinId: p.id });
|
|
809
|
+
}
|
|
810
|
+
}
|
|
811
|
+
for (const pin of registry.ports(n).inputs) {
|
|
812
|
+
if (pin.type === "exec" || connectedIn.has(`${n.id}\0${pin.id}`)) continue;
|
|
813
|
+
const v = props[pin.id];
|
|
814
|
+
if (pin.required && (v == null || String(v).trim() === "" || pin.noLiteral)) {
|
|
815
|
+
out.push({ severity: "error", code: "required-input", message: `Input "${pin.label || pin.id}" is not set`, args: [pin.label || pin.id], nodeId: n.id, pinId: pin.id });
|
|
816
|
+
}
|
|
817
|
+
if ((pin.type === "elements" || pin.type === "element") && typeof v === "string" && v.trim() && !check(v.trim())) {
|
|
818
|
+
out.push({ severity: "error", code: "bad-selector", message: `Invalid selector "${v}"`, args: [String(v)], nodeId: n.id, pinId: pin.id });
|
|
819
|
+
}
|
|
820
|
+
}
|
|
821
|
+
}
|
|
822
|
+
if (bp.nodes.length && !events) {
|
|
823
|
+
out.push({ severity: "warning", code: "no-events", message: "The blueprint has no event node, so nothing will ever run" });
|
|
824
|
+
}
|
|
825
|
+
return out;
|
|
826
|
+
}
|
|
827
|
+
function defaultSelectorCheck() {
|
|
828
|
+
if (typeof document === "undefined") return () => true;
|
|
829
|
+
const frag = document.createDocumentFragment();
|
|
830
|
+
return (sel) => {
|
|
831
|
+
try {
|
|
832
|
+
frag.querySelector(sel);
|
|
833
|
+
return true;
|
|
834
|
+
} catch {
|
|
835
|
+
return false;
|
|
836
|
+
}
|
|
837
|
+
};
|
|
838
|
+
}
|
|
839
|
+
exports.DATA_PIN_TYPES = DATA_PIN_TYPES;
|
|
840
|
+
exports.DEFAULT_BANNER = DEFAULT_BANNER;
|
|
841
|
+
exports.HELPERS = HELPERS;
|
|
842
|
+
exports.HELPER_PREFIX = HELPER_PREFIX;
|
|
843
|
+
exports.NodeRegistry = NodeRegistry;
|
|
844
|
+
exports.SCHEMA_VERSION = SCHEMA_VERSION;
|
|
845
|
+
exports.autoLayout = autoLayout;
|
|
846
|
+
exports.buildScript = buildScript;
|
|
847
|
+
exports.buildStandalone = buildStandalone;
|
|
848
|
+
exports.canConnect = canConnect;
|
|
849
|
+
exports.compileBlueprint = compileBlueprint;
|
|
850
|
+
exports.createBlueprint = createBlueprint;
|
|
851
|
+
exports.defaultProps = defaultProps;
|
|
852
|
+
exports.defineNode = defineNode;
|
|
853
|
+
exports.ident = ident;
|
|
854
|
+
exports.jsLiteral = jsLiteral;
|
|
855
|
+
exports.normalizeBlueprint = normalizeBlueprint;
|
|
856
|
+
exports.resolveHelpers = resolveHelpers;
|
|
857
|
+
exports.uid = uid;
|
|
858
|
+
exports.validateBlueprint = validateBlueprint;
|
|
859
|
+
//# sourceMappingURL=validate-Ddbqd8bD.cjs.map
|