persist selections to Local Storage

This commit is contained in:
Gavin McDonald
2025-05-13 10:10:19 -04:00
parent 0bd39683ff
commit e6b9ed1c5d
2 changed files with 52 additions and 13 deletions

View File

@@ -1,18 +1,51 @@
const Hooks = {};
const Hooks = {
EnterToNewline: {
mounted() {
this.el.addEventListener('keydown', (e) => {
if (e.key === 'Enter' && !e.shiftKey) {
e.preventDefault();
Hooks.EnterToNewline = {
mounted() {
this.el.addEventListener('keydown', (e) => {
if (e.key === 'Enter' && !e.shiftKey) {
e.preventDefault();
const cursorPos = this.el.selectionStart;
const value = this.el.value;
const cursorPos = this.el.selectionStart;
const value = this.el.value;
this.el.value = value.slice(0, cursorPos) + '\\n' + value.slice(cursorPos);
this.el.selectionStart = this.el.selectionEnd = cursorPos + 2;
}
});
},
},
this.el.value = value.slice(0, cursorPos) + '\\n' + value.slice(cursorPos);
this.el.selectionStart = this.el.selectionEnd = cursorPos + 2;
PersistData: {
mounted() {
const form = this.el;
const key = 'label';
const stored = JSON.parse(localStorage.getItem(key) || '{}');
let updated = false;
for (let [name, value] of Object.entries(stored)) {
const input = form.querySelector(`[name="${name}"]`);
if (input) {
input.value = value;
updated = true;
}
}
});
if (updated) {
this.pushEvent('update_label', stored);
}
form.addEventListener('input', () => {
const data = {};
for (let el of form.elements) {
if (el.name) data[el.name] = el.value;
}
localStorage.setItem(key, JSON.stringify(data));
});
},
},
};