feat: improve ArtCraft integration and configurable file opening
This commit is contained in:
1 parent
3bc62d2b69
commit
80bc2c458d
60 files changed
+1738
-232
No files matched your search
+34
-13
@@ -9,7 +9,7 @@
|
||||
if (!config || config.id !== id || parent === window || new URLSearchParams(location.search).get("host") !== "parent") return;
|
||||
const accepts = (name, list) => typeof name === "string" && list.some(extension => name.toLowerCase().endsWith(extension));
|
||||
const origin = location.origin;
|
||||
const limit = 64 * 1024 * 1024;
|
||||
const limit = 256 * 1024 * 1024;
|
||||
const jobs = new Map();
|
||||
const blobs = new Map();
|
||||
let opened = false, dirty = false, revision = 0, sequence = 0;
|
||||
@@ -19,8 +19,8 @@
|
||||
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");
|
||||
async function save(name, blob, saveAs = false) {
|
||||
if (blob.size > limit) throw new Error("Document exceeds 256 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;
|
||||
@@ -29,7 +29,7 @@
|
||||
return new Promise((resolve, reject) => {
|
||||
jobs.set(requestId, { resolve, reject, revision: savedRevision });
|
||||
blob.arrayBuffer().then(bytes => {
|
||||
send("save", { name, bytes, requestId }, [bytes]);
|
||||
send("save", { name, bytes, requestId, saveAs }, [bytes]);
|
||||
}).catch(error => { jobs.delete(requestId); reject(error); });
|
||||
});
|
||||
}
|
||||
@@ -51,17 +51,36 @@
|
||||
}
|
||||
return {
|
||||
kind: "file", name,
|
||||
queryPermission: async () => "granted",
|
||||
requestPermission: async () => "granted",
|
||||
async createWritable() {
|
||||
let chunks = [], size = 0;
|
||||
let content = new Blob(), position = 0;
|
||||
const offset = value => {
|
||||
if (!Number.isSafeInteger(value) || value < 0 || value > limit) throw new RangeError("Document exceeds 256 MiB or invalid position");
|
||||
return value;
|
||||
};
|
||||
const seek = async value => { position = offset(value); };
|
||||
const truncate = async value => {
|
||||
const size = offset(value);
|
||||
content = size <= content.size ? content.slice(0, size) : new Blob([content, new Uint8Array(size - content.size)]);
|
||||
position = Math.min(position, size);
|
||||
};
|
||||
return {
|
||||
async write(data) {
|
||||
// This build writes sequential Uint8Array chunks, never random-access data.
|
||||
if (data && typeof data === "object" && ["write", "seek", "truncate"].includes(data.type)) {
|
||||
if (data.type === "seek") return seek(data.position);
|
||||
if (data.type === "truncate") return truncate(data.size);
|
||||
if (data.position !== undefined) await seek(data.position);
|
||||
data = data.data;
|
||||
}
|
||||
const chunk = new Blob([data]);
|
||||
if (size + chunk.size > limit) throw new Error("Document exceeds 64 MiB");
|
||||
size += chunk.size; chunks.push(chunk);
|
||||
if (position + chunk.size > limit) throw new Error("Document exceeds 256 MiB");
|
||||
content = new Blob([content.slice(0, position), ...(position > content.size ? [new Uint8Array(position - content.size)] : []), chunk, content.slice(position + chunk.size)]);
|
||||
position += chunk.size;
|
||||
},
|
||||
async close() { const blob = new Blob(chunks); chunks = []; await save(name, blob); },
|
||||
async abort() { chunks = []; }
|
||||
seek, truncate,
|
||||
async close() { const blob = content; content = new Blob(); await save(name, blob, true); },
|
||||
async abort() { content = new Blob(); }
|
||||
};
|
||||
}
|
||||
};
|
||||
@@ -99,19 +118,21 @@
|
||||
window.addEventListener("pointerdown", event => { if (opened && event.isTrusted) markDirty(); }, true);
|
||||
window.addEventListener("beforeunload", event => { if (dirty || jobs.size) event.preventDefault(); });
|
||||
let announced = false;
|
||||
let domReady = document.readyState !== "loading";
|
||||
const reportFailure = error => send("failed", { error: String(error?.message || error || "Editor failed to start") });
|
||||
window.addEventListener("error", event => { if (!announced) reportFailure(event.error || event.message); });
|
||||
window.addEventListener("unhandledrejection", event => { if (!announced) reportFailure(event.reason); });
|
||||
function ready() {
|
||||
if (announced) return;
|
||||
if (announced || !domReady) return;
|
||||
const loading = document.getElementById(id + "_loading");
|
||||
if (loading) {
|
||||
if (loading && loading.textContent.trim() && !loading.hidden && loading.style.display !== "none" && (!window.getComputedStyle || getComputedStyle(loading).display !== "none")) {
|
||||
if (/failed to start/i.test(loading.textContent || "")) { send("failed", { error: loading.textContent }); observer.disconnect(); }
|
||||
return;
|
||||
}
|
||||
if (document.getElementById(id + "_canvas")) { announced = true; observer.disconnect(); send("ready"); }
|
||||
}
|
||||
const observer = new MutationObserver(ready);
|
||||
observer.observe(document.documentElement, { childList: true, subtree: true, characterData: true });
|
||||
window.addEventListener("DOMContentLoaded", () => { domReady = true; ready(); }, { once: true });
|
||||
observer.observe(document.documentElement, { childList: true, subtree: true, characterData: true, attributes: true, attributeFilter: ["style", "class", "hidden"] });
|
||||
ready();
|
||||
})();
|
||||
Reference in new issue
Block a user