From fb7ad061374085aa50ef8347f8d15fcca215b40a Mon Sep 17 00:00:00 2001 From: kj_sh604 Date: Thu, 12 Mar 2026 23:41:39 -0400 Subject: feat: form state --- src/static/main.js | 12 ++++++++++++ 1 file changed, 12 insertions(+) (limited to 'src/static') diff --git a/src/static/main.js b/src/static/main.js index 0f77b1e..9d217cb 100644 --- a/src/static/main.js +++ b/src/static/main.js @@ -4,6 +4,10 @@ const markdownInput = document.getElementById("markdown"); const imageInput = document.getElementById("image"); const mdFileInput = document.getElementById("md-file"); + +// stack overflow session state js +const convertForm=document.getElementById("convert-form"),PERSISTENCE_KEY="likha-pdf:form-state:v1";function readPersistedState(){try{const e=localStorage.getItem(PERSISTENCE_KEY);return e?JSON.parse(e):null}catch{return null}}function writePersistedState(e){try{localStorage.setItem(PERSISTENCE_KEY,JSON.stringify(e))}catch{}}function collectFormState(){if(!(convertForm instanceof HTMLFormElement))return;const e={},t=Array.from(convertForm.elements);for(const n of t){if(!(n instanceof HTMLElement))continue;const t=n.getAttribute("name");t&&"markdown"!==t&&(n instanceof HTMLInputElement?"radio"===n.type?n.checked&&(e[t]=n.value):"checkbox"===n.type&&(e[t]=n.checked):n instanceof HTMLSelectElement&&(e[t]=n.value))}writePersistedState({markdown:markdownInput?.value??"",fields:e})}function restoreFormState(){if(!(convertForm instanceof HTMLFormElement))return;const e=readPersistedState();if(!e||"object"!=typeof e)return;markdownInput&&"string"==typeof e.markdown&&(markdownInput.value=e.markdown,markdownInput.readOnly=!1),imageInput&&(imageInput.disabled=!1),uploadButton&&(uploadButton.disabled=!1);const t=e.fields;if(t&&"object"==typeof t)for(const[e,n]of Object.entries(t)){const t=convertForm.elements.namedItem(e);t&&(t instanceof RadioNodeList?t.value=String(n):t instanceof HTMLElement&&(t instanceof HTMLInputElement&&"checkbox"===t.type?t.checked=Boolean(n):(t instanceof HTMLInputElement||t instanceof HTMLSelectElement)&&(t.value=String(n))))}}restoreFormState(); + document.body.addEventListener("htmx:beforeRequest", (event) => { const elt = event.detail?.elt; if (!elt) { @@ -38,6 +42,11 @@ document.body.addEventListener("htmx:afterRequest", (event) => { } }); +if (convertForm instanceof HTMLFormElement) { + convertForm.addEventListener("input", collectFormState); + convertForm.addEventListener("change", collectFormState); +} + document.body.addEventListener("htmx:afterSwap", (event) => { const target = event.detail?.target; const requestElt = event.detail?.requestConfig?.elt; @@ -70,6 +79,7 @@ if (mdFileInput) { if (uploadButton) { uploadButton.disabled = true; } + collectFormState(); }; reader.readAsText(file); } else { @@ -83,6 +93,7 @@ if (mdFileInput) { if (uploadButton) { uploadButton.disabled = false; } + collectFormState(); } }); } @@ -107,4 +118,5 @@ document.body.addEventListener("click", (event) => { const prefix = needsLeadingNewline ? "\n" : ""; markdownInput.value += `${prefix}${snippet}\n`; markdownInput.focus(); + collectFormState(); }); -- cgit v1.2.3