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

View File

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