use project name as filename

This commit is contained in:
Reimar 2025-10-13 11:15:49 +02:00
parent 550fd878ef
commit ba3f96ecf6
2 changed files with 23 additions and 15 deletions

View File

@ -12,7 +12,7 @@ import { promptUpload } from "./prompt_upload.js";
import { GamelibCompleter } from "./gamelib_completer.js";
import { TextCompleter } from "./text_completer.js";
import { ConsoleInput } from "./console_input.js";
import { downloadFile } from "./utils.js";
import { downloadFile, slugify } from "./utils.js";
import { HtmlExporter } from "./html_exporter.js";
const playgroundConsole = new PlaygroundConsole(
@ -47,12 +47,6 @@ globalThis.karlkoder = {
},
};
addEventListener("keydown", (ev) => {
if (ev.ctrlKey && ev.key === "s") {
ev.preventDefault();
saveKarlKoder();
}
});
const editor = ace.edit("editor");
editor.setTheme("ace/theme/gruvbox");
@ -130,17 +124,27 @@ runButton.onclick = () => {
exportButton.onclick = async () => {
const html = await htmlExporter.export(projectName.value, editor.getValue());
downloadFile(html, ".html", "text/html");
downloadFile(slugify(projectName.value) || 'project', html, ".html", "text/html");
};
saveButton.onclick = () => {
function saveKarlKoder() {
downloadFile(
slugify(projectName.value) || 'project',
Vermiparous.en(
editor.getValue(),
spriteEditor.sprites,
),
".karlkode",
);
};
}
saveButton.onclick = () => saveKarlKoder();
addEventListener("keydown", (ev) => {
if (ev.ctrlKey && ev.key === "s") {
ev.preventDefault();
saveKarlKoder();
}
});
toggleSpriteEditorButton.addEventListener("click", () => spriteEditor.toggleEditor());

View File

@ -1,14 +1,11 @@
export function downloadFile(content, extension, mime) {
const filename = prompt("Filename?");
if (filename === null) return;
export function downloadFile(name, content, extension, mime) {
const blob = new Blob([content], { type: mime });
const url = URL.createObjectURL(blob);
const element = document.createElement("a");
element.href = url;
element.download = filename.endsWith(extension) ? filename : filename + extension;
element.download = name + extension;
element.style.display = "none";
document.body.appendChild(element);
@ -18,6 +15,13 @@ export function downloadFile(content, extension, mime) {
document.body.removeChild(element);
}
export function slugify(text) {
return text
.split(/\W+/)
.map(word => word.toLowerCase())
.join("-");
}
export function minifyJs(code) {
return code
.replace(/[\s\n]+/g, " ")