diff --git a/data/static/js/bits/progressive-enhancement.js b/data/static/js/bits/progressive-enhancement.js index 415031c..1ad7822 100644 --- a/data/static/js/bits/progressive-enhancement.js +++ b/data/static/js/bits/progressive-enhancement.js @@ -3,6 +3,10 @@ export const b = { } export function enhance(_, __, el) { + if (el === undefined) { // nothing to enhance but init still runs + return; + } + if (el.classList.contains('js-only')) { el.classList.remove('js-only'); } diff --git a/data/static/js/bits/ui.js b/data/static/js/bits/ui.js index ca27762..70665be 100644 --- a/data/static/js/bits/ui.js +++ b/data/static/js/bits/ui.js @@ -3,6 +3,14 @@ export const b = { init: 'babycodeEditorCharCountInit', } +const getThreadId = () => { + const scheme = window.location.pathname.split("/"); + if (scheme[1] !== 'threads' || scheme[2] === 'new') { + return -1; + } + return parseInt(scheme[2]); +} + export function setTab(_, sender, el) { if (sender.ariaSelected === 'true') { return; @@ -95,10 +103,24 @@ export function babycodeEditorCharCount(evOrPayload, sender, el) { const maxLength = sender.maxLength; const currentLength = sender.value.length; - el.innerText = `${currentLength}/${maxLength}` + el.innerText = `${currentLength}/${maxLength}`; + + const threadId = getThreadId(); + + if (threadId !== -1) { + localStorage.setItem(`thread-${threadId}`, sender.value); + } } export function babycodeEditorCharCountInit(_, __, el) { + if (el === undefined) { // no editors on page + return; + } + + const threadId = getThreadId(); + if (threadId !== -1) { + el.value = localStorage.getItem(`thread-${threadId}`); + } b.send({ sender: el }, 'babycodeEditorCharCount'); }