chore: sync from Academia-LMS 4eff528
This commit is contained in:
1 parent
dc8475d107
commit
0687af7f92
6 files changed
+896
-2
No files matched your search
Executable
+60
@@ -0,0 +1,60 @@
|
||||
#!/usr/bin/env bash
|
||||
# Builds the embeddable Scratch 3 editor/player into frontend/public/scratch/.
|
||||
# Source of truth for that (git-ignored) folder. Needs node + npm and network access.
|
||||
# Usage: scripts/build-scratch.sh (SCRATCH_BUILD_DIR=<dir> keeps the npm cache between runs,
|
||||
# SCRATCH_OUT=<dir> changes the output folder)
|
||||
set -euo pipefail
|
||||
|
||||
SCRATCH_GUI_VERSION="15.3.0" # @scratch/scratch-gui, AGPL-3.0-only (see docs/scratch-editor.md)
|
||||
SOURCE_URL="https://git.zenvairo.es/dasemu/futuredev-scratch-editor" # public mirror of our wrapper (AGPL §13)
|
||||
|
||||
ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
|
||||
SRC="$ROOT/scripts/scratch"
|
||||
OUT="${SCRATCH_OUT:-$ROOT/frontend/public/scratch}"
|
||||
WORK="${SCRATCH_BUILD_DIR:-$(mktemp -d)}"
|
||||
[ -n "${SCRATCH_BUILD_DIR:-}" ] || trap 'rm -rf "$WORK"' EXIT
|
||||
|
||||
mkdir -p "$WORK"
|
||||
cd "$WORK"
|
||||
[ -f package.json ] || npm init -y >/dev/null
|
||||
npm install --no-audit --no-fund --save-exact --ignore-scripts "@scratch/scratch-gui@$SCRATCH_GUI_VERSION"
|
||||
|
||||
PKG="$WORK/node_modules/@scratch/scratch-gui"
|
||||
DIST="$PKG/dist"
|
||||
[ -f "$DIST/scratch-gui-standalone.js" ] || { echo "standalone bundle missing in package" >&2; exit 1; }
|
||||
|
||||
# Assemble in a temp dir, then swap, so a failed build never leaves a half-written output.
|
||||
mkdir -p "$ROOT/frontend/public"
|
||||
STAGE="$(mktemp -d "$ROOT/frontend/public/.scratch-stage.XXXXXX")"
|
||||
trap 'rm -rf "$STAGE"; [ -n "${SCRATCH_BUILD_DIR:-}" ] || rm -rf "$WORK"' EXIT
|
||||
|
||||
cp "$DIST/scratch-gui-standalone.js" "$DIST/scratch-gui-standalone.js.LICENSE.txt" "$STAGE/"
|
||||
cp "$DIST/extension-worker.js" "$DIST/extension-worker.js.LICENSE.txt" "$STAGE/"
|
||||
cp "$DIST"/*.hex "$STAGE/"
|
||||
cp -r "$DIST/static" "$STAGE/static"
|
||||
mkdir -p "$STAGE/chunks"
|
||||
# Drop face-sensing model files (tens of MB, fetched from the network anyway) and source maps.
|
||||
find "$DIST/chunks" -maxdepth 1 -type f ! -name '*.map' -exec cp {} "$STAGE/chunks/" \;
|
||||
cp "$PKG/LICENSE" "$STAGE/LICENSE-scratch-gui.txt"
|
||||
cp "$PKG/TRADEMARK" "$STAGE/TRADEMARK-scratch.txt"
|
||||
cp "$SRC/index.html" "$SRC/storage-shim.js" "$SRC/bridge.js" "$STAGE/"
|
||||
|
||||
# The bundle is built with webpack publicPath "/" (main bundle and the nested fetch-worker
|
||||
# bundle); it is served from /scratch/, so rewrite every occurrence and fail if none/unknown.
|
||||
BUNDLE="$STAGE/scratch-gui-standalone.js"
|
||||
[ "$(grep -oE '__(nested_)?webpack_require(_[0-9]+)?__\.p="/"' "$BUNDLE" | wc -l)" -ge 1 ] \
|
||||
|| { echo "publicPath marker not found; bundle layout changed" >&2; exit 1; }
|
||||
sed -i -E 's#(__(nested_)?webpack_require(_[0-9]+)?__\.p)="/"#\1="/scratch/"#g' "$BUNDLE"
|
||||
! grep -qE '__(nested_)?webpack_require(_[0-9]+)?__\.p="/"' "$BUNDLE" \
|
||||
|| { echo "publicPath rewrite incomplete" >&2; exit 1; }
|
||||
cat > "$STAGE/SOURCE.txt" <<EOT
|
||||
Built from @scratch/scratch-gui@$SCRATCH_GUI_VERSION (AGPL-3.0-only).
|
||||
Corresponding source: https://github.com/scratchfoundation/scratch-editor (see npm package repository field).
|
||||
Our wrapper and build script (index.html, storage-shim.js, bridge.js, build-scratch.sh), AGPL-3.0:
|
||||
$SOURCE_URL
|
||||
EOT
|
||||
|
||||
rm -rf "$OUT"
|
||||
mv "$STAGE" "$OUT"
|
||||
chmod 755 "$OUT"
|
||||
echo "Scratch built into $OUT ($(du -sh "$OUT" | cut -f1))"
|
||||
@@ -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
|
||||
});
|
||||
})();
|
||||
@@ -0,0 +1,22 @@
|
||||
<!doctype html>
|
||||
<html lang="es">
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||
<meta name="google" content="notranslate">
|
||||
<!-- No external network at runtime: everything must come from this origin. -->
|
||||
<meta http-equiv="Content-Security-Policy"
|
||||
content="default-src 'self'; script-src 'self' 'unsafe-eval' 'wasm-unsafe-eval' blob:; style-src 'self' 'unsafe-inline'; img-src 'self' data: blob:; media-src 'self' data: blob:; font-src 'self' data:; connect-src 'self' data: blob:; worker-src 'self' blob:; object-src 'none'">
|
||||
<title>Scratch</title>
|
||||
<style>
|
||||
html, body { margin: 0; height: 100%; overflow: hidden; }
|
||||
#app { height: 100%; }
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<div id="app"></div>
|
||||
<script src="storage-shim.js"></script>
|
||||
<script src="scratch-gui-standalone.js"></script>
|
||||
<script src="bridge.js"></script>
|
||||
</body>
|
||||
</html>
|
||||
@@ -0,0 +1,28 @@
|
||||
/* Loaded before the GUI bundle (inline scripts are blocked by the CSP). */
|
||||
// Opaque origin (sandboxed iframe): localStorage/sessionStorage/cookies throw SecurityError.
|
||||
// Replace them with in-memory stand-ins before the GUI bundle loads.
|
||||
(function () {
|
||||
function mem() {
|
||||
var d = {};
|
||||
return {
|
||||
getItem: function (k) { return Object.prototype.hasOwnProperty.call(d, k) ? d[k] : null; },
|
||||
setItem: function (k, v) { d[k] = String(v); },
|
||||
removeItem: function (k) { delete d[k]; },
|
||||
clear: function () { d = {}; },
|
||||
key: function (i) { return Object.keys(d)[i] || null; },
|
||||
get length() { return Object.keys(d).length; }
|
||||
};
|
||||
}
|
||||
['localStorage', 'sessionStorage'].forEach(function (name) {
|
||||
var ok = false;
|
||||
try { ok = !!window[name]; } catch (e) { /* SecurityError */ }
|
||||
if (!ok) {
|
||||
try { Object.defineProperty(window, name, { value: mem(), configurable: true }); } catch (e) { /* ignore */ }
|
||||
}
|
||||
});
|
||||
var cookieOk = false;
|
||||
try { cookieOk = typeof document.cookie === 'string'; } catch (e) { /* SecurityError */ }
|
||||
if (!cookieOk) {
|
||||
try { Object.defineProperty(document, 'cookie', { get: function () { return ''; }, set: function () {}, configurable: true }); } catch (e) { /* ignore */ }
|
||||
}
|
||||
})();
|
||||
Reference in new issue
Block a user