import { api } from "./api.js?v=0.6.0"; function slugify(value, fallback) { return value.toLowerCase().normalize("NFKD").replace(/[\u0300-\u036f]/g, "").replace(/[^a-z0-9]+/g, "-").replace(/^-|-$/g, "") || fallback; } function bindPreview(inputId, previewId, countId, prefix, fallback) { const input = document.querySelector(inputId); const preview = document.querySelector(previewId); const count = document.querySelector(countId); const update = () => { count.textContent = `${input.value.length}/80`; preview.textContent = `${prefix}${slugify(input.value, fallback)}`; }; input.addEventListener("input", update); update(); } function setBusy(button, busy, idleText, busyText) { button.disabled = busy; button.textContent = busy ? busyText : idleText; } bindPreview("#pad-name", "#pad-slug-preview", "#pad-name-count", "/p/", "notatka"); bindPreview("#workspace-name", "#workspace-slug-preview", "#workspace-name-count", "/w/", "workspace"); document.querySelectorAll(".password-toggle").forEach((button) => { button.addEventListener("click", () => { const input = document.getElementById(button.dataset.target); const show = input.type === "password"; input.type = show ? "text" : "password"; button.textContent = show ? "Ukryj" : "Pokaż"; }); }); document.querySelector("#pad-form").addEventListener("submit", async (event) => { event.preventDefault(); const name = document.querySelector("#pad-name"); const password = document.querySelector("#pad-password"); const button = document.querySelector("#pad-button"); const error = document.querySelector("#pad-error"); error.textContent = ""; setBusy(button, true, "Utwórz notatkę", "Tworzenie…"); try { const payload = { name: name.value.trim() }; if (password.value) payload.password = password.value; const result = await api("/api/pads", { method: "POST", body: JSON.stringify(payload) }); if (password.value) sessionStorage.setItem(`rustpad:pad:${result.slug}:password`, password.value); window.location.assign(`${result.url}?view=split&mode=markdown`); } catch (requestError) { error.textContent = requestError.message; } finally { setBusy(button, false, "Utwórz notatkę", "Tworzenie…"); } }); document.querySelector("#workspace-form").addEventListener("submit", async (event) => { event.preventDefault(); const name = document.querySelector("#workspace-name"); const password = document.querySelector("#workspace-password"); const button = document.querySelector("#workspace-button"); const error = document.querySelector("#workspace-error"); error.textContent = ""; setBusy(button, true, "Utwórz workspace", "Tworzenie…"); try { const payload = { name: name.value.trim() }; if (password.value) payload.password = password.value; const result = await api("/api/workspaces", { method: "POST", body: JSON.stringify(payload) }); if (password.value) sessionStorage.setItem(`rustpad:workspace:${result.slug}:password`, password.value); window.location.assign(result.url); } catch (requestError) { error.textContent = requestError.message; } finally { setBusy(button, false, "Utwórz workspace", "Tworzenie…"); } });