fix loading from project file

This commit is contained in:
Reimar 2025-10-14 14:11:59 +02:00
parent 80f2077de3
commit 3c81e53e72
2 changed files with 15 additions and 9 deletions

View File

@ -107,9 +107,15 @@ loadButton.onclick = async () => {
});
const code = items.find((x) => x.tag === "code");
delete code.tag;
assetEditor.importAssets(
assets.map(({ name, mime, content }) => ({ name, mime, bytes: content })),
assets.map(({ name, mime, content }) => {
const file = new File([content], name, { type: mime });
return { name, file };
}),
);
projectName.value = code.name;
const dec = new TextDecoder();
editor.setValue(dec.decode(code.content));
@ -141,13 +147,13 @@ exportButton.onclick = async () => {
downloadFile(slugify(projectName.value) || "project", html, ".html", "text/html");
};
function saveKarlKoder() {
async function saveKarlKoder() {
downloadFile(
slugify(projectName.value) || "project",
KarlkoderCodec.en(
await KarlkoderCodec.en(
projectName.value,
editor.getValue(),
assetEditor.assets,
await assetEditor.getAssets(),
),
".karlkode",
);

View File

@ -13,19 +13,19 @@ function strToBytes(str) {
}
export class KarlkoderCodec {
static en(name, code, assets) {
static async en(name, code, assets) {
const ret = [];
for (const asset of assets) {
ret.push(...strToBytes("asset"));
ret.push(...strToBytes(asset.name.length.toString()));
ret.push(...strToBytes(";"));
ret.push(...strToBytes(asset.mime.length.toString()));
ret.push(...strToBytes(asset.file.type.length.toString()));
ret.push(...strToBytes(";"));
ret.push(...strToBytes(asset.bytes.length.toString()));
ret.push(...strToBytes(asset.file.size.toString()));
ret.push(...strToBytes(";"));
ret.push(...strToBytes(asset.name));
ret.push(...strToBytes(asset.mime));
ret.push(...asset.bytes);
ret.push(...strToBytes(asset.file.type));
ret.push(...await asset.file.bytes());
}
ret.push(...strToBytes("code"));
ret.push(...strToBytes(name.length.toString()));