Skip to content

Commit

Permalink
Add hotkey for saving
Browse files Browse the repository at this point in the history
  • Loading branch information
maghoff committed Aug 22, 2018
1 parent 42e7857 commit 94db59c
Showing 1 changed file with 30 additions and 9 deletions.
39 changes: 30 additions & 9 deletions assets/script.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
"use strict";

function autosizeTextarea(textarea, shadow) {
shadow.style.width = textarea.clientWidth + "px";
shadow.value = textarea.value;
Expand Down Expand Up @@ -83,10 +85,7 @@ function openEditor() {
textarea.addEventListener('input', () => autosizeTextarea(textarea, shadow));
window.addEventListener('resize', () => autosizeTextarea(textarea, shadow));

form.addEventListener("submit", function (ev) {
ev.preventDefault();
ev.stopPropagation();

function doSave() {
const body = queryArgsFromForm(form);
textarea.disabled = true;
// TODO Disable other interaction as well: title editor, cancel and OK buttons
Expand Down Expand Up @@ -153,19 +152,28 @@ function openEditor() {
console.error(err);
return alertAsync(err.toString());
});
});

cancel.addEventListener('click', function (ev) {
ev.preventDefault();
ev.stopPropagation();
}

function doCancel() {
Promise.resolve(!isEdited(form) || confirmDiscard())
.then(doReset => {
if (doReset) {
container.classList.remove('edit');
form.reset();
}
});
}

form.addEventListener("submit", function (ev) {
ev.preventDefault();
ev.stopPropagation();
doSave();
});

cancel.addEventListener('click', function (ev) {
ev.preventDefault();
ev.stopPropagation();
doCancel();
});

window.addEventListener("beforeunload", function (ev) {
Expand All @@ -174,6 +182,19 @@ function openEditor() {
return ev.returnValue = "Discard changes?";
}
});

document.addEventListener("keypress", function (ev) {
const accel = ev.ctrlKey || ev.metaKey; // Imprecise, but works cross platform
if (ev.key === "Enter" && accel) {
//TODO Disable when in the process of saving
//TODO Disable when not editing

ev.stopPropagation();
ev.preventDefault();

doSave();
}
});
}

document
Expand Down

0 comments on commit 94db59c

Please sign in to comment.