Skip to content

Commit

Permalink
Merge pull request #8 from AlexW00/main
Browse files Browse the repository at this point in the history
  • Loading branch information
revolter authored Jun 11, 2024
2 parents 6d16f3b + 96dcca6 commit 384bddf
Showing 1 changed file with 30 additions and 3 deletions.
33 changes: 30 additions & 3 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
const textarea = document.getElementById('{{ textarea_id }}');
loadValue(textarea);
addValueListener(textarea);
addFocusListener(textarea);
addHashListener(textarea);
}

Expand All @@ -53,11 +54,32 @@
}
}

const debounce = (func, delay) => {
let debounceTimer;
return function () {
const context = this;
const args = arguments;
clearTimeout(debounceTimer);
debounceTimer = setTimeout(() => func.apply(context, args), delay);
};
};

function addValueListener(textarea) {
textarea.addEventListener('input', (event) => {
textarea.addEventListener('input', debounce((event) => {
const value = event.target.value;
storeValue(value);
updateTitle(value);
onValueUpdate(value);
}, 300), false);
}

function addFocusListener(textarea) {
textarea.addEventListener('blur', () => {
const value = textarea.value;
onValueUpdate(value);
}, false);

window.addEventListener('blur', () => {
const value = textarea.value;
onValueUpdate(value);
}, false);
}

Expand All @@ -73,6 +95,11 @@
return deserialize(hash.substring(1));
}

function onValueUpdate(value) {
storeValue(value);
updateTitle(value);
}

function storeValue(value) {
window.location.hash = '#' + serialize(value);
}
Expand Down

0 comments on commit 384bddf

Please sign in to comment.