forked from flipcomputing/flock
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathblocklyutil.js
More file actions
652 lines (550 loc) · 19.9 KB
/
Copy pathblocklyutil.js
File metadata and controls
652 lines (550 loc) · 19.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
import * as Blockly from "blockly";
import {
addNumberShadow,
addXYZShadows,
addColourShadowSpec,
buildColorsListShadowSpec,
addPositionShadows,
addColourShadow,
} from "./blocklyshadowutil.js";
let lastAddMenuHighlighted = null;
// Whether a block has been locked (non-editable / non-movable / non-deletable).
export function isBlockLocked(block) {
return !!block?.locked;
}
// Recursively remove lock-related serialized attributes from a block-save JSON
// node and its inputs/next, so a pasted/duplicated copy is created unlocked.
export function stripLockState(node) {
if (!node || typeof node !== "object") return;
delete node.movable;
delete node.editable;
delete node.deletable;
if (node.inputs) {
for (const name of Object.keys(node.inputs)) {
stripLockState(node.inputs[name]?.block);
stripLockState(node.inputs[name]?.shadow);
}
}
if (node.next) {
stripLockState(node.next.block);
stripLockState(node.next.shadow);
}
}
// Lock or unlock a block and all of its descendants (including connected shadow
// blocks, so nested X/Y/Z, size and colour fields can't be edited either). Only
// the top block carries the persisted `locked` flag; descendants are re-cascaded
// on load by the flockLock serializer.
// Collect the block plus everything nested *inside* it: blocks plugged into its
// value/statement inputs (recursively), including the full statement stack of a
// DO-style input. Blocks connected after the root via its next connection are
// siblings, not nested, so they are deliberately excluded.
function getLockTargets(rootBlock) {
const targets = [];
const visit = (b, isRoot) => {
targets.push(b);
for (const input of b.inputList || []) {
const child = input.connection?.targetBlock?.();
if (child) visit(child, false);
}
// Follow the next chain only below the root: a statement stack inside a DO
// is nested and should be locked; the root's own next block is a sibling.
if (!isRoot) {
const next = b.getNextBlock?.();
if (next) visit(next, false);
}
};
visit(rootBlock, true);
return targets;
}
// A drag strategy that reports the block as movable (so Blockly treats a press
// as a block drag rather than falling back to panning the workspace) but does
// nothing — a locked block neither moves nor drags the canvas.
function makeNoopDragStrategy(block) {
return {
isMovable: () => true,
startDrag: () => block,
drag: () => {},
endDrag: () => {},
revertDrag: () => {},
};
}
// Apply (or clear) the locked state on a single block. Does not cascade.
export function applyBlockLockState(b, locked) {
if (!b) return;
b.locked = locked;
if (typeof b.setEditable === "function") b.setEditable(!locked);
// Shadow blocks are inherently non-movable/non-deletable; toggling those on
// them would wrongly make them draggable/removable when unlocked.
if (!b.isShadow?.()) {
if (typeof b.setDeletable === "function") b.setDeletable(!locked);
if (typeof b.setMovable === "function") b.setMovable(!locked);
// Swap in a no-op drag strategy while locked. setMovable(false) alone
// makes Blockly pan the workspace when you try to drag the block; a
// movable-but-no-op strategy consumes the drag gesture instead.
if (typeof b.setDragStrategy === "function") {
if (locked) {
if (!b._origDragStrategy) b._origDragStrategy = b.getDragStrategy?.();
b.setDragStrategy(makeNoopDragStrategy(b));
} else if (b._origDragStrategy) {
b.setDragStrategy(b._origDragStrategy);
delete b._origDragStrategy;
}
}
}
// Tag each locked block individually so the "no entry" cursor applies to the
// locked subtree only, not to sibling blocks nested below it in the SVG DOM.
b.getSvgRoot?.()?.classList.toggle("blockly-locked", locked);
}
// Colour-swatch blocks carry only a colour field. They stay editable even
// inside a locked block so the colour can still be changed while every other
// input stays frozen.
const COLOUR_BLOCK_TYPES = new Set(["colour", "skin_colour", "colour_picker"]);
function isColourBlock(b) {
return COLOUR_BLOCK_TYPES.has(b?.type);
}
export function setBlockLocked(block, locked) {
if (!block) return;
for (const b of getLockTargets(block)) {
// When locking, leave colour swatches editable; unlocking clears them all.
applyBlockLockState(b, locked && !isColourBlock(b));
}
}
// Toggle a comment on a block: remove it if present, otherwise add an empty one
// and open its bubble for editing. Used by the floating block toolbar's comment
// button (which is an add/remove affordance).
export function toggleBlockComment(block) {
if (!block) return;
if (block.getCommentText() !== null) {
block.setCommentText(null);
} else {
block.setCommentText("");
getCommentIcon(block)?.setBubbleVisible(true);
}
}
function getCommentIcon(block) {
return (
block?.getIcons?.().find((i) => typeof i.setBubbleVisible === "function") ??
null
);
}
// Toggle the comment bubble open/closed, creating the comment if the block
// doesn't have one yet. When opening, move keyboard focus into the comment
// editor so the user can type straight away. Used by the 'K' shortcut; never
// deletes (use deleteBlockComment for that). Async because Blockly's
// setBubbleVisible resolves once the bubble has been (re)rendered.
export async function toggleCommentBubble(block) {
if (!block) return;
if (block.getCommentText() === null) block.setCommentText("");
const icon = getCommentIcon(block);
if (!icon) return;
if (icon.bubbleIsVisible?.()) {
await icon.setBubbleVisible(false);
return;
}
await icon.setBubbleVisible(true);
// performAction() is the comment bubble's documented keyboard-navigation
// entry point: it focuses the editor's text area.
icon.getBubble?.()?.performAction?.();
}
// Remove a block's comment entirely (icon and text), if it has one.
export function deleteBlockComment(block) {
if (!block) return;
if (block.getCommentText() !== null) block.setCommentText(null);
}
function trackBlockHighlight(workspace, blockId) {
lastAddMenuHighlighted = { workspace, blockId };
const block = workspace.getBlockById(blockId);
block.select();
ensurePassiveFocusWrapper(workspace);
}
function ensurePassiveFocusWrapper(workspace) {
if (!workspace || workspace.__addMenuPassiveFocusWrapped) return;
const orig = workspace.getRestoredFocusableNode?.bind(workspace);
if (!orig) return;
workspace.getRestoredFocusableNode = (prevTree, prevNode) => {
if (lastAddMenuHighlighted?.workspace === workspace) {
const block = workspace.getBlockById(lastAddMenuHighlighted.blockId);
if (block) return block;
}
return orig(prevTree, prevNode);
};
workspace.__addMenuPassiveFocusWrapped = true;
}
function clearAddMenuHighlight(workspace, newSelectedId) {
if (
!lastAddMenuHighlighted ||
lastAddMenuHighlighted.workspace !== workspace ||
lastAddMenuHighlighted.blockId === newSelectedId
) {
return;
}
const block = workspace.getBlockById(lastAddMenuHighlighted.blockId);
block?.unselect?.();
lastAddMenuHighlighted = null;
}
export function appendWithUndo(spec, ws, groupId) {
let block;
try {
block = Blockly.serialization.blocks.append(spec, ws, {
recordUndo: true,
});
} catch {
block = Blockly.serialization.blocks.append(spec, ws);
const ev = new Blockly.Events.BlockCreate(block);
ev.group = groupId;
ev.recordUndo = true;
Blockly.Events.fire(ev);
}
block?.initSvg?.();
block?.render?.();
return block;
}
export function getLastHighlightedBlockId(workspace) {
return lastAddMenuHighlighted?.workspace === workspace
? lastAddMenuHighlighted.blockId
: null;
}
export function restoreBlockFocus(workspace, blockId) {
if (!workspace || !blockId) return;
const block = workspace.getBlockById(blockId);
if (!block) return;
// On a view switch (canvas -> code) just bring the block back into view.
// Deliberately do NOT select or focus it: re-selecting armed the persistent
// getRestoredFocusableNode override, which hijacked focus on the next tap
// (first tap showed no toolbar) and left a stale selection ring on the old
// block. Leaving nothing selected means the next tap selects cleanly.
scrollToBlockTopParentLeft(workspace, blockId);
}
export function highlightBlockById(workspace, block) {
if (!workspace || !block || block.workspace !== workspace) return;
// Select and scroll only when the code view is visible
if (window.codeMode === "both") {
ensureAddMenuSelectionCleanup(workspace);
clearAddMenuHighlight(workspace, block.id);
trackBlockHighlight(workspace, block.id);
// Scroll to position the block at the top and its parent at the left
scrollToBlockTopParentLeft(workspace, block.id);
}
}
function ensureAddMenuSelectionCleanup(workspace) {
if (!workspace || workspace.__addMenuSelectionCleanupAttached) return;
const listener = (event) => {
const isSelectEvent =
event.type === Blockly.Events.SELECTED ||
(event.type === Blockly.Events.UI && event.element === "selected");
if (isSelectEvent) {
clearAddMenuHighlight(workspace, event.newElementId);
}
};
workspace.addChangeListener(listener);
workspace.__addMenuSelectionCleanupAttached = true;
}
export function scrollToBlockTopParentLeft(workspace, blockId) {
if (!workspace.isMovable()) {
console.warn("Tried to move a non-movable workspace.");
return;
}
const block = blockId ? workspace.getBlockById(blockId) : null;
if (!block) {
return;
}
// Find the ultimate parent block
let ultimateParent = block;
while (ultimateParent.getParent()) {
ultimateParent = ultimateParent.getParent();
}
// Get the position of the target block (for top positioning)
const blockXY = block.getRelativeToSurfaceXY();
// Get the position of the ultimate parent (for left positioning)
const parentXY = ultimateParent.getRelativeToSurfaceXY();
// Workspace scale, used to convert from workspace coordinates to pixels
const scale = workspace.scale;
// Convert block positions to pixels
const pixelBlockY = blockXY.y * scale;
const pixelParentX = parentXY.x * scale;
const padding = 20;
const scrollToY = pixelBlockY - padding;
const scrollToX = pixelParentX - padding;
// Convert to canvas directions (negative values)
const x = -scrollToX;
const y = -scrollToY;
workspace.scroll(x, y);
}
// Updated setPositionValues function with rounding behavior
export function setPositionValues(block, position, blockType, decimals = 1) {
// Helper function to set position values on blocks
if (block && position) {
try {
// Helper function to set or create shadow block for position input
function setOrCreatePositionInput(inputName, value) {
const input = block.getInput(inputName);
if (!input) return;
// Round the value to the requested number of decimal places
const factor = 10 ** decimals;
const roundedValue = Math.round(value * factor) / factor;
let targetBlock = input.connection.targetBlock();
if (!targetBlock) {
// Create a shadow block if none exists
const shadowBlock =
Blockly.getMainWorkspace().newBlock("math_number");
shadowBlock.setFieldValue(String(roundedValue), "NUM");
shadowBlock.setShadow(true);
shadowBlock.setMovable(false);
shadowBlock.setDeletable(false);
shadowBlock.initSvg();
shadowBlock.render();
input.connection.connect(shadowBlock.outputConnection);
} else {
// Set the value if a block is already connected
targetBlock.setFieldValue(String(roundedValue), "NUM");
}
}
setOrCreatePositionInput("X", position.x);
setOrCreatePositionInput("Y", position.y);
setOrCreatePositionInput("Z", position.z);
} catch (e) {
console.warn("Could not set position values for block:", blockType, e);
}
}
}
export function getCanvasXAndCanvasYValues(event, canvasRect) {
return [event.clientX - canvasRect.left, event.clientY - canvasRect.top];
}
export function createBlockForObject(
workspace,
command,
objectName,
pickedPosition,
objectColours,
setPositionValues,
highlightBlockById,
) {
const prevGroup = Blockly.Events.getGroup();
const startTempGroup = !prevGroup;
if (startTempGroup) Blockly.Events.setGroup(true);
const groupId = Blockly.Events.getGroup();
const eventsWereEnabled = Blockly.Events.isEnabled();
if (!eventsWereEnabled) Blockly.Events.enable();
try {
Blockly.Events.setGroup(groupId);
const spec = { type: command, fields: {}, inputs: {} };
if (
command === "load_object" ||
command === "load_multi_object" ||
command === "load_model" ||
command === "load_character"
) {
spec.fields.MODELS = objectName;
}
addXYZShadows(spec, pickedPosition);
addNumberShadow(spec, "SCALE", 1);
if (command === "load_object") {
const configColors = objectColours?.[objectName];
const color = Array.isArray(configColors)
? configColors[0]
: configColors || "#FFD700";
addColourShadowSpec(spec, "COLOR", color, "colour");
}
if (command === "load_multi_object") {
spec.inputs.COLORS = {
shadow: buildColorsListShadowSpec(objectName),
};
}
const block = appendWithUndo(spec, workspace, groupId);
try {
setPositionValues?.(block, pickedPosition, command);
} catch (error) {
console.warn("setPositionValues failed for object block:", error);
}
const startBlock = appendWithUndo({ type: "start" }, workspace, groupId);
const conn = startBlock?.getInput("DO")?.connection;
if (conn && block?.previousConnection) {
try {
conn.connect(block.previousConnection);
} catch (e) {
console.error("connect error:", e);
}
}
try {
highlightBlockById?.(workspace, block);
} catch (error) {
console.warn("highlightBlockById failed for object block:", error);
}
} finally {
if (startTempGroup) Blockly.Events.setGroup(false);
else Blockly.Events.setGroup(prevGroup);
if (!eventsWereEnabled) Blockly.Events.disable();
}
}
/**
* Handles the creation of the "load_character" Blockly block
* and connects it to the 'start' block.
*/
export function createBlockForCharacter(
workspace,
characterName,
pickedPosition,
colorFields,
setPositionValues,
highlightBlockById,
) {
const prevGroup = Blockly.Events.getGroup();
const startTempGroup = !prevGroup;
if (startTempGroup) Blockly.Events.setGroup(true);
const groupId = Blockly.Events.getGroup();
const eventsWereEnabled = Blockly.Events.isEnabled();
if (!eventsWereEnabled) Blockly.Events.enable();
try {
Blockly.Events.setGroup(groupId);
const spec = {
type: "load_character",
fields: { MODELS: characterName },
inputs: {},
};
addPositionShadows(spec, pickedPosition);
addNumberShadow(spec, "SCALE", 1);
if (typeof colorFields === "object" && colorFields) {
for (const [inputName, hex] of Object.entries(colorFields)) {
const shadowType =
inputName === "SKIN_COLOR" ? "skin_colour" : "colour";
addColourShadow(spec, inputName, shadowType, hex);
}
}
const charBlock = appendWithUndo(spec, workspace, groupId);
try {
setPositionValues?.(charBlock, pickedPosition, "load_character");
} catch (error) {
console.warn("setPositionValues failed for character block:", error);
}
const startBlock = appendWithUndo({ type: "start" }, workspace, groupId);
const conn = startBlock?.getInput("DO")?.connection;
if (conn && charBlock?.previousConnection) {
try {
conn.connect(charBlock.previousConnection);
} catch (e) {
console.error(e);
}
}
try {
highlightBlockById?.(workspace, charBlock);
} catch (error) {
console.warn("highlightBlockById failed for character block:", error);
}
} finally {
if (startTempGroup) Blockly.Events.setGroup(false);
else Blockly.Events.setGroup(prevGroup);
if (!eventsWereEnabled) Blockly.Events.disable();
}
}
const roundToOneDecimal = (value) => Math.round(value * 10) / 10;
export function setBlockXYZ(block, x, y, z) {
const setInputValue = (inputName, value) => {
const input = block.getInput(inputName);
if (!input?.connection) return;
const connectedBlock = input.connection.targetBlock();
if (connectedBlock?.getField?.("NUM")) {
connectedBlock.setFieldValue(String(value), "NUM");
}
};
setInputValue("X", roundToOneDecimal(x));
setInputValue("Y", roundToOneDecimal(y));
setInputValue("Z", roundToOneDecimal(z));
}
export function duplicateBlockAndInsert(
originalBlock,
workspace,
pickedPosition,
) {
if (!originalBlock) {
return null;
}
Blockly.Events.setGroup("duplicate");
const blockJson = Blockly.serialization.blocks.save(originalBlock, {
includeShadows: true,
});
if (blockJson.next) {
delete blockJson.next;
}
// A copy of a locked block must come out unlocked/movable.
stripLockState(blockJson);
const duplicateBlock = Blockly.serialization.blocks.append(
blockJson,
workspace,
);
setPositionValues(duplicateBlock, pickedPosition, duplicateBlock.type);
if (originalBlock.nextConnection && duplicateBlock.previousConnection) {
originalBlock.nextConnection.connect(duplicateBlock.previousConnection);
} else {
duplicateBlock.moveBy(50, 50);
}
Blockly.Events.setGroup(false);
// Trigger update of mesh from block values
queueMicrotask(() => {
Blockly.Events.setGroup("duplicate");
const descendants = duplicateBlock.getDescendants(false);
for (const b of descendants) {
const transformChildTypes = ["rotate_to", "resize"];
if (!transformChildTypes.includes(b.type)) continue;
for (const input of b.inputList ?? []) {
const numBlock = input.connection?.targetBlock?.();
if (!numBlock) continue;
if (typeof numBlock.getFieldValue !== "function") continue;
const current = numBlock.getFieldValue("NUM");
if (current === null || current === undefined) continue;
const currNum = Number(current);
const oldValue = Number.isFinite(currNum)
? String(currNum - 1e-6)
: "__refresh_old__";
const newValue = String(current);
Blockly.Events.fire(
new Blockly.Events.BlockChange(
numBlock,
"field",
"NUM",
oldValue,
newValue,
),
);
}
}
Blockly.Events.setGroup(false);
});
duplicateBlock.initSvg();
duplicateBlock.render();
return duplicateBlock;
}
export function findParentWithBlockId(mesh) {
let currentNode = mesh;
while (currentNode) {
if (currentNode.metadata?.blockKey !== undefined) {
return currentNode;
}
currentNode = currentNode.parent;
}
return null;
}
export function calculateYPosition(mesh) {
if (!mesh) return 0;
mesh.computeWorldMatrix?.(true);
mesh.refreshBoundingInfo?.();
const boundingInfo = mesh.getBoundingInfo?.();
const minY = boundingInfo?.boundingBox?.minimumWorld?.y;
return Number.isFinite(minY) ? minY : mesh.position.y;
}
export function setNumberInputs(block, valuesByInputName) {
if (!block || !valuesByInputName) return;
for (const [inputName, value] of Object.entries(valuesByInputName)) {
if (value === undefined || value === null) continue;
const n = Number(value);
if (!Number.isFinite(n)) continue;
const target = block.getInput(inputName)?.connection?.targetBlock?.();
if (!target?.getField?.("NUM")) continue;
target.setFieldValue(String(roundToOneDecimal(n)), "NUM");
}
}
export function getNumberInput(block, inputName) {
const target = block?.getInput(inputName)?.connection?.targetBlock?.();
if (!target?.getFieldValue) return NaN;
const n = Number(target.getFieldValue("NUM"));
return Number.isFinite(n) ? n : NaN;
}