Cache the thread title as well as the contents

This commit is contained in:
Ben Visness 2021-07-21 20:48:52 -05:00
parent b27c673c15
commit 27b8157a89
1 changed files with 20 additions and 8 deletions

View File

@ -27,7 +27,7 @@
{{ csrftoken .Session }} {{ csrftoken .Session }}
{{ if not (or .PostReplyingTo .IsEditing) }} {{ if not (or .PostReplyingTo .IsEditing) }}
<input class="b w-100 mb1" name="title" type="text" placeholder="Post title..."/> <input id="title" class="b w-100 mb1" name="title" type="text" placeholder="Post title..."/>
{{ end }} {{ end }}
{{/* TODO: Reintroduce the toolbar in a way that makes sense for Markdown */}} {{/* TODO: Reintroduce the toolbar in a way that makes sense for Markdown */}}
{{/* {{/*
@ -151,7 +151,8 @@
<script> <script>
const form = document.querySelector('#form'); const form = document.querySelector('#form');
const tf = document.querySelector('#editor'); const titleField = document.querySelector('#title'); // may be undefined, be careful!
const textField = document.querySelector('#editor');
const preview = document.querySelector('#preview'); const preview = document.querySelector('#preview');
const storagePrefix = 'post-contents'; const storagePrefix = 'post-contents';
@ -178,10 +179,13 @@
// Load any stored content from localStorage // Load any stored content from localStorage
const storageKey = `${storagePrefix}/${window.location.host}${window.location.pathname}`; const storageKey = `${storagePrefix}/${window.location.host}${window.location.pathname}`;
const storedContents = window.localStorage.getItem(storageKey); const storedContents = window.localStorage.getItem(storageKey);
if (storedContents && !tf.value) { if (storedContents && !textField.value) {
try { try {
const { contents } = JSON.parse(storedContents); const { title, contents } = JSON.parse(storedContents);
tf.value = contents; if (titleField) {
titleField.value = title;
}
textField.value = contents;
} catch (e) { } catch (e) {
console.error(e); console.error(e);
} }
@ -197,16 +201,24 @@
}; };
function doMarkdown() { function doMarkdown() {
const md = tf.value; const md = textField.value;
previewWorker.postMessage(md); previewWorker.postMessage(md);
updateContentCache();
}
function updateContentCache() {
window.localStorage.setItem(storageKey, JSON.stringify({ window.localStorage.setItem(storageKey, JSON.stringify({
when: new Date().getTime(), when: new Date().getTime(),
contents: md, title: titleField ? titleField.value : '',
contents: textField.value,
})); }));
} }
doMarkdown(); doMarkdown();
tf.addEventListener('input', () => doMarkdown()); textField.addEventListener('input', () => doMarkdown());
if (titleField) {
titleField.addEventListener('input', () => updateContentCache());
}
form.addEventListener('submit', e => { form.addEventListener('submit', e => {
window.localStorage.removeItem(storageKey); window.localStorage.removeItem(storageKey);