Save status drafts in localstorage
This commit is contained in:
parent
d9090e723d
commit
03b608565d
|
@ -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;
|
||||||
|
}
|
||||||
|
}();
|
||||||
|
|
|
@ -251,6 +251,7 @@
|
||||||
</script>
|
</script>
|
||||||
<script src="{% static "js/bookwyrm.js" %}"></script>
|
<script src="{% static "js/bookwyrm.js" %}"></script>
|
||||||
<script src="{% static "js/localstorage.js" %}"></script>
|
<script src="{% static "js/localstorage.js" %}"></script>
|
||||||
|
<script src="{% static "js/status_cache.js" %}"></script>
|
||||||
{% block scripts %}{% endblock %}
|
{% block scripts %}{% endblock %}
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
|
|
@ -10,7 +10,8 @@ draft: an existing Status object that is providing default values for input fiel
|
||||||
{% endcomment %}
|
{% endcomment %}
|
||||||
<textarea
|
<textarea
|
||||||
name="content"
|
name="content"
|
||||||
class="textarea"
|
class="textarea save-draft"
|
||||||
|
data-cache-draft="id_content_{{ type }}_{{ book.id }}{{ reply_parent.id }}"
|
||||||
id="id_content_{{ type }}_{{ book.id }}{{ reply_parent.id }}"
|
id="id_content_{{ type }}_{{ book.id }}{{ reply_parent.id }}"
|
||||||
placeholder="{{ placeholder }}"
|
placeholder="{{ placeholder }}"
|
||||||
aria-label="{% if reply_parent %}{% trans 'Reply' %}{% else %}{% trans 'Content' %}{% endif %}"
|
aria-label="{% if reply_parent %}{% trans 'Reply' %}{% else %}{% trans 'Content' %}{% endif %}"
|
||||||
|
|
Loading…
Reference in New Issue