Repository navigation
Expand file tree
/
Copy pathmain.js
More file actions
508 lines (442 loc) · 21.5 KB
/
Copy pathmain.js
File metadata and controls
508 lines (442 loc) · 21.5 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
45
8000
5
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
// Flock - Creative coding in 3D
// Dr Tracy Gardner - https://github.com/tracygardner
// Flip Computing Limited - flipcomputing.com
import * as Blockly from "blockly";
import "@babylonjs/core/Debug/debugLayer";
import "@babylonjs/inspector";
import { flock, initializeFlock } from "../flock.js";
import { initializeVariableIndexes } from "../blocks";
import { meshMap } from "../generators";
import { enableGizmos } from "../ui/gizmos.js";
import { executeCode, stopCode } from "./execution.js";
import "../ui/addmeshes.js";
import "../ui/colourpicker-compact.js";
import {
initializeBlocks,
initializeWorkspace,
createBlocklyWorkspace,
overrideSearchPlugin,
workspace,
} from "./blocklyinit.js";
import {
saveWorkspace,
loadWorkspace,
exportCode,
setupFileInput,
loadExampleWrapper,
newProject,
importSnippet,
} from "./files.js";
import {
onResize,
toggleDesignMode,
togglePlayMode,
initializeUI,
switchView,
} from "./view.js";
import { hideLoadingScreen } from "./loading.js";
import "./debug.js";
import { initializeBlockHandling } from "./blockhandling.js";
import { setupInput } from "./input.js";
import { addExportContextMenuOptions } from "./export.js";
import {
setLanguage,
initializeLanguageMenu,
initializeSavedLanguage,
} from "./translation.js";
if ("serviceWorker" in navigator) {
navigator.serviceWorker
.register("./sw.js")
.then((registration) => {
console.log("Service Worker registered:", registration);
// Check for updates to the Service Worker
registration. => {
const newWorker = registration.installing;
if (newWorker) {
newWorker. => {
if (
newWorker.state ===
"installed"
) {
// If the old Service Worker is controlling the page
if (
navigator
.serviceWorker
.controller
) {
// Notify the user about the update
console.log(
"New update available",
);
showUpdateNotification();
}
}
};
}
};
})
.catch((error) => {
console.error(
"Service Worker registration failed:",
error,
);
});
}
async function showUpdateNotification() {
const notification = document.createElement("div");
notification.innerHTML = `
<div style="position: fixed; bottom: 0; left: 0; width: 100%; background: #511D91; color: white; text-align: center; padding: 10px; z-index: 1000;">
<span data-i18n="update_available">A new version of Flock is available.</span> <button id="reload-btn" style="background: white; color: #511D91; padding: 5px 10px; border: none; cursor: pointer;" data-i18n="reload_button">Reload</button>
</div>
`;
document.body.appendChild(notification);
// Apply translations to the new elements
const { applyTranslations } = await import("./translation.js");
applyTranslations();
document.getElementById("reload-btn").addEventListener("click", () => {
// Reload the page to activate the new service worker
window.location.reload();
});
}
console.log("Blockly version:", Blockly.VERSION);
function initializeApp() {
console.log("Initializing app...");
(() => {
const ws = () => Blockly.getMainWorkspace?.();
const flyout = () => ws()?.getToolbox?.()?.getFlyout?.();
const isSearchCategorySelected = () => {
const sel = document.querySelector(
".blocklyToolboxDiv .blocklyToolboxCategory.blocklyToolboxSelected",
);
return !!sel?.querySelector('input[type="search"]');
};
const clickIsInsideToolboxOrFlyout = (el) =>
!!el.closest(".blocklyToolboxDiv, .blocklyFlyout");
// Close search flyout on outside clicks *only when* search is the selected category.
const => {
if (!isSearchCategorySelected()) return; // only for search
if (clickIsInsideToolboxOrFlyout(e.target)) return; // ignore toolbox/flyout clicks
flyout()?.hide?.();
};
// Capture so we run even if something stops propagation later.
window.addEventListener("pointerdown", onOutside, {
capture: true,
});
window.addEventListener("click", onOutside, { capture: true });
})();
const observer = new MutationObserver((mutations) => {
const unmuteButton = document.getElementById(
"babylonUnmuteButton",
);
if (unmuteButton && !unmuteButton.getAttribute("aria-label")) {
unmuteButton.setAttribute("aria-label", "Unmute audio");
observer.disconnect(); // Stop observing once we've found it
}
});
observer.observe(document.body, {
childList: true,
subtree: true,
});
// Add event listeners for menu buttons and controls
const runCodeButton = document.getElementById("runCodeButton");
const toggleDesignButton = document.getElementById("toggleDesign");
const togglePlayButton = document.getElementById("togglePlay");
const stopCodeButton = document.getElementById("stopCodeButton");
const fileInput = document.getElementById("fileInput");
const exportCodeButton = document.getElementById("exportCodeButton");
const openButton = document.getElementById("openButton");
const menuButton = document.getElementById("menuBtn");
runCodeButton.addEventListener("click", executeCode);
stopCodeButton.addEventListener("click", stopCode);
exportCodeButton.addEventListener("click", exportCode);
// Make open button work with keyboard
openButton.addEventListener("click", () => {
fileInput.click();
});
openButton.addEventListener("keydown", (event) => {
if (event.key === "Enter" || event.key === " ") {
event.preventDefault();
fileInput.click();
}
});
// Enable the file input after initialization
fileInput.removeAttribute("disabled");
// keydown event listener
document.addEventListener("keydown", function (e) {
// Avoid in inputs
const tag = (e.target.tagName || "").toLowerCase();
if (
tag === "input" ||
tag === "textarea" ||
e.target.isContentEditable
)
return;
// Check for modifier key (Ctrl on Windows/Linux, Cmd on Mac)
if (!(e.ctrlKey || e.metaKey)) return;
const key = e.key.toLowerCase();
switch (key) {
case "o": // Ctrl+O - Open file
e.preventDefault();
document.getElementById("fileInput").click();
break;
case "s": // Ctrl+S - Save/Export
e.preventDefault();
exportCode(workspace); // Or saveWorkspace(workspace) for autosave
break;
case "p": // Ctrl+P - Execute code
e.preventDefault();
if (typeof executeCode === "function") {
executeCode();
} else {
console.warn(
"executeCode is not defined.",
);
}
break;
case "/": // Ctrl+/ - Toggle info details
e.preventDefault();
const infoSummary = document.querySelector(
"#info-details summary",
);
if (infoSummary) {
infoSummary.click(); // Simulate a click to toggle details
infoSummary.focus(); // Move focus to the summary
}
break;
case "m": // Ctrl+M - Open menu
e.preventDefault();
menuButton.click(); // Simulate click to open the menu
// Focus the first menu item
const menuDropdown =
document.getElementById("menuDropdown");
const firstMenuItem = menuDropdown
? menuDropdown.querySelector("li")
: null;
if (firstMenuItem) {
firstMenuItem.focus(); // Set focus on the first item
}
break;
case "g": // Ctrl+G - Focus shapes button
e.preventDefault();
const btn =
document.getElementById(
"showShapesButton",
);
if (
btn &&
!btn.disabled &&
btn.offsetParent !== null
) {
btn.focus();
}
break;
/* Uncomment if needed:
case "k": // Ctrl+K - Stop code
e.preventDefault();
// Force any focused element to blur, so pending changes are committed
if (document.activeElement && typeof document.activeElement.blur === "function") {
document.activeElement.blur();
}
// Give the browser a tick to finish handling blur before continuing
setTimeout(() => {
document.getElementById("stopCodeButton").click();
}, 0);
break;
*/
}
});
toggleDesignButton.addEventListener("click", toggleDesignMode);
togglePlayButton.addEventListener("click", togglePlayMode);
document.getElementById("fullscreenToggle").addEventListener(
"click",
function () {
if (!document.fullscreenElement) {
// Go fullscreen
if (
document.documentElement
.requestFullscreen
) {
document.documentElement.requestFullscreen();
} else if (
document.documentElement
.mozRequestFullScreen
) {
/* Firefox */
document.documentElement.mozRequestFullScreen();
} else if (
document.documentElement
.webkitRequestFullscreen
) {
/* Chrome, Safari & Opera */
document.documentElement.webkitRequestFullscreen();
} else if (
document.documentElement
.msRequestFullscreen
) {
/* IE/Edge */
document.documentElement.msRequestFullscreen();
}
} else {
// Exit fullscreen
if (document.exitFullscreen) {
document.exitFullscreen();
} else if (document.mozCancelFullScreen) {
/* Firefox */
document.mozCancelFullScreen();
} else if (document.webkitExitFullscreen) {
/* Chrome, Safari & Opera */
document.webkitExitFullscreen();
} else if (document.msExitFullscreen) {
/* IE/Edge */
document.msExitFullscreen();
}
}
},
);
document.getElementById("project-new").addEventListener(
"click",
function (e) {
e.preventDefault();
newProject();
document.getElementById("menuDropdown").classList.add(
"hidden",
);
},
);
document.getElementById("project-open").addEventListener(
"click",
function (e) {
e.preventDefault();
fileInput.click();
document.getElementById("menuDropdown").classList.add(
"hidden",
);
},
);
document.getElementById("project-save").addEventListener(
"click",
function (e) {
e.preventDefault();
exportCode();
document.getElementById("menuDropdown").classList.add(
"hidden",
);
},
);
initializeUI();
console.log("Enabling gizmos");
enableGizmos();
// Enable gizmo buttons
const exampleSelect = document.getElementById("exampleSelect");
const fullscreenToggle = document.getElementById("fullscreenToggle");
//toolboxControl.removeAttribute("disabled");
runCodeButton.removeAttribute("disabled");
exampleSelect.removeAttribute("disabled");
fullscreenToggle.removeAttribute("disabled");
// Add event listeners for buttons and controls
/*toolboxControl.addEventListener("mouseover", function () {
toolboxControl.style.cursor = "pointer";
toggleToolbox();
});*/
exampleSelect.addEventListener("change", loadExampleWrapper);
// Make setLanguage available globally for the menu
window.setLanguage = async (lang) => await setLanguage(lang);
// Initialize language menu
initializeLanguageMenu();
}
window. function () {
// Resize Blockly workspace and Babylon.js canvas when the window is resized
window.addEventListener("resize", onResize);
switchView("both");
const scriptElement = document.getElementById("flock");
if (scriptElement) {
initializeFlock();
console.log("Standalone Flock");
// Hide loading screen after a short delay for standalone flock
setTimeout(hideLoadingScreen, 1000);
return; // standalone flock
}
initializeBlocks();
// Initialize Blockly and add custom context menu options
addExportContextMenuOptions();
createBlocklyWorkspace();
initializeWorkspace();
overrideSearchPlugin(workspace);
initializeBlockHandling();
//Blockly.ContextMenuItems.registerCommentOptions();
/*const navigationController = new NavigationController();
navigationController.init();
navigationController.addWorkspace(workspace);*/
// Turns on keyboard navigation.
//keyboardNav = new KeyboardNavigation(workspace);
console.log("Welcome to Flock 🐑🐑🐑");
// Call this function to autosave periodically
setInterval(() => saveWorkspace(workspace), 30000); // Autosave every 30 seconds
(async () => {
await flock.initialize();
// Hide loading screen once Flock is fully initialized
setTimeout(hideLoadingScreen, 500);
})();
//workspace.getToolbox().setVisible(false);
workspace.addChangeListener(function (event) {
if (event.type === Blockly.Events.FINISHED_LOADING) {
initializeVariableIndexes();
window.loadingCode = false;
}
});
document.getElementById("info-details").addEventListener(
"toggle",
function (e) {
if (this.open) {
setTimeout(() => {
const content =
this.querySelector(".content");
if (content) {
content.setAttribute(
"tabindex",
"0",
); // Make it focusable
content.focus();
}
}, 10);
} else {
const content = this.querySelector(".content");
if (content) {
content.setAttribute("tabindex", "-1"); // Remove from tab order when closed
}
}
},
);
// Initial view setup
window.loadingCode = true;
// Initialize saved language before loading workspace
await initializeSavedLanguage();
// Refresh toolbox to ensure categories are translated after language initialization
const toolboxElement = document.getElementById("toolbox");
if (toolboxElement) {
workspace.updateToolbox(toolboxElement);
} else {
// If no toolbox element, import the toolbox configuration
const { toolbox } = await import("../toolbox.js");
workspace.updateToolbox(toolbox);
}
initializeApp();
setupFileInput(workspace, executeCode);
setupInput();
loadWorkspace(workspace, executeCode);
// Add event listener for keyboard shortcuts
document.addEventListener("keydown", function (event) {
// Check if Ctrl+Shift+2 is pressed
if (
event.ctrlKey &&
event.shiftKey &&
event.code === "Digit2"
) {
console.log("Exporting GLB of scene");
event.preventDefault(); // Prevent default browser behavior
// Call the exportMesh function with specified parameters
flock.exportMesh("scene", "GLB");
console.log("Export triggered: scene as GLB format");
}
});
};