morphic-blocks 0.0.1 → 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/LICENSE +202 -0
- package/NOTICE +5 -0
- package/README.md +74 -6
- package/definitions.schema.json +283 -0
- package/dist/code-editor-YFXMCcWg.js +4 -0
- package/dist/index.d.ts +10 -0
- package/dist/morphic/MorphicBlocks.d.ts +360 -0
- package/dist/morphic/behavior-runtime.d.ts +3 -0
- package/dist/morphic/block-font.d.ts +34 -0
- package/dist/morphic/block-namespace.d.ts +34 -0
- package/dist/morphic/block-view.d.ts +54 -0
- package/dist/morphic/code-editor.d.ts +172 -0
- package/dist/morphic/codegen.d.ts +4 -0
- package/dist/morphic/constants.d.ts +7 -0
- package/dist/morphic/definitions.d.ts +28 -0
- package/dist/morphic/element-types.d.ts +20 -0
- package/dist/morphic/resize.d.ts +35 -0
- package/dist/morphic/selection-sync.d.ts +51 -0
- package/dist/morphic/styles.d.ts +36 -0
- package/dist/morphic/syntax-highlight.d.ts +14 -0
- package/dist/morphic/template-codegen.d.ts +16 -0
- package/dist/morphic/template.d.ts +45 -0
- package/dist/morphic/toolbar.d.ts +48 -0
- package/dist/morphic/toolbox-canvas.d.ts +53 -0
- package/dist/morphic/toolbox.d.ts +3 -0
- package/dist/morphic/types.d.ts +697 -0
- package/dist/morphic/validate-definitions.d.ts +31 -0
- package/dist/morphic/view-resolver.d.ts +17 -0
- package/dist/morphic-blocks.js +4265 -0
- package/dist/morphic-blocks.umd.cjs +30 -0
- package/dist/toolbar-kxlfr8TT.js +4 -0
- package/package.json +58 -5
|
@@ -0,0 +1,4265 @@
|
|
|
1
|
+
var nt = Object.defineProperty;
|
|
2
|
+
var it = (n, t, e) => t in n ? nt(n, t, { enumerable: !0, configurable: !0, writable: !0, value: e }) : n[t] = e;
|
|
3
|
+
var g = (n, t, e) => it(n, typeof t != "symbol" ? t + "" : t, e);
|
|
4
|
+
import "blockly/blocks";
|
|
5
|
+
import * as B from "blockly";
|
|
6
|
+
import { javascriptGenerator as ce, Order as je } from "blockly/javascript";
|
|
7
|
+
function ae(n) {
|
|
8
|
+
if (!(!n || typeof n == "function"))
|
|
9
|
+
return n;
|
|
10
|
+
}
|
|
11
|
+
function st(n) {
|
|
12
|
+
if (n)
|
|
13
|
+
return typeof n == "function" ? n : n.generate;
|
|
14
|
+
}
|
|
15
|
+
const le = "morphic-mode-", de = "morphic-context-", Le = "morphic-category-", rt = "morphic-block-", ct = -1, at = 0, lt = 1;
|
|
16
|
+
function j(n) {
|
|
17
|
+
if (n !== void 0)
|
|
18
|
+
return typeof n == "string" ? n : n.type;
|
|
19
|
+
}
|
|
20
|
+
const U = 16;
|
|
21
|
+
function dt(n) {
|
|
22
|
+
if (!n || typeof n == "string")
|
|
23
|
+
return { width: U, height: U };
|
|
24
|
+
const t = n.size;
|
|
25
|
+
if (t === void 0)
|
|
26
|
+
return { width: U, height: U };
|
|
27
|
+
if (typeof t == "number")
|
|
28
|
+
return { width: t, height: t };
|
|
29
|
+
const e = /^(\d+)(?:x(\d+))?$/i.exec(t.trim());
|
|
30
|
+
if (!e)
|
|
31
|
+
return { width: U, height: U };
|
|
32
|
+
const o = Number.parseInt(e[1], 10), i = e[2] ? Number.parseInt(e[2], 10) : o;
|
|
33
|
+
return { width: o, height: i };
|
|
34
|
+
}
|
|
35
|
+
function ht(n, t) {
|
|
36
|
+
if (n != null && n.default) return n.default;
|
|
37
|
+
if (!t || typeof t == "string") return;
|
|
38
|
+
const e = t.empty;
|
|
39
|
+
if (!e) return;
|
|
40
|
+
const o = n != null && n.check ? Array.isArray(n.check) ? n.check[0] : n.check : void 0;
|
|
41
|
+
return (o ? e[o] : void 0) ?? e.default;
|
|
42
|
+
}
|
|
43
|
+
const Z = 18;
|
|
44
|
+
function Y(n) {
|
|
45
|
+
const t = [];
|
|
46
|
+
let e = 0;
|
|
47
|
+
for (; e < n.length; ) {
|
|
48
|
+
const o = n[e];
|
|
49
|
+
if (o === "<" && /^<img\b/i.test(n.slice(e))) {
|
|
50
|
+
const s = n.indexOf(">", e);
|
|
51
|
+
if (s === -1) {
|
|
52
|
+
Be(t, n.slice(e));
|
|
53
|
+
break;
|
|
54
|
+
}
|
|
55
|
+
const r = n.slice(e, s + 1), c = gt(r);
|
|
56
|
+
c && t.push(c), e = s + 1;
|
|
57
|
+
continue;
|
|
58
|
+
}
|
|
59
|
+
if (o === "%" && /\d/.test(n[e + 1] ?? "")) {
|
|
60
|
+
let s = e + 1;
|
|
61
|
+
for (; s < n.length && /\d/.test(n[s] ?? ""); )
|
|
62
|
+
s += 1;
|
|
63
|
+
const r = Number.parseInt(n.slice(e + 1, s), 10);
|
|
64
|
+
Number.isNaN(r) || t.push({ kind: "placeholder", index: r }), e = s;
|
|
65
|
+
continue;
|
|
66
|
+
}
|
|
67
|
+
if (o === "%" && /[A-Z_]/.test(n[e + 1] ?? "")) {
|
|
68
|
+
let s = e + 1;
|
|
69
|
+
for (; s < n.length && /[A-Z0-9_]/.test(n[s] ?? ""); )
|
|
70
|
+
s += 1;
|
|
71
|
+
const r = n.slice(e + 1, s);
|
|
72
|
+
t.push({ kind: "field", name: r }), e = s;
|
|
73
|
+
continue;
|
|
74
|
+
}
|
|
75
|
+
let i = e + 1;
|
|
76
|
+
for (; i < n.length; ) {
|
|
77
|
+
const s = n[i], r = s === "%" && /\d/.test(n[i + 1] ?? ""), c = s === "%" && /[A-Z_]/.test(n[i + 1] ?? "");
|
|
78
|
+
if (s === "<" && /^<img\b/i.test(n.slice(i)) || r || c)
|
|
79
|
+
break;
|
|
80
|
+
i += 1;
|
|
81
|
+
}
|
|
82
|
+
Be(t, n.slice(e, i)), e = i;
|
|
83
|
+
}
|
|
84
|
+
return kt(t);
|
|
85
|
+
}
|
|
86
|
+
function ut(n) {
|
|
87
|
+
return n.replace(/\s+/g, " ").trim();
|
|
88
|
+
}
|
|
89
|
+
function P(n) {
|
|
90
|
+
return n.trim().toLowerCase().replace(/[^a-z0-9_-]+/g, "-");
|
|
91
|
+
}
|
|
92
|
+
function Te(n) {
|
|
93
|
+
return n.map((t) => t.kind === "text" ? pt(t.value) : t.kind === "image" ? `<img src="${re(t.src)}" alt="${re(t.alt)}" width="${t.width}" height="${t.height}">` : t.kind === "field" ? `<span class="morphic-field-placeholder" data-field-name="${re(t.name)}"></span>` : `<span class="morphic-toolbox-slot" data-slot-index="${t.index}"></span>`).join("");
|
|
94
|
+
}
|
|
95
|
+
function pt(n) {
|
|
96
|
+
return n.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">").replace(/"/g, """);
|
|
97
|
+
}
|
|
98
|
+
function re(n) {
|
|
99
|
+
return n.replace(/"/g, """).replace(/'/g, "'");
|
|
100
|
+
}
|
|
101
|
+
const ft = /\.(svg|png|jpe?g|gif|webp)(?:[#?].*)?$/i;
|
|
102
|
+
function mt(n, t) {
|
|
103
|
+
if (!n) return n;
|
|
104
|
+
const e = n.trim();
|
|
105
|
+
return /^<img\b/i.test(e) ? n : ft.test(e) ? `<img src="${re(e)}" width="${t.width}" height="${t.height}">` : n;
|
|
106
|
+
}
|
|
107
|
+
function gt(n) {
|
|
108
|
+
if (!/^<img\b/i.test(n))
|
|
109
|
+
return null;
|
|
110
|
+
const t = q(n, "src");
|
|
111
|
+
if (!t)
|
|
112
|
+
return null;
|
|
113
|
+
const e = q(n, "alt") ?? "", o = Number.parseInt(q(n, "width") ?? `${Z}`, 10), i = Number.parseInt(q(n, "height") ?? `${Z}`, 10);
|
|
114
|
+
return {
|
|
115
|
+
kind: "image",
|
|
116
|
+
src: t,
|
|
117
|
+
alt: e,
|
|
118
|
+
width: Number.isNaN(o) ? Z : o,
|
|
119
|
+
height: Number.isNaN(i) ? Z : i
|
|
120
|
+
};
|
|
121
|
+
}
|
|
122
|
+
function q(n, t) {
|
|
123
|
+
const e = new RegExp(`${t}\\s*=\\s*(['"])(.*?)\\1`, "i").exec(n);
|
|
124
|
+
if (e != null && e[2])
|
|
125
|
+
return e[2];
|
|
126
|
+
const o = new RegExp(`${t}\\s*=\\s*([^\\s>]+)`, "i").exec(n);
|
|
127
|
+
return (o == null ? void 0 : o[1]) ?? null;
|
|
128
|
+
}
|
|
129
|
+
function Be(n, t) {
|
|
130
|
+
t && n.push({ kind: "text", value: t });
|
|
131
|
+
}
|
|
132
|
+
function kt(n) {
|
|
133
|
+
const t = [];
|
|
134
|
+
for (const e of n) {
|
|
135
|
+
const o = t[t.length - 1];
|
|
136
|
+
if (e.kind === "text" && (o == null ? void 0 : o.kind) === "text") {
|
|
137
|
+
o.value += e.value;
|
|
138
|
+
continue;
|
|
139
|
+
}
|
|
140
|
+
t.push(e);
|
|
141
|
+
}
|
|
142
|
+
return t;
|
|
143
|
+
}
|
|
144
|
+
function Ue(n) {
|
|
145
|
+
const { block: t, definition: e, view: o, mode: i, context: s, elementTypes: r, resolveBlocklyType: c } = n, a = Mt(t);
|
|
146
|
+
Ot(t), Et(t, e, o), $t(t, e), typeof e.inputsInline == "boolean" && t.setInputsInline(e.inputsInline), e.color !== void 0 && t.setColour(e.color), e.tooltip && t.setTooltip(e.tooltip), e.helpUrl && t.setHelpUrl(e.helpUrl), Dt(t, a), r && It(t, e, o, r, c), bt(t, i, s);
|
|
147
|
+
const l = t;
|
|
148
|
+
l.__morphicMode = i, l.__morphicContext = s, t.rendered && t.render();
|
|
149
|
+
}
|
|
150
|
+
function fe(n, t, e, o) {
|
|
151
|
+
J(n.classList, le), J(n.classList, de), n.classList.add(o), n.classList.add(`${le}${P(t)}`), n.classList.add(`${de}${e}`);
|
|
152
|
+
}
|
|
153
|
+
function vt(n) {
|
|
154
|
+
return n.__morphicMode;
|
|
155
|
+
}
|
|
156
|
+
function wt(n, t) {
|
|
157
|
+
const e = n.getSvgRoot();
|
|
158
|
+
e && (J(e.classList, Le), t && e.classList.add(`${Le}${t}`));
|
|
159
|
+
}
|
|
160
|
+
function yt(n, t) {
|
|
161
|
+
const e = n.getSvgRoot();
|
|
162
|
+
if (!e)
|
|
163
|
+
return;
|
|
164
|
+
const o = `${rt}${P(t)}`;
|
|
165
|
+
e.classList.contains(o) || e.classList.add(o);
|
|
166
|
+
}
|
|
167
|
+
function Ct(n) {
|
|
168
|
+
const t = n.getSvgRoot();
|
|
169
|
+
if (!t)
|
|
170
|
+
return;
|
|
171
|
+
const e = getComputedStyle(t).getPropertyValue("--morphic-block-color").trim();
|
|
172
|
+
e && (n.setColour(e), n.rendered && n.render());
|
|
173
|
+
}
|
|
174
|
+
function bt(n, t, e) {
|
|
175
|
+
const o = n.getSvgRoot();
|
|
176
|
+
o && (J(o.classList, le), J(o.classList, de), o.classList.add(`${le}${P(t)}`), o.classList.add(`${de}${e}`));
|
|
177
|
+
}
|
|
178
|
+
function Et(n, t, e) {
|
|
179
|
+
var r;
|
|
180
|
+
const o = Y(e.template), i = [], s = /* @__PURE__ */ new Set();
|
|
181
|
+
for (const c of o) {
|
|
182
|
+
if (c.kind === "text") {
|
|
183
|
+
const d = ut(c.value);
|
|
184
|
+
d && i.push(d);
|
|
185
|
+
continue;
|
|
186
|
+
}
|
|
187
|
+
if (c.kind === "image") {
|
|
188
|
+
i.push(
|
|
189
|
+
new B.FieldImage(c.src, c.width, c.height, c.alt)
|
|
190
|
+
);
|
|
191
|
+
continue;
|
|
192
|
+
}
|
|
193
|
+
if (c.kind === "field") {
|
|
194
|
+
const d = (r = t.fields) == null ? void 0 : r[c.name];
|
|
195
|
+
d && i.push({
|
|
196
|
+
field: Tt(d, e.elementName),
|
|
197
|
+
name: c.name
|
|
198
|
+
});
|
|
199
|
+
continue;
|
|
200
|
+
}
|
|
201
|
+
if (s.has(c.index)) {
|
|
202
|
+
const d = n.appendDummyInput(
|
|
203
|
+
`DUPLICATE_${c.index}_${n.inputList.length}`
|
|
204
|
+
);
|
|
205
|
+
Q(d, i), d.appendField(`%${c.index}`);
|
|
206
|
+
continue;
|
|
207
|
+
}
|
|
208
|
+
s.add(c.index);
|
|
209
|
+
const a = St(t, e, c.index);
|
|
210
|
+
if ((a == null ? void 0 : a.kind) === "statement" && i.length > 0) {
|
|
211
|
+
const d = n.appendDummyInput(`PRE_STMT_${n.inputList.length}`);
|
|
212
|
+
Q(d, i);
|
|
213
|
+
}
|
|
214
|
+
const l = xt(n, a, c.index);
|
|
215
|
+
Q(l, i), a != null && a.label && l.appendField(a.label);
|
|
216
|
+
}
|
|
217
|
+
if (i.length > 0) {
|
|
218
|
+
const c = n.appendDummyInput(
|
|
219
|
+
`TRAILING_${n.inputList.length}`
|
|
220
|
+
);
|
|
221
|
+
Q(c, i);
|
|
222
|
+
}
|
|
223
|
+
n.inputList.length === 0 && n.appendDummyInput("LABEL").appendField(t.identifier);
|
|
224
|
+
}
|
|
225
|
+
function St(n, t, e) {
|
|
226
|
+
var i, s;
|
|
227
|
+
const o = `${e}`;
|
|
228
|
+
return ((i = t.inputSlots) == null ? void 0 : i[o]) ?? ((s = n.inputSlots) == null ? void 0 : s[o]);
|
|
229
|
+
}
|
|
230
|
+
function xt(n, t, e) {
|
|
231
|
+
const o = (t == null ? void 0 : t.name) ?? `ARG${e}`, i = (t == null ? void 0 : t.kind) ?? "value";
|
|
232
|
+
let s;
|
|
233
|
+
return i === "statement" ? (s = n.appendStatementInput(o), t != null && t.check && s.setCheck(t.check)) : (s = n.appendValueInput(o), t != null && t.check && s.setCheck(t.check)), t != null && t.align && s.setAlign(Lt(t.align)), s;
|
|
234
|
+
}
|
|
235
|
+
function Lt(n) {
|
|
236
|
+
return n === "right" ? lt : n === "centre" ? at : ct;
|
|
237
|
+
}
|
|
238
|
+
function Q(n, t) {
|
|
239
|
+
for (const e of t)
|
|
240
|
+
typeof e == "object" && "field" in e ? n.appendField(e.field, e.name) : n.appendField(e);
|
|
241
|
+
t.length = 0;
|
|
242
|
+
}
|
|
243
|
+
function Tt(n, t) {
|
|
244
|
+
switch (n.type) {
|
|
245
|
+
case "dropdown": {
|
|
246
|
+
const e = n.options.map((i) => Bt(i, t)), o = new B.FieldDropdown(e);
|
|
247
|
+
return n.default !== void 0 && o.setValue(n.default), o;
|
|
248
|
+
}
|
|
249
|
+
case "number":
|
|
250
|
+
return new B.FieldNumber(n.default ?? 0, n.min, n.max, n.precision);
|
|
251
|
+
case "checkbox":
|
|
252
|
+
return new B.FieldCheckbox(n.default ? "TRUE" : "FALSE");
|
|
253
|
+
case "text":
|
|
254
|
+
default:
|
|
255
|
+
return new B.FieldTextInput(n.default ?? "");
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
function Bt(n, t) {
|
|
259
|
+
var o;
|
|
260
|
+
if (typeof n == "string") return [n, n];
|
|
261
|
+
if (Array.isArray(n)) {
|
|
262
|
+
const [i, s] = n;
|
|
263
|
+
return [s ?? i, i];
|
|
264
|
+
}
|
|
265
|
+
return [(t ? (o = n.display) == null ? void 0 : o[t] : void 0) ?? n.label ?? n.value, n.value];
|
|
266
|
+
}
|
|
267
|
+
function Mt(n) {
|
|
268
|
+
var e;
|
|
269
|
+
const t = /* @__PURE__ */ new Map();
|
|
270
|
+
for (const o of n.inputList) {
|
|
271
|
+
const i = (e = o.connection) == null ? void 0 : e.targetBlock();
|
|
272
|
+
i && !i.isShadow() && t.set(o.name, i);
|
|
273
|
+
}
|
|
274
|
+
return t;
|
|
275
|
+
}
|
|
276
|
+
function It(n, t, e, o, i = (s) => s) {
|
|
277
|
+
var a, l;
|
|
278
|
+
const s = e.elementName ? o[e.elementName] : void 0, r = /* @__PURE__ */ new Map();
|
|
279
|
+
for (const d of Object.values(t.inputSlots ?? {}))
|
|
280
|
+
d != null && d.name && (d.kind ?? "value") === "value" && r.set(d.name, d);
|
|
281
|
+
if (r.size === 0) return;
|
|
282
|
+
const c = n.workspace;
|
|
283
|
+
for (const d of n.inputList) {
|
|
284
|
+
const u = d.connection;
|
|
285
|
+
if (!u || u.type !== B.INPUT_VALUE) continue;
|
|
286
|
+
const w = r.get(d.name);
|
|
287
|
+
if (!w) continue;
|
|
288
|
+
const v = ht(w, s);
|
|
289
|
+
if (v) {
|
|
290
|
+
if (v.shadow && !(((a = u.getShadowState) == null ? void 0 : a.call(u, !0)) ?? ((l = u.getShadowDom) == null ? void 0 : l.call(u))))
|
|
291
|
+
try {
|
|
292
|
+
u.setShadowState({
|
|
293
|
+
type: i(v.shadow),
|
|
294
|
+
fields: v.fieldValues ?? {}
|
|
295
|
+
});
|
|
296
|
+
} catch {
|
|
297
|
+
}
|
|
298
|
+
if (v.placeholder) {
|
|
299
|
+
const b = n, E = b.__morphicPlaceholderSlots ?? (b.__morphicPlaceholderSlots = /* @__PURE__ */ new Set()), k = u.targetBlock(), m = !k || k.isShadow(), p = E.has(d.name);
|
|
300
|
+
if (E.add(d.name), m && !p)
|
|
301
|
+
try {
|
|
302
|
+
const y = c.newBlock(i(v.placeholder));
|
|
303
|
+
if (v.fieldValues)
|
|
304
|
+
for (const [S, h] of Object.entries(v.fieldValues)) {
|
|
305
|
+
const f = y.getField(S);
|
|
306
|
+
f && f.setValue(h);
|
|
307
|
+
}
|
|
308
|
+
n.rendered && (y.initSvg(), y.render()), y.outputConnection && u.connect(y.outputConnection);
|
|
309
|
+
} catch {
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
}
|
|
314
|
+
}
|
|
315
|
+
function Dt(n, t) {
|
|
316
|
+
for (const [e, o] of t) {
|
|
317
|
+
const i = n.getInput(e);
|
|
318
|
+
if (!(i != null && i.connection) || i.connection.isConnected())
|
|
319
|
+
continue;
|
|
320
|
+
const s = [
|
|
321
|
+
o.outputConnection,
|
|
322
|
+
o.previousConnection
|
|
323
|
+
];
|
|
324
|
+
for (const r of s)
|
|
325
|
+
if (r)
|
|
326
|
+
try {
|
|
327
|
+
i.connection.connect(r);
|
|
328
|
+
break;
|
|
329
|
+
} catch {
|
|
330
|
+
continue;
|
|
331
|
+
}
|
|
332
|
+
}
|
|
333
|
+
}
|
|
334
|
+
function At(n) {
|
|
335
|
+
const t = /* @__PURE__ */ new Map();
|
|
336
|
+
for (const e of n.inputList)
|
|
337
|
+
for (const o of e.fieldRow)
|
|
338
|
+
o.name && t.set(o.name, o.getValue());
|
|
339
|
+
return t;
|
|
340
|
+
}
|
|
341
|
+
function Nt(n, t) {
|
|
342
|
+
for (const [e, o] of t) {
|
|
343
|
+
const i = n.getField(e);
|
|
344
|
+
if (i && o !== void 0 && o !== null)
|
|
345
|
+
try {
|
|
346
|
+
i.setValue(o);
|
|
347
|
+
} catch {
|
|
348
|
+
}
|
|
349
|
+
}
|
|
350
|
+
}
|
|
351
|
+
function Ot(n) {
|
|
352
|
+
for (; n.inputList.length > 0; ) {
|
|
353
|
+
const t = n.inputList[0];
|
|
354
|
+
if (!t)
|
|
355
|
+
break;
|
|
356
|
+
n.removeInput(t.name, !0);
|
|
357
|
+
}
|
|
358
|
+
}
|
|
359
|
+
function $t(n, t) {
|
|
360
|
+
t.output !== void 0 && Ht(n, t.output), t.previousStatement !== void 0 && Pt(n, t.previousStatement), t.nextStatement !== void 0 && Ft(n, t.nextStatement);
|
|
361
|
+
}
|
|
362
|
+
function Ht(n, t) {
|
|
363
|
+
if (t === !1) {
|
|
364
|
+
n.setOutput(!1);
|
|
365
|
+
return;
|
|
366
|
+
}
|
|
367
|
+
if (t === !0) {
|
|
368
|
+
n.setOutput(!0);
|
|
369
|
+
return;
|
|
370
|
+
}
|
|
371
|
+
n.setOutput(!0, t);
|
|
372
|
+
}
|
|
373
|
+
function Pt(n, t) {
|
|
374
|
+
if (t === !1) {
|
|
375
|
+
n.setPreviousStatement(!1);
|
|
376
|
+
return;
|
|
377
|
+
}
|
|
378
|
+
if (t === !0) {
|
|
379
|
+
n.setPreviousStatement(!0);
|
|
380
|
+
return;
|
|
381
|
+
}
|
|
382
|
+
n.setPreviousStatement(!0, t);
|
|
383
|
+
}
|
|
384
|
+
function Ft(n, t) {
|
|
385
|
+
if (t === !1) {
|
|
386
|
+
n.setNextStatement(!1);
|
|
387
|
+
return;
|
|
388
|
+
}
|
|
389
|
+
if (t === !0) {
|
|
390
|
+
n.setNextStatement(!0);
|
|
391
|
+
return;
|
|
392
|
+
}
|
|
393
|
+
n.setNextStatement(!0, t);
|
|
394
|
+
}
|
|
395
|
+
function J(n, t) {
|
|
396
|
+
const e = Array.from(n).filter(
|
|
397
|
+
(o) => o.startsWith(t)
|
|
398
|
+
);
|
|
399
|
+
for (const o of e)
|
|
400
|
+
n.remove(o);
|
|
401
|
+
}
|
|
402
|
+
function Me(n, t, e) {
|
|
403
|
+
if (!e) return [];
|
|
404
|
+
const o = new Set(e.keywords ?? []), i = e.strings ?? [], s = e.comment, r = e.numbers !== !1, c = n.Decoration.mark({ class: "morphic-tok-keyword" }), a = n.Decoration.mark({ class: "morphic-tok-string" }), l = n.Decoration.mark({ class: "morphic-tok-number" }), d = n.Decoration.mark({ class: "morphic-tok-comment" }), u = (p, y, S) => {
|
|
405
|
+
const h = p.length;
|
|
406
|
+
let f = 0;
|
|
407
|
+
for (; f < h; ) {
|
|
408
|
+
if (s && p.startsWith(s, f)) {
|
|
409
|
+
S.add(y + f, y + h, d);
|
|
410
|
+
return;
|
|
411
|
+
}
|
|
412
|
+
let x = !1;
|
|
413
|
+
for (const C of i)
|
|
414
|
+
if (p.startsWith(C, f)) {
|
|
415
|
+
const I = f;
|
|
416
|
+
for (f += C.length; f < h; ) {
|
|
417
|
+
if (p[f] === "\\" && f + 1 < h) {
|
|
418
|
+
f += 2;
|
|
419
|
+
continue;
|
|
420
|
+
}
|
|
421
|
+
if (p.startsWith(C, f)) {
|
|
422
|
+
f += C.length;
|
|
423
|
+
break;
|
|
424
|
+
}
|
|
425
|
+
f += 1;
|
|
426
|
+
}
|
|
427
|
+
S.add(y + I, y + f, a), x = !0;
|
|
428
|
+
break;
|
|
429
|
+
}
|
|
430
|
+
if (x) continue;
|
|
431
|
+
if (r) {
|
|
432
|
+
const C = /^\d+(?:\.\d+)?/.exec(p.slice(f));
|
|
433
|
+
if (C) {
|
|
434
|
+
S.add(y + f, y + f + C[0].length, l), f += C[0].length;
|
|
435
|
+
continue;
|
|
436
|
+
}
|
|
437
|
+
}
|
|
438
|
+
const T = /^[A-Za-z_]\w*/.exec(p.slice(f));
|
|
439
|
+
if (T) {
|
|
440
|
+
o.has(T[0]) && S.add(y + f, y + f + T[0].length, c), f += T[0].length;
|
|
441
|
+
continue;
|
|
442
|
+
}
|
|
443
|
+
f += 1;
|
|
444
|
+
}
|
|
445
|
+
}, w = (p) => {
|
|
446
|
+
const y = new t.RangeSetBuilder(), S = p.state.doc;
|
|
447
|
+
for (const { from: h, to: f } of p.visibleRanges) {
|
|
448
|
+
let x = h;
|
|
449
|
+
for (; x <= f; ) {
|
|
450
|
+
const T = S.lineAt(x);
|
|
451
|
+
if (u(T.text, T.from, y), T.to >= f) break;
|
|
452
|
+
x = T.to + 1;
|
|
453
|
+
}
|
|
454
|
+
}
|
|
455
|
+
return y.finish();
|
|
456
|
+
}, v = n.ViewPlugin.fromClass(
|
|
457
|
+
class {
|
|
458
|
+
constructor(p) {
|
|
459
|
+
g(this, "decorations");
|
|
460
|
+
this.decorations = w(p);
|
|
461
|
+
}
|
|
462
|
+
update(p) {
|
|
463
|
+
(p.docChanged || p.viewportChanged) && (this.decorations = w(p.view));
|
|
464
|
+
}
|
|
465
|
+
},
|
|
466
|
+
{ decorations: (p) => p.decorations }
|
|
467
|
+
), b = n.EditorView.baseTheme({
|
|
468
|
+
".morphic-tok-keyword": { color: "#cc7832" },
|
|
469
|
+
".morphic-tok-string": { color: "#6a8759" },
|
|
470
|
+
".morphic-tok-number": { color: "#6897bb" },
|
|
471
|
+
".morphic-tok-comment": { color: "#808080", fontStyle: "italic" }
|
|
472
|
+
}), E = e.colors ?? {}, k = {};
|
|
473
|
+
E.keyword && (k[".morphic-tok-keyword"] = { color: E.keyword }), E.string && (k[".morphic-tok-string"] = { color: E.string }), E.number && (k[".morphic-tok-number"] = { color: E.number }), E.comment && (k[".morphic-tok-comment"] = { color: E.comment, fontStyle: "italic" });
|
|
474
|
+
const m = [v, b];
|
|
475
|
+
return Object.keys(k).length > 0 && m.push(n.EditorView.theme(k)), m;
|
|
476
|
+
}
|
|
477
|
+
const Ce = "morphic/block-id";
|
|
478
|
+
let he;
|
|
479
|
+
function Ie() {
|
|
480
|
+
return he;
|
|
481
|
+
}
|
|
482
|
+
function De(n) {
|
|
483
|
+
he = n;
|
|
484
|
+
}
|
|
485
|
+
const Ge = {
|
|
486
|
+
fontSize: "14px",
|
|
487
|
+
fontFamily: "monospace",
|
|
488
|
+
lineHeight: 1.5,
|
|
489
|
+
background: "#1e1e1e",
|
|
490
|
+
foreground: "#d4d4d4",
|
|
491
|
+
gutterBackground: "#1e1e1e",
|
|
492
|
+
gutterForeground: "#858585",
|
|
493
|
+
selectionBackground: "#264f78"
|
|
494
|
+
}, ee = (n, t) => `color-mix(in srgb, ${n} ${t}%, transparent)`;
|
|
495
|
+
let Ae = !1;
|
|
496
|
+
async function Rt() {
|
|
497
|
+
if (Ae) return;
|
|
498
|
+
Ae = !0;
|
|
499
|
+
const t = (await import("./code-editor-YFXMCcWg.js")).default, e = document.createElement("style");
|
|
500
|
+
e.dataset.morphicSource = "code-editor", e.textContent = t, document.head.appendChild(e);
|
|
501
|
+
}
|
|
502
|
+
const Vt = 150, _t = /* @__PURE__ */ new Set([
|
|
503
|
+
"create",
|
|
504
|
+
// block created
|
|
505
|
+
"delete",
|
|
506
|
+
// block deleted
|
|
507
|
+
"move",
|
|
508
|
+
// block moved / connected / disconnected
|
|
509
|
+
"change"
|
|
510
|
+
// field value changed
|
|
511
|
+
]);
|
|
512
|
+
function Wt(n) {
|
|
513
|
+
return _t.has(n.type);
|
|
514
|
+
}
|
|
515
|
+
async function zt() {
|
|
516
|
+
try {
|
|
517
|
+
const [n, t, e] = await Promise.all([
|
|
518
|
+
import("@codemirror/view"),
|
|
519
|
+
import("@codemirror/state"),
|
|
520
|
+
import("@codemirror/lang-javascript")
|
|
521
|
+
]);
|
|
522
|
+
return { view: n, state: t, langJs: e };
|
|
523
|
+
} catch {
|
|
524
|
+
throw new Error(
|
|
525
|
+
`CodeMirror is required for the code editor. Install it:
|
|
526
|
+
bun add codemirror @codemirror/view @codemirror/state @codemirror/lang-javascript
|
|
527
|
+
# or: npm install codemirror @codemirror/view @codemirror/state @codemirror/lang-javascript`
|
|
528
|
+
);
|
|
529
|
+
}
|
|
530
|
+
}
|
|
531
|
+
function me(n, t) {
|
|
532
|
+
const e = { ...Ge, ...t };
|
|
533
|
+
return n.EditorView.theme({
|
|
534
|
+
"&": {
|
|
535
|
+
fontSize: e.fontSize,
|
|
536
|
+
fontFamily: e.fontFamily,
|
|
537
|
+
backgroundColor: e.background,
|
|
538
|
+
color: e.foreground
|
|
539
|
+
},
|
|
540
|
+
".cm-content": {
|
|
541
|
+
lineHeight: String(e.lineHeight)
|
|
542
|
+
},
|
|
543
|
+
".cm-gutters": {
|
|
544
|
+
backgroundColor: e.gutterBackground,
|
|
545
|
+
color: e.gutterForeground,
|
|
546
|
+
border: "none"
|
|
547
|
+
},
|
|
548
|
+
"&.cm-focused .cm-selectionBackground, .cm-selectionBackground": {
|
|
549
|
+
backgroundColor: `${e.selectionBackground} !important`
|
|
550
|
+
},
|
|
551
|
+
// Note: theme-independent placeholder-marker styling (border-radius,
|
|
552
|
+
// padding, italic/opacity) lives in code-editor.css, injected once by
|
|
553
|
+
// ensureCodeEditorStyles(). Only theme-colour-derived rules remain here.
|
|
554
|
+
".morphic-delete-marker": {
|
|
555
|
+
color: ee(e.foreground, 70),
|
|
556
|
+
backgroundColor: ee(e.foreground, 12)
|
|
557
|
+
},
|
|
558
|
+
".morphic-delete-marker:hover": {
|
|
559
|
+
color: e.foreground,
|
|
560
|
+
backgroundColor: ee(e.foreground, 22)
|
|
561
|
+
},
|
|
562
|
+
".morphic-grip-marker": {
|
|
563
|
+
color: ee(e.foreground, 75)
|
|
564
|
+
},
|
|
565
|
+
".morphic-grip-marker:hover": {
|
|
566
|
+
color: e.foreground
|
|
567
|
+
}
|
|
568
|
+
});
|
|
569
|
+
}
|
|
570
|
+
class ge {
|
|
571
|
+
constructor(t, e, o, i = {}) {
|
|
572
|
+
g(this, "editorView");
|
|
573
|
+
g(this, "container");
|
|
574
|
+
g(this, "options");
|
|
575
|
+
g(this, "workspace");
|
|
576
|
+
g(this, "generateWithMetadata");
|
|
577
|
+
g(this, "visible", !0);
|
|
578
|
+
g(this, "syncTimer");
|
|
579
|
+
g(this, "changeListener");
|
|
580
|
+
g(this, "lastCode", "");
|
|
581
|
+
/** Latest metadata from the most recent code generation. */
|
|
582
|
+
g(this, "metadata", /* @__PURE__ */ new Map());
|
|
583
|
+
/**
|
|
584
|
+
* Latest placeholder ranges. Sorted by `start` ascending. Used to suppress
|
|
585
|
+
* block-line highlighting when the cursor lands inside a value-slot range
|
|
586
|
+
* (a placeholder is its own selectable target).
|
|
587
|
+
*/
|
|
588
|
+
g(this, "placeholders", []);
|
|
589
|
+
/** DOM root of the active inline placeholder editor (input/select), if open. */
|
|
590
|
+
g(this, "placeholderEditorEl");
|
|
591
|
+
g(this, "placeholderEditorScrollHandler");
|
|
592
|
+
g(this, "placeholderEditorOutsideHandler");
|
|
593
|
+
/** Called when the user's cursor line changes in the editor. */
|
|
594
|
+
g(this, "onCursorLine");
|
|
595
|
+
/**
|
|
596
|
+
* Called when the user clicks in the empty area below the last rendered
|
|
597
|
+
* line. CodeMirror snaps the cursor to end-of-doc on such clicks, which
|
|
598
|
+
* isn't enough to detect "user wants to deselect" — the cursor often lands
|
|
599
|
+
* on a line that's still inside the currently selected block's range.
|
|
600
|
+
*/
|
|
601
|
+
g(this, "onEmptyClick");
|
|
602
|
+
// Cached CodeMirror modules (loaded once on first mount).
|
|
603
|
+
g(this, "cm");
|
|
604
|
+
g(this, "themeCompartment");
|
|
605
|
+
g(this, "syntaxHighlightCompartment");
|
|
606
|
+
g(this, "highlightEffect");
|
|
607
|
+
g(this, "highlightColor", "rgba(255, 255, 255, 0.07)");
|
|
608
|
+
g(this, "metadataEffect");
|
|
609
|
+
g(this, "placeholderEffect");
|
|
610
|
+
g(this, "dropIndicatorEffect");
|
|
611
|
+
g(this, "valueSlotHighlightEffect");
|
|
612
|
+
g(this, "hoverHighlightEffect");
|
|
613
|
+
g(this, "editableHoverHighlightEffect");
|
|
614
|
+
g(this, "emptyClickListener");
|
|
615
|
+
// Set by dispose(); a mount still waiting for CodeMirror then stops.
|
|
616
|
+
g(this, "disposed", !1);
|
|
617
|
+
g(this, "zoomFactor", 1);
|
|
618
|
+
this.container = t, this.workspace = e, this.generateWithMetadata = o, this.options = i;
|
|
619
|
+
}
|
|
620
|
+
/** Read-only view of the latest placeholder ranges. */
|
|
621
|
+
getPlaceholders() {
|
|
622
|
+
return this.placeholders;
|
|
623
|
+
}
|
|
624
|
+
/**
|
|
625
|
+
* Find the placeholder that covers `pos`. End is inclusive so a click that
|
|
626
|
+
* lands just past a single-digit marker still resolves; when ranges nest
|
|
627
|
+
* (e.g. "0 < 0" outer with two inner "0" markers) the narrowest match wins
|
|
628
|
+
* so clicks resolve to the editable inner range. Returns `null` when the
|
|
629
|
+
* editor has placeholder markers disabled (e.g. the preview editor) so
|
|
630
|
+
* clicks fall through to the block-line selection path instead.
|
|
631
|
+
*/
|
|
632
|
+
findPlaceholderAtPos(t) {
|
|
633
|
+
if (this.options.showPlaceholderMarkers === !1) return null;
|
|
634
|
+
let e = null, o = 1 / 0;
|
|
635
|
+
for (const i of this.placeholders) {
|
|
636
|
+
if (i.start > t) break;
|
|
637
|
+
if (t >= i.start && t <= i.end) {
|
|
638
|
+
const s = i.end - i.start;
|
|
639
|
+
s < o && (o = s, e = i);
|
|
640
|
+
}
|
|
641
|
+
}
|
|
642
|
+
return e;
|
|
643
|
+
}
|
|
644
|
+
closePlaceholderEditor() {
|
|
645
|
+
const t = this.placeholderEditorEl;
|
|
646
|
+
t && (this.placeholderEditorEl = void 0, this.placeholderEditorScrollHandler && this.editorView && (this.editorView.scrollDOM.removeEventListener("scroll", this.placeholderEditorScrollHandler), this.placeholderEditorScrollHandler = void 0), this.placeholderEditorOutsideHandler && (document.removeEventListener("mousedown", this.placeholderEditorOutsideHandler), this.placeholderEditorOutsideHandler = void 0), t.parentNode && t.parentNode.removeChild(t));
|
|
647
|
+
}
|
|
648
|
+
/**
|
|
649
|
+
* Open an inline editor (input/select) over the placeholder range and call
|
|
650
|
+
* `onPlaceholderApply` when the user commits. Read-only CodeMirror stays
|
|
651
|
+
* read-only; this overlay is a separate DOM element positioned via
|
|
652
|
+
* `coordsAtPos`. Closed on Enter/blur (apply), Escape (cancel), or doc
|
|
653
|
+
* change / scroll.
|
|
654
|
+
*/
|
|
655
|
+
openPlaceholderEditor(t) {
|
|
656
|
+
if (!this.editorView || !t.edit) return;
|
|
657
|
+
const e = this.options.onPlaceholderApply;
|
|
658
|
+
if (!e) return;
|
|
659
|
+
this.closePlaceholderEditor();
|
|
660
|
+
const o = this.editorView, i = o.coordsAtPos(t.start), s = o.coordsAtPos(t.end);
|
|
661
|
+
if (!i || !s) return;
|
|
662
|
+
const r = o.scrollDOM.getBoundingClientRect(), c = o.state.doc.sliceString(t.start, t.end), a = t.edit;
|
|
663
|
+
let l;
|
|
664
|
+
if (a.fieldType === "dropdown") {
|
|
665
|
+
const b = document.createElement("select");
|
|
666
|
+
for (const [E, k] of a.options ?? []) {
|
|
667
|
+
const m = document.createElement("option");
|
|
668
|
+
m.value = k, m.textContent = E, b.appendChild(m);
|
|
669
|
+
}
|
|
670
|
+
l = b;
|
|
671
|
+
} else {
|
|
672
|
+
const b = document.createElement("input");
|
|
673
|
+
b.type = a.fieldType === "number" ? "number" : "text", b.value = c, l = b;
|
|
674
|
+
}
|
|
675
|
+
const d = getComputedStyle(o.contentDOM), u = (() => {
|
|
676
|
+
let b = o.dom;
|
|
677
|
+
for (; b; ) {
|
|
678
|
+
const E = getComputedStyle(b).backgroundColor;
|
|
679
|
+
if (E && E !== "rgba(0, 0, 0, 0)" && E !== "transparent") return E;
|
|
680
|
+
b = b.parentElement;
|
|
681
|
+
}
|
|
682
|
+
return "#fff";
|
|
683
|
+
})();
|
|
684
|
+
Object.assign(l.style, {
|
|
685
|
+
position: "absolute",
|
|
686
|
+
left: `${i.left - r.left + o.scrollDOM.scrollLeft}px`,
|
|
687
|
+
top: `${i.top - r.top + o.scrollDOM.scrollTop}px`,
|
|
688
|
+
minWidth: `${Math.max(40, s.right - i.left + 8)}px`,
|
|
689
|
+
height: `${i.bottom - i.top}px`,
|
|
690
|
+
font: d.font,
|
|
691
|
+
lineHeight: d.lineHeight,
|
|
692
|
+
padding: "0 2px",
|
|
693
|
+
margin: "0",
|
|
694
|
+
border: `1px solid ${d.color}`,
|
|
695
|
+
borderRadius: "2px",
|
|
696
|
+
background: u,
|
|
697
|
+
color: d.color,
|
|
698
|
+
boxSizing: "border-box",
|
|
699
|
+
zIndex: "20"
|
|
700
|
+
}), o.scrollDOM.appendChild(l), this.placeholderEditorEl = l;
|
|
701
|
+
const w = () => {
|
|
702
|
+
const b = l.value;
|
|
703
|
+
this.closePlaceholderEditor(), e(a, b);
|
|
704
|
+
}, v = () => {
|
|
705
|
+
this.closePlaceholderEditor();
|
|
706
|
+
};
|
|
707
|
+
if (l.addEventListener("keydown", (b) => {
|
|
708
|
+
const E = b.key;
|
|
709
|
+
E === "Enter" ? (b.preventDefault(), w()) : E === "Escape" && (b.preventDefault(), v());
|
|
710
|
+
}), l.addEventListener("mousedown", (b) => b.stopPropagation()), l.addEventListener("click", (b) => b.stopPropagation()), a.fieldType === "dropdown") {
|
|
711
|
+
l.addEventListener("change", () => w());
|
|
712
|
+
const b = this.workspace.getBlockById(a.blockId), E = b == null ? void 0 : b.getField(a.fieldName), k = E == null ? void 0 : E.getValue();
|
|
713
|
+
k != null && (l.value = String(k)), requestAnimationFrame(() => {
|
|
714
|
+
this.placeholderEditorOutsideHandler = (m) => {
|
|
715
|
+
m.target instanceof Node && !l.contains(m.target) && v();
|
|
716
|
+
}, document.addEventListener("mousedown", this.placeholderEditorOutsideHandler);
|
|
717
|
+
});
|
|
718
|
+
} else
|
|
719
|
+
l.addEventListener("blur", () => w());
|
|
720
|
+
this.placeholderEditorScrollHandler = () => this.closePlaceholderEditor(), o.scrollDOM.addEventListener("scroll", this.placeholderEditorScrollHandler), requestAnimationFrame(() => {
|
|
721
|
+
l.focus(), l instanceof HTMLInputElement && l.select();
|
|
722
|
+
});
|
|
723
|
+
}
|
|
724
|
+
async mount() {
|
|
725
|
+
if (Rt(), this.cm = await zt(), this.disposed) return;
|
|
726
|
+
const { view: t, state: e, langJs: o } = this.cm;
|
|
727
|
+
this.themeCompartment = new e.Compartment(), this.syntaxHighlightCompartment = new e.Compartment();
|
|
728
|
+
const i = me(t, this.options.theme ?? {}), s = Me(
|
|
729
|
+
t,
|
|
730
|
+
e,
|
|
731
|
+
this.options.highlightRules
|
|
732
|
+
), r = e.StateEffect.define();
|
|
733
|
+
this.metadataEffect = r;
|
|
734
|
+
const c = e.StateEffect.define();
|
|
735
|
+
this.placeholderEffect = c;
|
|
736
|
+
const a = e.StateField.define({
|
|
737
|
+
create() {
|
|
738
|
+
return t.Decoration.none;
|
|
739
|
+
},
|
|
740
|
+
update(k, m) {
|
|
741
|
+
for (const p of m.effects)
|
|
742
|
+
if (p.is(c)) {
|
|
743
|
+
const y = p.value;
|
|
744
|
+
if (!y || y.length === 0) return t.Decoration.none;
|
|
745
|
+
const S = m.state.doc.length, h = [...y].filter((C) => C.start < C.end && C.end <= S).sort((C, I) => C.start - I.start || C.end - I.end), f = new e.RangeSetBuilder(), x = t.Decoration.mark({ class: "morphic-placeholder-default" }), T = t.Decoration.mark({ class: "morphic-placeholder-set" });
|
|
746
|
+
for (const C of h)
|
|
747
|
+
f.add(C.start, C.end, C.kind === "default" ? x : T);
|
|
748
|
+
return f.finish();
|
|
749
|
+
}
|
|
750
|
+
return m.changes.empty ? k : t.Decoration.none;
|
|
751
|
+
},
|
|
752
|
+
provide(k) {
|
|
753
|
+
return t.EditorView.decorations.from(k);
|
|
754
|
+
}
|
|
755
|
+
}), l = e.StateEffect.define();
|
|
756
|
+
this.highlightEffect = l;
|
|
757
|
+
const d = this, u = e.StateField.define({
|
|
758
|
+
create() {
|
|
759
|
+
return t.Decoration.none;
|
|
760
|
+
},
|
|
761
|
+
update(k, m) {
|
|
762
|
+
for (const p of m.effects)
|
|
763
|
+
if (p.is(l)) {
|
|
764
|
+
const y = p.value;
|
|
765
|
+
if (!y) return t.Decoration.none;
|
|
766
|
+
const S = Array.isArray(y) ? y : [y];
|
|
767
|
+
if (S.length === 0) return t.Decoration.none;
|
|
768
|
+
const h = m.state.doc, f = t.Decoration.mark({
|
|
769
|
+
class: "morphic-highlight",
|
|
770
|
+
attributes: { style: `background: ${d.highlightColor}` }
|
|
771
|
+
}), x = S.map((T) => {
|
|
772
|
+
const C = h.line(Math.min(T.fromLine, h.lines)).from, I = h.line(Math.min(T.toLine, h.lines)).to;
|
|
773
|
+
return f.range(C, I);
|
|
774
|
+
}).sort((T, C) => T.from - C.from);
|
|
775
|
+
return t.Decoration.set(x);
|
|
776
|
+
}
|
|
777
|
+
return m.changes.empty ? k : t.Decoration.none;
|
|
778
|
+
},
|
|
779
|
+
provide(k) {
|
|
780
|
+
return t.EditorView.decorations.from(k);
|
|
781
|
+
}
|
|
782
|
+
}), w = t.EditorView.updateListener.of((k) => {
|
|
783
|
+
var S;
|
|
784
|
+
if (k.docChanged || k.transactions.some((h) => h.isUserEvent("select.pointer")))
|
|
785
|
+
return;
|
|
786
|
+
const m = k.state.selection.main.head, p = k.startState.selection.main.head;
|
|
787
|
+
if (m === p) return;
|
|
788
|
+
if (d.findPlaceholderAtPos(m)) {
|
|
789
|
+
(S = d.onEmptyClick) == null || S.call(d);
|
|
790
|
+
return;
|
|
791
|
+
}
|
|
792
|
+
if (!d.onCursorLine) return;
|
|
793
|
+
const y = k.state.doc.lineAt(m).number;
|
|
794
|
+
d.onCursorLine(y);
|
|
795
|
+
}), v = this.options.showPlaceholderMarkers !== !1, b = [
|
|
796
|
+
t.lineNumbers(),
|
|
797
|
+
t.highlightSpecialChars(),
|
|
798
|
+
e.EditorState.readOnly.of(!0),
|
|
799
|
+
o.javascript(),
|
|
800
|
+
u,
|
|
801
|
+
...v ? [a] : [],
|
|
802
|
+
w,
|
|
803
|
+
this.themeCompartment.of(i),
|
|
804
|
+
this.syntaxHighlightCompartment.of(s),
|
|
805
|
+
...this.buildDeleteExtensions(t, e, r),
|
|
806
|
+
...this.buildGripExtensions(t, e, r),
|
|
807
|
+
...this.buildDropIndicatorExtensions(t, e),
|
|
808
|
+
...this.buildValueSlotHighlightExtensions(t, e),
|
|
809
|
+
...this.buildHoverHighlightExtensions(t, e),
|
|
810
|
+
...this.buildEditableHoverHighlightExtensions(t, e),
|
|
811
|
+
...this.options.extensions ?? []
|
|
812
|
+
], E = this.generateWithMetadata();
|
|
813
|
+
this.lastCode = E.code, this.metadata = E.metadata, this.placeholders = [...E.placeholders].sort((k, m) => k.start - m.start), this.editorView = new t.EditorView({
|
|
814
|
+
parent: this.container,
|
|
815
|
+
state: e.EditorState.create({
|
|
816
|
+
doc: this.lastCode,
|
|
817
|
+
extensions: b
|
|
818
|
+
})
|
|
819
|
+
}), this.metadataEffect && this.metadata.size > 0 && this.editorView.dispatch({
|
|
820
|
+
effects: this.metadataEffect.of(this.metadata)
|
|
821
|
+
}), this.placeholderEffect && E.placeholders.length > 0 && this.editorView.dispatch({
|
|
822
|
+
effects: this.placeholderEffect.of(E.placeholders)
|
|
823
|
+
}), this.emptyClickListener = (k) => {
|
|
824
|
+
var p, y, S;
|
|
825
|
+
if (this.isBelowLastLine(k.clientY)) {
|
|
826
|
+
(p = this.onEmptyClick) == null || p.call(this);
|
|
827
|
+
return;
|
|
828
|
+
}
|
|
829
|
+
const m = (y = this.editorView) == null ? void 0 : y.posAtCoords({ x: k.clientX, y: k.clientY });
|
|
830
|
+
if (typeof m == "number") {
|
|
831
|
+
const h = this.findPlaceholderAtPos(m);
|
|
832
|
+
if (h) {
|
|
833
|
+
(S = this.onEmptyClick) == null || S.call(this), h.edit && this.options.onPlaceholderApply && requestAnimationFrame(() => this.openPlaceholderEditor(h));
|
|
834
|
+
return;
|
|
835
|
+
}
|
|
836
|
+
}
|
|
837
|
+
if (this.onCursorLine) {
|
|
838
|
+
const h = this.getLineAtCoords(k.clientX, k.clientY);
|
|
839
|
+
h !== null && this.onCursorLine(h);
|
|
840
|
+
}
|
|
841
|
+
}, this.editorView.scrollDOM.addEventListener("click", this.emptyClickListener), this.attachSyncListener();
|
|
842
|
+
}
|
|
843
|
+
buildDeleteExtensions(t, e, o) {
|
|
844
|
+
const i = this.options.onDelete;
|
|
845
|
+
if (!i) return [];
|
|
846
|
+
class s extends t.GutterMarker {
|
|
847
|
+
toDOM() {
|
|
848
|
+
const u = document.createElement("span");
|
|
849
|
+
return u.textContent = "✕", u.className = "morphic-delete-marker", u.style.cssText = [
|
|
850
|
+
"display: inline-flex",
|
|
851
|
+
"align-items: center",
|
|
852
|
+
"justify-content: center",
|
|
853
|
+
"width: 16px",
|
|
854
|
+
"height: 16px",
|
|
855
|
+
"margin: 0 4px",
|
|
856
|
+
"border-radius: 50%",
|
|
857
|
+
"font-size: 10px",
|
|
858
|
+
"line-height: 1",
|
|
859
|
+
"cursor: pointer",
|
|
860
|
+
"transition: background 0.15s, color 0.15s"
|
|
861
|
+
].join(";"), u;
|
|
862
|
+
}
|
|
863
|
+
}
|
|
864
|
+
const r = new s(), c = e.StateField.define({
|
|
865
|
+
create() {
|
|
866
|
+
return e.RangeSet.empty;
|
|
867
|
+
},
|
|
868
|
+
update(d, u) {
|
|
869
|
+
for (const w of u.effects)
|
|
870
|
+
if (w.is(o)) {
|
|
871
|
+
const v = w.value, b = u.state.doc, E = /* @__PURE__ */ new Set();
|
|
872
|
+
for (const { startLine: m } of v.values())
|
|
873
|
+
m >= 1 && m <= b.lines && E.add(m);
|
|
874
|
+
const k = Array.from(E).sort((m, p) => m - p).map((m) => r.range(b.line(m).from));
|
|
875
|
+
return e.RangeSet.of(k, !0);
|
|
876
|
+
}
|
|
877
|
+
return d.map(u.changes);
|
|
878
|
+
}
|
|
879
|
+
}), a = t.gutter({
|
|
880
|
+
class: "morphic-delete-gutter",
|
|
881
|
+
markers(d) {
|
|
882
|
+
return d.state.field(c);
|
|
883
|
+
},
|
|
884
|
+
initialSpacer: () => r,
|
|
885
|
+
domEventHandlers: {
|
|
886
|
+
click(d, u) {
|
|
887
|
+
const w = d.state.doc.lineAt(u.from).number;
|
|
888
|
+
return i(w), !0;
|
|
889
|
+
}
|
|
890
|
+
}
|
|
891
|
+
}), l = t.keymap.of([
|
|
892
|
+
{
|
|
893
|
+
key: "Delete",
|
|
894
|
+
run: (d) => {
|
|
895
|
+
if (!d.state.selection.main.empty) return !1;
|
|
896
|
+
const u = d.state.doc.lineAt(d.state.selection.main.head).number;
|
|
897
|
+
return i(u), !0;
|
|
898
|
+
}
|
|
899
|
+
},
|
|
900
|
+
{
|
|
901
|
+
key: "Backspace",
|
|
902
|
+
run: (d) => {
|
|
903
|
+
if (!d.state.selection.main.empty) return !1;
|
|
904
|
+
const u = d.state.doc.lineAt(d.state.selection.main.head).number;
|
|
905
|
+
return i(u), !0;
|
|
906
|
+
}
|
|
907
|
+
}
|
|
908
|
+
]);
|
|
909
|
+
return [c, a, e.Prec.highest(l)];
|
|
910
|
+
}
|
|
911
|
+
buildGripExtensions(t, e, o) {
|
|
912
|
+
const i = this.options.canDragBlock;
|
|
913
|
+
if (!i) return [];
|
|
914
|
+
class s extends t.GutterMarker {
|
|
915
|
+
constructor(l) {
|
|
916
|
+
super(), this.blockId = l;
|
|
917
|
+
}
|
|
918
|
+
eq(l) {
|
|
919
|
+
return l instanceof s && l.blockId === this.blockId;
|
|
920
|
+
}
|
|
921
|
+
toDOM() {
|
|
922
|
+
const l = document.createElement("span");
|
|
923
|
+
l.textContent = "⠿", l.draggable = !0, l.className = "morphic-grip-marker", l.style.cssText = [
|
|
924
|
+
"display: inline-flex",
|
|
925
|
+
"align-items: center",
|
|
926
|
+
"justify-content: center",
|
|
927
|
+
"width: 16px",
|
|
928
|
+
"height: 16px",
|
|
929
|
+
"margin: 0 2px",
|
|
930
|
+
"font-size: 14px",
|
|
931
|
+
"line-height: 1",
|
|
932
|
+
"cursor: grab",
|
|
933
|
+
"user-select: none",
|
|
934
|
+
"transition: color 0.15s"
|
|
935
|
+
].join(";");
|
|
936
|
+
const d = this.blockId;
|
|
937
|
+
return l.addEventListener("dragstart", (u) => {
|
|
938
|
+
u.dataTransfer && (u.dataTransfer.setData(Ce, d), u.dataTransfer.effectAllowed = "move", l.style.cursor = "grabbing", he = d);
|
|
939
|
+
}), l.addEventListener("dragend", () => {
|
|
940
|
+
l.style.cursor = "grab", he = void 0;
|
|
941
|
+
}), l;
|
|
942
|
+
}
|
|
943
|
+
}
|
|
944
|
+
const r = e.StateField.define({
|
|
945
|
+
create() {
|
|
946
|
+
return e.RangeSet.empty;
|
|
947
|
+
},
|
|
948
|
+
update(a, l) {
|
|
949
|
+
for (const d of l.effects)
|
|
950
|
+
if (d.is(o)) {
|
|
951
|
+
const u = d.value, w = l.state.doc, v = /* @__PURE__ */ new Map();
|
|
952
|
+
for (const [E, { startLine: k, startChar: m, endChar: p }] of u) {
|
|
953
|
+
if (k < 1 || k > w.lines || !i(E)) continue;
|
|
954
|
+
const y = (p ?? 0) - (m ?? 0), S = v.get(k);
|
|
955
|
+
(!S || y > S.width) && v.set(k, { id: E, width: y });
|
|
956
|
+
}
|
|
957
|
+
const b = Array.from(v.entries()).sort((E, k) => E[0] - k[0]).map(([E, { id: k }]) => new s(k).range(w.line(E).from));
|
|
958
|
+
return e.RangeSet.of(b, !0);
|
|
959
|
+
}
|
|
960
|
+
return a.map(l.changes);
|
|
961
|
+
}
|
|
962
|
+
}), c = t.gutter({
|
|
963
|
+
class: "morphic-grip-gutter",
|
|
964
|
+
markers(a) {
|
|
965
|
+
return a.state.field(r);
|
|
966
|
+
},
|
|
967
|
+
initialSpacer: () => new s("")
|
|
968
|
+
});
|
|
969
|
+
return [r, c];
|
|
970
|
+
}
|
|
971
|
+
buildDropIndicatorExtensions(t, e) {
|
|
972
|
+
const o = e.StateEffect.define();
|
|
973
|
+
this.dropIndicatorEffect = o;
|
|
974
|
+
const i = e.StateField.define({
|
|
975
|
+
create() {
|
|
976
|
+
return t.Decoration.none;
|
|
977
|
+
},
|
|
978
|
+
update(r, c) {
|
|
979
|
+
for (const a of c.effects)
|
|
980
|
+
if (a.is(o)) {
|
|
981
|
+
const l = a.value;
|
|
982
|
+
if (!l) return t.Decoration.none;
|
|
983
|
+
const d = c.state.doc, u = Math.max(1, Math.min(l.line, d.lines)), w = d.line(u), v = l.position === "above" ? "morphic-drop-indicator-above" : "morphic-drop-indicator-below", b = t.Decoration.line({ class: v });
|
|
984
|
+
return t.Decoration.set([b.range(w.from)]);
|
|
985
|
+
}
|
|
986
|
+
return c.changes.empty ? r : t.Decoration.none;
|
|
987
|
+
},
|
|
988
|
+
provide(r) {
|
|
989
|
+
return t.EditorView.decorations.from(r);
|
|
990
|
+
}
|
|
991
|
+
}), s = t.EditorView.baseTheme({
|
|
992
|
+
".morphic-drop-indicator-above": {
|
|
993
|
+
boxShadow: "inset 0 2px 0 0 #5a86bc"
|
|
994
|
+
},
|
|
995
|
+
".morphic-drop-indicator-below": {
|
|
996
|
+
boxShadow: "inset 0 -2px 0 0 #5a86bc"
|
|
997
|
+
}
|
|
998
|
+
});
|
|
999
|
+
return [i, s];
|
|
1000
|
+
}
|
|
1001
|
+
/**
|
|
1002
|
+
* Highlight the char range of the value slot a dragged block would land in.
|
|
1003
|
+
* Distinct from `dropIndicator` (a horizontal line above/below a line) so the
|
|
1004
|
+
* user can tell at a glance that the drop will be value-typed, not statement.
|
|
1005
|
+
*/
|
|
1006
|
+
buildValueSlotHighlightExtensions(t, e) {
|
|
1007
|
+
const o = e.StateEffect.define();
|
|
1008
|
+
this.valueSlotHighlightEffect = o;
|
|
1009
|
+
const i = t.Decoration.mark({ class: "morphic-value-slot-target" }), s = e.StateField.define({
|
|
1010
|
+
create() {
|
|
1011
|
+
return t.Decoration.none;
|
|
1012
|
+
},
|
|
1013
|
+
update(c, a) {
|
|
1014
|
+
for (const l of a.effects)
|
|
1015
|
+
if (l.is(o)) {
|
|
1016
|
+
const d = l.value;
|
|
1017
|
+
if (!d) return t.Decoration.none;
|
|
1018
|
+
const u = a.state.doc.length, w = Math.max(0, Math.min(d.from, u)), v = Math.max(w, Math.min(d.to, u));
|
|
1019
|
+
return w === v ? t.Decoration.none : t.Decoration.set([i.range(w, v)]);
|
|
1020
|
+
}
|
|
1021
|
+
return a.changes.empty ? c : t.Decoration.none;
|
|
1022
|
+
},
|
|
1023
|
+
provide(c) {
|
|
1024
|
+
return t.EditorView.decorations.from(c);
|
|
1025
|
+
}
|
|
1026
|
+
}), r = t.EditorView.baseTheme({
|
|
1027
|
+
".morphic-value-slot-target": {
|
|
1028
|
+
outline: "1px solid #5a86bc",
|
|
1029
|
+
outlineOffset: "1px",
|
|
1030
|
+
borderRadius: "2px"
|
|
1031
|
+
}
|
|
1032
|
+
});
|
|
1033
|
+
return [s, r];
|
|
1034
|
+
}
|
|
1035
|
+
/**
|
|
1036
|
+
* Hover-time hint: when the mouse moves over a block in the codespace, the
|
|
1037
|
+
* innermost block under the cursor gets a subtle background tint so the
|
|
1038
|
+
* user can see what "click to edit" or "right-click drag" will target
|
|
1039
|
+
* before they act. Driven from `setHoverHighlight` (called by MorphicBlocks
|
|
1040
|
+
* on the codespace container's `mousemove`/`mouseleave`).
|
|
1041
|
+
*/
|
|
1042
|
+
buildHoverHighlightExtensions(t, e) {
|
|
1043
|
+
const o = e.StateEffect.define();
|
|
1044
|
+
this.hoverHighlightEffect = o;
|
|
1045
|
+
const i = t.Decoration.mark({ class: "morphic-hover-target" }), s = e.StateField.define({
|
|
1046
|
+
create() {
|
|
1047
|
+
return t.Decoration.none;
|
|
1048
|
+
},
|
|
1049
|
+
update(c, a) {
|
|
1050
|
+
for (const l of a.effects)
|
|
1051
|
+
if (l.is(o)) {
|
|
1052
|
+
const d = l.value;
|
|
1053
|
+
if (!d) return t.Decoration.none;
|
|
1054
|
+
const u = a.state.doc.length, w = Math.max(0, Math.min(d.from, u)), v = Math.max(w, Math.min(d.to, u));
|
|
1055
|
+
return w === v ? t.Decoration.none : t.Decoration.set([i.range(w, v)]);
|
|
1056
|
+
}
|
|
1057
|
+
return a.changes.empty ? c : t.Decoration.none;
|
|
1058
|
+
},
|
|
1059
|
+
provide(c) {
|
|
1060
|
+
return t.EditorView.decorations.from(c);
|
|
1061
|
+
}
|
|
1062
|
+
}), r = t.EditorView.baseTheme({
|
|
1063
|
+
".morphic-hover-target": {
|
|
1064
|
+
backgroundColor: "rgba(127, 127, 127, 0.10)",
|
|
1065
|
+
borderRadius: "2px"
|
|
1066
|
+
}
|
|
1067
|
+
});
|
|
1068
|
+
return [s, r];
|
|
1069
|
+
}
|
|
1070
|
+
/** Highlight the char range of the block currently under the mouse cursor. */
|
|
1071
|
+
setHoverHighlight(t) {
|
|
1072
|
+
!this.editorView || !this.hoverHighlightEffect || this.editorView.dispatch({
|
|
1073
|
+
effects: this.hoverHighlightEffect.of(t)
|
|
1074
|
+
});
|
|
1075
|
+
}
|
|
1076
|
+
/**
|
|
1077
|
+
* Editable-hover layer: a single outline around the innermost editable
|
|
1078
|
+
* placeholder under the cursor. Mirrors the structure of the value-slot /
|
|
1079
|
+
* hover layers above — separate from the placeholder marks themselves so
|
|
1080
|
+
* we don't double-render outlines for nested wrappers.
|
|
1081
|
+
*/
|
|
1082
|
+
buildEditableHoverHighlightExtensions(t, e) {
|
|
1083
|
+
const o = e.StateEffect.define();
|
|
1084
|
+
this.editableHoverHighlightEffect = o;
|
|
1085
|
+
const i = t.Decoration.mark({ class: "morphic-editable-hover" }), s = e.StateField.define({
|
|
1086
|
+
create() {
|
|
1087
|
+
return t.Decoration.none;
|
|
1088
|
+
},
|
|
1089
|
+
update(c, a) {
|
|
1090
|
+
for (const l of a.effects)
|
|
1091
|
+
if (l.is(o)) {
|
|
1092
|
+
const d = l.value;
|
|
1093
|
+
if (!d) return t.Decoration.none;
|
|
1094
|
+
const u = a.state.doc.length, w = Math.max(0, Math.min(d.from, u)), v = Math.max(w, Math.min(d.to, u));
|
|
1095
|
+
return w === v ? t.Decoration.none : t.Decoration.set([i.range(w, v)]);
|
|
1096
|
+
}
|
|
1097
|
+
return a.changes.empty ? c : t.Decoration.none;
|
|
1098
|
+
},
|
|
1099
|
+
provide(c) {
|
|
1100
|
+
return t.EditorView.decorations.from(c);
|
|
1101
|
+
}
|
|
1102
|
+
}), r = t.EditorView.baseTheme({
|
|
1103
|
+
".morphic-editable-hover": {
|
|
1104
|
+
outline: "1px solid #5a86bc",
|
|
1105
|
+
outlineOffset: "1px",
|
|
1106
|
+
borderRadius: "2px"
|
|
1107
|
+
}
|
|
1108
|
+
});
|
|
1109
|
+
return [s, r];
|
|
1110
|
+
}
|
|
1111
|
+
/** Outline the innermost editable placeholder under the cursor (one only). */
|
|
1112
|
+
setEditableHoverHighlight(t) {
|
|
1113
|
+
!this.editorView || !this.editableHoverHighlightEffect || this.editorView.dispatch({
|
|
1114
|
+
effects: this.editableHoverHighlightEffect.of(t)
|
|
1115
|
+
});
|
|
1116
|
+
}
|
|
1117
|
+
show() {
|
|
1118
|
+
this.visible || (this.visible = !0, this.container.style.display = "", this.syncNow(), this.attachSyncListener());
|
|
1119
|
+
}
|
|
1120
|
+
hide() {
|
|
1121
|
+
this.visible && (this.visible = !1, this.container.style.display = "none", this.detachSyncListener());
|
|
1122
|
+
}
|
|
1123
|
+
isVisible() {
|
|
1124
|
+
return this.visible;
|
|
1125
|
+
}
|
|
1126
|
+
/** Highlight one or more ranges of 1-based lines in the editor. */
|
|
1127
|
+
highlightLines(t) {
|
|
1128
|
+
!this.editorView || !this.highlightEffect || this.editorView.dispatch({
|
|
1129
|
+
effects: this.highlightEffect.of(t)
|
|
1130
|
+
});
|
|
1131
|
+
}
|
|
1132
|
+
/** Remove all line highlights. */
|
|
1133
|
+
clearHighlight() {
|
|
1134
|
+
!this.editorView || !this.highlightEffect || this.editorView.dispatch({
|
|
1135
|
+
effects: this.highlightEffect.of(null)
|
|
1136
|
+
});
|
|
1137
|
+
}
|
|
1138
|
+
/** Update the highlight background colour. */
|
|
1139
|
+
setHighlightColor(t) {
|
|
1140
|
+
this.highlightColor = t;
|
|
1141
|
+
}
|
|
1142
|
+
/** Show a drop-position indicator above (or below) the given 1-based line. */
|
|
1143
|
+
showDropIndicator(t, e = "above") {
|
|
1144
|
+
!this.editorView || !this.dropIndicatorEffect || this.editorView.dispatch({
|
|
1145
|
+
effects: this.dropIndicatorEffect.of({ line: t, position: e })
|
|
1146
|
+
});
|
|
1147
|
+
}
|
|
1148
|
+
/** Hide the drop-position indicator if currently shown. */
|
|
1149
|
+
hideDropIndicator() {
|
|
1150
|
+
!this.editorView || !this.dropIndicatorEffect || this.editorView.dispatch({
|
|
1151
|
+
effects: this.dropIndicatorEffect.of(null)
|
|
1152
|
+
});
|
|
1153
|
+
}
|
|
1154
|
+
/** Outline the char range of the value slot the drop would land in. */
|
|
1155
|
+
showValueSlotHighlight(t, e) {
|
|
1156
|
+
!this.editorView || !this.valueSlotHighlightEffect || this.editorView.dispatch({
|
|
1157
|
+
effects: this.valueSlotHighlightEffect.of({ from: t, to: e })
|
|
1158
|
+
});
|
|
1159
|
+
}
|
|
1160
|
+
/** Clear the value-slot highlight if currently shown. */
|
|
1161
|
+
hideValueSlotHighlight() {
|
|
1162
|
+
!this.editorView || !this.valueSlotHighlightEffect || this.editorView.dispatch({
|
|
1163
|
+
effects: this.valueSlotHighlightEffect.of(null)
|
|
1164
|
+
});
|
|
1165
|
+
}
|
|
1166
|
+
/** Return the 1-based line number at the given client coordinates, or null. */
|
|
1167
|
+
getLineAtCoords(t, e) {
|
|
1168
|
+
if (!this.editorView) return null;
|
|
1169
|
+
const o = this.editorView.posAtCoords({ x: t, y: e });
|
|
1170
|
+
return o === null ? null : this.editorView.state.doc.lineAt(o).number;
|
|
1171
|
+
}
|
|
1172
|
+
/** Return the 0-based char offset at the given client coordinates, or null. */
|
|
1173
|
+
charAtCoords(t, e) {
|
|
1174
|
+
return this.editorView ? this.editorView.posAtCoords({ x: t, y: e }) ?? null : null;
|
|
1175
|
+
}
|
|
1176
|
+
/** Total line count of the editor's current document. */
|
|
1177
|
+
getLineCount() {
|
|
1178
|
+
var t;
|
|
1179
|
+
return ((t = this.editorView) == null ? void 0 : t.state.doc.lines) ?? 0;
|
|
1180
|
+
}
|
|
1181
|
+
/** Whether the given clientY is past the bottom of the last rendered line. */
|
|
1182
|
+
isBelowLastLine(t) {
|
|
1183
|
+
if (!this.editorView) return !1;
|
|
1184
|
+
const e = this.editorView.state.doc, o = e.line(e.lines).to, i = this.editorView.coordsAtPos(o);
|
|
1185
|
+
return i ? t > i.bottom : !1;
|
|
1186
|
+
}
|
|
1187
|
+
/** Whether clientY is in the lower half of the given 1-based line's bbox. */
|
|
1188
|
+
isInLowerHalfOfLine(t, e) {
|
|
1189
|
+
if (!this.editorView) return !1;
|
|
1190
|
+
const o = this.editorView.state.doc;
|
|
1191
|
+
if (t < 1 || t > o.lines) return !1;
|
|
1192
|
+
const i = o.line(t), s = this.editorView.coordsAtPos(i.from), r = this.editorView.coordsAtPos(i.to);
|
|
1193
|
+
return !s || !r ? !1 : e > (s.top + r.bottom) / 2;
|
|
1194
|
+
}
|
|
1195
|
+
setTheme(t) {
|
|
1196
|
+
if (!this.editorView || !this.cm || !this.themeCompartment) return;
|
|
1197
|
+
const e = me(this.cm.view, t);
|
|
1198
|
+
this.editorView.dispatch({
|
|
1199
|
+
effects: this.themeCompartment.reconfigure(e)
|
|
1200
|
+
});
|
|
1201
|
+
}
|
|
1202
|
+
/**
|
|
1203
|
+
* Swap the active token-highlight rules. Pass `undefined` to clear. Used by
|
|
1204
|
+
* MorphicBlocks on `setModes()` so the codespace and preview re-tokenize for
|
|
1205
|
+
* the new mode's source element.
|
|
1206
|
+
*/
|
|
1207
|
+
setHighlightRules(t) {
|
|
1208
|
+
if (!this.editorView || !this.cm || !this.syntaxHighlightCompartment) return;
|
|
1209
|
+
const e = Me(this.cm.view, this.cm.state, t);
|
|
1210
|
+
this.editorView.dispatch({
|
|
1211
|
+
effects: this.syntaxHighlightCompartment.reconfigure(e)
|
|
1212
|
+
});
|
|
1213
|
+
}
|
|
1214
|
+
dispose() {
|
|
1215
|
+
var t;
|
|
1216
|
+
this.disposed = !0, this.detachSyncListener(), this.syncTimer !== void 0 && (clearTimeout(this.syncTimer), this.syncTimer = void 0), this.emptyClickListener && this.editorView && (this.editorView.scrollDOM.removeEventListener("click", this.emptyClickListener), this.emptyClickListener = void 0), (t = this.editorView) == null || t.destroy(), this.editorView = void 0, this.metadata = /* @__PURE__ */ new Map(), this.placeholders = [], this.lastCode = "";
|
|
1217
|
+
}
|
|
1218
|
+
/** Force a regeneration now (e.g. after the underlying template source changes). */
|
|
1219
|
+
refresh() {
|
|
1220
|
+
this.syncNow();
|
|
1221
|
+
}
|
|
1222
|
+
/** Current editor text. Empty string when the editor is not mounted yet. */
|
|
1223
|
+
getValue() {
|
|
1224
|
+
var t;
|
|
1225
|
+
return ((t = this.editorView) == null ? void 0 : t.state.doc.toString()) ?? this.lastCode ?? "";
|
|
1226
|
+
}
|
|
1227
|
+
/** 1-based line number under the cursor. Returns 1 when no cursor / not mounted. */
|
|
1228
|
+
getCursorLine() {
|
|
1229
|
+
if (!this.editorView) return 1;
|
|
1230
|
+
const t = this.editorView.state.selection.main.head;
|
|
1231
|
+
return this.editorView.state.doc.lineAt(t).number;
|
|
1232
|
+
}
|
|
1233
|
+
/**
|
|
1234
|
+
* Adjust the editor's zoom by reconfiguring the theme compartment with a
|
|
1235
|
+
* scaled font-size. `"fit"` resets to the theme's configured default.
|
|
1236
|
+
* One step = 1.2× / 0.83× — matches CodeMirror's natural rhythm.
|
|
1237
|
+
*/
|
|
1238
|
+
adjustZoom(t) {
|
|
1239
|
+
if (!this.editorView || !this.cm || !this.themeCompartment) return;
|
|
1240
|
+
const e = this.options.theme ?? {}, o = parseFloat(e.fontSize ?? Ge.fontSize);
|
|
1241
|
+
t === "fit" ? this.zoomFactor = 1 : t === "in" ? this.zoomFactor = Math.min(4, this.zoomFactor * 1.2) : this.zoomFactor = Math.max(0.25, this.zoomFactor / 1.2);
|
|
1242
|
+
const i = {
|
|
1243
|
+
...e,
|
|
1244
|
+
fontSize: `${(o * this.zoomFactor).toFixed(2)}px`
|
|
1245
|
+
}, s = me(this.cm.view, i);
|
|
1246
|
+
this.editorView.dispatch({
|
|
1247
|
+
effects: this.themeCompartment.reconfigure(s)
|
|
1248
|
+
});
|
|
1249
|
+
}
|
|
1250
|
+
syncNow() {
|
|
1251
|
+
if (!this.editorView || !this.cm) return;
|
|
1252
|
+
const t = this.generateWithMetadata();
|
|
1253
|
+
if (t.code === this.lastCode) return;
|
|
1254
|
+
this.closePlaceholderEditor(), this.lastCode = t.code, this.metadata = t.metadata, this.placeholders = [...t.placeholders].sort((o, i) => o.start - i.start);
|
|
1255
|
+
const e = [];
|
|
1256
|
+
this.metadataEffect && e.push(this.metadataEffect.of(t.metadata)), this.placeholderEffect && e.push(this.placeholderEffect.of(t.placeholders)), this.editorView.dispatch({
|
|
1257
|
+
changes: {
|
|
1258
|
+
from: 0,
|
|
1259
|
+
to: this.editorView.state.doc.length,
|
|
1260
|
+
insert: t.code
|
|
1261
|
+
},
|
|
1262
|
+
effects: e.length > 0 ? e : void 0
|
|
1263
|
+
});
|
|
1264
|
+
}
|
|
1265
|
+
scheduleDebouncedSync() {
|
|
1266
|
+
this.syncTimer !== void 0 && clearTimeout(this.syncTimer), this.syncTimer = setTimeout(() => {
|
|
1267
|
+
this.syncTimer = void 0, this.syncNow();
|
|
1268
|
+
}, Vt);
|
|
1269
|
+
}
|
|
1270
|
+
attachSyncListener() {
|
|
1271
|
+
this.changeListener || (this.changeListener = (t) => {
|
|
1272
|
+
Wt(t) && this.scheduleDebouncedSync();
|
|
1273
|
+
}, this.workspace.addChangeListener(this.changeListener));
|
|
1274
|
+
}
|
|
1275
|
+
detachSyncListener() {
|
|
1276
|
+
this.changeListener && (this.workspace.removeChangeListener(this.changeListener), this.changeListener = void 0, this.syncTimer !== void 0 && (clearTimeout(this.syncTimer), this.syncTimer = void 0));
|
|
1277
|
+
}
|
|
1278
|
+
}
|
|
1279
|
+
const be = "morphic:";
|
|
1280
|
+
function Ee(n) {
|
|
1281
|
+
return be + n;
|
|
1282
|
+
}
|
|
1283
|
+
function W(n) {
|
|
1284
|
+
return n.startsWith(be) ? n.slice(be.length) : n;
|
|
1285
|
+
}
|
|
1286
|
+
function z(n, t) {
|
|
1287
|
+
return t.has(n) ? Ee(n) : n;
|
|
1288
|
+
}
|
|
1289
|
+
function jt(n, t, e, o) {
|
|
1290
|
+
return Xe(ce, { behaviors: e, definitions: t }, o, !1), ce.workspaceToCode(n);
|
|
1291
|
+
}
|
|
1292
|
+
function Ut(n, t, e, o) {
|
|
1293
|
+
Xe(ce, { behaviors: e, definitions: t }, o, !0);
|
|
1294
|
+
const s = ce.workspaceToCode(n);
|
|
1295
|
+
return io(s);
|
|
1296
|
+
}
|
|
1297
|
+
const Gt = "// __MORPHIC_BLOCK_START:", Xt = "// __MORPHIC_BLOCK_END:", Ne = "__";
|
|
1298
|
+
function Xe(n, t, e, o = !1) {
|
|
1299
|
+
n.STATEMENT_PREFIX = (e == null ? void 0 : e.statementPrefix) ?? null, n.STATEMENT_SUFFIX = (e == null ? void 0 : e.statementSuffix) ?? null, n.INFINITE_LOOP_TRAP = (e == null ? void 0 : e.infiniteLoopTrap) ?? null, e != null && e.reservedWords && n.addReservedWords(e.reservedWords);
|
|
1300
|
+
for (const [i, s] of t.definitions) {
|
|
1301
|
+
const r = st(t.behaviors[i]);
|
|
1302
|
+
n.forBlock[Ee(i)] = (c, a) => {
|
|
1303
|
+
const l = Kt(c, a), d = r ? r(l) : to(i, s);
|
|
1304
|
+
if (qt(c, s))
|
|
1305
|
+
return [eo(d), je.NONE];
|
|
1306
|
+
const u = Qt(d);
|
|
1307
|
+
return o ? `${Gt}${c.id}${Ne}
|
|
1308
|
+
${u}${Xt}${c.id}${Ne}
|
|
1309
|
+
` : u;
|
|
1310
|
+
};
|
|
1311
|
+
}
|
|
1312
|
+
}
|
|
1313
|
+
function Kt(n, t) {
|
|
1314
|
+
var c;
|
|
1315
|
+
const e = {}, o = {}, i = {}, s = n.workspace.isFlyout ? "toolbox" : "workspace", r = Ke(n) >= 2;
|
|
1316
|
+
for (const a of n.inputList) {
|
|
1317
|
+
const l = (c = a.connection) == null ? void 0 : c.targetBlock();
|
|
1318
|
+
if (l)
|
|
1319
|
+
if (l.outputConnection) {
|
|
1320
|
+
const d = t.valueToCode(n, a.name, je.NONE) || "undefined";
|
|
1321
|
+
e[a.name] = r ? Yt(d, l) : d;
|
|
1322
|
+
} else
|
|
1323
|
+
e[a.name] = t.statementToCode(n, a.name).trimEnd();
|
|
1324
|
+
for (const d of a.fieldRow) {
|
|
1325
|
+
if (!d.name)
|
|
1326
|
+
continue;
|
|
1327
|
+
const u = String(d.getValue() ?? "");
|
|
1328
|
+
o[d.name] = u, i[d.name] = JSON.stringify(u);
|
|
1329
|
+
}
|
|
1330
|
+
}
|
|
1331
|
+
return {
|
|
1332
|
+
blockId: n.id,
|
|
1333
|
+
blockType: W(n.type),
|
|
1334
|
+
mode: vt(n) ?? "default",
|
|
1335
|
+
context: s,
|
|
1336
|
+
inputs: e,
|
|
1337
|
+
fields: o,
|
|
1338
|
+
quoted: i
|
|
1339
|
+
};
|
|
1340
|
+
}
|
|
1341
|
+
function Yt(n, t) {
|
|
1342
|
+
return !Jt(t) || Zt(n) ? n : `(${n})`;
|
|
1343
|
+
}
|
|
1344
|
+
function Jt(n) {
|
|
1345
|
+
return Ke(n) > 0;
|
|
1346
|
+
}
|
|
1347
|
+
function Ke(n) {
|
|
1348
|
+
return n.inputList.filter(
|
|
1349
|
+
(t) => {
|
|
1350
|
+
var e;
|
|
1351
|
+
return ((e = t.connection) == null ? void 0 : e.type) === B.INPUT_VALUE;
|
|
1352
|
+
}
|
|
1353
|
+
).length;
|
|
1354
|
+
}
|
|
1355
|
+
function Zt(n) {
|
|
1356
|
+
if (!n.startsWith("(") || !n.endsWith(")"))
|
|
1357
|
+
return !1;
|
|
1358
|
+
let t = 0;
|
|
1359
|
+
for (let e = 0; e < n.length; e++) {
|
|
1360
|
+
const o = n[e];
|
|
1361
|
+
if (o === "(")
|
|
1362
|
+
t++;
|
|
1363
|
+
else if (o === ")" && (t--, t === 0))
|
|
1364
|
+
return e === n.length - 1;
|
|
1365
|
+
}
|
|
1366
|
+
return !1;
|
|
1367
|
+
}
|
|
1368
|
+
function qt(n, t) {
|
|
1369
|
+
return n.outputConnection ? !0 : t.output === !1 ? !1 : t.output !== void 0;
|
|
1370
|
+
}
|
|
1371
|
+
function Qt(n) {
|
|
1372
|
+
const t = n.trim();
|
|
1373
|
+
return t ? t.endsWith(";") || t.endsWith("}") || t.endsWith(`
|
|
1374
|
+
`) ? `${t}
|
|
1375
|
+
` : `${t};
|
|
1376
|
+
` : "";
|
|
1377
|
+
}
|
|
1378
|
+
function eo(n) {
|
|
1379
|
+
const t = n.trim();
|
|
1380
|
+
return t ? t.endsWith(";") ? t.slice(0, -1) : t : "undefined";
|
|
1381
|
+
}
|
|
1382
|
+
function to(n, t) {
|
|
1383
|
+
return t.output !== void 0 && t.output !== !1 ? "undefined" : `// No generator behavior defined for "${n}"`;
|
|
1384
|
+
}
|
|
1385
|
+
const oo = /^\s*\/\/ __MORPHIC_BLOCK_START:(.+)__$/, no = /^\s*\/\/ __MORPHIC_BLOCK_END:(.+)__$/;
|
|
1386
|
+
function io(n) {
|
|
1387
|
+
const t = n.split(`
|
|
1388
|
+
`), e = [], o = /* @__PURE__ */ new Map(), i = /* @__PURE__ */ new Map();
|
|
1389
|
+
let s = 0;
|
|
1390
|
+
for (const r of t) {
|
|
1391
|
+
const c = r.match(oo);
|
|
1392
|
+
if (c) {
|
|
1393
|
+
i.set(c[1], s + 1);
|
|
1394
|
+
continue;
|
|
1395
|
+
}
|
|
1396
|
+
const a = r.match(no);
|
|
1397
|
+
if (a) {
|
|
1398
|
+
const l = a[1], d = i.get(l);
|
|
1399
|
+
d !== void 0 && (o.set(l, { startLine: d, endLine: s }), i.delete(l));
|
|
1400
|
+
continue;
|
|
1401
|
+
}
|
|
1402
|
+
e.push(r), s++;
|
|
1403
|
+
}
|
|
1404
|
+
return { code: e.join(`
|
|
1405
|
+
`), metadata: o, placeholders: [] };
|
|
1406
|
+
}
|
|
1407
|
+
function te(n, t = {}) {
|
|
1408
|
+
for (const e of n.elements)
|
|
1409
|
+
if (j(t[e]) === "code") return e;
|
|
1410
|
+
}
|
|
1411
|
+
function Se(n, t, e = {}, o = []) {
|
|
1412
|
+
const i = n.elements, s = o.find((c) => c.name === t);
|
|
1413
|
+
if (s) {
|
|
1414
|
+
for (const c of s.elements)
|
|
1415
|
+
if (j(e[c]) === "code" && i[c] !== void 0)
|
|
1416
|
+
return { mode: t, template: i[c], elementName: c, inputSlots: n.inputSlots };
|
|
1417
|
+
}
|
|
1418
|
+
for (const [c, a] of Object.entries(i))
|
|
1419
|
+
if (j(e[c]) === "code")
|
|
1420
|
+
return { mode: t, template: a, elementName: c, inputSlots: n.inputSlots };
|
|
1421
|
+
if (i.block !== void 0)
|
|
1422
|
+
return { mode: t, template: i.block, elementName: "block", inputSlots: n.inputSlots };
|
|
1423
|
+
const r = Object.entries(i)[0];
|
|
1424
|
+
if (r === void 0)
|
|
1425
|
+
throw new Error(`Block "${n.identifier}" does not have any elements.`);
|
|
1426
|
+
return { mode: t, template: r[1], elementName: r[0], inputSlots: n.inputSlots };
|
|
1427
|
+
}
|
|
1428
|
+
function F(n, t) {
|
|
1429
|
+
if (t.length === 0) return;
|
|
1430
|
+
if (n.indent === "" || !t.includes(`
|
|
1431
|
+
`)) {
|
|
1432
|
+
n.output += t;
|
|
1433
|
+
return;
|
|
1434
|
+
}
|
|
1435
|
+
const e = t.split(`
|
|
1436
|
+
`);
|
|
1437
|
+
n.output += e[0];
|
|
1438
|
+
for (let o = 1; o < e.length; o++)
|
|
1439
|
+
n.output += `
|
|
1440
|
+
` + n.indent + e[o];
|
|
1441
|
+
}
|
|
1442
|
+
function ke(n, t, e, o, i, s) {
|
|
1443
|
+
const r = { output: "", metadata: /* @__PURE__ */ new Map(), placeholders: [], indent: "" }, c = { mode: t, definitions: e, elementTypes: o, modeDefs: i, elementOverride: s };
|
|
1444
|
+
let a = !0;
|
|
1445
|
+
for (const l of n.getTopBlocks(!0)) {
|
|
1446
|
+
const d = r.output.length;
|
|
1447
|
+
a || (r.output += `
|
|
1448
|
+
`), Ye(l, c, r), r.output.length === d + (a ? 0 : 1) ? r.output = r.output.slice(0, d) : a = !1;
|
|
1449
|
+
}
|
|
1450
|
+
return { code: r.output, metadata: r.metadata, placeholders: r.placeholders };
|
|
1451
|
+
}
|
|
1452
|
+
function Ye(n, t, e) {
|
|
1453
|
+
const o = 1 + e.indent.length;
|
|
1454
|
+
let i = n, s = !0;
|
|
1455
|
+
for (; i; ) {
|
|
1456
|
+
const r = e.output.length;
|
|
1457
|
+
s || F(e, `
|
|
1458
|
+
`), Je(i, t, e), e.output.length === r + (s ? 0 : o) ? e.output = e.output.slice(0, r) : s = !1, i = i.getNextBlock();
|
|
1459
|
+
}
|
|
1460
|
+
}
|
|
1461
|
+
function so(n) {
|
|
1462
|
+
return n.inputList.some(
|
|
1463
|
+
(t) => {
|
|
1464
|
+
var e;
|
|
1465
|
+
return ((e = t.connection) == null ? void 0 : e.type) === B.INPUT_VALUE;
|
|
1466
|
+
}
|
|
1467
|
+
);
|
|
1468
|
+
}
|
|
1469
|
+
function ro(n, t) {
|
|
1470
|
+
var o;
|
|
1471
|
+
let e = 0;
|
|
1472
|
+
for (const i of n) {
|
|
1473
|
+
if (i.kind !== "placeholder") continue;
|
|
1474
|
+
const s = (o = t.inputSlots) == null ? void 0 : o[String(i.index)];
|
|
1475
|
+
((s == null ? void 0 : s.kind) ?? "value") === "value" && e++;
|
|
1476
|
+
}
|
|
1477
|
+
return e;
|
|
1478
|
+
}
|
|
1479
|
+
function Je(n, t, e, o = !1) {
|
|
1480
|
+
var p, y, S;
|
|
1481
|
+
const i = t.definitions.get(W(n.type));
|
|
1482
|
+
if (!i) {
|
|
1483
|
+
const h = e.output.length, f = G(e.output);
|
|
1484
|
+
if (co(n, e), e.output.length === h) return;
|
|
1485
|
+
const x = G(e.output), T = e.output.endsWith(`
|
|
1486
|
+
`) ? Math.max(f, x - 1) : x;
|
|
1487
|
+
let C = e.output.length;
|
|
1488
|
+
for (; C > h && e.output[C - 1] === `
|
|
1489
|
+
`; ) C--;
|
|
1490
|
+
const I = oe(n);
|
|
1491
|
+
e.metadata.set(n.id, {
|
|
1492
|
+
startLine: f,
|
|
1493
|
+
endLine: T,
|
|
1494
|
+
startChar: h,
|
|
1495
|
+
endChar: C,
|
|
1496
|
+
atomic: !!I
|
|
1497
|
+
}), X(e, h, "set", I ?? void 0);
|
|
1498
|
+
return;
|
|
1499
|
+
}
|
|
1500
|
+
let s, r;
|
|
1501
|
+
if (t.elementOverride) {
|
|
1502
|
+
const h = i.elements[t.elementOverride];
|
|
1503
|
+
if (h === void 0) return;
|
|
1504
|
+
s = h, r = t.elementOverride;
|
|
1505
|
+
} else
|
|
1506
|
+
try {
|
|
1507
|
+
const h = Se(i, t.mode, t.elementTypes, t.modeDefs);
|
|
1508
|
+
s = h.template, r = h.elementName;
|
|
1509
|
+
} catch {
|
|
1510
|
+
return;
|
|
1511
|
+
}
|
|
1512
|
+
const c = r ? t.elementTypes[r] : void 0, a = e.output.length;
|
|
1513
|
+
o && F(e, "(");
|
|
1514
|
+
const l = e.output.length, d = G(e.output), u = {}, w = Y(s), v = ro(w, i) >= 2;
|
|
1515
|
+
for (const h of w) {
|
|
1516
|
+
if (h.kind === "text") {
|
|
1517
|
+
F(e, h.value);
|
|
1518
|
+
continue;
|
|
1519
|
+
}
|
|
1520
|
+
if (h.kind === "field") {
|
|
1521
|
+
const O = n.getField(h.name);
|
|
1522
|
+
if (!O) continue;
|
|
1523
|
+
const R = e.output.length;
|
|
1524
|
+
F(e, $e(i, r, h.name, O));
|
|
1525
|
+
const L = Oe(n, O, h.name);
|
|
1526
|
+
L && X(e, R, "set", L);
|
|
1527
|
+
continue;
|
|
1528
|
+
}
|
|
1529
|
+
if (h.kind === "image")
|
|
1530
|
+
continue;
|
|
1531
|
+
const f = (p = i.inputSlots) == null ? void 0 : p[String(h.index)], x = f == null ? void 0 : f.name;
|
|
1532
|
+
if (!x) continue;
|
|
1533
|
+
const T = n.getInput(x), C = ((y = T == null ? void 0 : T.connection) == null ? void 0 : y.targetBlock()) ?? null, I = C != null && C.isShadow() ? null : C;
|
|
1534
|
+
if ((f == null ? void 0 : f.kind) === "statement") {
|
|
1535
|
+
const O = G(e.output), R = e.indent, L = e.output.lastIndexOf(`
|
|
1536
|
+
`) + 1;
|
|
1537
|
+
e.indent = ((S = /^[ \t]*/.exec(e.output.slice(L))) == null ? void 0 : S[0]) ?? "", I && Ye(I, t, e), e.indent = R;
|
|
1538
|
+
const M = G(e.output), A = e.output.endsWith(`
|
|
1539
|
+
`) ? Math.max(O, M - 1) : M;
|
|
1540
|
+
u[x] = { startLine: O, endLine: A };
|
|
1541
|
+
continue;
|
|
1542
|
+
}
|
|
1543
|
+
let D = !1;
|
|
1544
|
+
if (!I) {
|
|
1545
|
+
const O = C != null && C.isShadow() ? C.inputList.flatMap(($) => $.fieldRow).filter(($) => !!$.name) : [], R = !(C != null && C.isShadow()) && T ? T.fieldRow.filter(($) => {
|
|
1546
|
+
var V;
|
|
1547
|
+
return !!$.name && !((V = i.fields) != null && V[$.name]);
|
|
1548
|
+
}) : [], L = O.length > 0 || R.length > 0, M = L ? lo(f, c) : void 0;
|
|
1549
|
+
M && F(e, M);
|
|
1550
|
+
const A = e.output.length;
|
|
1551
|
+
if (O.length > 0) {
|
|
1552
|
+
const $ = C ? t.definitions.get(W(C.type)) : void 0;
|
|
1553
|
+
for (const V of O)
|
|
1554
|
+
F(
|
|
1555
|
+
e,
|
|
1556
|
+
$ && V.name ? $e($, r, V.name, V) : ue(V)
|
|
1557
|
+
);
|
|
1558
|
+
} else if (R.length > 0)
|
|
1559
|
+
for (const $ of R) {
|
|
1560
|
+
const V = e.output.length;
|
|
1561
|
+
F(e, ue($));
|
|
1562
|
+
const xe = Oe(n, $, $.name);
|
|
1563
|
+
xe && (X(e, V, "set", xe), D = !0);
|
|
1564
|
+
}
|
|
1565
|
+
let H;
|
|
1566
|
+
if (L || (F(e, ao(f == null ? void 0 : f.check)), H = { parentBlockId: n.id, inputName: x }), !D) {
|
|
1567
|
+
const $ = C != null && C.isShadow() ? oe(C) ?? void 0 : void 0;
|
|
1568
|
+
X(e, A, "default", $, H);
|
|
1569
|
+
}
|
|
1570
|
+
M && F(e, M);
|
|
1571
|
+
continue;
|
|
1572
|
+
}
|
|
1573
|
+
const _ = e.output.length;
|
|
1574
|
+
Je(I, t, e, v && so(I)), X(e, _, "set", oe(I) ?? void 0);
|
|
1575
|
+
}
|
|
1576
|
+
if (e.output.length === l) {
|
|
1577
|
+
e.output = e.output.slice(0, a);
|
|
1578
|
+
return;
|
|
1579
|
+
}
|
|
1580
|
+
o && F(e, ")");
|
|
1581
|
+
const b = G(e.output), E = e.output.endsWith(`
|
|
1582
|
+
`) ? Math.max(d, b - 1) : b;
|
|
1583
|
+
let k = e.output.length;
|
|
1584
|
+
for (; k > a && e.output[k - 1] === `
|
|
1585
|
+
`; ) k--;
|
|
1586
|
+
const m = {
|
|
1587
|
+
startLine: d,
|
|
1588
|
+
endLine: E,
|
|
1589
|
+
startChar: a,
|
|
1590
|
+
endChar: k
|
|
1591
|
+
};
|
|
1592
|
+
Object.keys(u).length > 0 && (m.statementSlots = u), oe(n) && (m.atomic = !0), e.metadata.set(n.id, m);
|
|
1593
|
+
}
|
|
1594
|
+
function co(n, t) {
|
|
1595
|
+
for (const e of n.inputList)
|
|
1596
|
+
for (const o of e.fieldRow)
|
|
1597
|
+
o.name && F(t, ue(o));
|
|
1598
|
+
}
|
|
1599
|
+
function X(n, t, e, o, i) {
|
|
1600
|
+
const s = n.output.length;
|
|
1601
|
+
if (s > t) {
|
|
1602
|
+
const r = { start: t, end: s, kind: e };
|
|
1603
|
+
o && (r.edit = o), i && (r.emptySlot = i), n.placeholders.push(r);
|
|
1604
|
+
}
|
|
1605
|
+
}
|
|
1606
|
+
function ao(n) {
|
|
1607
|
+
const t = Array.isArray(n) ? n[0] : n;
|
|
1608
|
+
return t ? `[${{ Number: "NUMBER", String: "TEXT", Boolean: "BOOL" }[t] ?? t.toUpperCase()}]` : "[VALUE]";
|
|
1609
|
+
}
|
|
1610
|
+
function lo(n, t) {
|
|
1611
|
+
if (!(!(n != null && n.check) || (Array.isArray(n.check) ? n.check[0] : n.check) !== "String") && !(!t || typeof t == "string"))
|
|
1612
|
+
return t.stringQuote;
|
|
1613
|
+
}
|
|
1614
|
+
function Oe(n, t, e) {
|
|
1615
|
+
let o, i;
|
|
1616
|
+
if (t instanceof B.FieldNumber)
|
|
1617
|
+
o = "number";
|
|
1618
|
+
else if (t instanceof B.FieldDropdown) {
|
|
1619
|
+
o = "dropdown";
|
|
1620
|
+
const s = t.getOptions();
|
|
1621
|
+
Array.isArray(s) && (i = s.filter((r) => Array.isArray(r) && r.length >= 2).map(([r, c]) => [String(r), String(c)]));
|
|
1622
|
+
} else if (t instanceof B.FieldTextInput)
|
|
1623
|
+
o = "text";
|
|
1624
|
+
else
|
|
1625
|
+
return null;
|
|
1626
|
+
return { blockId: n.id, fieldName: e, fieldType: o, options: i };
|
|
1627
|
+
}
|
|
1628
|
+
function oe(n) {
|
|
1629
|
+
let t = null, e = null, o = 0;
|
|
1630
|
+
for (const c of n.inputList) {
|
|
1631
|
+
if (c.connection && c.connection.type === B.INPUT_VALUE) return null;
|
|
1632
|
+
for (const a of c.fieldRow)
|
|
1633
|
+
a.name && (o++, t = a.name, e = a);
|
|
1634
|
+
}
|
|
1635
|
+
if (o !== 1 || !t || !e) return null;
|
|
1636
|
+
const i = e.constructor.name;
|
|
1637
|
+
let s = "text", r;
|
|
1638
|
+
if (i.includes("Number"))
|
|
1639
|
+
s = "number";
|
|
1640
|
+
else if (i.includes("Dropdown")) {
|
|
1641
|
+
s = "dropdown";
|
|
1642
|
+
const c = e.getOptions;
|
|
1643
|
+
if (typeof c == "function") {
|
|
1644
|
+
const a = c.call(e);
|
|
1645
|
+
Array.isArray(a) && (r = a.filter((l) => Array.isArray(l) && l.length >= 2).map(([l, d]) => [String(l), String(d)]));
|
|
1646
|
+
}
|
|
1647
|
+
}
|
|
1648
|
+
return { blockId: n.id, fieldName: t, fieldType: s, options: r };
|
|
1649
|
+
}
|
|
1650
|
+
function $e(n, t, e, o) {
|
|
1651
|
+
var s, r;
|
|
1652
|
+
const i = (s = n.fields) == null ? void 0 : s[e];
|
|
1653
|
+
if ((i == null ? void 0 : i.type) === "dropdown" && t) {
|
|
1654
|
+
const c = String(o.getValue());
|
|
1655
|
+
for (const a of i.options)
|
|
1656
|
+
if (!(typeof a != "object" || Array.isArray(a)) && a.value === c)
|
|
1657
|
+
return ((r = a.display) == null ? void 0 : r[t]) ?? c;
|
|
1658
|
+
}
|
|
1659
|
+
return ue(o);
|
|
1660
|
+
}
|
|
1661
|
+
function ue(n) {
|
|
1662
|
+
if (!n) return "";
|
|
1663
|
+
if (n instanceof B.FieldDropdown)
|
|
1664
|
+
return String(n.getValue());
|
|
1665
|
+
const t = n.getText;
|
|
1666
|
+
return typeof t == "function" ? t.call(n) : String(n.getValue());
|
|
1667
|
+
}
|
|
1668
|
+
function G(n) {
|
|
1669
|
+
let t = 1;
|
|
1670
|
+
for (let e = 0; e < n.length; e++)
|
|
1671
|
+
n[e] === `
|
|
1672
|
+
` && t++;
|
|
1673
|
+
return t;
|
|
1674
|
+
}
|
|
1675
|
+
const ho = "rgba(255, 255, 255, 0.07)";
|
|
1676
|
+
class uo {
|
|
1677
|
+
constructor(t, e, o = {}) {
|
|
1678
|
+
g(this, "workspace");
|
|
1679
|
+
g(this, "editors");
|
|
1680
|
+
g(this, "blockToCode");
|
|
1681
|
+
g(this, "codeToBlock");
|
|
1682
|
+
g(this, "blockListener");
|
|
1683
|
+
g(this, "cursorCallbacks", /* @__PURE__ */ new Map());
|
|
1684
|
+
g(this, "emptyClickCallbacks", /* @__PURE__ */ new Map());
|
|
1685
|
+
/** Prevents circular updates. */
|
|
1686
|
+
g(this, "guard", !1);
|
|
1687
|
+
this.workspace = t, this.editors = Array.isArray(e) ? [...e] : [e], this.blockToCode = o.blockToCode !== !1, this.codeToBlock = o.codeToBlock !== !1;
|
|
1688
|
+
const i = o.highlightColor ?? ho;
|
|
1689
|
+
for (const s of this.editors)
|
|
1690
|
+
s.setHighlightColor(i);
|
|
1691
|
+
}
|
|
1692
|
+
/** Start listening for selection events in both directions. */
|
|
1693
|
+
enable() {
|
|
1694
|
+
this.blockToCode && this.attachBlockListener(), this.codeToBlock && this.attachCursorListeners();
|
|
1695
|
+
}
|
|
1696
|
+
/** Stop listening and clear highlights / selection. */
|
|
1697
|
+
disable() {
|
|
1698
|
+
this.detachBlockListener(), this.detachCursorListeners();
|
|
1699
|
+
for (const t of this.editors)
|
|
1700
|
+
t.clearHighlight();
|
|
1701
|
+
}
|
|
1702
|
+
// ── Block → Code ────────────────────────────────────────
|
|
1703
|
+
attachBlockListener() {
|
|
1704
|
+
this.blockListener || (this.blockListener = (t) => {
|
|
1705
|
+
if (t.type !== "selected" || this.guard) return;
|
|
1706
|
+
const e = t.newElementId ?? void 0;
|
|
1707
|
+
this.guard = !0, this.broadcastHighlight(e), this.guard = !1;
|
|
1708
|
+
}, this.workspace.addChangeListener(this.blockListener));
|
|
1709
|
+
}
|
|
1710
|
+
detachBlockListener() {
|
|
1711
|
+
this.blockListener && (this.workspace.removeChangeListener(this.blockListener), this.blockListener = void 0);
|
|
1712
|
+
}
|
|
1713
|
+
/** Update every editor's highlight for the given block (or clear if undefined). */
|
|
1714
|
+
broadcastHighlight(t) {
|
|
1715
|
+
for (const e of this.editors) {
|
|
1716
|
+
if (!t) {
|
|
1717
|
+
e.clearHighlight();
|
|
1718
|
+
continue;
|
|
1719
|
+
}
|
|
1720
|
+
const o = this.computeOwnSpans(t, e.metadata);
|
|
1721
|
+
o ? e.highlightLines(o) : e.clearHighlight();
|
|
1722
|
+
}
|
|
1723
|
+
}
|
|
1724
|
+
// ── Code → Block ────────────────────────────────────────
|
|
1725
|
+
attachCursorListeners() {
|
|
1726
|
+
for (const t of this.editors) {
|
|
1727
|
+
if (!this.cursorCallbacks.has(t)) {
|
|
1728
|
+
const e = (o) => {
|
|
1729
|
+
if (this.guard) return;
|
|
1730
|
+
const i = this.findBlockAtLine(o, t.metadata);
|
|
1731
|
+
if (!i) {
|
|
1732
|
+
this.clearAll();
|
|
1733
|
+
return;
|
|
1734
|
+
}
|
|
1735
|
+
const s = this.workspace.getBlockById(i);
|
|
1736
|
+
s && B.common.getSelected() !== s && (this.guard = !0, B.common.setSelected(s), this.broadcastHighlight(i), this.guard = !1);
|
|
1737
|
+
};
|
|
1738
|
+
this.cursorCallbacks.set(t, e), t.onCursorLine = e;
|
|
1739
|
+
}
|
|
1740
|
+
if (!this.emptyClickCallbacks.has(t)) {
|
|
1741
|
+
const e = () => {
|
|
1742
|
+
this.guard || this.clearAll();
|
|
1743
|
+
};
|
|
1744
|
+
this.emptyClickCallbacks.set(t, e), t.onEmptyClick = e;
|
|
1745
|
+
}
|
|
1746
|
+
}
|
|
1747
|
+
}
|
|
1748
|
+
detachCursorListeners() {
|
|
1749
|
+
for (const [t, e] of this.cursorCallbacks)
|
|
1750
|
+
t.onCursorLine === e && (t.onCursorLine = void 0);
|
|
1751
|
+
this.cursorCallbacks.clear();
|
|
1752
|
+
for (const [t, e] of this.emptyClickCallbacks)
|
|
1753
|
+
t.onEmptyClick === e && (t.onEmptyClick = void 0);
|
|
1754
|
+
this.emptyClickCallbacks.clear();
|
|
1755
|
+
}
|
|
1756
|
+
/**
|
|
1757
|
+
* Clear the workspace selection and every editor's line highlight. Public
|
|
1758
|
+
* so consumers can reset selection state on view transitions (e.g. when
|
|
1759
|
+
* switching between workspace and codespace presentations).
|
|
1760
|
+
*/
|
|
1761
|
+
clearAll() {
|
|
1762
|
+
this.guard = !0, B.common.setSelected(null);
|
|
1763
|
+
for (const t of this.editors) t.clearHighlight();
|
|
1764
|
+
this.guard = !1;
|
|
1765
|
+
}
|
|
1766
|
+
// ── Helpers ─────────────────────────────────────────────
|
|
1767
|
+
/**
|
|
1768
|
+
* Compute the line spans that belong to a block *excluding* its children
|
|
1769
|
+
* within the given metadata.
|
|
1770
|
+
*/
|
|
1771
|
+
computeOwnSpans(t, e) {
|
|
1772
|
+
const o = e.get(t);
|
|
1773
|
+
if (!o) return;
|
|
1774
|
+
const i = [];
|
|
1775
|
+
for (const [c, a] of e)
|
|
1776
|
+
c !== t && a.startLine >= o.startLine && a.endLine <= o.endLine && i.push(a);
|
|
1777
|
+
if (i.length === 0)
|
|
1778
|
+
return [{ fromLine: o.startLine, toLine: o.endLine }];
|
|
1779
|
+
i.sort((c, a) => c.startLine - a.startLine);
|
|
1780
|
+
const s = [];
|
|
1781
|
+
let r = o.startLine;
|
|
1782
|
+
for (const c of i)
|
|
1783
|
+
r < c.startLine && s.push({ fromLine: r, toLine: c.startLine - 1 }), r = Math.max(r, c.endLine + 1);
|
|
1784
|
+
return r <= o.endLine && s.push({ fromLine: r, toLine: o.endLine }), s.length > 0 ? s : void 0;
|
|
1785
|
+
}
|
|
1786
|
+
/** Scan metadata to find which block contains the given line. */
|
|
1787
|
+
findBlockAtLine(t, e) {
|
|
1788
|
+
let o;
|
|
1789
|
+
for (const [i, s] of e)
|
|
1790
|
+
if (t >= s.startLine && t <= s.endLine) {
|
|
1791
|
+
const r = s.endLine - s.startLine;
|
|
1792
|
+
(!o || r < o.size) && (o = { id: i, size: r });
|
|
1793
|
+
}
|
|
1794
|
+
return o == null ? void 0 : o.id;
|
|
1795
|
+
}
|
|
1796
|
+
}
|
|
1797
|
+
const Ze = {
|
|
1798
|
+
statement: { above: !0, below: !0 },
|
|
1799
|
+
start: { above: !1, below: !0 },
|
|
1800
|
+
end: { above: !0, below: !1 },
|
|
1801
|
+
standalone: { above: !1, below: !1 }
|
|
1802
|
+
}, He = Object.keys(Ze);
|
|
1803
|
+
function po(n) {
|
|
1804
|
+
return n.map((t) => {
|
|
1805
|
+
const e = t.shape ? Ze[t.shape] : void 0;
|
|
1806
|
+
if (!e) return t;
|
|
1807
|
+
const o = { ...t };
|
|
1808
|
+
return e.above && o.previousStatement === void 0 && (o.previousStatement = !0), e.below && o.nextStatement === void 0 && (o.nextStatement = !0), o;
|
|
1809
|
+
});
|
|
1810
|
+
}
|
|
1811
|
+
const pe = "default";
|
|
1812
|
+
function fo(n, t) {
|
|
1813
|
+
const e = Object.keys(t).filter(
|
|
1814
|
+
(o) => j(t[o]) === "code"
|
|
1815
|
+
);
|
|
1816
|
+
return n.map((o) => {
|
|
1817
|
+
var a;
|
|
1818
|
+
const i = (a = o.elements) == null ? void 0 : a[pe];
|
|
1819
|
+
if (i === void 0 || e.length === 0) return o;
|
|
1820
|
+
const { [pe]: s, ...r } = o.elements, c = { ...r };
|
|
1821
|
+
for (const l of e)
|
|
1822
|
+
c[l] === void 0 && (c[l] = i);
|
|
1823
|
+
return { ...o, elements: c };
|
|
1824
|
+
});
|
|
1825
|
+
}
|
|
1826
|
+
function mo(n) {
|
|
1827
|
+
const t = /* @__PURE__ */ new Map(), e = Array.isArray(n) ? n : [n];
|
|
1828
|
+
for (const o of e)
|
|
1829
|
+
go(o, t), t.set(o.identifier, o);
|
|
1830
|
+
return t;
|
|
1831
|
+
}
|
|
1832
|
+
function go(n, t) {
|
|
1833
|
+
if (!n.identifier.trim())
|
|
1834
|
+
throw new Error("Each block definition must include a non-empty identifier.");
|
|
1835
|
+
if (Object.keys(n.elements).length === 0)
|
|
1836
|
+
throw new Error(`Block "${n.identifier}" must define at least one element.`);
|
|
1837
|
+
if (t.has(n.identifier))
|
|
1838
|
+
throw new Error(`Duplicate block identifier "${n.identifier}".`);
|
|
1839
|
+
}
|
|
1840
|
+
const ko = 4 / 3, vo = { normal: "400", bold: "700" }, qe = (n) => vo[n] ?? n;
|
|
1841
|
+
function wo(n) {
|
|
1842
|
+
const t = parseFloat(n);
|
|
1843
|
+
if (!Number.isFinite(t) || t <= 0) return;
|
|
1844
|
+
const e = n.trim().endsWith("pt") ? t : t / ko;
|
|
1845
|
+
return Math.round(e * 100) / 100;
|
|
1846
|
+
}
|
|
1847
|
+
function Qe(n) {
|
|
1848
|
+
const t = n.getRenderer().getConstants();
|
|
1849
|
+
return {
|
|
1850
|
+
family: t.FIELD_TEXT_FONTFAMILY,
|
|
1851
|
+
size: t.FIELD_TEXT_FONTSIZE,
|
|
1852
|
+
weight: qe(t.FIELD_TEXT_FONTWEIGHT)
|
|
1853
|
+
};
|
|
1854
|
+
}
|
|
1855
|
+
function et(n, t, e) {
|
|
1856
|
+
const o = document.createElement("div");
|
|
1857
|
+
o.setAttribute("aria-hidden", "true"), o.style.cssText = "position:absolute;visibility:hidden;width:0;height:0;overflow:hidden;";
|
|
1858
|
+
let i = o;
|
|
1859
|
+
for (const l of t) {
|
|
1860
|
+
const d = document.createElement("div");
|
|
1861
|
+
d.className = l.join(" "), i.appendChild(d), i = d;
|
|
1862
|
+
}
|
|
1863
|
+
const s = document.createElementNS("http://www.w3.org/2000/svg", "svg"), r = document.createElementNS("http://www.w3.org/2000/svg", "text");
|
|
1864
|
+
r.setAttribute("class", "blocklyText"), s.appendChild(r), i.appendChild(s), n.appendChild(o);
|
|
1865
|
+
const c = getComputedStyle(r), a = {
|
|
1866
|
+
family: c.fontFamily || e.family,
|
|
1867
|
+
size: wo(c.fontSize) ?? e.size,
|
|
1868
|
+
weight: c.fontWeight ? qe(c.fontWeight) : e.weight
|
|
1869
|
+
};
|
|
1870
|
+
return o.remove(), a;
|
|
1871
|
+
}
|
|
1872
|
+
const yo = (n, t) => n.family === t.family && n.size === t.size && n.weight === t.weight, ve = /* @__PURE__ */ new Map();
|
|
1873
|
+
function Co(n, t) {
|
|
1874
|
+
const e = `${n.name}
|
|
1875
|
+
${t.family}
|
|
1876
|
+
${t.size}
|
|
1877
|
+
${t.weight}`;
|
|
1878
|
+
let o = ve.get(e);
|
|
1879
|
+
if (!o) {
|
|
1880
|
+
const i = `${n.name}-morphic-font-${ve.size + 1}`;
|
|
1881
|
+
o = B.Theme.defineTheme(i, {
|
|
1882
|
+
name: i,
|
|
1883
|
+
base: n,
|
|
1884
|
+
fontStyle: { family: t.family, size: t.size, weight: t.weight },
|
|
1885
|
+
...n.startHats !== void 0 && { startHats: n.startHats }
|
|
1886
|
+
}), ve.set(e, o);
|
|
1887
|
+
}
|
|
1888
|
+
return o;
|
|
1889
|
+
}
|
|
1890
|
+
function tt(n, t, e, o) {
|
|
1891
|
+
const i = yo(o, e) ? t : Co(t, o);
|
|
1892
|
+
return n.getTheme() === i ? !1 : (n.setTheme(i), !0);
|
|
1893
|
+
}
|
|
1894
|
+
function Pe(n) {
|
|
1895
|
+
for (const t of n.getAllBlocks(!1)) {
|
|
1896
|
+
t.markDirty();
|
|
1897
|
+
for (const e of t.inputList)
|
|
1898
|
+
for (const o of e.fieldRow) o.getConstants();
|
|
1899
|
+
}
|
|
1900
|
+
n.render();
|
|
1901
|
+
}
|
|
1902
|
+
function bo(n) {
|
|
1903
|
+
var m, p, y, S;
|
|
1904
|
+
const { definitions: t, elementTypes: e, behaviors: o, modes: i, presets: s, highlighting: r, categories: c } = n, a = [], l = [], d = new Set(Object.keys(e)), u = new Set(
|
|
1905
|
+
Object.keys(e).filter(
|
|
1906
|
+
(h) => j(e[h]) === "code"
|
|
1907
|
+
)
|
|
1908
|
+
), w = new Set((c ?? []).map((h) => h.name));
|
|
1909
|
+
d.has(pe) && a.push(
|
|
1910
|
+
`elementTypes declares "${pe}", which is reserved for a block's fallback template. Rename the element.`
|
|
1911
|
+
);
|
|
1912
|
+
const v = (h) => t.has(h) || B.Blocks[h] !== void 0;
|
|
1913
|
+
for (const h of t.values()) {
|
|
1914
|
+
const f = h.identifier;
|
|
1915
|
+
if (h.shape !== void 0)
|
|
1916
|
+
if (!He.includes(h.shape))
|
|
1917
|
+
a.push(
|
|
1918
|
+
`Block "${f}": unknown shape "${h.shape}". Use one of: ${He.join(", ")}.`
|
|
1919
|
+
);
|
|
1920
|
+
else {
|
|
1921
|
+
const L = (A) => A !== void 0 && A !== !1, M = [];
|
|
1922
|
+
L(h.output) && M.push("it also has an output, which makes it a value block"), (h.shape === "start" || h.shape === "standalone") && L(h.previousStatement) && M.push("it also connects above (previousStatement)"), (h.shape === "end" || h.shape === "standalone") && L(h.nextStatement) && M.push("it also connects below (nextStatement)"), h.shape === "statement" && (h.previousStatement === !1 || h.nextStatement === !1) && M.push("a connection flag is set to false");
|
|
1923
|
+
for (const A of M)
|
|
1924
|
+
l.push(`Block "${f}": shape "${h.shape}" contradicts its flags: ${A}. The flags win.`);
|
|
1925
|
+
}
|
|
1926
|
+
const x = ae(o[f]), T = !!(x != null && x.onViewApplied);
|
|
1927
|
+
for (const L of Object.keys(h.elements))
|
|
1928
|
+
d.has(L) || l.push(
|
|
1929
|
+
`Block "${f}": element "${L}" is not declared in elementTypes — it renders with no known type.`
|
|
1930
|
+
);
|
|
1931
|
+
const C = /* @__PURE__ */ new Map(), I = /* @__PURE__ */ new Set(), D = /* @__PURE__ */ new Set();
|
|
1932
|
+
for (const [L, M] of Object.entries(h.elements)) {
|
|
1933
|
+
if (!u.has(L)) continue;
|
|
1934
|
+
const A = /* @__PURE__ */ new Set();
|
|
1935
|
+
for (const H of Y(M))
|
|
1936
|
+
H.kind === "placeholder" ? (A.add(H.index), I.add(H.index)) : H.kind === "field" && D.add(H.name);
|
|
1937
|
+
C.set(L, A);
|
|
1938
|
+
}
|
|
1939
|
+
const _ = [...C.entries()];
|
|
1940
|
+
if (_.length > 1) {
|
|
1941
|
+
const [L, M] = _[0];
|
|
1942
|
+
for (const [A, H] of _.slice(1))
|
|
1943
|
+
Eo(M, H) || a.push(
|
|
1944
|
+
`Block "${f}": code elements "${L}" and "${A}" use different placeholders (${_e(M)} vs ${_e(H)}). All code elements must share the same %N set, or an input disappears when switching to the differing mode.`
|
|
1945
|
+
);
|
|
1946
|
+
}
|
|
1947
|
+
const O = new Set(
|
|
1948
|
+
Object.keys(h.inputSlots ?? {}).map((L) => Number(L)).filter((L) => Number.isInteger(L))
|
|
1949
|
+
);
|
|
1950
|
+
for (const L of I)
|
|
1951
|
+
(m = h.inputSlots) != null && m[String(L)] || l.push(
|
|
1952
|
+
`Block "${f}": placeholder %${L} has no inputSlots["${L}"] entry — the input is auto-named ARG${L} and can't be read by name in behaviors.`
|
|
1953
|
+
);
|
|
1954
|
+
for (const L of O)
|
|
1955
|
+
I.has(L) || l.push(
|
|
1956
|
+
`Block "${f}": inputSlots["${L}"] has no matching %${L} placeholder in any code element — the config is ignored.`
|
|
1957
|
+
);
|
|
1958
|
+
const R = new Set(Object.keys(h.fields ?? {}));
|
|
1959
|
+
for (const L of D)
|
|
1960
|
+
!((p = h.fields) != null && p[L]) && !T && a.push(
|
|
1961
|
+
`Block "${f}": field token %${L} has no fields["${L}"] entry and the block has no onViewApplied behavior to create it — the field will never appear.`
|
|
1962
|
+
);
|
|
1963
|
+
for (const L of R)
|
|
1964
|
+
D.has(L) || l.push(
|
|
1965
|
+
`Block "${f}": fields["${L}"] has no matching %${L} token in any code element — the config is ignored.`
|
|
1966
|
+
);
|
|
1967
|
+
for (const [L, M] of Object.entries(h.fields ?? {}))
|
|
1968
|
+
if ((M == null ? void 0 : M.type) === "dropdown") {
|
|
1969
|
+
for (const A of M.options)
|
|
1970
|
+
if (!(typeof A != "object" || Array.isArray(A) || !A.display))
|
|
1971
|
+
for (const H of Object.keys(A.display))
|
|
1972
|
+
u.has(H) || l.push(
|
|
1973
|
+
`Block "${f}": fields["${L}"] option "${A.value}" has display for "${H}", which is not a code element — that entry never applies.`
|
|
1974
|
+
);
|
|
1975
|
+
}
|
|
1976
|
+
h.category !== void 0 && w.size > 0 && !w.has(h.category) && l.push(
|
|
1977
|
+
`Block "${f}": category "${h.category}" is not declared in categories — the block loses its colour and grouping.`
|
|
1978
|
+
);
|
|
1979
|
+
for (const [L, M] of Object.entries(h.inputSlots ?? {}))
|
|
1980
|
+
(M == null ? void 0 : M.kind) !== void 0 && !Re.includes(M.kind) && a.push(
|
|
1981
|
+
`Block "${f}": inputSlots["${L}"] has unknown kind "${M.kind}". Use one of: ${Re.join(", ")}.`
|
|
1982
|
+
);
|
|
1983
|
+
for (const [L, M] of Object.entries(h.fields ?? {}))
|
|
1984
|
+
Ve.includes(M == null ? void 0 : M.type) || a.push(
|
|
1985
|
+
`Block "${f}": fields["${L}"] has unknown type "${M == null ? void 0 : M.type}". Use one of: ${Ve.join(", ")}.`
|
|
1986
|
+
);
|
|
1987
|
+
for (const [L, M] of Object.entries(h.inputSlots ?? {}))
|
|
1988
|
+
k((y = M == null ? void 0 : M.default) == null ? void 0 : y.shadow, `Block "${f}": inputSlots["${L}"].default.shadow`), k((S = M == null ? void 0 : M.default) == null ? void 0 : S.placeholder, `Block "${f}": inputSlots["${L}"].default.placeholder`);
|
|
1989
|
+
}
|
|
1990
|
+
for (const [h, f] of Object.entries(e)) {
|
|
1991
|
+
const x = j(f);
|
|
1992
|
+
Fe.includes(x) || a.push(
|
|
1993
|
+
`elementTypes."${h}": unknown type "${x}". Use one of: ${Fe.join(", ")}.`
|
|
1994
|
+
);
|
|
1995
|
+
}
|
|
1996
|
+
for (const [h, f] of Object.entries(e)) {
|
|
1997
|
+
if (typeof f == "string") continue;
|
|
1998
|
+
const x = f.empty;
|
|
1999
|
+
if (x)
|
|
2000
|
+
for (const [T, C] of Object.entries(x))
|
|
2001
|
+
k(C.shadow, `elementTypes.${h}.empty.${T}.shadow`), k(C.placeholder, `elementTypes.${h}.empty.${T}.placeholder`);
|
|
2002
|
+
}
|
|
2003
|
+
for (const [h, f] of Object.entries(e)) {
|
|
2004
|
+
if (typeof f == "string") continue;
|
|
2005
|
+
const x = f.type;
|
|
2006
|
+
f.size !== void 0 && x !== "image" && l.push(
|
|
2007
|
+
`elementTypes."${h}": "size" applies only to image elements — ignored for a ${x} element.`
|
|
2008
|
+
), f.stringQuote !== void 0 && x !== "code" && l.push(
|
|
2009
|
+
`elementTypes."${h}": "stringQuote" applies only to code elements — ignored for a ${x} element.`
|
|
2010
|
+
), f.empty !== void 0 && x !== "code" && l.push(
|
|
2011
|
+
`elementTypes."${h}": "empty" applies only to code elements — ignored for a ${x} element.`
|
|
2012
|
+
);
|
|
2013
|
+
}
|
|
2014
|
+
for (const h of i ?? [])
|
|
2015
|
+
for (const f of h.elements)
|
|
2016
|
+
d.has(f) || l.push(
|
|
2017
|
+
`Mode "${h.name}": element "${f}" is not declared in elementTypes — it renders nothing.`
|
|
2018
|
+
);
|
|
2019
|
+
for (const h of Object.keys(r ?? {}))
|
|
2020
|
+
u.has(h) || l.push(
|
|
2021
|
+
`highlighting["${h}"] is not a code element — its rules will never apply.`
|
|
2022
|
+
);
|
|
2023
|
+
const b = /* @__PURE__ */ new Map(), E = (h, f) => {
|
|
2024
|
+
const x = b.get(h) ?? /* @__PURE__ */ new Set();
|
|
2025
|
+
x.add(f), b.set(h, x);
|
|
2026
|
+
};
|
|
2027
|
+
for (const h of d) E(h, "element");
|
|
2028
|
+
for (const h of i ?? []) E(h.name, "mode");
|
|
2029
|
+
for (const h of s ?? []) E(h.name, "preset");
|
|
2030
|
+
for (const [h, f] of b)
|
|
2031
|
+
f.size > 1 && l.push(
|
|
2032
|
+
`Name "${h}" is used as ${[...f].sort().join(" and ")} — reusing a name across element / mode / preset is allowed but confusing; consider distinct names.`
|
|
2033
|
+
);
|
|
2034
|
+
return { errors: a, warnings: l };
|
|
2035
|
+
function k(h, f) {
|
|
2036
|
+
!h || v(h) || a.push(
|
|
2037
|
+
`${f}: "${h}" is neither a defined block nor a registered Blockly block type.`
|
|
2038
|
+
);
|
|
2039
|
+
}
|
|
2040
|
+
}
|
|
2041
|
+
const Fe = ["text", "code", "image"], Re = ["value", "statement"], Ve = ["dropdown", "text", "number", "checkbox"];
|
|
2042
|
+
function Eo(n, t) {
|
|
2043
|
+
if (n.size !== t.size) return !1;
|
|
2044
|
+
for (const e of n) if (!t.has(e)) return !1;
|
|
2045
|
+
return !0;
|
|
2046
|
+
}
|
|
2047
|
+
function _e(n) {
|
|
2048
|
+
const t = [...n].sort((e, o) => e - o);
|
|
2049
|
+
return t.length ? t.map((e) => `%${e}`).join(" ") : "(none)";
|
|
2050
|
+
}
|
|
2051
|
+
class So {
|
|
2052
|
+
/**
|
|
2053
|
+
* Injects visibility CSS derived from mode definitions.
|
|
2054
|
+
* Hides all morphic elements by default, then shows only those listed
|
|
2055
|
+
* in each mode's `elements` array. Developer CSS only needs to handle styling.
|
|
2056
|
+
*/
|
|
2057
|
+
ensureModeVisibilityStyles(t) {
|
|
2058
|
+
const e = ['[class^="morphic-element-"] { display: none; }'];
|
|
2059
|
+
for (const o of t) {
|
|
2060
|
+
const i = P(o.name);
|
|
2061
|
+
for (const s of o.elements) {
|
|
2062
|
+
const r = P(s);
|
|
2063
|
+
e.push(`.morphic-mode-${i} > .morphic-element-${r} { display: block; }`);
|
|
2064
|
+
}
|
|
2065
|
+
}
|
|
2066
|
+
ne("mode-visibility", e.join(`
|
|
2067
|
+
`));
|
|
2068
|
+
}
|
|
2069
|
+
ensureStyles(t, e) {
|
|
2070
|
+
this.ensureStyleBundle(t, "base");
|
|
2071
|
+
for (const o of e)
|
|
2072
|
+
this.ensureStyleBundle(o, `mode:${o.mode}`);
|
|
2073
|
+
}
|
|
2074
|
+
/**
|
|
2075
|
+
* Inject the framework-shipped toolbar layout CSS once per document. The
|
|
2076
|
+
* stylesheet itself lives at `toolbar.css`; we import it as a string via
|
|
2077
|
+
* Vite's `?inline` query so consumers don't need to add a CSS import.
|
|
2078
|
+
* Everything visual inherits from the parent by default; CSS variables let
|
|
2079
|
+
* the host theme without overriding class selectors.
|
|
2080
|
+
*/
|
|
2081
|
+
async ensureToolbarStyles() {
|
|
2082
|
+
const t = await import("./toolbar-kxlfr8TT.js");
|
|
2083
|
+
ne("toolbar", t.default);
|
|
2084
|
+
}
|
|
2085
|
+
ensureCategoryStyles(t) {
|
|
2086
|
+
for (const e of t) {
|
|
2087
|
+
if (!e.color)
|
|
2088
|
+
continue;
|
|
2089
|
+
const o = P(e.name);
|
|
2090
|
+
ne(
|
|
2091
|
+
`category:${o}`,
|
|
2092
|
+
`.morphic-category-${o} { --morphic-category-color: ${e.color}; }`
|
|
2093
|
+
);
|
|
2094
|
+
}
|
|
2095
|
+
}
|
|
2096
|
+
validateModeCoverage(t, e) {
|
|
2097
|
+
const o = new Set(t.map((s) => s.mode)), i = e.filter(
|
|
2098
|
+
(s) => !o.has(s)
|
|
2099
|
+
);
|
|
2100
|
+
i.length > 0 && console.warn(
|
|
2101
|
+
`[MorphicBlocks] Modes without explicit CSS definition: ${i.join(", ")}.`
|
|
2102
|
+
);
|
|
2103
|
+
}
|
|
2104
|
+
ensureStyleBundle(t, e) {
|
|
2105
|
+
t && (t.href && xo(e, t.href), t.cssText && ne(e, t.cssText));
|
|
2106
|
+
}
|
|
2107
|
+
}
|
|
2108
|
+
function ne(n, t) {
|
|
2109
|
+
for (const o of Array.from(document.head.querySelectorAll("style[data-morphic-source]")))
|
|
2110
|
+
if (o.dataset.morphicSource === n && o.textContent === t) return;
|
|
2111
|
+
const e = document.createElement("style");
|
|
2112
|
+
e.dataset.morphicSource = n, e.textContent = t, document.head.appendChild(e);
|
|
2113
|
+
}
|
|
2114
|
+
function xo(n, t) {
|
|
2115
|
+
for (const o of Array.from(document.head.querySelectorAll("link[data-morphic-source]")))
|
|
2116
|
+
if (o.getAttribute("href") === t) return;
|
|
2117
|
+
const e = document.createElement("link");
|
|
2118
|
+
e.rel = "stylesheet", e.href = t, e.dataset.morphicSource = n, document.head.appendChild(e);
|
|
2119
|
+
}
|
|
2120
|
+
const K = "morphic/block-type";
|
|
2121
|
+
class Lo {
|
|
2122
|
+
constructor(t) {
|
|
2123
|
+
g(this, "container");
|
|
2124
|
+
g(this, "workspaceContainer");
|
|
2125
|
+
g(this, "workspace");
|
|
2126
|
+
g(this, "definitions");
|
|
2127
|
+
g(this, "blockColors");
|
|
2128
|
+
g(this, "behaviors");
|
|
2129
|
+
g(this, "elementTypes");
|
|
2130
|
+
g(this, "options");
|
|
2131
|
+
g(this, "modes");
|
|
2132
|
+
g(this, "currentMode");
|
|
2133
|
+
/** Per-element block/text override for code elements in tiles (from the active preset). */
|
|
2134
|
+
g(this, "renderOverride");
|
|
2135
|
+
g(this, "previewWorkspace");
|
|
2136
|
+
g(this, "onPreviewWorkspace");
|
|
2137
|
+
g(this, "previewContainer");
|
|
2138
|
+
/** The preview workspace's own theme and font, before any tile font. */
|
|
2139
|
+
g(this, "previewBase");
|
|
2140
|
+
g(this, "onDragOver");
|
|
2141
|
+
g(this, "onDrop");
|
|
2142
|
+
this.container = t.container, this.workspaceContainer = t.workspaceContainer, this.workspace = t.workspace, this.onPreviewWorkspace = t.onPreviewWorkspace, this.definitions = t.definitions, this.blockColors = t.blockColors, this.behaviors = t.behaviors, this.elementTypes = t.elementTypes ?? {}, this.currentMode = t.mode, this.renderOverride = t.render, this.modes = t.modes ?? [], this.options = t.options ?? {}, this.onDragOver = (e) => {
|
|
2143
|
+
var o;
|
|
2144
|
+
(o = e.dataTransfer) != null && o.types.includes(K) && e.preventDefault();
|
|
2145
|
+
}, this.onDrop = (e) => {
|
|
2146
|
+
var i;
|
|
2147
|
+
const o = (i = e.dataTransfer) == null ? void 0 : i.getData(K);
|
|
2148
|
+
o && (e.preventDefault(), this.createBlockAtPosition(o, e.clientX, e.clientY));
|
|
2149
|
+
}, this.workspaceContainer.addEventListener("dragover", this.onDragOver), this.workspaceContainer.addEventListener("drop", this.onDrop), this.render();
|
|
2150
|
+
}
|
|
2151
|
+
rerender(t, e) {
|
|
2152
|
+
this.currentMode = t, this.renderOverride = e, this.render();
|
|
2153
|
+
}
|
|
2154
|
+
dispose() {
|
|
2155
|
+
var t;
|
|
2156
|
+
this.workspaceContainer.removeEventListener("dragover", this.onDragOver), this.workspaceContainer.removeEventListener("drop", this.onDrop), this.container.innerHTML = "", this.previewWorkspace && (this.previewWorkspace.dispose(), this.previewWorkspace = void 0, this.previewBase = void 0), (t = this.previewContainer) != null && t.parentNode && (this.previewContainer.parentNode.removeChild(this.previewContainer), this.previewContainer = void 0);
|
|
2157
|
+
}
|
|
2158
|
+
render() {
|
|
2159
|
+
if (this.container.innerHTML = "", this.options.modeLabel !== !1) {
|
|
2160
|
+
const i = document.createElement("div");
|
|
2161
|
+
i.className = "morphic-toolbox-header";
|
|
2162
|
+
const s = document.createElement("span");
|
|
2163
|
+
s.className = "morphic-toolbar-label", s.textContent = `Mode: ${this.currentMode}`, i.appendChild(s), this.container.appendChild(i);
|
|
2164
|
+
}
|
|
2165
|
+
const t = this.resolveBlockIds(), e = this.options.categories ?? [];
|
|
2166
|
+
if (e.length === 0) {
|
|
2167
|
+
for (const i of t) {
|
|
2168
|
+
const s = this.definitions.get(i);
|
|
2169
|
+
s && this.container.appendChild(this.createTile(s));
|
|
2170
|
+
}
|
|
2171
|
+
return;
|
|
2172
|
+
}
|
|
2173
|
+
const o = /* @__PURE__ */ new Set();
|
|
2174
|
+
for (const i of e) {
|
|
2175
|
+
const s = this.resolveCategoryBlockIds(i, t);
|
|
2176
|
+
if (s.length === 0) continue;
|
|
2177
|
+
const r = document.createElement("div");
|
|
2178
|
+
r.setAttribute("data-category", P(i.name)), i.color && r.style.setProperty("--morphic-category-color", i.color);
|
|
2179
|
+
for (const c of s) {
|
|
2180
|
+
const a = this.definitions.get(c);
|
|
2181
|
+
a && (r.appendChild(this.createTile(a)), o.add(c));
|
|
2182
|
+
}
|
|
2183
|
+
this.container.appendChild(r);
|
|
2184
|
+
}
|
|
2185
|
+
for (const i of t)
|
|
2186
|
+
if (!o.has(i)) {
|
|
2187
|
+
const s = this.definitions.get(i);
|
|
2188
|
+
s && this.container.appendChild(this.createTile(s));
|
|
2189
|
+
}
|
|
2190
|
+
}
|
|
2191
|
+
createTile(t) {
|
|
2192
|
+
var d;
|
|
2193
|
+
const e = P(this.currentMode), o = P(t.identifier), i = this.blockColors.get(t.identifier), s = document.createElement("div");
|
|
2194
|
+
s.setAttribute("draggable", "true"), s.setAttribute("data-block-type", t.identifier), s.className = `morphic-block morphic-mode-${e} morphic-block-${o}`, i && s.style.setProperty("--morphic-block-color", i);
|
|
2195
|
+
const r = this.modes.find((u) => u.name === this.currentMode), c = (r == null ? void 0 : r.elements) ?? [], a = Object.entries(t.elements), l = [
|
|
2196
|
+
...c.map((u) => a.find(([w]) => w === u)).filter((u) => u !== void 0),
|
|
2197
|
+
...a.filter(([u]) => !c.includes(u))
|
|
2198
|
+
];
|
|
2199
|
+
for (const [u, w] of l) {
|
|
2200
|
+
const v = document.createElement("div");
|
|
2201
|
+
v.className = `morphic-element-${P(u)}`;
|
|
2202
|
+
const b = this.elementTypes[u], E = j(b), k = E === "code", m = E === "image", p = ((d = this.renderOverride) == null ? void 0 : d[u]) ?? (k ? "block" : "text"), y = m ? mt(w, dt(b)) : w;
|
|
2203
|
+
if (k && p === "block") {
|
|
2204
|
+
this.syncPreviewFont(u);
|
|
2205
|
+
const S = this.createBlockPreviewSvg(t, this.currentMode);
|
|
2206
|
+
S ? v.appendChild(S) : v.innerHTML = Te(Y(y));
|
|
2207
|
+
} else
|
|
2208
|
+
v.innerHTML = Te(Y(y));
|
|
2209
|
+
s.appendChild(v);
|
|
2210
|
+
}
|
|
2211
|
+
return s.addEventListener("dragstart", (u) => {
|
|
2212
|
+
var w;
|
|
2213
|
+
(w = u.dataTransfer) == null || w.setData(K, t.identifier);
|
|
2214
|
+
}), s;
|
|
2215
|
+
}
|
|
2216
|
+
ensurePreviewWorkspace() {
|
|
2217
|
+
var t;
|
|
2218
|
+
return this.previewWorkspace ? this.previewWorkspace : (this.previewContainer = document.createElement("div"), this.previewContainer.style.cssText = "position:absolute;left:-9999px;top:-9999px;width:800px;height:600px;overflow:hidden;", document.body.appendChild(this.previewContainer), this.previewWorkspace = B.inject(this.previewContainer, {
|
|
2219
|
+
scrollbars: !1
|
|
2220
|
+
}), (t = this.onPreviewWorkspace) == null || t.call(this, this.previewWorkspace), this.previewWorkspace);
|
|
2221
|
+
}
|
|
2222
|
+
/**
|
|
2223
|
+
* The tile shows a copy of the rendered block outside Blockly, so its text
|
|
2224
|
+
* takes the font the tile's CSS gives that element. Measure with that font.
|
|
2225
|
+
*/
|
|
2226
|
+
syncPreviewFont(t) {
|
|
2227
|
+
const e = this.ensurePreviewWorkspace();
|
|
2228
|
+
this.previewBase ?? (this.previewBase = { theme: e.getTheme(), font: Qe(e) });
|
|
2229
|
+
const o = et(
|
|
2230
|
+
this.container,
|
|
2231
|
+
[
|
|
2232
|
+
["morphic-block", `morphic-mode-${P(this.currentMode)}`],
|
|
2233
|
+
[`morphic-element-${P(t)}`]
|
|
2234
|
+
],
|
|
2235
|
+
this.previewBase.font
|
|
2236
|
+
);
|
|
2237
|
+
tt(e, this.previewBase.theme, this.previewBase.font, o);
|
|
2238
|
+
}
|
|
2239
|
+
createBlockPreviewSvg(t, e) {
|
|
2240
|
+
var o;
|
|
2241
|
+
try {
|
|
2242
|
+
const i = this.ensurePreviewWorkspace(), s = i.newBlock(
|
|
2243
|
+
z(t.identifier, this.definitions)
|
|
2244
|
+
), r = this.blockColors.get(t.identifier);
|
|
2245
|
+
r && s.setColour(r);
|
|
2246
|
+
const c = Se(t, e, this.elementTypes, this.modes);
|
|
2247
|
+
Ue({
|
|
2248
|
+
block: s,
|
|
2249
|
+
definition: t,
|
|
2250
|
+
view: c,
|
|
2251
|
+
mode: "block",
|
|
2252
|
+
context: "toolbox"
|
|
2253
|
+
});
|
|
2254
|
+
const a = ae(
|
|
2255
|
+
this.behaviors[t.identifier]
|
|
2256
|
+
);
|
|
2257
|
+
(o = a == null ? void 0 : a.onViewApplied) == null || o.call(a, s, {
|
|
2258
|
+
Blockly: B,
|
|
2259
|
+
workspace: i,
|
|
2260
|
+
mode: e,
|
|
2261
|
+
context: "toolbox",
|
|
2262
|
+
definition: t
|
|
2263
|
+
}), s.initSvg(), s.render();
|
|
2264
|
+
const l = s.getSvgRoot();
|
|
2265
|
+
if (!l)
|
|
2266
|
+
return s.dispose(!1), null;
|
|
2267
|
+
const d = l.getBBox(), u = l.cloneNode(!0), w = 4, v = document.createElementNS(
|
|
2268
|
+
"http://www.w3.org/2000/svg",
|
|
2269
|
+
"svg"
|
|
2270
|
+
);
|
|
2271
|
+
return v.setAttribute("width", String(Math.ceil(d.width + w * 2))), v.setAttribute("height", String(Math.ceil(d.height + w * 2))), v.setAttribute(
|
|
2272
|
+
"viewBox",
|
|
2273
|
+
`${d.x - w} ${d.y - w} ${d.width + w * 2} ${d.height + w * 2}`
|
|
2274
|
+
), v.appendChild(u), s.dispose(!1), v;
|
|
2275
|
+
} catch {
|
|
2276
|
+
return null;
|
|
2277
|
+
}
|
|
2278
|
+
}
|
|
2279
|
+
createBlockAtPosition(t, e, o) {
|
|
2280
|
+
const i = this.workspace, s = i.getInjectionDiv().getBoundingClientRect(), r = (e - s.left - i.scrollX) / i.scale, c = (o - s.top - i.scrollY) / i.scale, a = i.newBlock(
|
|
2281
|
+
z(t, this.definitions)
|
|
2282
|
+
);
|
|
2283
|
+
a.initSvg(), a.render(), a.moveTo(new B.utils.Coordinate(r, c));
|
|
2284
|
+
}
|
|
2285
|
+
resolveBlockIds() {
|
|
2286
|
+
return this.options.blocks ?? [...this.definitions.keys()];
|
|
2287
|
+
}
|
|
2288
|
+
resolveCategoryBlockIds(t, e) {
|
|
2289
|
+
return t.blocks && t.blocks.length > 0 ? t.blocks.filter((o) => e.includes(o)) : e.filter(
|
|
2290
|
+
(o) => {
|
|
2291
|
+
var i, s;
|
|
2292
|
+
return ((s = (i = this.definitions.get(o)) == null ? void 0 : i.category) == null ? void 0 : s.toLowerCase()) === t.name.toLowerCase();
|
|
2293
|
+
}
|
|
2294
|
+
);
|
|
2295
|
+
}
|
|
2296
|
+
}
|
|
2297
|
+
function we(n, t) {
|
|
2298
|
+
var o;
|
|
2299
|
+
if (n.blocks && n.blocks.length > 0)
|
|
2300
|
+
return n.blocks;
|
|
2301
|
+
const e = [];
|
|
2302
|
+
for (const i of t.values())
|
|
2303
|
+
((o = i.category) == null ? void 0 : o.toLowerCase()) === n.name.toLowerCase() && e.push(i.identifier);
|
|
2304
|
+
return e;
|
|
2305
|
+
}
|
|
2306
|
+
function We(n, t) {
|
|
2307
|
+
var i, s, r;
|
|
2308
|
+
if ((n.kind ?? ((i = n.categories) != null && i.length ? "categoryToolbox" : "flyoutToolbox")) === "categoryToolbox")
|
|
2309
|
+
return {
|
|
2310
|
+
kind: "categoryToolbox",
|
|
2311
|
+
contents: (n.categories ?? []).map((a) => ({
|
|
2312
|
+
kind: "category",
|
|
2313
|
+
name: a.name,
|
|
2314
|
+
// Blockly's toolbox API uses British "colour" — read from our `color` field.
|
|
2315
|
+
colour: a.color,
|
|
2316
|
+
contents: we(a, t).map((l) => ({
|
|
2317
|
+
kind: "block",
|
|
2318
|
+
type: z(l, t)
|
|
2319
|
+
}))
|
|
2320
|
+
}))
|
|
2321
|
+
};
|
|
2322
|
+
if ((s = n.categories) != null && s.length) {
|
|
2323
|
+
const c = n.categories, a = [];
|
|
2324
|
+
return c.forEach((d, u) => {
|
|
2325
|
+
const w = P(d.name);
|
|
2326
|
+
a.push({
|
|
2327
|
+
kind: "label",
|
|
2328
|
+
id: `morphic-category-${w}`,
|
|
2329
|
+
text: d.name,
|
|
2330
|
+
"web-class": `morphic-flyout-category-label morphic-category-${w}`
|
|
2331
|
+
});
|
|
2332
|
+
for (const v of ze(
|
|
2333
|
+
we(d, t)
|
|
2334
|
+
))
|
|
2335
|
+
a.push({
|
|
2336
|
+
kind: "block",
|
|
2337
|
+
type: z(v, t)
|
|
2338
|
+
});
|
|
2339
|
+
u < c.length - 1 && a.push({
|
|
2340
|
+
kind: "sep",
|
|
2341
|
+
gap: 14
|
|
2342
|
+
});
|
|
2343
|
+
}), {
|
|
2344
|
+
kind: "flyoutToolbox",
|
|
2345
|
+
contents: a
|
|
2346
|
+
};
|
|
2347
|
+
}
|
|
2348
|
+
return {
|
|
2349
|
+
kind: "flyoutToolbox",
|
|
2350
|
+
contents: (n.blocks ?? ze(
|
|
2351
|
+
((r = n.categories) == null ? void 0 : r.flatMap(
|
|
2352
|
+
(c) => we(c, t)
|
|
2353
|
+
)) ?? [...t.keys()]
|
|
2354
|
+
)).map((c) => ({
|
|
2355
|
+
kind: "block",
|
|
2356
|
+
type: z(c, t)
|
|
2357
|
+
}))
|
|
2358
|
+
};
|
|
2359
|
+
}
|
|
2360
|
+
function ze(n) {
|
|
2361
|
+
return n.filter((t, e, o) => o.indexOf(t) === e);
|
|
2362
|
+
}
|
|
2363
|
+
const To = '<svg viewBox="0 0 16 16" width="14" height="14" fill="none" stroke="currentColor" stroke-width="1.5"><rect x="4" y="4" width="9" height="10" rx="1.5"/><path d="M3 11V3a1 1 0 0 1 1-1h7"/></svg>', Bo = '<svg viewBox="0 0 16 16" width="14" height="14" fill="none" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round"><path d="M5 3h6v2H5z"/><path d="M4 3H3a1 1 0 0 0-1 1v9a1 1 0 0 0 1 1h10a1 1 0 0 0 1-1V4a1 1 0 0 0-1-1h-1"/></svg>', Mo = '<svg viewBox="0 0 16 16" width="14" height="14" fill="none" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round"><path d="M3 8h7a3 3 0 0 1 0 6H6"/><polyline points="5 5 2 8 5 11"/></svg>', Io = '<svg viewBox="0 0 16 16" width="14" height="14" fill="none" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round"><path d="M13 8H6a3 3 0 0 0 0 6h4"/><polyline points="11 5 14 8 11 11"/></svg>', Do = '<svg viewBox="0 0 16 16" width="14" height="14" fill="none" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round"><polyline points="3 4 13 4"/><path d="M5 4V3a1 1 0 0 1 1-1h4a1 1 0 0 1 1 1v1"/><path d="M4 4l1 9a1 1 0 0 0 1 1h4a1 1 0 0 0 1-1l1-9"/></svg>', Ao = '<svg viewBox="0 0 16 16" width="14" height="14" fill="currentColor"><polygon points="4,3 13,8 4,13"/></svg>', No = '<svg viewBox="0 0 16 16" width="14" height="14" fill="none" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round"><circle cx="7" cy="7" r="4"/><line x1="10" y1="10" x2="14" y2="14"/><line x1="5" y1="7" x2="9" y2="7"/><line x1="7" y1="5" x2="7" y2="9"/></svg>', Oo = '<svg viewBox="0 0 16 16" width="14" height="14" fill="none" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round"><circle cx="7" cy="7" r="4"/><line x1="10" y1="10" x2="14" y2="14"/><line x1="5" y1="7" x2="9" y2="7"/></svg>', $o = '<svg viewBox="0 0 16 16" width="14" height="14" fill="none" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round"><polyline points="3 6 3 3 6 3"/><polyline points="13 6 13 3 10 3"/><polyline points="3 10 3 13 6 13"/><polyline points="13 10 13 13 10 13"/></svg>', Ho = '<svg viewBox="0 0 16 16" width="14" height="14" fill="none" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round"><path d="M3 2h8l2 2v10H3z"/><rect x="5" y="2" width="6" height="4"/><rect x="5" y="9" width="6" height="4"/></svg>', Po = '<svg viewBox="0 0 16 16" width="14" height="14" fill="none" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round"><path d="M3 8v5a1 1 0 0 0 1 1h8a1 1 0 0 0 1-1V8"/><polyline points="5 5 8 2 11 5"/><line x1="8" y1="2" x2="8" y2="10"/></svg>', Fo = '<svg viewBox="0 0 16 16" width="14" height="14" fill="none" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round"><path d="M3 8a5 5 0 1 0 1.5-3.5"/><polyline points="3 2 3 5 6 5"/></svg>', Ro = '<svg viewBox="0 0 16 16" width="14" height="14" fill="none" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round"><path d="M2 8s2-4 6-4 6 4 6 4-2 4-6 4-6-4-6-4z"/><circle cx="8" cy="8" r="1.5"/></svg>', N = {
|
|
2364
|
+
/** "Mode: <name>" label for the pane's active mode. */
|
|
2365
|
+
modeLabel() {
|
|
2366
|
+
return {
|
|
2367
|
+
id: "mode-label",
|
|
2368
|
+
render: (n) => {
|
|
2369
|
+
const t = document.createElement("span");
|
|
2370
|
+
return t.className = "morphic-toolbar-label", t.textContent = Vo(n), t;
|
|
2371
|
+
}
|
|
2372
|
+
};
|
|
2373
|
+
},
|
|
2374
|
+
/**
|
|
2375
|
+
* Block-aware copy. Resolves the active block via selection-sync or cursor
|
|
2376
|
+
* line, then copies it to the framework's internal clipboard. Mirrors the
|
|
2377
|
+
* generated code text to the system clipboard as a best-effort.
|
|
2378
|
+
*/
|
|
2379
|
+
copy() {
|
|
2380
|
+
return {
|
|
2381
|
+
id: "copy",
|
|
2382
|
+
label: "Copy",
|
|
2383
|
+
title: "Copy block",
|
|
2384
|
+
icon: To,
|
|
2385
|
+
onClick: (n) => {
|
|
2386
|
+
n.engine.copyActiveBlock(n.pane);
|
|
2387
|
+
}
|
|
2388
|
+
};
|
|
2389
|
+
},
|
|
2390
|
+
paste() {
|
|
2391
|
+
return {
|
|
2392
|
+
id: "paste",
|
|
2393
|
+
label: "Paste",
|
|
2394
|
+
title: "Paste block",
|
|
2395
|
+
icon: Bo,
|
|
2396
|
+
disabled: (n) => !n.engine.hasClipboardContents(),
|
|
2397
|
+
onClick: (n) => {
|
|
2398
|
+
n.engine.pasteActiveBlock(n.pane);
|
|
2399
|
+
}
|
|
2400
|
+
};
|
|
2401
|
+
},
|
|
2402
|
+
undo() {
|
|
2403
|
+
return {
|
|
2404
|
+
id: "undo",
|
|
2405
|
+
label: "Undo",
|
|
2406
|
+
title: "Undo (block model)",
|
|
2407
|
+
icon: Mo,
|
|
2408
|
+
onClick: (n) => {
|
|
2409
|
+
var t;
|
|
2410
|
+
(t = n.engine.getWorkspace()) == null || t.undo(!1);
|
|
2411
|
+
},
|
|
2412
|
+
disabled: (n) => {
|
|
2413
|
+
var t;
|
|
2414
|
+
return !((t = n.engine.getWorkspace()) != null && t.getUndoStack().length);
|
|
2415
|
+
}
|
|
2416
|
+
};
|
|
2417
|
+
},
|
|
2418
|
+
redo() {
|
|
2419
|
+
return {
|
|
2420
|
+
id: "redo",
|
|
2421
|
+
label: "Redo",
|
|
2422
|
+
title: "Redo (block model)",
|
|
2423
|
+
icon: Io,
|
|
2424
|
+
onClick: (n) => {
|
|
2425
|
+
var t;
|
|
2426
|
+
(t = n.engine.getWorkspace()) == null || t.undo(!0);
|
|
2427
|
+
},
|
|
2428
|
+
disabled: (n) => {
|
|
2429
|
+
var t;
|
|
2430
|
+
return !((t = n.engine.getWorkspace()) != null && t.getRedoStack().length);
|
|
2431
|
+
}
|
|
2432
|
+
};
|
|
2433
|
+
},
|
|
2434
|
+
zoomIn() {
|
|
2435
|
+
return {
|
|
2436
|
+
id: "zoom-in",
|
|
2437
|
+
label: "Zoom in",
|
|
2438
|
+
title: "Zoom in",
|
|
2439
|
+
icon: No,
|
|
2440
|
+
onClick: (n) => n.engine.zoomPane(n.pane, "in")
|
|
2441
|
+
};
|
|
2442
|
+
},
|
|
2443
|
+
zoomOut() {
|
|
2444
|
+
return {
|
|
2445
|
+
id: "zoom-out",
|
|
2446
|
+
label: "Zoom out",
|
|
2447
|
+
title: "Zoom out",
|
|
2448
|
+
icon: Oo,
|
|
2449
|
+
onClick: (n) => n.engine.zoomPane(n.pane, "out")
|
|
2450
|
+
};
|
|
2451
|
+
},
|
|
2452
|
+
zoomFit() {
|
|
2453
|
+
return {
|
|
2454
|
+
id: "zoom-fit",
|
|
2455
|
+
label: "Fit",
|
|
2456
|
+
title: "Reset zoom",
|
|
2457
|
+
icon: $o,
|
|
2458
|
+
onClick: (n) => n.engine.zoomPane(n.pane, "fit")
|
|
2459
|
+
};
|
|
2460
|
+
},
|
|
2461
|
+
clear() {
|
|
2462
|
+
return {
|
|
2463
|
+
id: "clear",
|
|
2464
|
+
label: "Clear",
|
|
2465
|
+
title: "Clear workspace",
|
|
2466
|
+
icon: Do,
|
|
2467
|
+
onClick: (n) => {
|
|
2468
|
+
var t;
|
|
2469
|
+
window.confirm("Clear workspace? This cannot be undone.") && ((t = n.engine.getWorkspace()) == null || t.clear());
|
|
2470
|
+
}
|
|
2471
|
+
};
|
|
2472
|
+
},
|
|
2473
|
+
/**
|
|
2474
|
+
* Run the workspace's generated JavaScript and dispatch a "morphic-run"
|
|
2475
|
+
* event on the engine. Not in any default set — hosts typically wire a
|
|
2476
|
+
* global Run button via `engine.runJavaScript()` instead. Kept here for
|
|
2477
|
+
* cases where a toolbar Run button is desired.
|
|
2478
|
+
*/
|
|
2479
|
+
runCode() {
|
|
2480
|
+
return {
|
|
2481
|
+
id: "run",
|
|
2482
|
+
label: "Run",
|
|
2483
|
+
title: "Run generated code",
|
|
2484
|
+
icon: Ao,
|
|
2485
|
+
onClick: (n) => {
|
|
2486
|
+
n.engine.runJavaScript();
|
|
2487
|
+
}
|
|
2488
|
+
};
|
|
2489
|
+
},
|
|
2490
|
+
/** Serialize the workspace and download as JSON. Not in defaults. */
|
|
2491
|
+
save() {
|
|
2492
|
+
return {
|
|
2493
|
+
id: "save",
|
|
2494
|
+
label: "Save",
|
|
2495
|
+
title: "Save workspace as JSON",
|
|
2496
|
+
icon: Ho,
|
|
2497
|
+
onClick: (n) => {
|
|
2498
|
+
if (!n.engine.getWorkspace()) return;
|
|
2499
|
+
const e = n.engine.serializeWorkspace(), o = new Blob([JSON.stringify(e, null, 2)], { type: "application/json" }), i = URL.createObjectURL(o), s = document.createElement("a");
|
|
2500
|
+
s.href = i, s.download = "workspace.json", s.click(), URL.revokeObjectURL(i);
|
|
2501
|
+
}
|
|
2502
|
+
};
|
|
2503
|
+
},
|
|
2504
|
+
/** Load a workspace JSON file via a file picker. Not in defaults. */
|
|
2505
|
+
load() {
|
|
2506
|
+
return {
|
|
2507
|
+
id: "load",
|
|
2508
|
+
label: "Load",
|
|
2509
|
+
title: "Load workspace JSON",
|
|
2510
|
+
icon: Po,
|
|
2511
|
+
onClick: (n) => {
|
|
2512
|
+
const t = document.createElement("input");
|
|
2513
|
+
t.type = "file", t.accept = "application/json", t.addEventListener("change", async () => {
|
|
2514
|
+
var o;
|
|
2515
|
+
const e = (o = t.files) == null ? void 0 : o[0];
|
|
2516
|
+
if (e)
|
|
2517
|
+
try {
|
|
2518
|
+
const i = await e.text(), s = JSON.parse(i);
|
|
2519
|
+
n.engine.loadWorkspace(s);
|
|
2520
|
+
} catch (i) {
|
|
2521
|
+
window.alert(`Load failed: ${i instanceof Error ? i.message : String(i)}`);
|
|
2522
|
+
}
|
|
2523
|
+
}), t.click();
|
|
2524
|
+
}
|
|
2525
|
+
};
|
|
2526
|
+
},
|
|
2527
|
+
/** Reset the workspace to empty. With confirm. Not in defaults. */
|
|
2528
|
+
reset() {
|
|
2529
|
+
return {
|
|
2530
|
+
id: "reset",
|
|
2531
|
+
label: "Reset",
|
|
2532
|
+
title: "Reset workspace",
|
|
2533
|
+
icon: Fo,
|
|
2534
|
+
onClick: (n) => {
|
|
2535
|
+
var t;
|
|
2536
|
+
window.confirm("Reset workspace to empty? This cannot be undone.") && ((t = n.engine.getWorkspace()) == null || t.clear());
|
|
2537
|
+
}
|
|
2538
|
+
};
|
|
2539
|
+
},
|
|
2540
|
+
/**
|
|
2541
|
+
* Toggle the preview pane's host container visibility. Not in defaults — the
|
|
2542
|
+
* developer decides whether the user controls this. Looks for an element
|
|
2543
|
+
* with `data-morphic-preview-host` and flips its `hidden` attribute.
|
|
2544
|
+
*/
|
|
2545
|
+
togglePreview() {
|
|
2546
|
+
return {
|
|
2547
|
+
id: "toggle-preview",
|
|
2548
|
+
label: "Preview",
|
|
2549
|
+
title: "Toggle preview pane",
|
|
2550
|
+
icon: Ro,
|
|
2551
|
+
onClick: () => {
|
|
2552
|
+
const n = document.querySelector("[data-morphic-preview-host]");
|
|
2553
|
+
n && (n.hidden = !n.hidden);
|
|
2554
|
+
}
|
|
2555
|
+
};
|
|
2556
|
+
},
|
|
2557
|
+
readOnlyBadge() {
|
|
2558
|
+
return {
|
|
2559
|
+
id: "read-only",
|
|
2560
|
+
align: "right",
|
|
2561
|
+
render: () => {
|
|
2562
|
+
const n = document.createElement("span");
|
|
2563
|
+
return n.className = "morphic-toolbar-badge", n.textContent = "read-only", n;
|
|
2564
|
+
}
|
|
2565
|
+
};
|
|
2566
|
+
},
|
|
2567
|
+
spacer() {
|
|
2568
|
+
return {
|
|
2569
|
+
id: "spacer",
|
|
2570
|
+
render: () => {
|
|
2571
|
+
const n = document.createElement("span");
|
|
2572
|
+
return n.className = "morphic-toolbar-spacer", n;
|
|
2573
|
+
}
|
|
2574
|
+
};
|
|
2575
|
+
},
|
|
2576
|
+
defaultsFor(n) {
|
|
2577
|
+
switch (n) {
|
|
2578
|
+
case "workspace":
|
|
2579
|
+
case "codespace":
|
|
2580
|
+
return [
|
|
2581
|
+
N.modeLabel(),
|
|
2582
|
+
N.spacer(),
|
|
2583
|
+
N.undo(),
|
|
2584
|
+
N.redo(),
|
|
2585
|
+
N.copy(),
|
|
2586
|
+
N.paste(),
|
|
2587
|
+
N.zoomIn(),
|
|
2588
|
+
N.zoomOut(),
|
|
2589
|
+
N.zoomFit(),
|
|
2590
|
+
N.clear()
|
|
2591
|
+
];
|
|
2592
|
+
case "preview":
|
|
2593
|
+
return [
|
|
2594
|
+
N.modeLabel(),
|
|
2595
|
+
N.spacer(),
|
|
2596
|
+
N.copy(),
|
|
2597
|
+
N.zoomIn(),
|
|
2598
|
+
N.zoomOut(),
|
|
2599
|
+
N.zoomFit(),
|
|
2600
|
+
N.readOnlyBadge()
|
|
2601
|
+
];
|
|
2602
|
+
}
|
|
2603
|
+
}
|
|
2604
|
+
};
|
|
2605
|
+
function Vo(n) {
|
|
2606
|
+
const t = n.engine, e = n.pane === "workspace" ? t.getWorkspaceMode() : n.pane === "codespace" ? t.getCodespaceMode() : t.getPreviewMode();
|
|
2607
|
+
return e ? `Mode: ${e}` : "";
|
|
2608
|
+
}
|
|
2609
|
+
function _o(n, t, e) {
|
|
2610
|
+
const o = t.items ?? N.defaultsFor(t.pane), i = t.display ?? "icon";
|
|
2611
|
+
n.classList.add("morphic-toolbar"), n.setAttribute("data-morphic-pane", t.pane);
|
|
2612
|
+
const s = document.createElement("div");
|
|
2613
|
+
s.className = "morphic-toolbar-left";
|
|
2614
|
+
const r = document.createElement("div");
|
|
2615
|
+
r.className = "morphic-toolbar-right";
|
|
2616
|
+
const c = () => {
|
|
2617
|
+
s.replaceChildren(), r.replaceChildren();
|
|
2618
|
+
let a = !1;
|
|
2619
|
+
for (const l of o) {
|
|
2620
|
+
if (l.visible && !l.visible(e)) continue;
|
|
2621
|
+
if (l.id === "spacer") {
|
|
2622
|
+
a = !0;
|
|
2623
|
+
continue;
|
|
2624
|
+
}
|
|
2625
|
+
(l.align === "right" || a && l.align !== "left" ? r : s).appendChild(Wo(l, i, e));
|
|
2626
|
+
}
|
|
2627
|
+
};
|
|
2628
|
+
return n.replaceChildren(s, r), c(), { refresh: c, pane: t.pane, dispose: () => n.replaceChildren() };
|
|
2629
|
+
}
|
|
2630
|
+
function Wo(n, t, e) {
|
|
2631
|
+
if (n.render) {
|
|
2632
|
+
const i = n.render(e);
|
|
2633
|
+
if (typeof i == "string") {
|
|
2634
|
+
const s = document.createElement("span");
|
|
2635
|
+
return s.className = "morphic-toolbar-item", s.dataset.toolbarId = n.id, s.textContent = i, s;
|
|
2636
|
+
}
|
|
2637
|
+
return i.classList.add("morphic-toolbar-item"), i.dataset.toolbarId = n.id, i;
|
|
2638
|
+
}
|
|
2639
|
+
const o = document.createElement("button");
|
|
2640
|
+
return o.className = "morphic-toolbar-item", o.dataset.toolbarId = n.id, o.type = "button", n.title && (o.title = n.title), zo(o, n, t), n.disabled && n.disabled(e) && (o.disabled = !0), n.onClick && o.addEventListener("click", () => n.onClick(e)), o;
|
|
2641
|
+
}
|
|
2642
|
+
function zo(n, t, e) {
|
|
2643
|
+
const o = e !== "label" && !!t.icon, i = e !== "icon" && !!t.label, s = o || e === "icon" && !t.label, r = i || e === "label" && !t.icon;
|
|
2644
|
+
if (s && t.icon) {
|
|
2645
|
+
const c = document.createElement("span");
|
|
2646
|
+
c.className = "morphic-toolbar-icon", c.innerHTML = t.icon, n.appendChild(c);
|
|
2647
|
+
}
|
|
2648
|
+
if (r && t.label) {
|
|
2649
|
+
const c = document.createElement("span");
|
|
2650
|
+
c.className = "morphic-toolbar-text", c.textContent = t.label, n.appendChild(c);
|
|
2651
|
+
}
|
|
2652
|
+
!n.title && t.label && (n.title = t.label), !n.childNodes.length && t.label && (n.textContent = t.label);
|
|
2653
|
+
}
|
|
2654
|
+
function ot(n, t) {
|
|
2655
|
+
return t.includes("{") ? { mode: n, cssText: t } : { mode: n, href: t };
|
|
2656
|
+
}
|
|
2657
|
+
function jo(n) {
|
|
2658
|
+
const t = [];
|
|
2659
|
+
for (const [e, o] of Object.entries(n)) {
|
|
2660
|
+
const s = (e.split("/").pop() ?? e).replace(/\.css$/i, ""), r = typeof o == "string" ? o : typeof o == "object" && o !== null && "default" in o ? String(o.default) : void 0;
|
|
2661
|
+
r && t.push(ot(s, r));
|
|
2662
|
+
}
|
|
2663
|
+
return t;
|
|
2664
|
+
}
|
|
2665
|
+
function ye(n) {
|
|
2666
|
+
return typeof n == "string" ? { mode: n } : { mode: n.mode, render: n.render };
|
|
2667
|
+
}
|
|
2668
|
+
const ie = /* @__PURE__ */ new WeakMap(), se = /* @__PURE__ */ new Map();
|
|
2669
|
+
class Yo extends EventTarget {
|
|
2670
|
+
/**
|
|
2671
|
+
* Build an engine from a whole definitions file plus its behaviors — the two
|
|
2672
|
+
* artifacts a developer authors. `blocks` and `elementTypes` configure the
|
|
2673
|
+
* engine; `modes` / `presets` / `highlighting` / `categories` are remembered
|
|
2674
|
+
* as defaults for `mount()` / `mountToolbox()` (a call may still override them).
|
|
2675
|
+
*/
|
|
2676
|
+
constructor(e, o = {}) {
|
|
2677
|
+
super();
|
|
2678
|
+
g(this, "definitions");
|
|
2679
|
+
g(this, "behaviors");
|
|
2680
|
+
g(this, "elementTypes");
|
|
2681
|
+
g(this, "styles", new So());
|
|
2682
|
+
g(this, "registeredBlockTypes", /* @__PURE__ */ new Set());
|
|
2683
|
+
g(this, "toolbars", /* @__PURE__ */ new Set());
|
|
2684
|
+
/** Blockly events listener installed when the first toolbar mounts. */
|
|
2685
|
+
g(this, "toolbarsBlocklyListener");
|
|
2686
|
+
/** Latest internal clipboard contents — set by `copyActiveBlock`, used by `pasteActiveBlock`. */
|
|
2687
|
+
g(this, "lastCopyData");
|
|
2688
|
+
g(this, "mountConfig");
|
|
2689
|
+
g(this, "workspace");
|
|
2690
|
+
g(this, "flyoutWorkspace");
|
|
2691
|
+
g(this, "toolboxCanvas");
|
|
2692
|
+
g(this, "codeEditor");
|
|
2693
|
+
g(this, "codespace");
|
|
2694
|
+
g(this, "previewEditor");
|
|
2695
|
+
g(this, "selectionSync");
|
|
2696
|
+
g(this, "toolboxDefinition");
|
|
2697
|
+
g(this, "blockCategoryIndex", /* @__PURE__ */ new Map());
|
|
2698
|
+
g(this, "appliedWorkspaceClasses", []);
|
|
2699
|
+
g(this, "appliedToolboxFlyoutClasses", []);
|
|
2700
|
+
g(this, "appliedToolboxShellClasses", []);
|
|
2701
|
+
/** Offscreen div created when no user-supplied workspaceContainer is present. */
|
|
2702
|
+
g(this, "headlessWorkspaceHost");
|
|
2703
|
+
/** Teardown fn for codespace drag/drop listeners; set when codespace is mounted. */
|
|
2704
|
+
g(this, "codespaceDropTeardown");
|
|
2705
|
+
/** Redraws the workspace whenever its container changes size. */
|
|
2706
|
+
g(this, "workspaceResizeObserver");
|
|
2707
|
+
/** Preset most recently applied, at mount or via `applyPreset`. */
|
|
2708
|
+
g(this, "activePreset");
|
|
2709
|
+
/** The workspace's theme and font before any mode's CSS font was applied. */
|
|
2710
|
+
g(this, "baseTheme");
|
|
2711
|
+
g(this, "baseFont");
|
|
2712
|
+
/** Counts mounts, so async view setup can tell it was superseded. */
|
|
2713
|
+
g(this, "mountGeneration", 0);
|
|
2714
|
+
/** Options of the active selection sync, reused when an editor is replaced. */
|
|
2715
|
+
g(this, "selectionSyncOptions");
|
|
2716
|
+
/** Format-level fields remembered from the constructor and used as `mount()`
|
|
2717
|
+
* defaults, so the developer hands the whole definitions file in once and the
|
|
2718
|
+
* runtime `mount()` call only carries DOM / runtime concerns. */
|
|
2719
|
+
g(this, "formatModes");
|
|
2720
|
+
g(this, "formatPresets");
|
|
2721
|
+
g(this, "formatHighlighting");
|
|
2722
|
+
g(this, "formatCategories");
|
|
2723
|
+
g(this, "onWorkspaceChange", (e) => {
|
|
2724
|
+
if (!this.workspace || !this.mountConfig || e.workspaceId !== this.workspace.id || e.type !== B.Events.BLOCK_CREATE)
|
|
2725
|
+
return;
|
|
2726
|
+
const o = e;
|
|
2727
|
+
for (const i of o.ids ?? []) {
|
|
2728
|
+
const s = this.workspace.getBlockById(i);
|
|
2729
|
+
if (!s)
|
|
2730
|
+
continue;
|
|
2731
|
+
const r = this.definitions.get(W(s.type));
|
|
2732
|
+
r && (this.applyView(
|
|
2733
|
+
s,
|
|
2734
|
+
r,
|
|
2735
|
+
this.mountConfig.workspaceMode,
|
|
2736
|
+
"workspace"
|
|
2737
|
+
), s.getSvgRoot() || this.deferApplyView(i, this.mountConfig.workspaceMode, "workspace"));
|
|
2738
|
+
}
|
|
2739
|
+
});
|
|
2740
|
+
g(this, "onFlyoutChange", (e) => {
|
|
2741
|
+
if (!this.flyoutWorkspace || !this.mountConfig || e.workspaceId !== this.flyoutWorkspace.id || e.type !== B.Events.BLOCK_CREATE)
|
|
2742
|
+
return;
|
|
2743
|
+
const o = e;
|
|
2744
|
+
for (const i of o.ids ?? []) {
|
|
2745
|
+
const s = this.flyoutWorkspace.getBlockById(
|
|
2746
|
+
i
|
|
2747
|
+
);
|
|
2748
|
+
if (!s)
|
|
2749
|
+
continue;
|
|
2750
|
+
const r = this.definitions.get(W(s.type));
|
|
2751
|
+
r && (this.applyView(
|
|
2752
|
+
s,
|
|
2753
|
+
r,
|
|
2754
|
+
this.mountConfig.toolboxMode,
|
|
2755
|
+
"toolbox"
|
|
2756
|
+
), s.getSvgRoot() || this.deferApplyView(i, this.mountConfig.toolboxMode, "toolbox"));
|
|
2757
|
+
}
|
|
2758
|
+
});
|
|
2759
|
+
g(this, "onStylesLoaded", () => {
|
|
2760
|
+
var e;
|
|
2761
|
+
!this.workspace || !this.mountConfig || (this.syncWorkspaceFont(), Pe(this.workspace), (e = this.toolboxCanvas) == null || e.rerender(this.mountConfig.toolboxMode, this.mountConfig.toolboxRender));
|
|
2762
|
+
});
|
|
2763
|
+
const i = e;
|
|
2764
|
+
this.definitions = mo(
|
|
2765
|
+
po(fo(i.blocks, i.elementTypes ?? {}))
|
|
2766
|
+
), this.behaviors = o, this.elementTypes = i.elementTypes ?? {}, this.formatModes = i.modes, this.formatPresets = i.presets, this.formatHighlighting = i.highlighting, this.formatCategories = i.categories;
|
|
2767
|
+
}
|
|
2768
|
+
/**
|
|
2769
|
+
* Set the editor up. Everything given a container is mounted: workspace,
|
|
2770
|
+
* toolbox, codespace, preview, code editor and toolbars. The workspace is
|
|
2771
|
+
* ready as soon as this returns; the returned promise settles once the text
|
|
2772
|
+
* editors, which load in the background, are ready too.
|
|
2773
|
+
*/
|
|
2774
|
+
mount(e) {
|
|
2775
|
+
var p, y, S, h, f, x, T;
|
|
2776
|
+
this.dispose();
|
|
2777
|
+
const o = {
|
|
2778
|
+
...e,
|
|
2779
|
+
modes: e.modes ?? this.formatModes,
|
|
2780
|
+
presets: e.presets ?? this.formatPresets,
|
|
2781
|
+
highlighting: e.highlighting ?? this.formatHighlighting,
|
|
2782
|
+
toolbox: e.toolbox || this.formatCategories ? {
|
|
2783
|
+
...e.toolbox,
|
|
2784
|
+
categories: ((p = e.toolbox) == null ? void 0 : p.categories) ?? this.formatCategories
|
|
2785
|
+
} : void 0
|
|
2786
|
+
}, i = o.modesFolder ? jo(o.modesFolder) : [], s = [
|
|
2787
|
+
...i,
|
|
2788
|
+
...Object.entries(o.modeStyles ?? {}).filter(([C]) => !i.some((I) => I.mode === C)).map(([C, I]) => ot(C, I))
|
|
2789
|
+
];
|
|
2790
|
+
this.validateDefinitions(o), this.validatePresets(o);
|
|
2791
|
+
const r = (y = o.presets) != null && y.length ? o.presets.find((C) => C.name === o.preset) ?? o.presets[0] : void 0;
|
|
2792
|
+
if (o.preset && !r)
|
|
2793
|
+
throw new Error(`MorphicBlocks.mount: unknown preset "${o.preset}".`);
|
|
2794
|
+
const c = (o.modes ?? []).map((C) => C.name), a = c[0] ?? ((S = s[0]) == null ? void 0 : S.mode) ?? "default", l = r ? ye(r.toolbox) : void 0, d = r ? r.workspace ?? r.codespace ?? a : a, u = l ? l.mode : a, w = l == null ? void 0 : l.render, v = r == null ? void 0 : r.codespace, b = r == null ? void 0 : r.preview;
|
|
2795
|
+
this.validateContainers(o);
|
|
2796
|
+
const E = o.workspaceContainer ?? this.createHeadlessHost(), k = {
|
|
2797
|
+
...o,
|
|
2798
|
+
modeStyles: s,
|
|
2799
|
+
workspaceMode: d,
|
|
2800
|
+
toolboxMode: u,
|
|
2801
|
+
toolboxRender: w,
|
|
2802
|
+
codespaceMode: v,
|
|
2803
|
+
previewMode: b,
|
|
2804
|
+
workspaceHost: E,
|
|
2805
|
+
// A toolbox container means the custom HTML toolbox.
|
|
2806
|
+
canvasToolbox: o.canvasToolbox ?? !!o.toolboxContainer
|
|
2807
|
+
};
|
|
2808
|
+
this.mountConfig = k, this.styles.validateModeCoverage(s, c), (h = k.modes) != null && h.length && this.styles.ensureModeVisibilityStyles(k.modes), this.styles.ensureStyles(k.baseStyle, s);
|
|
2809
|
+
for (const C of Array.from(document.head.querySelectorAll('link[data-morphic-source^="mode:"]')))
|
|
2810
|
+
C.sheet || C.addEventListener("load", this.onStylesLoaded, { once: !0 });
|
|
2811
|
+
(f = document.fonts) == null || f.addEventListener("loadingdone", this.onStylesLoaded), this.blockCategoryIndex = this.createCategoryIndex(
|
|
2812
|
+
k.toolbox,
|
|
2813
|
+
k
|
|
2814
|
+
), this.styles.ensureCategoryStyles(((x = k.toolbox) == null ? void 0 : x.categories) ?? []), this.registerBlocks(), this.toolboxDefinition = this.resolveToolboxDefinition(k);
|
|
2815
|
+
const m = k.blockly ?? {};
|
|
2816
|
+
if (this.workspace = B.inject(k.workspaceHost, {
|
|
2817
|
+
...m,
|
|
2818
|
+
...k.canvasToolbox ? {} : { toolbox: this.toolboxDefinition }
|
|
2819
|
+
}), this.workspace.addChangeListener(this.onWorkspaceChange), ie.set(this.workspace, this), this.baseTheme = this.workspace.getTheme(), this.baseFont = Qe(this.workspace), o.workspaceContainer && typeof ResizeObserver < "u") {
|
|
2820
|
+
const C = this.workspace;
|
|
2821
|
+
this.workspaceResizeObserver = new ResizeObserver(() => B.svgResize(C)), this.workspaceResizeObserver.observe(o.workspaceContainer);
|
|
2822
|
+
}
|
|
2823
|
+
return this.applyWorkspaceContainerClass(), this.syncWorkspaceFont(), this.refreshToolbox(), this.bindFlyoutWorkspace(), this.renderWorkspaceBlocks(), this.renderFlyoutBlocks(), o.toolboxContainer && this.mountToolbox(o.toolboxContainer), this.activePreset = r, r && ((T = k.onPresetApplied) == null || T.call(k, r)), this.mountViews(o, ++this.mountGeneration);
|
|
2824
|
+
}
|
|
2825
|
+
/**
|
|
2826
|
+
* Set up the text editors and what depends on them (toolbars, selection
|
|
2827
|
+
* sync). Runs after the synchronous part of `mount()`; bails out quietly
|
|
2828
|
+
* when the engine was disposed or mounted again meanwhile.
|
|
2829
|
+
*/
|
|
2830
|
+
async mountViews(e, o) {
|
|
2831
|
+
const i = e.editorTheme ? { theme: e.editorTheme } : void 0, s = e.previewTheme ?? e.editorTheme, r = [];
|
|
2832
|
+
if (e.codespaceContainer && r.push(this.mountCodespace(i)), e.previewContainer && r.push(this.mountPreview(e.previewContainer, s ? { theme: s } : void 0)), e.codeEditorContainer && r.push(this.mountCodeEditor(e.codeEditorContainer, i).then(() => {
|
|
2833
|
+
o === this.mountGeneration && this.hideCodeEditor();
|
|
2834
|
+
})), await Promise.all(r), o !== this.mountGeneration || !this.workspace) return;
|
|
2835
|
+
for (const [l, d] of Object.entries(e.toolbarContainers ?? {}))
|
|
2836
|
+
d && this.mountToolbar(d, { pane: l });
|
|
2837
|
+
const c = (e.workspaceContainer ? 1 : 0) + r.length, a = e.selectionSync ?? c > 1;
|
|
2838
|
+
a && r.length > 0 && this.enableSelectionSync(typeof a == "object" ? a : void 0);
|
|
2839
|
+
}
|
|
2840
|
+
/** Presets declared at mount (empty when none were provided). */
|
|
2841
|
+
getPresets() {
|
|
2842
|
+
var e;
|
|
2843
|
+
return [...((e = this.mountConfig) == null ? void 0 : e.presets) ?? []];
|
|
2844
|
+
}
|
|
2845
|
+
/**
|
|
2846
|
+
* The preset most recently applied, at mount or via `applyPreset`, so a host
|
|
2847
|
+
* can mark the active preset button without tracking it itself. `undefined`
|
|
2848
|
+
* when no preset has been applied. Lower level `setModes` calls do not
|
|
2849
|
+
* change it.
|
|
2850
|
+
*/
|
|
2851
|
+
getActivePreset() {
|
|
2852
|
+
return this.activePreset;
|
|
2853
|
+
}
|
|
2854
|
+
/**
|
|
2855
|
+
* Apply a preset by name or index: sets the per-view modes derived from the
|
|
2856
|
+
* preset and notifies `onPresetApplied` so the host can update pane
|
|
2857
|
+
* visibility. Returns the applied preset.
|
|
2858
|
+
*/
|
|
2859
|
+
applyPreset(e) {
|
|
2860
|
+
var r, c;
|
|
2861
|
+
if (!this.mountConfig || !this.workspace)
|
|
2862
|
+
throw new Error(
|
|
2863
|
+
"MorphicBlocks must be mounted before applyPreset can be used."
|
|
2864
|
+
);
|
|
2865
|
+
const o = this.mountConfig.presets ?? [], i = typeof e == "number" ? o[e] : o.find((a) => a.name === e);
|
|
2866
|
+
if (!i)
|
|
2867
|
+
throw new Error(`applyPreset: unknown preset "${e}".`);
|
|
2868
|
+
const s = ye(i.toolbox);
|
|
2869
|
+
return this.setModes({
|
|
2870
|
+
toolboxMode: s.mode,
|
|
2871
|
+
toolboxRender: s.render ?? null,
|
|
2872
|
+
workspaceMode: i.workspace ?? i.codespace,
|
|
2873
|
+
codespaceMode: i.codespace ?? null,
|
|
2874
|
+
previewMode: i.preview ?? null
|
|
2875
|
+
}), this.activePreset = i, (c = (r = this.mountConfig).onPresetApplied) == null || c.call(r, i), i;
|
|
2876
|
+
}
|
|
2877
|
+
/** Validate preset definitions against modes, element types, and containers. */
|
|
2878
|
+
/**
|
|
2879
|
+
* Static validation of the block definitions against the mount config. Warns
|
|
2880
|
+
* on degraded / dead config and throws (listing every structural problem at
|
|
2881
|
+
* once) on breakage that guarantees wrong output. See `validate-definitions`.
|
|
2882
|
+
*/
|
|
2883
|
+
validateDefinitions(e) {
|
|
2884
|
+
var s;
|
|
2885
|
+
const { errors: o, warnings: i } = bo({
|
|
2886
|
+
definitions: this.definitions,
|
|
2887
|
+
elementTypes: this.elementTypes,
|
|
2888
|
+
behaviors: this.behaviors,
|
|
2889
|
+
modes: e.modes,
|
|
2890
|
+
presets: e.presets,
|
|
2891
|
+
highlighting: e.highlighting,
|
|
2892
|
+
categories: (s = e.toolbox) == null ? void 0 : s.categories
|
|
2893
|
+
});
|
|
2894
|
+
if (i.length > 0 && console.warn(
|
|
2895
|
+
`[MorphicBlocks] Definition warnings:
|
|
2896
|
+
- ${i.join(`
|
|
2897
|
+
- `)}`
|
|
2898
|
+
), o.length > 0)
|
|
2899
|
+
throw new Error(
|
|
2900
|
+
`[MorphicBlocks] Invalid definitions:
|
|
2901
|
+
- ${o.join(`
|
|
2902
|
+
- `)}`
|
|
2903
|
+
);
|
|
2904
|
+
}
|
|
2905
|
+
validatePresets(e) {
|
|
2906
|
+
const o = e.presets ?? [];
|
|
2907
|
+
if (o.length === 0) return;
|
|
2908
|
+
const i = e.modes ?? [], s = new Map(i.map((c) => [c.name, c])), r = /* @__PURE__ */ new Set();
|
|
2909
|
+
for (const c of o) {
|
|
2910
|
+
if (r.has(c.name))
|
|
2911
|
+
throw new Error(`Preset "${c.name}" is defined more than once.`);
|
|
2912
|
+
if (r.add(c.name), !c.workspace && !c.codespace)
|
|
2913
|
+
throw new Error(
|
|
2914
|
+
`Preset "${c.name}": at least one of workspace or codespace must be set.`
|
|
2915
|
+
);
|
|
2916
|
+
if (c.codespace && !e.codespaceContainer)
|
|
2917
|
+
throw new Error(
|
|
2918
|
+
`Preset "${c.name}" uses a codespace but no codespaceContainer was provided.`
|
|
2919
|
+
);
|
|
2920
|
+
const a = ye(c.toolbox), l = [
|
|
2921
|
+
["toolbox", a.mode],
|
|
2922
|
+
["workspace", c.workspace],
|
|
2923
|
+
["codespace", c.codespace],
|
|
2924
|
+
["preview", c.preview]
|
|
2925
|
+
];
|
|
2926
|
+
for (const [d, u] of l) {
|
|
2927
|
+
if (u === void 0) continue;
|
|
2928
|
+
const w = s.get(u);
|
|
2929
|
+
if (!w)
|
|
2930
|
+
throw new Error(
|
|
2931
|
+
`Preset "${c.name}": unknown mode "${u}" for ${d}.`
|
|
2932
|
+
);
|
|
2933
|
+
if ((d === "codespace" || d === "preview") && te(w, this.elementTypes) === void 0)
|
|
2934
|
+
throw new Error(
|
|
2935
|
+
`Preset "${c.name}": mode "${u}" has no code element to render in the ${d}.`
|
|
2936
|
+
);
|
|
2937
|
+
}
|
|
2938
|
+
for (const d of Object.values(a.render ?? {}))
|
|
2939
|
+
if (d !== "block" && d !== "text")
|
|
2940
|
+
throw new Error(
|
|
2941
|
+
`Preset "${c.name}": toolbox render values must be "block" or "text".`
|
|
2942
|
+
);
|
|
2943
|
+
}
|
|
2944
|
+
}
|
|
2945
|
+
dispose() {
|
|
2946
|
+
var e, o, i, s, r, c, a, l;
|
|
2947
|
+
for (const [d, u] of se)
|
|
2948
|
+
u === this && se.delete(d);
|
|
2949
|
+
(e = this.workspaceResizeObserver) == null || e.disconnect(), this.workspaceResizeObserver = void 0, (o = document.fonts) == null || o.removeEventListener("loadingdone", this.onStylesLoaded), this.activePreset = void 0, (i = this.selectionSync) == null || i.disable(), this.selectionSync = void 0, (s = this.codeEditor) == null || s.dispose(), this.codeEditor = void 0, (r = this.codespaceDropTeardown) == null || r.call(this), this.codespaceDropTeardown = void 0, (c = this.codespace) == null || c.dispose(), this.codespace = void 0, (a = this.previewEditor) == null || a.dispose(), this.previewEditor = void 0, (l = this.toolboxCanvas) == null || l.dispose(), this.toolboxCanvas = void 0;
|
|
2950
|
+
for (const d of [...this.toolbars]) d.dispose();
|
|
2951
|
+
this.flyoutWorkspace && (this.flyoutWorkspace.removeChangeListener(this.onFlyoutChange), this.flyoutWorkspace = void 0), this.workspace && (this.workspace.removeChangeListener(this.onWorkspaceChange), this.workspace.dispose(), this.workspace = void 0), this.headlessWorkspaceHost && (this.headlessWorkspaceHost.remove(), this.headlessWorkspaceHost = void 0), this.mountConfig = void 0, this.baseTheme = void 0, this.baseFont = void 0, this.toolboxDefinition = void 0, this.blockCategoryIndex.clear(), this.appliedWorkspaceClasses = [], this.appliedToolboxFlyoutClasses = [], this.appliedToolboxShellClasses = [];
|
|
2952
|
+
}
|
|
2953
|
+
mountToolbox(e, o) {
|
|
2954
|
+
var s, r;
|
|
2955
|
+
if (!this.workspace || !this.mountConfig)
|
|
2956
|
+
throw new Error(
|
|
2957
|
+
"MorphicBlocks must be mounted before mountToolbox can be used."
|
|
2958
|
+
);
|
|
2959
|
+
(s = this.toolboxCanvas) == null || s.dispose(), this.styles.ensureToolbarStyles(), this.mountConfig.canvasToolbox || this.workspace.updateToolbox({ kind: "flyoutToolbox", contents: [] });
|
|
2960
|
+
const i = {
|
|
2961
|
+
...o,
|
|
2962
|
+
categories: (o == null ? void 0 : o.categories) ?? ((r = this.mountConfig.toolbox) == null ? void 0 : r.categories)
|
|
2963
|
+
};
|
|
2964
|
+
this.toolboxCanvas = new Lo({
|
|
2965
|
+
container: e,
|
|
2966
|
+
workspaceContainer: this.mountConfig.workspaceHost,
|
|
2967
|
+
workspace: this.workspace,
|
|
2968
|
+
definitions: this.definitions,
|
|
2969
|
+
blockColors: this.buildBlockColorMap(),
|
|
2970
|
+
behaviors: this.behaviors,
|
|
2971
|
+
elementTypes: this.elementTypes,
|
|
2972
|
+
mode: this.mountConfig.toolboxMode,
|
|
2973
|
+
render: this.mountConfig.toolboxRender,
|
|
2974
|
+
modes: this.mountConfig.modes,
|
|
2975
|
+
options: i,
|
|
2976
|
+
onPreviewWorkspace: (c) => ie.set(c, this)
|
|
2977
|
+
});
|
|
2978
|
+
}
|
|
2979
|
+
/**
|
|
2980
|
+
* The underlying Blockly workspace, for the rare case that needs Blockly
|
|
2981
|
+
* directly (e.g. creating blocks from code). Everyday use goes through the
|
|
2982
|
+
* engine. `undefined` before `mount`.
|
|
2983
|
+
*/
|
|
2984
|
+
getWorkspace() {
|
|
2985
|
+
return this.workspace;
|
|
2986
|
+
}
|
|
2987
|
+
/**
|
|
2988
|
+
* The declared mode names (`modes[].name` from the mount config). Empty until
|
|
2989
|
+
* `mount()` runs. Note: this is *modes*, not element names — an element like
|
|
2990
|
+
* `icon` or `python` is not a mode unless a mode is named after it.
|
|
2991
|
+
*/
|
|
2992
|
+
getAvailableModes() {
|
|
2993
|
+
var e;
|
|
2994
|
+
return (((e = this.mountConfig) == null ? void 0 : e.modes) ?? []).map((o) => o.name);
|
|
2995
|
+
}
|
|
2996
|
+
/** Current workspace mode name, or `undefined` if not mounted. */
|
|
2997
|
+
getWorkspaceMode() {
|
|
2998
|
+
var e;
|
|
2999
|
+
return (e = this.mountConfig) == null ? void 0 : e.workspaceMode;
|
|
3000
|
+
}
|
|
3001
|
+
/**
|
|
3002
|
+
* Effective codespace mode name: the independent `codespaceMode` when set,
|
|
3003
|
+
* else the workspace mode. `undefined` if not mounted.
|
|
3004
|
+
*/
|
|
3005
|
+
getCodespaceMode() {
|
|
3006
|
+
var e, o;
|
|
3007
|
+
return ((e = this.mountConfig) == null ? void 0 : e.codespaceMode) ?? ((o = this.mountConfig) == null ? void 0 : o.workspaceMode);
|
|
3008
|
+
}
|
|
3009
|
+
/** Preview mode name, or `undefined` when no preview mode is set. */
|
|
3010
|
+
getPreviewMode() {
|
|
3011
|
+
var e;
|
|
3012
|
+
return (e = this.mountConfig) == null ? void 0 : e.previewMode;
|
|
3013
|
+
}
|
|
3014
|
+
/** Mode definition by name, or `undefined`. */
|
|
3015
|
+
modeDef(e) {
|
|
3016
|
+
var o;
|
|
3017
|
+
if (e)
|
|
3018
|
+
return (((o = this.mountConfig) == null ? void 0 : o.modes) ?? []).find((i) => i.name === e);
|
|
3019
|
+
}
|
|
3020
|
+
/** Source element rendered by the codespace (label + highlighting source). */
|
|
3021
|
+
getActivePrimarySourceElement() {
|
|
3022
|
+
const e = this.modeDef(this.getCodespaceMode());
|
|
3023
|
+
if (e)
|
|
3024
|
+
return te(e, this.elementTypes);
|
|
3025
|
+
}
|
|
3026
|
+
/** Source element rendered by the preview (label + highlighting source). */
|
|
3027
|
+
getActivePreviewElement() {
|
|
3028
|
+
var o;
|
|
3029
|
+
const e = this.modeDef((o = this.mountConfig) == null ? void 0 : o.previewMode);
|
|
3030
|
+
if (e)
|
|
3031
|
+
return te(e, this.elementTypes);
|
|
3032
|
+
}
|
|
3033
|
+
/**
|
|
3034
|
+
* Mount a toolbar into the developer-provided container, bound to one of the
|
|
3035
|
+
* three editor surfaces. When `items` is omitted, the default set for the
|
|
3036
|
+
* pane is rendered. Returns a handle for `refresh()` / `dispose()`.
|
|
3037
|
+
*
|
|
3038
|
+
* The toolbar's stateful items (undo/redo enabled-state, language label)
|
|
3039
|
+
* refresh automatically when:
|
|
3040
|
+
* - any Blockly event fires on the workspace (covers undo/redo,
|
|
3041
|
+
* drops, edits — both workspace- and codespace-originated)
|
|
3042
|
+
* - `setModes` is called (covers language label changes)
|
|
3043
|
+
*/
|
|
3044
|
+
mountToolbar(e, o) {
|
|
3045
|
+
if (!this.workspace)
|
|
3046
|
+
throw new Error(
|
|
3047
|
+
"MorphicBlocks must be mounted before mountToolbar can be used."
|
|
3048
|
+
);
|
|
3049
|
+
this.styles.ensureToolbarStyles();
|
|
3050
|
+
const i = _o(e, o, {
|
|
3051
|
+
engine: this,
|
|
3052
|
+
pane: o.pane,
|
|
3053
|
+
getText: () => this.toolbarTextFor(o.pane),
|
|
3054
|
+
refresh: () => this.toolbarRefreshFor(o.pane)
|
|
3055
|
+
});
|
|
3056
|
+
this.toolbars.add(i), this.ensureToolbarBlocklyListener();
|
|
3057
|
+
const s = i.dispose;
|
|
3058
|
+
return i.dispose = () => {
|
|
3059
|
+
this.toolbars.delete(i), this.toolbars.size === 0 && this.teardownToolbarBlocklyListener(), s();
|
|
3060
|
+
}, i;
|
|
3061
|
+
}
|
|
3062
|
+
toolbarTextFor(e) {
|
|
3063
|
+
var o, i;
|
|
3064
|
+
if (e === "codespace")
|
|
3065
|
+
return ((o = this.codespace) == null ? void 0 : o.getValue()) ?? this.generateCodespaceText().code;
|
|
3066
|
+
if (e === "preview")
|
|
3067
|
+
return ((i = this.previewEditor) == null ? void 0 : i.getValue()) ?? "";
|
|
3068
|
+
if (this.codespace) return this.codespace.getValue();
|
|
3069
|
+
try {
|
|
3070
|
+
return this.generateJavaScript();
|
|
3071
|
+
} catch {
|
|
3072
|
+
return "";
|
|
3073
|
+
}
|
|
3074
|
+
}
|
|
3075
|
+
toolbarRefreshFor(e) {
|
|
3076
|
+
var o, i;
|
|
3077
|
+
e === "codespace" ? (o = this.codespace) == null || o.refresh() : e === "preview" && ((i = this.previewEditor) == null || i.refresh());
|
|
3078
|
+
}
|
|
3079
|
+
/**
|
|
3080
|
+
* Generate JavaScript from the current workspace and execute it.
|
|
3081
|
+
* Pass an optional `console` to capture `console.log` / `.warn` / `.error`
|
|
3082
|
+
* calls — useful when routing output to a panel rather than the browser
|
|
3083
|
+
* devtools. Dispatches a `morphic-run` CustomEvent with the result so
|
|
3084
|
+
* additional listeners can react.
|
|
3085
|
+
*/
|
|
3086
|
+
runJavaScript(e) {
|
|
3087
|
+
let o = "", i, s = null;
|
|
3088
|
+
const r = [], c = (e == null ? void 0 : e.console) ?? (e != null && e.logToConsole ? console : void 0), a = (u) => (...w) => {
|
|
3089
|
+
r.push({ level: u, text: w.map(String).join(" ") }), c && (c[u] ?? c.log).apply(c, w);
|
|
3090
|
+
}, l = { log: a("log"), warn: a("warn"), error: a("error") };
|
|
3091
|
+
try {
|
|
3092
|
+
o = this.generateJavaScript(), i = new Function("console", o)(l);
|
|
3093
|
+
} catch (u) {
|
|
3094
|
+
s = u instanceof Error ? u : new Error(String(u));
|
|
3095
|
+
}
|
|
3096
|
+
const d = { code: o, result: i, error: s, output: r };
|
|
3097
|
+
return this.dispatchEvent(new CustomEvent("morphic-run", { detail: d })), d;
|
|
3098
|
+
}
|
|
3099
|
+
/** Whether the framework's internal clipboard has a copyable to paste. */
|
|
3100
|
+
hasClipboardContents() {
|
|
3101
|
+
return !!this.lastCopyData;
|
|
3102
|
+
}
|
|
3103
|
+
/**
|
|
3104
|
+
* Copy the currently active block to the framework's internal clipboard and
|
|
3105
|
+
* mirror its generated code text to the system clipboard. Resolution order:
|
|
3106
|
+
* (1) Blockly's selected block, (2) for codespace/preview, the deepest block
|
|
3107
|
+
* enclosing the active line. Returns true when something was copied.
|
|
3108
|
+
*/
|
|
3109
|
+
copyActiveBlock(e) {
|
|
3110
|
+
var s;
|
|
3111
|
+
const o = this.resolveActiveBlock(e);
|
|
3112
|
+
if (!o) return !1;
|
|
3113
|
+
const i = o.toCopyData();
|
|
3114
|
+
if (!i) return !1;
|
|
3115
|
+
this.lastCopyData = i;
|
|
3116
|
+
try {
|
|
3117
|
+
const r = this.toolbarTextFor(e === "workspace" ? "codespace" : e);
|
|
3118
|
+
r && ((s = navigator.clipboard) == null || s.writeText(r));
|
|
3119
|
+
} catch {
|
|
3120
|
+
}
|
|
3121
|
+
for (const r of this.toolbars) r.refresh();
|
|
3122
|
+
return !0;
|
|
3123
|
+
}
|
|
3124
|
+
/**
|
|
3125
|
+
* Paste the last copied block at the active pane's natural location.
|
|
3126
|
+
* Workspace: Blockly's default (workspace centre, slightly offset).
|
|
3127
|
+
* Codespace/preview: the block lands at the workspace level too — the text
|
|
3128
|
+
* view re-renders to include it. (Cursor-position-aware paste into a
|
|
3129
|
+
* specific slot is deferred; the current behaviour matches Ctrl+V in
|
|
3130
|
+
* standard Blockly.) Returns true if something was pasted.
|
|
3131
|
+
*/
|
|
3132
|
+
pasteActiveBlock(e) {
|
|
3133
|
+
return !this.lastCopyData || !this.workspace ? !1 : B.clipboard.paste(this.lastCopyData, this.workspace) !== null;
|
|
3134
|
+
}
|
|
3135
|
+
resolveActiveBlock(e) {
|
|
3136
|
+
var i, s;
|
|
3137
|
+
const o = B.common.getSelected();
|
|
3138
|
+
if (o && "id" in o && ((i = this.workspace) != null && i.getBlockById(o.id)))
|
|
3139
|
+
return o;
|
|
3140
|
+
if (e === "codespace" || e === "preview") {
|
|
3141
|
+
const r = e === "codespace" ? this.codespace : this.previewEditor;
|
|
3142
|
+
if (r) {
|
|
3143
|
+
const c = r.metadata, a = r.getCursorLine();
|
|
3144
|
+
let l = null;
|
|
3145
|
+
for (const [d, u] of c)
|
|
3146
|
+
if (u.startLine <= a && u.endLine >= a) {
|
|
3147
|
+
const w = u.endLine - u.startLine;
|
|
3148
|
+
(!l || w < l.size) && (l = { id: d, size: w });
|
|
3149
|
+
}
|
|
3150
|
+
if (l) {
|
|
3151
|
+
const d = (s = this.workspace) == null ? void 0 : s.getBlockById(l.id);
|
|
3152
|
+
if (d) return d;
|
|
3153
|
+
}
|
|
3154
|
+
}
|
|
3155
|
+
}
|
|
3156
|
+
return null;
|
|
3157
|
+
}
|
|
3158
|
+
/** Serialize the workspace to a plain object (Blockly's native format). */
|
|
3159
|
+
serializeWorkspace() {
|
|
3160
|
+
return this.workspace ? B.serialization.workspaces.save(this.workspace) : null;
|
|
3161
|
+
}
|
|
3162
|
+
/** Restore the workspace from a previously serialized state. */
|
|
3163
|
+
loadWorkspace(e) {
|
|
3164
|
+
this.workspace && (this.workspace.clear(), B.serialization.workspaces.load(e, this.workspace));
|
|
3165
|
+
}
|
|
3166
|
+
/**
|
|
3167
|
+
* Adjust the pane's zoom level. Direction is `"in" | "out" | "fit"`. For
|
|
3168
|
+
* the workspace pane this maps to `workspace.zoomCenter(±1)` /
|
|
3169
|
+
* `zoomToFit()`. For codespace/preview it scales the editor's font-size.
|
|
3170
|
+
*/
|
|
3171
|
+
zoomPane(e, o) {
|
|
3172
|
+
if (e === "workspace") {
|
|
3173
|
+
if (!this.workspace) return;
|
|
3174
|
+
o === "in" ? this.workspace.zoomCenter(1) : o === "out" ? this.workspace.zoomCenter(-1) : this.workspace.zoomToFit();
|
|
3175
|
+
return;
|
|
3176
|
+
}
|
|
3177
|
+
const i = e === "codespace" ? this.codespace : this.previewEditor;
|
|
3178
|
+
i == null || i.adjustZoom(o);
|
|
3179
|
+
}
|
|
3180
|
+
ensureToolbarBlocklyListener() {
|
|
3181
|
+
if (this.toolbarsBlocklyListener || !this.workspace) return;
|
|
3182
|
+
const e = (o) => {
|
|
3183
|
+
for (const i of this.toolbars) i.refresh();
|
|
3184
|
+
};
|
|
3185
|
+
this.toolbarsBlocklyListener = e, this.workspace.addChangeListener(e);
|
|
3186
|
+
}
|
|
3187
|
+
teardownToolbarBlocklyListener() {
|
|
3188
|
+
!this.toolbarsBlocklyListener || !this.workspace || (this.workspace.removeChangeListener(this.toolbarsBlocklyListener), this.toolbarsBlocklyListener = void 0);
|
|
3189
|
+
}
|
|
3190
|
+
setModes(e) {
|
|
3191
|
+
var o, i, s, r, c, a;
|
|
3192
|
+
if (!this.mountConfig || !this.workspace)
|
|
3193
|
+
throw new Error(
|
|
3194
|
+
"MorphicBlocks must be mounted before setModes can be used."
|
|
3195
|
+
);
|
|
3196
|
+
for (const [l, d] of [
|
|
3197
|
+
["workspaceMode", e.workspaceMode],
|
|
3198
|
+
["toolboxMode", e.toolboxMode],
|
|
3199
|
+
["codespaceMode", e.codespaceMode],
|
|
3200
|
+
["previewMode", e.previewMode]
|
|
3201
|
+
])
|
|
3202
|
+
if (typeof d == "string" && !this.modeDef(d))
|
|
3203
|
+
throw new Error(`setModes: unknown mode "${d}" for ${l}.`);
|
|
3204
|
+
if (typeof e.codespaceMode == "string" && !this.mountConfig.codespaceContainer)
|
|
3205
|
+
throw new Error(
|
|
3206
|
+
"setModes: codespaceMode requires a codespaceContainer at mount."
|
|
3207
|
+
);
|
|
3208
|
+
e.workspaceMode && (this.mountConfig.workspaceMode = e.workspaceMode), e.codespaceMode !== void 0 && (this.mountConfig.codespaceMode = e.codespaceMode ?? void 0), e.previewMode !== void 0 && (this.mountConfig.previewMode = e.previewMode ?? void 0), e.toolboxRender !== void 0 && (this.mountConfig.toolboxRender = e.toolboxRender ?? void 0), (e.toolboxMode || e.toolboxRender !== void 0) && (e.toolboxMode && (this.mountConfig.toolboxMode = e.toolboxMode), (o = this.toolboxCanvas) == null || o.rerender(
|
|
3209
|
+
this.mountConfig.toolboxMode,
|
|
3210
|
+
this.mountConfig.toolboxRender
|
|
3211
|
+
)), this.applyWorkspaceContainerClass(), this.syncWorkspaceFont(), this.refreshToolbox(), this.bindFlyoutWorkspace(), this.renderWorkspaceBlocks(), this.renderFlyoutBlocks(), (i = this.codespace) == null || i.setHighlightRules(this.resolveHighlightRules("codespace")), (s = this.previewEditor) == null || s.setHighlightRules(this.resolveHighlightRules("preview")), (r = this.codespace) == null || r.refresh(), (c = this.previewEditor) == null || c.refresh();
|
|
3212
|
+
for (const l of this.toolbars) l.refresh();
|
|
3213
|
+
(a = this.selectionSync) == null || a.clearAll();
|
|
3214
|
+
}
|
|
3215
|
+
/**
|
|
3216
|
+
* Resolve the highlight rules for the codespace or preview editor by
|
|
3217
|
+
* looking up that pane's source element name in the
|
|
3218
|
+
* `mountConfig.highlighting` registry. Returns `undefined` when no
|
|
3219
|
+
* matching entry is configured.
|
|
3220
|
+
*/
|
|
3221
|
+
resolveHighlightRules(e) {
|
|
3222
|
+
var i;
|
|
3223
|
+
if (!this.mountConfig) return;
|
|
3224
|
+
const o = e === "codespace" ? this.getActivePrimarySourceElement() : this.getActivePreviewElement();
|
|
3225
|
+
if (o)
|
|
3226
|
+
return (i = this.mountConfig.highlighting) == null ? void 0 : i[o];
|
|
3227
|
+
}
|
|
3228
|
+
generateJavaScript() {
|
|
3229
|
+
if (!this.workspace || !this.mountConfig)
|
|
3230
|
+
throw new Error(
|
|
3231
|
+
"MorphicBlocks must be mounted before generateJavaScript can be used."
|
|
3232
|
+
);
|
|
3233
|
+
return jt(
|
|
3234
|
+
this.workspace,
|
|
3235
|
+
this.definitions,
|
|
3236
|
+
this.behaviors,
|
|
3237
|
+
this.mountConfig.javascript
|
|
3238
|
+
);
|
|
3239
|
+
}
|
|
3240
|
+
generateJavaScriptWithMetadata() {
|
|
3241
|
+
if (!this.workspace || !this.mountConfig)
|
|
3242
|
+
throw new Error(
|
|
3243
|
+
"MorphicBlocks must be mounted before generateJavaScriptWithMetadata can be used."
|
|
3244
|
+
);
|
|
3245
|
+
return Ut(
|
|
3246
|
+
this.workspace,
|
|
3247
|
+
this.definitions,
|
|
3248
|
+
this.behaviors,
|
|
3249
|
+
this.mountConfig.javascript
|
|
3250
|
+
);
|
|
3251
|
+
}
|
|
3252
|
+
async mountCodeEditor(e, o) {
|
|
3253
|
+
var s;
|
|
3254
|
+
if (!this.workspace || !this.mountConfig)
|
|
3255
|
+
throw new Error(
|
|
3256
|
+
"MorphicBlocks must be mounted before mountCodeEditor can be used."
|
|
3257
|
+
);
|
|
3258
|
+
(s = this.codeEditor) == null || s.dispose(), this.codeEditor = new ge(
|
|
3259
|
+
e,
|
|
3260
|
+
this.workspace,
|
|
3261
|
+
() => this.generateJavaScriptWithMetadata(),
|
|
3262
|
+
o
|
|
3263
|
+
);
|
|
3264
|
+
const i = this.codeEditor;
|
|
3265
|
+
await i.mount(), this.codeEditor === i && this.refreshSelectionSync();
|
|
3266
|
+
}
|
|
3267
|
+
/**
|
|
3268
|
+
* Mounts the primary text editor (codespace) into the `codespaceContainer`
|
|
3269
|
+
* declared at `mount()`. Renders the workspace as text using the codespace
|
|
3270
|
+
* mode's source element. Read-only for now; Task 4 adds drops.
|
|
3271
|
+
*/
|
|
3272
|
+
async mountCodespace(e) {
|
|
3273
|
+
var s, r;
|
|
3274
|
+
if (!this.workspace || !this.mountConfig)
|
|
3275
|
+
throw new Error(
|
|
3276
|
+
"MorphicBlocks must be mounted before mountCodespace can be used."
|
|
3277
|
+
);
|
|
3278
|
+
if (!this.mountConfig.codespaceContainer)
|
|
3279
|
+
throw new Error(
|
|
3280
|
+
"mountCodespace requires a codespaceContainer to be passed to mount()."
|
|
3281
|
+
);
|
|
3282
|
+
(s = this.codespace) == null || s.dispose(), (r = this.codespaceDropTeardown) == null || r.call(this);
|
|
3283
|
+
const o = {
|
|
3284
|
+
...e,
|
|
3285
|
+
onDelete: (e == null ? void 0 : e.onDelete) ?? ((c) => this.deleteBlockAtCodespaceLine(c)),
|
|
3286
|
+
canDragBlock: (e == null ? void 0 : e.canDragBlock) ?? ((c) => {
|
|
3287
|
+
var l;
|
|
3288
|
+
const a = (l = this.workspace) == null ? void 0 : l.getBlockById(c);
|
|
3289
|
+
return !!(a != null && a.previousConnection || a != null && a.outputConnection);
|
|
3290
|
+
}),
|
|
3291
|
+
highlightRules: (e == null ? void 0 : e.highlightRules) ?? this.resolveHighlightRules("codespace"),
|
|
3292
|
+
onPlaceholderApply: (e == null ? void 0 : e.onPlaceholderApply) ?? ((c, a) => this.applyPlaceholderEdit(c, a))
|
|
3293
|
+
};
|
|
3294
|
+
this.codespace = new ge(
|
|
3295
|
+
this.mountConfig.codespaceContainer,
|
|
3296
|
+
this.workspace,
|
|
3297
|
+
() => this.generateCodespaceText(),
|
|
3298
|
+
o
|
|
3299
|
+
);
|
|
3300
|
+
const i = this.codespace;
|
|
3301
|
+
await i.mount(), this.codespace === i && (this.codespaceDropTeardown = this.attachCodespaceDropTarget(
|
|
3302
|
+
this.mountConfig.codespaceContainer,
|
|
3303
|
+
this.workspace
|
|
3304
|
+
), this.refreshSelectionSync());
|
|
3305
|
+
}
|
|
3306
|
+
attachCodespaceDropTarget(e, o) {
|
|
3307
|
+
const i = (m) => {
|
|
3308
|
+
var y;
|
|
3309
|
+
const p = (y = m.dataTransfer) == null ? void 0 : y.types;
|
|
3310
|
+
return p ? p.includes(K) || p.includes(Ce) : !1;
|
|
3311
|
+
}, s = (m) => {
|
|
3312
|
+
var y, S, h, f, x, T;
|
|
3313
|
+
if (!i(m)) return;
|
|
3314
|
+
m.preventDefault();
|
|
3315
|
+
const p = this.computeCodespaceDrop(m.clientX, m.clientY);
|
|
3316
|
+
if (!p) {
|
|
3317
|
+
(y = this.codespace) == null || y.hideDropIndicator(), (S = this.codespace) == null || S.hideValueSlotHighlight();
|
|
3318
|
+
return;
|
|
3319
|
+
}
|
|
3320
|
+
p.indicator.kind === "line" ? ((h = this.codespace) == null || h.showDropIndicator(p.indicator.line, p.indicator.position), (f = this.codespace) == null || f.hideValueSlotHighlight()) : ((x = this.codespace) == null || x.showValueSlotHighlight(p.indicator.from, p.indicator.to), (T = this.codespace) == null || T.hideDropIndicator());
|
|
3321
|
+
}, r = (m) => {
|
|
3322
|
+
var y, S;
|
|
3323
|
+
const p = m.relatedTarget;
|
|
3324
|
+
p && e.contains(p) || ((y = this.codespace) == null || y.hideDropIndicator(), (S = this.codespace) == null || S.hideValueSlotHighlight());
|
|
3325
|
+
}, c = (m) => {
|
|
3326
|
+
var x, T, C, I;
|
|
3327
|
+
if (!i(m)) return;
|
|
3328
|
+
m.preventDefault(), (x = this.codespace) == null || x.hideDropIndicator(), (T = this.codespace) == null || T.hideValueSlotHighlight();
|
|
3329
|
+
const p = this.computeCodespaceDrop(m.clientX, m.clientY);
|
|
3330
|
+
if (!p) return;
|
|
3331
|
+
const y = (C = m.dataTransfer) == null ? void 0 : C.getData(K), S = (I = m.dataTransfer) == null ? void 0 : I.getData(Ce);
|
|
3332
|
+
let h = null;
|
|
3333
|
+
if (y)
|
|
3334
|
+
h = o.newBlock(z(y, this.definitions)), h.initSvg(), h.render();
|
|
3335
|
+
else if (S) {
|
|
3336
|
+
if (h = o.getBlockById(S), !h) return;
|
|
3337
|
+
} else
|
|
3338
|
+
return;
|
|
3339
|
+
let f = !1;
|
|
3340
|
+
B.Events.setGroup(!0);
|
|
3341
|
+
try {
|
|
3342
|
+
if (S && h.getParent() && h.unplug(!0), p.target.kind === "top")
|
|
3343
|
+
this.placeAtTopIndex(o, h, p.target.index), f = !0;
|
|
3344
|
+
else if (p.target.kind === "statement") {
|
|
3345
|
+
if (h.previousConnection) {
|
|
3346
|
+
const D = o.getBlockById(p.target.targetBlockId);
|
|
3347
|
+
D && D !== h && (this.connectStatement(h, D, p.target.position), f = !0);
|
|
3348
|
+
}
|
|
3349
|
+
} else if (p.target.kind === "into-slot") {
|
|
3350
|
+
if (h.previousConnection) {
|
|
3351
|
+
const D = o.getBlockById(p.target.parentBlockId);
|
|
3352
|
+
D && D !== h && (this.connectIntoSlot(h, D, p.target.inputName), f = !0);
|
|
3353
|
+
}
|
|
3354
|
+
} else if (p.target.kind === "value-slot") {
|
|
3355
|
+
if (h.outputConnection) {
|
|
3356
|
+
const D = o.getBlockById(p.target.parentBlockId);
|
|
3357
|
+
D && D !== h && !this.isDescendantOf(D, h) && (f = this.connectIntoValueSlot(h, D, p.target.inputName));
|
|
3358
|
+
}
|
|
3359
|
+
!f && y && h.dispose(!1);
|
|
3360
|
+
}
|
|
3361
|
+
} finally {
|
|
3362
|
+
B.Events.setGroup(!1);
|
|
3363
|
+
}
|
|
3364
|
+
!f && h.workspace && !h.getParent() && this.placeOrphanBelowTops(h), B.svgResize(o);
|
|
3365
|
+
};
|
|
3366
|
+
let a = null;
|
|
3367
|
+
const l = 3, d = (m) => m.button === 2 || m.button === 0 && m.ctrlKey, u = (m) => {
|
|
3368
|
+
var y, S, h, f, x, T;
|
|
3369
|
+
if (!a) return;
|
|
3370
|
+
if (!a.dragging) {
|
|
3371
|
+
const C = Math.abs(m.clientX - a.startX), I = Math.abs(m.clientY - a.startY);
|
|
3372
|
+
if (C + I < l) return;
|
|
3373
|
+
a.dragging = !0;
|
|
3374
|
+
}
|
|
3375
|
+
const p = this.computeCodespaceDrop(m.clientX, m.clientY);
|
|
3376
|
+
if (!p) {
|
|
3377
|
+
(y = this.codespace) == null || y.hideDropIndicator(), (S = this.codespace) == null || S.hideValueSlotHighlight();
|
|
3378
|
+
return;
|
|
3379
|
+
}
|
|
3380
|
+
p.indicator.kind === "line" ? ((h = this.codespace) == null || h.showDropIndicator(p.indicator.line, p.indicator.position), (f = this.codespace) == null || f.hideValueSlotHighlight()) : ((x = this.codespace) == null || x.showValueSlotHighlight(p.indicator.from, p.indicator.to), (T = this.codespace) == null || T.hideDropIndicator());
|
|
3381
|
+
}, w = (m) => {
|
|
3382
|
+
var h, f;
|
|
3383
|
+
window.removeEventListener("mousemove", u), window.removeEventListener("mouseup", w), (h = this.codespace) == null || h.hideDropIndicator(), (f = this.codespace) == null || f.hideValueSlotHighlight();
|
|
3384
|
+
const p = a;
|
|
3385
|
+
if (a = null, De(void 0), !(p != null && p.dragging)) return;
|
|
3386
|
+
const y = this.computeCodespaceDrop(m.clientX, m.clientY);
|
|
3387
|
+
if (!y) return;
|
|
3388
|
+
const S = o.getBlockById(p.sourceId);
|
|
3389
|
+
S && this.applyRightDragMove(o, S, y);
|
|
3390
|
+
}, v = (m) => {
|
|
3391
|
+
var S;
|
|
3392
|
+
if (!d(m)) return;
|
|
3393
|
+
const p = (S = this.codespace) == null ? void 0 : S.charAtCoords(m.clientX, m.clientY);
|
|
3394
|
+
if (p == null) return;
|
|
3395
|
+
const y = this.findInnermostBlockAtChar(p);
|
|
3396
|
+
y && (m.preventDefault(), m.stopPropagation(), a = {
|
|
3397
|
+
sourceId: y.id,
|
|
3398
|
+
startX: m.clientX,
|
|
3399
|
+
startY: m.clientY,
|
|
3400
|
+
dragging: !1
|
|
3401
|
+
}, De(y.id), window.addEventListener("mousemove", u), window.addEventListener("mouseup", w));
|
|
3402
|
+
}, b = (m) => {
|
|
3403
|
+
m.preventDefault();
|
|
3404
|
+
}, E = (m) => {
|
|
3405
|
+
var f, x, T, C, I, D, _, O;
|
|
3406
|
+
if (a) return;
|
|
3407
|
+
const p = (f = this.codespace) == null ? void 0 : f.charAtCoords(m.clientX, m.clientY);
|
|
3408
|
+
if (p == null) {
|
|
3409
|
+
(x = this.codespace) == null || x.setHoverHighlight(null), (T = this.codespace) == null || T.setEditableHoverHighlight(null);
|
|
3410
|
+
return;
|
|
3411
|
+
}
|
|
3412
|
+
const y = this.findInnermostEditablePlaceholderAtChar(p);
|
|
3413
|
+
(C = this.codespace) == null || C.setEditableHoverHighlight(
|
|
3414
|
+
y ? { from: y.start, to: y.end } : null
|
|
3415
|
+
);
|
|
3416
|
+
const S = this.findHoverBlockAtChar(p);
|
|
3417
|
+
if (!S) {
|
|
3418
|
+
(I = this.codespace) == null || I.setHoverHighlight(null);
|
|
3419
|
+
return;
|
|
3420
|
+
}
|
|
3421
|
+
const h = (D = this.codespace) == null ? void 0 : D.metadata.get(S.id);
|
|
3422
|
+
if (!h || h.startChar === void 0 || h.endChar === void 0) {
|
|
3423
|
+
(_ = this.codespace) == null || _.setHoverHighlight(null);
|
|
3424
|
+
return;
|
|
3425
|
+
}
|
|
3426
|
+
(O = this.codespace) == null || O.setHoverHighlight({ from: h.startChar, to: h.endChar });
|
|
3427
|
+
}, k = () => {
|
|
3428
|
+
var m, p;
|
|
3429
|
+
(m = this.codespace) == null || m.setHoverHighlight(null), (p = this.codespace) == null || p.setEditableHoverHighlight(null);
|
|
3430
|
+
};
|
|
3431
|
+
return e.addEventListener("dragover", s), e.addEventListener("dragleave", r), e.addEventListener("drop", c), e.addEventListener("mousedown", v, !0), e.addEventListener("contextmenu", b), e.addEventListener("mousemove", E), e.addEventListener("mouseleave", k), () => {
|
|
3432
|
+
e.removeEventListener("dragover", s), e.removeEventListener("dragleave", r), e.removeEventListener("drop", c), e.removeEventListener("mousedown", v, !0), e.removeEventListener("contextmenu", b), e.removeEventListener("mousemove", E), e.removeEventListener("mouseleave", k), window.removeEventListener("mousemove", u), window.removeEventListener("mouseup", w);
|
|
3433
|
+
};
|
|
3434
|
+
}
|
|
3435
|
+
/** Innermost (smallest-range) block in metadata whose char range contains `charOffset`. */
|
|
3436
|
+
findInnermostBlockAtChar(e) {
|
|
3437
|
+
if (!this.workspace || !this.codespace) return null;
|
|
3438
|
+
let o = null;
|
|
3439
|
+
for (const [i, s] of this.codespace.metadata) {
|
|
3440
|
+
if (s.startChar === void 0 || s.endChar === void 0 || e < s.startChar || e >= s.endChar) continue;
|
|
3441
|
+
const r = s.endChar - s.startChar;
|
|
3442
|
+
(o === null || r < o.size) && (o = { id: i, size: r });
|
|
3443
|
+
}
|
|
3444
|
+
return o ? this.workspace.getBlockById(o.id) : null;
|
|
3445
|
+
}
|
|
3446
|
+
/**
|
|
3447
|
+
* Hover-background target: the smallest *non-atomic* block whose range
|
|
3448
|
+
* contains the cursor. Atomic single-field blocks (numbers, strings,
|
|
3449
|
+
* booleans) are skipped so hovering on `3` highlights the surrounding
|
|
3450
|
+
* `1 + 2` expression instead of just the digit. Falls back to the smallest
|
|
3451
|
+
* block found when only atomic blocks contain the cursor (e.g. a top-level
|
|
3452
|
+
* orphan number on its own line).
|
|
3453
|
+
*/
|
|
3454
|
+
findHoverBlockAtChar(e) {
|
|
3455
|
+
if (!this.workspace || !this.codespace) return null;
|
|
3456
|
+
let o = null, i = null;
|
|
3457
|
+
for (const [r, c] of this.codespace.metadata) {
|
|
3458
|
+
if (c.startChar === void 0 || c.endChar === void 0 || e < c.startChar || e >= c.endChar) continue;
|
|
3459
|
+
const a = c.endChar - c.startChar;
|
|
3460
|
+
(i === null || a < i.size) && (i = { id: r, size: a }), !c.atomic && (o === null || a < o.size) && (o = { id: r, size: a });
|
|
3461
|
+
}
|
|
3462
|
+
const s = o ?? i;
|
|
3463
|
+
return s ? this.workspace.getBlockById(s.id) : null;
|
|
3464
|
+
}
|
|
3465
|
+
/** Innermost placeholder with an editable `edit` target whose range contains `charOffset`. */
|
|
3466
|
+
findInnermostEditablePlaceholderAtChar(e) {
|
|
3467
|
+
if (!this.codespace) return null;
|
|
3468
|
+
let o = null;
|
|
3469
|
+
for (const i of this.codespace.getPlaceholders()) {
|
|
3470
|
+
if (!i.edit || e < i.start || e >= i.end) continue;
|
|
3471
|
+
const s = i.end - i.start;
|
|
3472
|
+
(o === null || s < o.size) && (o = { ph: i, size: s });
|
|
3473
|
+
}
|
|
3474
|
+
return o ? { start: o.ph.start, end: o.ph.end } : null;
|
|
3475
|
+
}
|
|
3476
|
+
/**
|
|
3477
|
+
* Reusable drop-result applicator for the right-click drag path: same set
|
|
3478
|
+
* of target kinds as the HTML5 drop handler, but the source is always an
|
|
3479
|
+
* existing workspace block (no toolbox-spawn case).
|
|
3480
|
+
*/
|
|
3481
|
+
applyRightDragMove(e, o, i) {
|
|
3482
|
+
if (!i) return;
|
|
3483
|
+
let s = !1;
|
|
3484
|
+
B.Events.setGroup(!0);
|
|
3485
|
+
try {
|
|
3486
|
+
if (o.getParent() && o.unplug(!0), i.target.kind === "top")
|
|
3487
|
+
this.placeAtTopIndex(e, o, i.target.index), s = !0;
|
|
3488
|
+
else if (i.target.kind === "statement") {
|
|
3489
|
+
if (o.previousConnection) {
|
|
3490
|
+
const r = e.getBlockById(i.target.targetBlockId);
|
|
3491
|
+
r && r !== o && (this.connectStatement(o, r, i.target.position), s = !0);
|
|
3492
|
+
}
|
|
3493
|
+
} else if (i.target.kind === "into-slot") {
|
|
3494
|
+
if (o.previousConnection) {
|
|
3495
|
+
const r = e.getBlockById(i.target.parentBlockId);
|
|
3496
|
+
r && r !== o && (this.connectIntoSlot(o, r, i.target.inputName), s = !0);
|
|
3497
|
+
}
|
|
3498
|
+
} else if (i.target.kind === "value-slot" && o.outputConnection) {
|
|
3499
|
+
const r = e.getBlockById(i.target.parentBlockId);
|
|
3500
|
+
r && r !== o && !this.isDescendantOf(r, o) && (s = this.connectIntoValueSlot(o, r, i.target.inputName));
|
|
3501
|
+
}
|
|
3502
|
+
} finally {
|
|
3503
|
+
B.Events.setGroup(!1);
|
|
3504
|
+
}
|
|
3505
|
+
!s && o.workspace && !o.getParent() && this.placeOrphanBelowTops(o), B.svgResize(e);
|
|
3506
|
+
}
|
|
3507
|
+
/**
|
|
3508
|
+
* Resolve a codespace drop into both a visual indicator and a typed target.
|
|
3509
|
+
*
|
|
3510
|
+
* `target.kind === "top"` means the drop becomes a top-level block at
|
|
3511
|
+
* `target.index`. `target.kind === "statement"` means it should be wired into
|
|
3512
|
+
* an existing block's statement chain — `targetBlockId` is the block under
|
|
3513
|
+
* the cursor and `position` says whether to land before or after it.
|
|
3514
|
+
*
|
|
3515
|
+
* Above/below is decided by upper/lower-half of the cursor's line; cursor
|
|
3516
|
+
* past the last rendered line always resolves to "after all" at top level.
|
|
3517
|
+
*/
|
|
3518
|
+
computeCodespaceDrop(e, o) {
|
|
3519
|
+
var u, w;
|
|
3520
|
+
if (!this.workspace || !this.codespace) return null;
|
|
3521
|
+
const i = this.workspace.getTopBlocks(!0), s = this.codespace.metadata, r = this.codespace.getLineCount();
|
|
3522
|
+
if (i.length === 0)
|
|
3523
|
+
return {
|
|
3524
|
+
indicator: { kind: "line", line: 1, position: "above" },
|
|
3525
|
+
target: { kind: "top", index: 0 }
|
|
3526
|
+
};
|
|
3527
|
+
if (this.codespace.isBelowLastLine(o)) {
|
|
3528
|
+
const v = s.get(i[i.length - 1].id);
|
|
3529
|
+
return {
|
|
3530
|
+
indicator: { kind: "line", line: (v == null ? void 0 : v.endLine) ?? r, position: "below" },
|
|
3531
|
+
target: { kind: "top", index: i.length }
|
|
3532
|
+
};
|
|
3533
|
+
}
|
|
3534
|
+
const c = this.codespace.getLineAtCoords(e, o);
|
|
3535
|
+
if (c === null) {
|
|
3536
|
+
const v = s.get(i[0].id);
|
|
3537
|
+
return {
|
|
3538
|
+
indicator: { kind: "line", line: (v == null ? void 0 : v.startLine) ?? 1, position: "above" },
|
|
3539
|
+
target: { kind: "top", index: 0 }
|
|
3540
|
+
};
|
|
3541
|
+
}
|
|
3542
|
+
const a = this.codespace.charAtCoords(e, o);
|
|
3543
|
+
if (a !== null) {
|
|
3544
|
+
const v = this.findValueSlotDropAtChar(a);
|
|
3545
|
+
if (v)
|
|
3546
|
+
return {
|
|
3547
|
+
indicator: {
|
|
3548
|
+
kind: "value-slot",
|
|
3549
|
+
from: v.highlight.from,
|
|
3550
|
+
to: v.highlight.to
|
|
3551
|
+
},
|
|
3552
|
+
target: {
|
|
3553
|
+
kind: "value-slot",
|
|
3554
|
+
parentBlockId: v.parentBlockId,
|
|
3555
|
+
inputName: v.inputName
|
|
3556
|
+
}
|
|
3557
|
+
};
|
|
3558
|
+
}
|
|
3559
|
+
const l = this.findInnermostStatementSlotAtLine(c, s);
|
|
3560
|
+
if (l) {
|
|
3561
|
+
const v = this.workspace.getBlockById(l.blockId);
|
|
3562
|
+
if (v) {
|
|
3563
|
+
const b = Ie(), E = [];
|
|
3564
|
+
let k = (w = (u = v.getInput(l.inputName)) == null ? void 0 : u.connection) == null ? void 0 : w.targetBlock();
|
|
3565
|
+
for (; k; )
|
|
3566
|
+
k.id !== b && E.push(k), k = k.getNextBlock();
|
|
3567
|
+
for (const m of E) {
|
|
3568
|
+
const p = s.get(m.id);
|
|
3569
|
+
if (!p || c < p.startLine || c > p.endLine) continue;
|
|
3570
|
+
const y = c === p.endLine, S = this.codespace.isInLowerHalfOfLine(c, o);
|
|
3571
|
+
return y && S ? {
|
|
3572
|
+
indicator: { kind: "line", line: p.endLine, position: "below" },
|
|
3573
|
+
target: { kind: "statement", targetBlockId: m.id, position: "after" }
|
|
3574
|
+
} : {
|
|
3575
|
+
indicator: { kind: "line", line: p.startLine, position: "above" },
|
|
3576
|
+
target: { kind: "statement", targetBlockId: m.id, position: "before" }
|
|
3577
|
+
};
|
|
3578
|
+
}
|
|
3579
|
+
if (E.length > 0) {
|
|
3580
|
+
const m = E[E.length - 1], p = s.get(m.id);
|
|
3581
|
+
return {
|
|
3582
|
+
indicator: { kind: "line", line: (p == null ? void 0 : p.endLine) ?? l.range.endLine, position: "below" },
|
|
3583
|
+
target: {
|
|
3584
|
+
kind: "into-slot",
|
|
3585
|
+
parentBlockId: l.blockId,
|
|
3586
|
+
inputName: l.inputName
|
|
3587
|
+
}
|
|
3588
|
+
};
|
|
3589
|
+
}
|
|
3590
|
+
return {
|
|
3591
|
+
indicator: { kind: "line", line: l.range.startLine, position: "above" },
|
|
3592
|
+
target: {
|
|
3593
|
+
kind: "into-slot",
|
|
3594
|
+
parentBlockId: l.blockId,
|
|
3595
|
+
inputName: l.inputName
|
|
3596
|
+
}
|
|
3597
|
+
};
|
|
3598
|
+
}
|
|
3599
|
+
}
|
|
3600
|
+
for (let v = 0; v < i.length; v++) {
|
|
3601
|
+
const b = s.get(i[v].id);
|
|
3602
|
+
if (!b || c < b.startLine || c > b.endLine) continue;
|
|
3603
|
+
const E = c === b.endLine, k = this.codespace.isInLowerHalfOfLine(c, o);
|
|
3604
|
+
return E && k ? {
|
|
3605
|
+
indicator: { kind: "line", line: b.endLine, position: "below" },
|
|
3606
|
+
target: { kind: "top", index: v + 1 }
|
|
3607
|
+
} : {
|
|
3608
|
+
indicator: { kind: "line", line: b.startLine, position: "above" },
|
|
3609
|
+
target: { kind: "top", index: v }
|
|
3610
|
+
};
|
|
3611
|
+
}
|
|
3612
|
+
const d = s.get(i[i.length - 1].id);
|
|
3613
|
+
return {
|
|
3614
|
+
indicator: { kind: "line", line: (d == null ? void 0 : d.endLine) ?? r, position: "below" },
|
|
3615
|
+
target: { kind: "top", index: i.length }
|
|
3616
|
+
};
|
|
3617
|
+
}
|
|
3618
|
+
/**
|
|
3619
|
+
* Deepest statement-input slot whose body range contains `line`. Used for
|
|
3620
|
+
* empty/whitespace bodies where no child block anchors the line.
|
|
3621
|
+
*/
|
|
3622
|
+
findInnermostStatementSlotAtLine(e, o) {
|
|
3623
|
+
let i = null;
|
|
3624
|
+
for (const [s, r] of o)
|
|
3625
|
+
if (r.statementSlots)
|
|
3626
|
+
for (const [c, a] of Object.entries(r.statementSlots)) {
|
|
3627
|
+
if (e < a.startLine || e > a.endLine) continue;
|
|
3628
|
+
const l = a.endLine - a.startLine, d = this.computeBlockDepth(s);
|
|
3629
|
+
(i === null || l < i.size || l === i.size && d > i.depth) && (i = { blockId: s, inputName: c, range: a, size: l, depth: d });
|
|
3630
|
+
}
|
|
3631
|
+
return i ? { blockId: i.blockId, inputName: i.inputName, range: i.range } : null;
|
|
3632
|
+
}
|
|
3633
|
+
/**
|
|
3634
|
+
* Append `source` to the chain in `parent`'s statement input named `inputName`.
|
|
3635
|
+
* Connects to the slot directly when the slot is empty; otherwise walks to
|
|
3636
|
+
* the chain's tail and connects there.
|
|
3637
|
+
*/
|
|
3638
|
+
connectIntoSlot(e, o, i) {
|
|
3639
|
+
if (!e.previousConnection) return;
|
|
3640
|
+
const s = o.getInput(i), r = s == null ? void 0 : s.connection;
|
|
3641
|
+
if (!r) return;
|
|
3642
|
+
const c = r.targetBlock();
|
|
3643
|
+
if (!c) {
|
|
3644
|
+
r.connect(e.previousConnection);
|
|
3645
|
+
return;
|
|
3646
|
+
}
|
|
3647
|
+
let a = c, l = a.getNextBlock();
|
|
3648
|
+
for (; l; )
|
|
3649
|
+
a = l, l = a.getNextBlock();
|
|
3650
|
+
a.nextConnection && a.nextConnection.connect(e.previousConnection);
|
|
3651
|
+
}
|
|
3652
|
+
/**
|
|
3653
|
+
* Resolve a char-offset in the codespace to the value slot the drop should
|
|
3654
|
+
* land in. Two cases, both yielding the parent block + input name:
|
|
3655
|
+
*
|
|
3656
|
+
* 1. Empty slot — the offset is inside a placeholder range with a
|
|
3657
|
+
* shadow/placeholder block (`edit.blockId`). The shadow's parent
|
|
3658
|
+
* connection identifies the slot.
|
|
3659
|
+
* 2. Occupied slot — the offset is inside an inner value block's char
|
|
3660
|
+
* range (innermost wins by smallest range). The block's own parent
|
|
3661
|
+
* connection identifies the slot; on drop, the child is replaced.
|
|
3662
|
+
*
|
|
3663
|
+
* Excludes the active grip-drag source so dragging a value block onto its
|
|
3664
|
+
* own rendered range is a no-op rather than a self-replace.
|
|
3665
|
+
*/
|
|
3666
|
+
findValueSlotDropAtChar(e) {
|
|
3667
|
+
var s, r;
|
|
3668
|
+
if (!this.workspace || !this.codespace) return null;
|
|
3669
|
+
const o = Ie();
|
|
3670
|
+
for (const c of this.codespace.getPlaceholders()) {
|
|
3671
|
+
if (e < c.start || e >= c.end) continue;
|
|
3672
|
+
if (c.emptySlot) {
|
|
3673
|
+
const { parentBlockId: u, inputName: w } = c.emptySlot;
|
|
3674
|
+
if (u === o) continue;
|
|
3675
|
+
const v = this.workspace.getBlockById(u);
|
|
3676
|
+
if (!((s = v == null ? void 0 : v.getInput(w)) != null && s.connection)) continue;
|
|
3677
|
+
return { parentBlockId: u, inputName: w, highlight: { from: c.start, to: c.end } };
|
|
3678
|
+
}
|
|
3679
|
+
const a = (r = c.edit) == null ? void 0 : r.blockId;
|
|
3680
|
+
if (!a || a === o) continue;
|
|
3681
|
+
const l = this.workspace.getBlockById(a);
|
|
3682
|
+
if (!l) continue;
|
|
3683
|
+
const d = this.resolveValueParent(l);
|
|
3684
|
+
if (d)
|
|
3685
|
+
return {
|
|
3686
|
+
parentBlockId: d.parentBlockId,
|
|
3687
|
+
inputName: d.inputName,
|
|
3688
|
+
highlight: { from: c.start, to: c.end }
|
|
3689
|
+
};
|
|
3690
|
+
}
|
|
3691
|
+
let i = null;
|
|
3692
|
+
for (const [c, a] of this.codespace.metadata) {
|
|
3693
|
+
if (a.startChar === void 0 || a.endChar === void 0 || e < a.startChar || e >= a.endChar || c === o) continue;
|
|
3694
|
+
const l = this.workspace.getBlockById(c);
|
|
3695
|
+
if (!(l != null && l.outputConnection)) continue;
|
|
3696
|
+
const d = a.endChar - a.startChar;
|
|
3697
|
+
(i === null || d < i.size) && (i = { id: c, size: d, from: a.startChar, to: a.endChar });
|
|
3698
|
+
}
|
|
3699
|
+
if (i) {
|
|
3700
|
+
const c = this.workspace.getBlockById(i.id);
|
|
3701
|
+
if (c) {
|
|
3702
|
+
const a = this.resolveValueParent(c);
|
|
3703
|
+
if (a)
|
|
3704
|
+
return {
|
|
3705
|
+
parentBlockId: a.parentBlockId,
|
|
3706
|
+
inputName: a.inputName,
|
|
3707
|
+
highlight: { from: i.from, to: i.to }
|
|
3708
|
+
};
|
|
3709
|
+
}
|
|
3710
|
+
}
|
|
3711
|
+
return null;
|
|
3712
|
+
}
|
|
3713
|
+
/** Find the value-input that holds `child`, returning its parent + input name. */
|
|
3714
|
+
resolveValueParent(e) {
|
|
3715
|
+
var s;
|
|
3716
|
+
const o = (s = e.outputConnection) == null ? void 0 : s.targetConnection;
|
|
3717
|
+
if (!o) return null;
|
|
3718
|
+
const i = o.getSourceBlock();
|
|
3719
|
+
if (!i) return null;
|
|
3720
|
+
for (const r of i.inputList)
|
|
3721
|
+
if (r.connection === o)
|
|
3722
|
+
return { parentBlockId: i.id, inputName: r.name };
|
|
3723
|
+
return null;
|
|
3724
|
+
}
|
|
3725
|
+
/**
|
|
3726
|
+
* Place a freshly-orphaned block at a clear workspace coordinate, below the
|
|
3727
|
+
* current bottom-most top block. Keeps replaced value children visible in
|
|
3728
|
+
* the codespace instead of overlapping the parent's slot position.
|
|
3729
|
+
*/
|
|
3730
|
+
placeOrphanBelowTops(e) {
|
|
3731
|
+
var s, r;
|
|
3732
|
+
if (!this.workspace) return;
|
|
3733
|
+
const o = this.workspace.getTopBlocks(!0).filter((c) => c.id !== e.id);
|
|
3734
|
+
if (o.length === 0) {
|
|
3735
|
+
e.moveTo(new B.utils.Coordinate(20, 20));
|
|
3736
|
+
return;
|
|
3737
|
+
}
|
|
3738
|
+
let i = 0;
|
|
3739
|
+
for (const c of o) {
|
|
3740
|
+
const a = c.getRelativeToSurfaceXY(), l = ((r = (s = c.getHeightWidth) == null ? void 0 : s.call(c)) == null ? void 0 : r.height) ?? 50;
|
|
3741
|
+
i = Math.max(i, a.y + l);
|
|
3742
|
+
}
|
|
3743
|
+
e.moveTo(new B.utils.Coordinate(20, i + 20));
|
|
3744
|
+
}
|
|
3745
|
+
/** True when `candidate` is `ancestor` itself or anywhere in its subtree. */
|
|
3746
|
+
isDescendantOf(e, o) {
|
|
3747
|
+
let i = e;
|
|
3748
|
+
for (; i; ) {
|
|
3749
|
+
if (i === o) return !0;
|
|
3750
|
+
i = i.getParent();
|
|
3751
|
+
}
|
|
3752
|
+
return !1;
|
|
3753
|
+
}
|
|
3754
|
+
/**
|
|
3755
|
+
* Connect `source`'s output into the value input named `inputName` on
|
|
3756
|
+
* `parent`. A real (non-shadow) child currently in the slot is `unplug`'d
|
|
3757
|
+
* to top level; a shadow disconnects implicitly when the new block
|
|
3758
|
+
* connects. The Blockly connection-check (output-type vs slot `check`)
|
|
3759
|
+
* runs inside `connect`; an incompatible drop throws and is rolled back
|
|
3760
|
+
* by restoring the prior real child when there was one.
|
|
3761
|
+
*/
|
|
3762
|
+
connectIntoValueSlot(e, o, i) {
|
|
3763
|
+
if (!e.outputConnection) return !1;
|
|
3764
|
+
const s = o.getInput(i), r = s == null ? void 0 : s.connection;
|
|
3765
|
+
if (!r) return !1;
|
|
3766
|
+
const c = r.targetBlock(), a = !!c && !c.isShadow();
|
|
3767
|
+
a && (c.unplug(!1), this.placeOrphanBelowTops(c));
|
|
3768
|
+
const d = r.check_;
|
|
3769
|
+
r.setCheck(null);
|
|
3770
|
+
try {
|
|
3771
|
+
return r.connect(e.outputConnection), !0;
|
|
3772
|
+
} catch {
|
|
3773
|
+
if (a && (c != null && c.outputConnection))
|
|
3774
|
+
try {
|
|
3775
|
+
r.connect(c.outputConnection);
|
|
3776
|
+
} catch {
|
|
3777
|
+
}
|
|
3778
|
+
return !1;
|
|
3779
|
+
} finally {
|
|
3780
|
+
r.setCheck(d);
|
|
3781
|
+
}
|
|
3782
|
+
}
|
|
3783
|
+
computeBlockDepth(e) {
|
|
3784
|
+
var s, r;
|
|
3785
|
+
let o = 0, i = ((r = (s = this.workspace) == null ? void 0 : s.getBlockById(e)) == null ? void 0 : r.getParent()) ?? null;
|
|
3786
|
+
for (; i; )
|
|
3787
|
+
o++, i = i.getParent();
|
|
3788
|
+
return o;
|
|
3789
|
+
}
|
|
3790
|
+
/** Place `block` at top-level `index`, re-spacing all top blocks. */
|
|
3791
|
+
placeAtTopIndex(e, o, i) {
|
|
3792
|
+
const s = e.getTopBlocks(!0).map((c) => c.id).filter((c) => c !== o.id), r = Math.max(0, Math.min(i, s.length));
|
|
3793
|
+
s.splice(r, 0, o.id), this.applyTopBlockOrder(e, s);
|
|
3794
|
+
}
|
|
3795
|
+
/** Connect `source` into `target`'s statement chain before/after `target`. */
|
|
3796
|
+
connectStatement(e, o, i) {
|
|
3797
|
+
var s, r, c;
|
|
3798
|
+
if (e.previousConnection)
|
|
3799
|
+
if (i === "before") {
|
|
3800
|
+
const a = ((s = o.previousConnection) == null ? void 0 : s.targetConnection) ?? null;
|
|
3801
|
+
(r = o.previousConnection) == null || r.disconnect(), a && a.connect(e.previousConnection), e.nextConnection && o.previousConnection && e.nextConnection.connect(o.previousConnection);
|
|
3802
|
+
} else {
|
|
3803
|
+
const a = o.getNextBlock();
|
|
3804
|
+
(c = a == null ? void 0 : a.previousConnection) == null || c.disconnect(), o.nextConnection && o.nextConnection.connect(e.previousConnection), a != null && a.previousConnection && e.nextConnection && e.nextConnection.connect(a.previousConnection);
|
|
3805
|
+
}
|
|
3806
|
+
}
|
|
3807
|
+
applyTopBlockOrder(e, o) {
|
|
3808
|
+
var s, r, c;
|
|
3809
|
+
const i = B.Events.disable !== void 0;
|
|
3810
|
+
i && B.Events.disable();
|
|
3811
|
+
try {
|
|
3812
|
+
for (let a = 0; a < o.length; a++) {
|
|
3813
|
+
const l = e.getBlockById(o[a]);
|
|
3814
|
+
l && l.moveTo(new B.utils.Coordinate(20, 20 + a * 100));
|
|
3815
|
+
}
|
|
3816
|
+
} finally {
|
|
3817
|
+
i && B.Events.enable();
|
|
3818
|
+
}
|
|
3819
|
+
B.svgResize(e), (s = this.codespace) == null || s.refresh(), (r = this.previewEditor) == null || r.refresh(), (c = this.codeEditor) == null || c.refresh();
|
|
3820
|
+
}
|
|
3821
|
+
/**
|
|
3822
|
+
* Mounts a read-only preview editor that renders the current mode's
|
|
3823
|
+
* `preview` element as text. Re-renders on workspace and mode changes.
|
|
3824
|
+
* If the active mode has no `preview` declared, the editor stays empty.
|
|
3825
|
+
*/
|
|
3826
|
+
async mountPreview(e, o) {
|
|
3827
|
+
var r;
|
|
3828
|
+
if (!this.workspace || !this.mountConfig)
|
|
3829
|
+
throw new Error(
|
|
3830
|
+
"MorphicBlocks must be mounted before mountPreview can be used."
|
|
3831
|
+
);
|
|
3832
|
+
(r = this.previewEditor) == null || r.dispose();
|
|
3833
|
+
const i = {
|
|
3834
|
+
...o,
|
|
3835
|
+
highlightRules: (o == null ? void 0 : o.highlightRules) ?? this.resolveHighlightRules("preview"),
|
|
3836
|
+
// Preview is read-only by design; the form-field underline marker is
|
|
3837
|
+
// only meaningful on the editable codespace, so suppress it here.
|
|
3838
|
+
showPlaceholderMarkers: (o == null ? void 0 : o.showPlaceholderMarkers) ?? !1
|
|
3839
|
+
};
|
|
3840
|
+
this.previewEditor = new ge(
|
|
3841
|
+
e,
|
|
3842
|
+
this.workspace,
|
|
3843
|
+
() => this.generatePreviewText(),
|
|
3844
|
+
i
|
|
3845
|
+
);
|
|
3846
|
+
const s = this.previewEditor;
|
|
3847
|
+
await s.mount(), this.previewEditor === s && this.refreshSelectionSync();
|
|
3848
|
+
}
|
|
3849
|
+
generatePreviewText() {
|
|
3850
|
+
if (!this.workspace || !this.mountConfig)
|
|
3851
|
+
return { code: "", metadata: /* @__PURE__ */ new Map(), placeholders: [] };
|
|
3852
|
+
const e = this.mountConfig.previewMode, o = this.getActivePreviewElement();
|
|
3853
|
+
return !e || !o ? { code: "", metadata: /* @__PURE__ */ new Map(), placeholders: [] } : ke(
|
|
3854
|
+
this.workspace,
|
|
3855
|
+
e,
|
|
3856
|
+
this.definitions,
|
|
3857
|
+
this.elementTypes,
|
|
3858
|
+
this.mountConfig.modes ?? [],
|
|
3859
|
+
o
|
|
3860
|
+
);
|
|
3861
|
+
}
|
|
3862
|
+
/**
|
|
3863
|
+
* Write `newValue` to `edit.fieldName` on the Blockly block identified by
|
|
3864
|
+
* `edit.blockId`. The change event listener will trigger codespace re-sync.
|
|
3865
|
+
*/
|
|
3866
|
+
applyPlaceholderEdit(e, o) {
|
|
3867
|
+
var a;
|
|
3868
|
+
if (!this.workspace) return;
|
|
3869
|
+
let i = this.workspace.getBlockById(e.blockId);
|
|
3870
|
+
if (!i) return;
|
|
3871
|
+
if (i.isShadow()) {
|
|
3872
|
+
const l = (a = i.outputConnection) == null ? void 0 : a.targetConnection;
|
|
3873
|
+
if (l) {
|
|
3874
|
+
const d = this.workspace.newBlock(i.type);
|
|
3875
|
+
for (const u of i.inputList)
|
|
3876
|
+
for (const w of u.fieldRow) {
|
|
3877
|
+
if (!w.name) continue;
|
|
3878
|
+
const v = d.getField(w.name);
|
|
3879
|
+
v && v.setValue(w.getValue());
|
|
3880
|
+
}
|
|
3881
|
+
i.rendered && (d.initSvg(), d.render()), l.connect(d.outputConnection), i = d;
|
|
3882
|
+
}
|
|
3883
|
+
}
|
|
3884
|
+
const s = i.getField(e.fieldName);
|
|
3885
|
+
if (!s) return;
|
|
3886
|
+
s.setValue(o);
|
|
3887
|
+
const r = i;
|
|
3888
|
+
r.rendered && typeof r.render == "function" && r.render();
|
|
3889
|
+
const c = i.getParent();
|
|
3890
|
+
if (c) {
|
|
3891
|
+
const l = c;
|
|
3892
|
+
l.rendered && typeof l.render == "function" && l.render();
|
|
3893
|
+
}
|
|
3894
|
+
}
|
|
3895
|
+
deleteBlockAtCodespaceLine(e) {
|
|
3896
|
+
var r, c;
|
|
3897
|
+
const o = (r = this.codespace) == null ? void 0 : r.metadata;
|
|
3898
|
+
if (!this.workspace || !o || o.size === 0) return;
|
|
3899
|
+
let i = null, s = 1 / 0;
|
|
3900
|
+
for (const [a, { startLine: l, endLine: d }] of o) {
|
|
3901
|
+
if (e < l || e > d) continue;
|
|
3902
|
+
const u = d - l;
|
|
3903
|
+
u < s && (s = u, i = a);
|
|
3904
|
+
}
|
|
3905
|
+
i && ((c = this.workspace.getBlockById(i)) == null || c.dispose(!0));
|
|
3906
|
+
}
|
|
3907
|
+
generateCodespaceText() {
|
|
3908
|
+
if (!this.workspace || !this.mountConfig)
|
|
3909
|
+
return { code: "", metadata: /* @__PURE__ */ new Map(), placeholders: [] };
|
|
3910
|
+
const e = this.mountConfig.codespaceMode;
|
|
3911
|
+
if (e) {
|
|
3912
|
+
const o = this.modeDef(e), i = o ? te(o, this.elementTypes) : void 0;
|
|
3913
|
+
return ke(
|
|
3914
|
+
this.workspace,
|
|
3915
|
+
e,
|
|
3916
|
+
this.definitions,
|
|
3917
|
+
this.elementTypes,
|
|
3918
|
+
this.mountConfig.modes ?? [],
|
|
3919
|
+
i
|
|
3920
|
+
);
|
|
3921
|
+
}
|
|
3922
|
+
return ke(
|
|
3923
|
+
this.workspace,
|
|
3924
|
+
this.mountConfig.workspaceMode,
|
|
3925
|
+
this.definitions,
|
|
3926
|
+
this.elementTypes,
|
|
3927
|
+
this.mountConfig.modes ?? []
|
|
3928
|
+
);
|
|
3929
|
+
}
|
|
3930
|
+
showCodeEditor() {
|
|
3931
|
+
var e;
|
|
3932
|
+
(e = this.codeEditor) == null || e.show();
|
|
3933
|
+
}
|
|
3934
|
+
hideCodeEditor() {
|
|
3935
|
+
var e;
|
|
3936
|
+
(e = this.codeEditor) == null || e.hide();
|
|
3937
|
+
}
|
|
3938
|
+
isCodeEditorVisible() {
|
|
3939
|
+
var e;
|
|
3940
|
+
return ((e = this.codeEditor) == null ? void 0 : e.isVisible()) ?? !1;
|
|
3941
|
+
}
|
|
3942
|
+
setCodeEditorTheme(e) {
|
|
3943
|
+
var o;
|
|
3944
|
+
(o = this.codeEditor) == null || o.setTheme(e);
|
|
3945
|
+
}
|
|
3946
|
+
setCodespaceTheme(e) {
|
|
3947
|
+
var o;
|
|
3948
|
+
(o = this.codespace) == null || o.setTheme(e);
|
|
3949
|
+
}
|
|
3950
|
+
setPreviewTheme(e) {
|
|
3951
|
+
var o;
|
|
3952
|
+
(o = this.previewEditor) == null || o.setTheme(e);
|
|
3953
|
+
}
|
|
3954
|
+
/**
|
|
3955
|
+
* Enable bidirectional selection sync between the Blockly workspace and every
|
|
3956
|
+
* currently-mounted editor (code editor, codespace, preview). Requires at
|
|
3957
|
+
* least one of those editors to be mounted first.
|
|
3958
|
+
*/
|
|
3959
|
+
enableSelectionSync(e) {
|
|
3960
|
+
var i;
|
|
3961
|
+
if (!this.workspace)
|
|
3962
|
+
throw new Error(
|
|
3963
|
+
"MorphicBlocks must be mounted before enableSelectionSync can be used."
|
|
3964
|
+
);
|
|
3965
|
+
const o = [this.codeEditor, this.codespace, this.previewEditor].filter(
|
|
3966
|
+
(s) => s !== void 0
|
|
3967
|
+
);
|
|
3968
|
+
if (o.length === 0)
|
|
3969
|
+
throw new Error(
|
|
3970
|
+
"enableSelectionSync requires at least one of mountCodeEditor, mountCodespace, or mountPreview to have been called first."
|
|
3971
|
+
);
|
|
3972
|
+
(i = this.selectionSync) == null || i.disable(), this.selectionSyncOptions = e, this.selectionSync = new uo(
|
|
3973
|
+
this.workspace,
|
|
3974
|
+
o,
|
|
3975
|
+
e
|
|
3976
|
+
), this.selectionSync.enable();
|
|
3977
|
+
}
|
|
3978
|
+
/**
|
|
3979
|
+
* Rebuild an active selection sync after an editor was (re)mounted, so it
|
|
3980
|
+
* links the current editors rather than a disposed one.
|
|
3981
|
+
*/
|
|
3982
|
+
refreshSelectionSync() {
|
|
3983
|
+
this.selectionSync && this.enableSelectionSync(this.selectionSyncOptions);
|
|
3984
|
+
}
|
|
3985
|
+
/** Disable selection sync and clear any active highlights. */
|
|
3986
|
+
disableSelectionSync() {
|
|
3987
|
+
var e;
|
|
3988
|
+
(e = this.selectionSync) == null || e.disable(), this.selectionSync = void 0, this.selectionSyncOptions = void 0;
|
|
3989
|
+
}
|
|
3990
|
+
registerBlocks() {
|
|
3991
|
+
for (const e of this.definitions.values()) {
|
|
3992
|
+
const o = Ee(e.identifier);
|
|
3993
|
+
if (se.set(o, this), this.registeredBlockTypes.has(o))
|
|
3994
|
+
continue;
|
|
3995
|
+
const i = e.identifier;
|
|
3996
|
+
B.Blocks[o] = {
|
|
3997
|
+
init() {
|
|
3998
|
+
const s = ie.get(this.workspace) ?? se.get(o);
|
|
3999
|
+
s == null || s.initManagedBlock(this, i);
|
|
4000
|
+
}
|
|
4001
|
+
}, this.registeredBlockTypes.add(o);
|
|
4002
|
+
}
|
|
4003
|
+
}
|
|
4004
|
+
/** Build a newly created block with this engine's definition, mode and behavior. */
|
|
4005
|
+
initManagedBlock(e, o) {
|
|
4006
|
+
var a;
|
|
4007
|
+
const i = this.definitions.get(o);
|
|
4008
|
+
if (!i)
|
|
4009
|
+
return;
|
|
4010
|
+
const s = e.workspace.isFlyout ? "toolbox" : "workspace", r = this.resolveMode(s);
|
|
4011
|
+
this.applyView(e, i, r, s);
|
|
4012
|
+
const c = ae(this.behaviors[i.identifier]);
|
|
4013
|
+
(a = c == null ? void 0 : c.init) == null || a.call(c, e, this.createBehaviorContext(e, i, r, s));
|
|
4014
|
+
}
|
|
4015
|
+
validateContainers(e) {
|
|
4016
|
+
if (!e.workspaceContainer && !e.codespaceContainer)
|
|
4017
|
+
throw new Error(
|
|
4018
|
+
"MorphicBlocks.mount requires at least one of workspaceContainer or codespaceContainer."
|
|
4019
|
+
);
|
|
4020
|
+
}
|
|
4021
|
+
createHeadlessHost() {
|
|
4022
|
+
const e = document.createElement("div");
|
|
4023
|
+
return e.style.cssText = "position:absolute;left:-9999px;top:-9999px;width:800px;height:600px;overflow:hidden;", document.body.appendChild(e), this.headlessWorkspaceHost = e, e;
|
|
4024
|
+
}
|
|
4025
|
+
resolveMode(e) {
|
|
4026
|
+
return this.mountConfig ? e === "toolbox" ? this.mountConfig.toolboxMode : this.mountConfig.workspaceMode : "default";
|
|
4027
|
+
}
|
|
4028
|
+
applyView(e, o, i, s) {
|
|
4029
|
+
var u, w;
|
|
4030
|
+
if (e.isDeadOrDying())
|
|
4031
|
+
return;
|
|
4032
|
+
const r = this.blockCategoryIndex.get(o.identifier);
|
|
4033
|
+
o.color === void 0 && (r == null ? void 0 : r.color) !== void 0 && e.setColour(r.color);
|
|
4034
|
+
const c = At(e), a = Se(o, i, this.elementTypes, ((u = this.mountConfig) == null ? void 0 : u.modes) ?? []), l = e.workspace === this.workspace;
|
|
4035
|
+
Ue({
|
|
4036
|
+
block: e,
|
|
4037
|
+
definition: o,
|
|
4038
|
+
view: a,
|
|
4039
|
+
mode: i,
|
|
4040
|
+
context: s,
|
|
4041
|
+
elementTypes: l ? this.elementTypes : void 0,
|
|
4042
|
+
resolveBlocklyType: (v) => z(v, this.definitions)
|
|
4043
|
+
}), wt(e, r == null ? void 0 : r.token), yt(e, o.identifier), Ct(e), o.color !== void 0 && (e.setColour(o.color), e.rendered && e.render());
|
|
4044
|
+
const d = ae(
|
|
4045
|
+
this.behaviors[o.identifier]
|
|
4046
|
+
);
|
|
4047
|
+
(w = d == null ? void 0 : d.onViewApplied) == null || w.call(
|
|
4048
|
+
d,
|
|
4049
|
+
e,
|
|
4050
|
+
this.createBehaviorContext(e, o, i, s)
|
|
4051
|
+
), Nt(e, c);
|
|
4052
|
+
}
|
|
4053
|
+
applyWorkspaceContainerClass() {
|
|
4054
|
+
var i;
|
|
4055
|
+
if (!this.mountConfig)
|
|
4056
|
+
return;
|
|
4057
|
+
const e = this.mountConfig.workspaceHost;
|
|
4058
|
+
fe(
|
|
4059
|
+
e,
|
|
4060
|
+
this.mountConfig.workspaceMode,
|
|
4061
|
+
"workspace",
|
|
4062
|
+
"morphic-workspace-root"
|
|
4063
|
+
);
|
|
4064
|
+
const o = this.resolveClassNames(
|
|
4065
|
+
(i = this.mountConfig.ui) == null ? void 0 : i.workspaceClassName
|
|
4066
|
+
);
|
|
4067
|
+
this.replaceUserClasses(
|
|
4068
|
+
e,
|
|
4069
|
+
this.appliedWorkspaceClasses,
|
|
4070
|
+
o
|
|
4071
|
+
), this.appliedWorkspaceClasses = o;
|
|
4072
|
+
}
|
|
4073
|
+
/**
|
|
4074
|
+
* Measure block text with the font the workspace mode's CSS draws it in.
|
|
4075
|
+
* The probe sits in the workspace container with Blockly's renderer and
|
|
4076
|
+
* base theme classes, so it gets Blockly's own font unless the mode's CSS
|
|
4077
|
+
* overrides it, exactly like real block text.
|
|
4078
|
+
*/
|
|
4079
|
+
syncWorkspaceFont() {
|
|
4080
|
+
var s;
|
|
4081
|
+
const e = this.workspace, o = (s = this.mountConfig) == null ? void 0 : s.workspaceHost;
|
|
4082
|
+
if (!e || !o || !this.baseTheme || !this.baseFont) return;
|
|
4083
|
+
const i = et(
|
|
4084
|
+
o,
|
|
4085
|
+
[[e.getRenderer().getClassName(), this.baseTheme.getClassName()]],
|
|
4086
|
+
this.baseFont
|
|
4087
|
+
);
|
|
4088
|
+
tt(e, this.baseTheme, this.baseFont, i) && Pe(e);
|
|
4089
|
+
}
|
|
4090
|
+
applyFlyoutClass() {
|
|
4091
|
+
var c, a, l;
|
|
4092
|
+
if (!this.workspace || !this.mountConfig)
|
|
4093
|
+
return;
|
|
4094
|
+
const e = this.resolveClassNames(
|
|
4095
|
+
(c = this.mountConfig.ui) == null ? void 0 : c.toolboxClassName
|
|
4096
|
+
), o = this.workspace.getToolbox(), i = ((a = o == null ? void 0 : o.getDiv) == null ? void 0 : a.call(o)) ?? null;
|
|
4097
|
+
i && (fe(
|
|
4098
|
+
i,
|
|
4099
|
+
this.mountConfig.toolboxMode,
|
|
4100
|
+
"toolbox",
|
|
4101
|
+
"morphic-toolbox-shell"
|
|
4102
|
+
), this.replaceUserClasses(
|
|
4103
|
+
i,
|
|
4104
|
+
this.appliedToolboxShellClasses,
|
|
4105
|
+
e
|
|
4106
|
+
), this.appliedToolboxShellClasses = e);
|
|
4107
|
+
const s = (l = this.workspace.getFlyout()) == null ? void 0 : l.getWorkspace(), r = s == null ? void 0 : s.getParentSvg();
|
|
4108
|
+
r && (fe(
|
|
4109
|
+
r,
|
|
4110
|
+
this.mountConfig.toolboxMode,
|
|
4111
|
+
"toolbox",
|
|
4112
|
+
"morphic-toolbox-root"
|
|
4113
|
+
), this.replaceUserClasses(
|
|
4114
|
+
r,
|
|
4115
|
+
this.appliedToolboxFlyoutClasses,
|
|
4116
|
+
e
|
|
4117
|
+
), this.appliedToolboxFlyoutClasses = e);
|
|
4118
|
+
}
|
|
4119
|
+
renderWorkspaceBlocks() {
|
|
4120
|
+
if (!(!this.workspace || !this.mountConfig))
|
|
4121
|
+
for (const e of this.workspace.getAllBlocks(!1)) {
|
|
4122
|
+
const o = e, i = this.definitions.get(W(o.type));
|
|
4123
|
+
i && this.applyView(
|
|
4124
|
+
o,
|
|
4125
|
+
i,
|
|
4126
|
+
this.mountConfig.workspaceMode,
|
|
4127
|
+
"workspace"
|
|
4128
|
+
);
|
|
4129
|
+
}
|
|
4130
|
+
}
|
|
4131
|
+
renderFlyoutBlocks() {
|
|
4132
|
+
if (!(!this.flyoutWorkspace || !this.mountConfig)) {
|
|
4133
|
+
for (const e of this.flyoutWorkspace.getAllBlocks(!1)) {
|
|
4134
|
+
const o = e, i = this.definitions.get(W(o.type));
|
|
4135
|
+
i && this.applyView(
|
|
4136
|
+
o,
|
|
4137
|
+
i,
|
|
4138
|
+
this.mountConfig.toolboxMode,
|
|
4139
|
+
"toolbox"
|
|
4140
|
+
);
|
|
4141
|
+
}
|
|
4142
|
+
this.applyFlyoutClass();
|
|
4143
|
+
}
|
|
4144
|
+
}
|
|
4145
|
+
refreshToolbox() {
|
|
4146
|
+
var e;
|
|
4147
|
+
!this.workspace || !this.toolboxDefinition || (e = this.mountConfig) != null && e.canvasToolbox || this.workspace.updateToolbox(this.toolboxDefinition);
|
|
4148
|
+
}
|
|
4149
|
+
bindFlyoutWorkspace() {
|
|
4150
|
+
var o;
|
|
4151
|
+
if (this.flyoutWorkspace && (this.flyoutWorkspace.removeChangeListener(this.onFlyoutChange), this.flyoutWorkspace = void 0), !this.workspace)
|
|
4152
|
+
return;
|
|
4153
|
+
const e = (o = this.workspace.getFlyout()) == null ? void 0 : o.getWorkspace();
|
|
4154
|
+
e && (this.flyoutWorkspace = e, ie.set(e, this), this.flyoutWorkspace.addChangeListener(this.onFlyoutChange), this.applyFlyoutClass());
|
|
4155
|
+
}
|
|
4156
|
+
resolveToolboxDefinition(e) {
|
|
4157
|
+
if (e.toolbox) {
|
|
4158
|
+
const s = this.resolveToolboxKind(e.toolboxLayout), r = s ? { ...e.toolbox, kind: s } : e.toolbox;
|
|
4159
|
+
return We(r, this.definitions);
|
|
4160
|
+
}
|
|
4161
|
+
const o = e.blockly;
|
|
4162
|
+
if (o != null && o.toolbox)
|
|
4163
|
+
return o.toolbox;
|
|
4164
|
+
const i = this.resolveToolboxKind(e.toolboxLayout);
|
|
4165
|
+
return We(
|
|
4166
|
+
{
|
|
4167
|
+
...i !== void 0 ? { kind: i } : {},
|
|
4168
|
+
blocks: [...this.definitions.keys()]
|
|
4169
|
+
},
|
|
4170
|
+
this.definitions
|
|
4171
|
+
);
|
|
4172
|
+
}
|
|
4173
|
+
createBehaviorContext(e, o, i, s) {
|
|
4174
|
+
return {
|
|
4175
|
+
Blockly: B,
|
|
4176
|
+
workspace: e.workspace,
|
|
4177
|
+
mode: i,
|
|
4178
|
+
context: s,
|
|
4179
|
+
definition: o
|
|
4180
|
+
};
|
|
4181
|
+
}
|
|
4182
|
+
resolveClassNames(e) {
|
|
4183
|
+
return e ? (Array.isArray(e) ? e.join(" ") : e).split(/\s+/).map((i) => i.trim()).filter(
|
|
4184
|
+
(i, s, r) => !!i && r.indexOf(i) === s
|
|
4185
|
+
) : [];
|
|
4186
|
+
}
|
|
4187
|
+
replaceUserClasses(e, o, i) {
|
|
4188
|
+
for (const s of o)
|
|
4189
|
+
e.classList.remove(s);
|
|
4190
|
+
for (const s of i)
|
|
4191
|
+
e.classList.add(s);
|
|
4192
|
+
}
|
|
4193
|
+
buildBlockColorMap() {
|
|
4194
|
+
var o;
|
|
4195
|
+
const e = /* @__PURE__ */ new Map();
|
|
4196
|
+
for (const [i, s] of this.definitions) {
|
|
4197
|
+
const r = s.color ?? ((o = this.blockCategoryIndex.get(i)) == null ? void 0 : o.color);
|
|
4198
|
+
r !== void 0 && e.set(i, String(r));
|
|
4199
|
+
}
|
|
4200
|
+
return e;
|
|
4201
|
+
}
|
|
4202
|
+
resolveToolboxKind(e) {
|
|
4203
|
+
if (e)
|
|
4204
|
+
return e === "category" ? "categoryToolbox" : "flyoutToolbox";
|
|
4205
|
+
}
|
|
4206
|
+
createCategoryIndex(e, o) {
|
|
4207
|
+
const i = /* @__PURE__ */ new Map(), s = (e == null ? void 0 : e.categories) ?? [], r = /* @__PURE__ */ new Map();
|
|
4208
|
+
for (const a of s) {
|
|
4209
|
+
const l = P(a.name);
|
|
4210
|
+
if (r.set(a.name.toLowerCase(), {
|
|
4211
|
+
token: l,
|
|
4212
|
+
color: a.color
|
|
4213
|
+
}), a.blocks)
|
|
4214
|
+
for (const d of a.blocks)
|
|
4215
|
+
i.has(d) || i.set(d, { token: l, color: a.color });
|
|
4216
|
+
}
|
|
4217
|
+
const c = o ? this.definitions : /* @__PURE__ */ new Map();
|
|
4218
|
+
for (const a of c.values()) {
|
|
4219
|
+
if (!a.category || i.has(a.identifier))
|
|
4220
|
+
continue;
|
|
4221
|
+
const l = r.get(a.category.toLowerCase());
|
|
4222
|
+
l && i.set(a.identifier, l);
|
|
4223
|
+
}
|
|
4224
|
+
return i;
|
|
4225
|
+
}
|
|
4226
|
+
deferApplyView(e, o, i) {
|
|
4227
|
+
const s = i === "toolbox" ? this.flyoutWorkspace : this.workspace;
|
|
4228
|
+
s && requestAnimationFrame(() => {
|
|
4229
|
+
const r = s.getBlockById(e);
|
|
4230
|
+
if (!r)
|
|
4231
|
+
return;
|
|
4232
|
+
const c = this.definitions.get(W(r.type));
|
|
4233
|
+
c && this.applyView(r, c, o, i);
|
|
4234
|
+
});
|
|
4235
|
+
}
|
|
4236
|
+
}
|
|
4237
|
+
function Uo(n, t, e) {
|
|
4238
|
+
return Math.min(Math.max(n, t), e);
|
|
4239
|
+
}
|
|
4240
|
+
function Jo(n, t) {
|
|
4241
|
+
const { target: e, axis: o, invert: i = !1, onResize: s } = t, r = t.min ?? 0, c = t.max ?? Number.POSITIVE_INFINITY, a = (l) => {
|
|
4242
|
+
if (l.button !== 0) return;
|
|
4243
|
+
l.preventDefault();
|
|
4244
|
+
const d = e.getBoundingClientRect(), u = o === "x" ? d.width : d.height, w = o === "x" ? l.clientX : l.clientY, v = document.body.style.userSelect, b = document.body.style.cursor;
|
|
4245
|
+
document.body.style.userSelect = "none", document.body.style.cursor = o === "x" ? "col-resize" : "row-resize", n.setPointerCapture(l.pointerId);
|
|
4246
|
+
const E = (m) => {
|
|
4247
|
+
const y = (o === "x" ? m.clientX : m.clientY) - w, S = Uo(u + (i ? -y : y), r, c);
|
|
4248
|
+
e.style.flexBasis = `${S}px`, s == null || s(S);
|
|
4249
|
+
}, k = () => {
|
|
4250
|
+
n.removeEventListener("pointermove", E), n.removeEventListener("pointerup", k), n.removeEventListener("lostpointercapture", k), document.body.style.userSelect = v, document.body.style.cursor = b;
|
|
4251
|
+
};
|
|
4252
|
+
n.addEventListener("pointermove", E), n.addEventListener("pointerup", k), n.addEventListener("lostpointercapture", k);
|
|
4253
|
+
};
|
|
4254
|
+
return n.addEventListener("pointerdown", a), {
|
|
4255
|
+
dispose: () => n.removeEventListener("pointerdown", a)
|
|
4256
|
+
};
|
|
4257
|
+
}
|
|
4258
|
+
export {
|
|
4259
|
+
Yo as MorphicBlocks,
|
|
4260
|
+
Lo as MorphicToolboxCanvas,
|
|
4261
|
+
Jo as makeResizable,
|
|
4262
|
+
_o as renderToolbar,
|
|
4263
|
+
N as toolbarItems,
|
|
4264
|
+
bo as validateDefinitions
|
|
4265
|
+
};
|