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