From 03b608565dcc12c199ce8f2c009cf4743475f207 Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Wed, 8 Sep 2021 13:58:10 -0700 Subject: [PATCH] Save status drafts in localstorage --- bookwyrm/static/js/status_cache.js | 40 +++++++++++++++++++ bookwyrm/templates/layout.html | 1 + .../snippets/create_status/content_field.html | 3 +- 3 files changed, 43 insertions(+), 1 deletion(-) create mode 100644 bookwyrm/static/js/status_cache.js diff --git a/bookwyrm/static/js/status_cache.js b/bookwyrm/static/js/status_cache.js new file mode 100644 index 000000000..9d86c313f --- /dev/null +++ b/bookwyrm/static/js/status_cache.js @@ -0,0 +1,40 @@ +/* exported StatusCache */ +/* globals BookWyrm */ + +let StatusCache = new class { + constructor() { + document.querySelectorAll('[data-cache-draft]') + .forEach(t => t.addEventListener('change', this.updateDraft.bind(this))); + document.querySelectorAll('[data-cache-draft]') + .forEach(t => this.populateDraft(t)); + } + + /** + * Update localStorage copy of drafted status + * + * @param {Event} event + * @return {undefined} + */ + updateDraft(event) { + // Used in set reading goal + let key = event.target.dataset.cacheDraft; + let value = event.target.value; + + window.localStorage.setItem(key, value); + } + + /** + * Toggle display of a DOM node based on its value in the localStorage. + * + * @param {object} node - DOM node to toggle. + * @return {undefined} + */ + populateDraft(node) { + // Used in set reading goal + let key = node.dataset.cacheDraft; + let value = window.localStorage.getItem(key); + + node.value = value; + } +}(); + diff --git a/bookwyrm/templates/layout.html b/bookwyrm/templates/layout.html index 43ca81c74..e855bf600 100644 --- a/bookwyrm/templates/layout.html +++ b/bookwyrm/templates/layout.html @@ -251,6 +251,7 @@ + {% block scripts %}{% endblock %} diff --git a/bookwyrm/templates/snippets/create_status/content_field.html b/bookwyrm/templates/snippets/create_status/content_field.html index 907467362..c2b383b9a 100644 --- a/bookwyrm/templates/snippets/create_status/content_field.html +++ b/bookwyrm/templates/snippets/create_status/content_field.html @@ -10,7 +10,8 @@ draft: an existing Status object that is providing default values for input fiel {% endcomment %}