Add WordCraft and DeckCraft office editors with protected server saves
This commit is contained in:
1 parent
bc9709e500
commit
6e36b6c4b7
20 files changed
+582
-40
No files matched your search
@@ -0,0 +1,108 @@
|
||||
// Adapter for official office builds without a native parent-host protocol.
|
||||
// Loaded before WASM starts; vendor files on disk remain checksum-verifiable.
|
||||
(() => {
|
||||
"use strict";
|
||||
const segments = location.pathname.split("/");
|
||||
const id = segments[segments.lastIndexOf("addons") + 1];
|
||||
if (!["wordcraft", "deckcraft"].includes(id) || parent === window || new URLSearchParams(location.search).get("host") !== "parent") return;
|
||||
const extension = id === "wordcraft" ? ".docx" : ".pptx";
|
||||
const origin = location.origin;
|
||||
const limit = 64 * 1024 * 1024;
|
||||
const jobs = new Map();
|
||||
const blobs = new Map();
|
||||
let opened = false, dirty = false, revision = 0, sequence = 0;
|
||||
const send = (type, data = {}, transfer = []) => parent.postMessage({ type: id + ":" + type, ...data }, origin, transfer);
|
||||
const managed = name => typeof name === "string" && (name.toLowerCase().endsWith(extension) || name.toLowerCase().endsWith(".pdf"));
|
||||
const markDirty = () => { dirty = true; revision++; send("dirty", { dirty: true }); };
|
||||
const createURL = URL.createObjectURL.bind(URL), revokeURL = URL.revokeObjectURL.bind(URL);
|
||||
URL.createObjectURL = blob => { const url = createURL(blob); blobs.set(url, blob); return url; };
|
||||
URL.revokeObjectURL = url => { blobs.delete(url); revokeURL(url); };
|
||||
async function save(name, blob) {
|
||||
if (blob.size > limit) throw new Error("Document exceeds 64 MiB");
|
||||
// Bound in-flight copies even if the user repeatedly clicks Save.
|
||||
if (jobs.size >= 2) throw new Error("A save is already pending");
|
||||
const requestId = String(++sequence), savedRevision = revision;
|
||||
dirty = true;
|
||||
send("dirty", { dirty: true });
|
||||
return new Promise((resolve, reject) => {
|
||||
jobs.set(requestId, { resolve, reject, revision: savedRevision });
|
||||
blob.arrayBuffer().then(bytes => {
|
||||
send("save", { name, bytes, requestId }, [bytes]);
|
||||
}).catch(error => { jobs.delete(requestId); reject(error); });
|
||||
});
|
||||
}
|
||||
const click = HTMLAnchorElement.prototype.click;
|
||||
HTMLAnchorElement.prototype.click = function () {
|
||||
const blob = blobs.get(this.href);
|
||||
if (blob && managed(this.download)) {
|
||||
save(this.download, blob).catch(error => send("failed", { error: String(error) }));
|
||||
return;
|
||||
}
|
||||
return click.call(this);
|
||||
};
|
||||
const picker = window.showSaveFilePicker?.bind(window);
|
||||
window.showSaveFilePicker = async options => {
|
||||
const name = options?.suggestedName;
|
||||
if (!managed(name)) {
|
||||
if (picker) return picker(options);
|
||||
throw new DOMException("Use browser download for this format", "NotSupportedError");
|
||||
}
|
||||
return {
|
||||
kind: "file", name,
|
||||
async createWritable() {
|
||||
let chunks = [], size = 0;
|
||||
return {
|
||||
async write(data) {
|
||||
// This build writes sequential Uint8Array chunks, never random-access data.
|
||||
const chunk = new Blob([data]);
|
||||
if (size + chunk.size > limit) throw new Error("Document exceeds 64 MiB");
|
||||
size += chunk.size; chunks.push(chunk);
|
||||
},
|
||||
async close() { const blob = new Blob(chunks); chunks = []; await save(name, blob); },
|
||||
async abort() { chunks = []; }
|
||||
};
|
||||
}
|
||||
};
|
||||
};
|
||||
window.addEventListener("message", event => {
|
||||
if (event.source !== parent || event.origin !== origin || !event.data) return;
|
||||
const message = event.data;
|
||||
if (message.type === id + ":open" && !opened && message.bytes instanceof ArrayBuffer && message.bytes.byteLength <= limit && typeof message.name === "string" && message.name.toLowerCase().endsWith(extension)) {
|
||||
const canvas = document.getElementById(id + "_canvas");
|
||||
if (!canvas) { send("failed", { error: "Editor canvas unavailable" }); return; }
|
||||
opened = true;
|
||||
const transfer = new DataTransfer();
|
||||
transfer.items.add(new File([message.bytes], message.name));
|
||||
canvas.dispatchEvent(new DragEvent("drop", { bubbles: true, cancelable: true, dataTransfer: transfer }));
|
||||
canvas.focus();
|
||||
} else if (message.type === id + ":saved" || message.type === id + ":save-error") {
|
||||
const job = jobs.get(message.requestId);
|
||||
if (!job) return;
|
||||
jobs.delete(message.requestId);
|
||||
if (message.type === id + ":saved") {
|
||||
if (revision === job.revision && jobs.size === 0) { dirty = false; send("dirty", { dirty: false }); }
|
||||
job.resolve();
|
||||
} else job.reject(new Error("Server could not save the document; use the recovery controls"));
|
||||
}
|
||||
});
|
||||
// Conservative fallback for builds without dirty-state notifications. Only real
|
||||
// editing input counts; synthetic file loading and ordinary pointer movement don't.
|
||||
window.addEventListener("keydown", event => {
|
||||
if (!opened || !event.isTrusted) return;
|
||||
if ((event.ctrlKey || event.metaKey) && ["s", "c", "a", "f", "p"].includes(event.key.toLowerCase())) return;
|
||||
if (event.key.length === 1 || ["Backspace", "Delete", "Enter"].includes(event.key)) markDirty();
|
||||
}, true);
|
||||
window.addEventListener("pointerdown", event => { if (opened && event.isTrusted) markDirty(); }, true);
|
||||
window.addEventListener("beforeunload", event => { if (dirty || jobs.size) event.preventDefault(); });
|
||||
function ready() {
|
||||
const loading = document.getElementById(id + "_loading");
|
||||
if (loading) {
|
||||
if (/failed to start/i.test(loading.textContent || "")) { send("failed", { error: loading.textContent }); observer.disconnect(); }
|
||||
return;
|
||||
}
|
||||
if (document.getElementById(id + "_canvas")) { observer.disconnect(); send("ready"); }
|
||||
}
|
||||
const observer = new MutationObserver(ready);
|
||||
observer.observe(document.documentElement, { childList: true, subtree: true, characterData: true });
|
||||
ready();
|
||||
})();
|
||||
Reference in new issue
Block a user