chore: sync from Academia-LMS 4eff528

This commit is contained in:
dasemu committed 2026-10-28 15:54:35 +01:00
1 parent dc8475d107
commit 0687af7f92
6 files changed
+896 -2

No files matched your search

+97
View File
@@ -0,0 +1,97 @@
/* postMessage bridge between the embedded Scratch GUI and the parent portal page.
* Protocol: see docs/scratch-editor.md. Runs in an opaque origin (sandboxed iframe): peers are identified by window, not origin. */
(function () {
'use strict';
var params = new URLSearchParams(location.search);
var isPlayer = params.get('mode') === 'player';
var CHANGE_DEBOUNCE_MS = 1500;
var vm = null;
var ready = false;
var pendingLoad = null;
var changeTimer = null;
var suppressChanges = 0;
function post(msg, transfer) {
if (window.parent === window) return;
// Opaque origin: the only possible targetOrigin is '*'. Messages carry no secrets.
window.parent.postMessage(msg, '*', transfer || []);
}
function scheduleChanged() {
if (isPlayer || suppressChanges > 0) return;
clearTimeout(changeTimer);
changeTimer = setTimeout(function () { post({ type: 'scratch:changed' }); }, CHANGE_DEBOUNCE_MS);
}
function loadProject(buffer) {
suppressChanges++;
return vm.loadProject(buffer)
.catch(function (err) { console.error('scratch:load failed', err); })
.then(function () {
// PROJECT_CHANGED events may fire right after load; ignore them for a moment.
setTimeout(function () { suppressChanges--; }, 2000);
});
}
// Always hand the parent a plain, transferable ArrayBuffer of this realm.
function toArrayBuffer(x) {
if (x instanceof Blob) return x.arrayBuffer();
if (ArrayBuffer.isView(x)) return new Uint8Array(x.buffer, x.byteOffset, x.byteLength).slice().buffer;
return new Uint8Array(x).slice().buffer;
}
function saveProject(id) {
return vm.saveProjectSb3('arraybuffer').then(toArrayBuffer).then(function (sb3) {
post({ type: 'scratch:saved', id: id, sb3: sb3 }, [sb3]);
}).catch(function (err) {
console.error('scratch:save failed', err);
post({ type: 'scratch:saved', id: id, error: String(err && err.message || err) });
});
}
window.addEventListener('message', function (event) {
if (event.source !== window.parent) return;
var data = event.data;
if (!data || typeof data.type !== 'string') return;
if (data.type === 'scratch:load') {
if (Object.prototype.toString.call(data.sb3) !== '[object ArrayBuffer]') return;
if (ready) loadProject(data.sb3); else pendingLoad = data.sb3;
} else if (data.type === 'scratch:save') {
if (ready) saveProject(data.id);
}
});
function onVmReady() {
if (ready) return;
ready = true;
vm.on('PROJECT_CHANGED', scheduleChanged);
if (pendingLoad) {
var buf = pendingLoad;
pendingLoad = null;
loadProject(buf);
}
post({ type: 'scratch:ready' });
}
var state = new GUI.EditorState({ locale: 'es', isPlayerOnly: isPlayer });
var app = document.getElementById('app');
GUI.setAppElement(app);
var root = GUI.createStandaloneRoot(state, app);
root.render({
projectId: '0', // default (empty) project; the parent replaces it via scratch:load
canSave: false,
canEditTitle: false,
canChangeLanguage: false,
canChangeTheme: false,
canUseCloud: false,
canShare: false,
canRemix: false,
canCreateCopy: false,
backpackVisible: false,
showComingSoon: false,
onVmInit: function (v) { vm = v; },
onProjectLoaded: onVmReady
});
})();