Add hotkey for saving

This commit is contained in:
Magnus Hovland Hoff 2018-08-22 08:14:25 +02:00
parent 42e7857fcd
commit 94db59c44c

View file

@ -1,3 +1,5 @@
"use strict";
function autosizeTextarea(textarea, shadow) { function autosizeTextarea(textarea, shadow) {
shadow.style.width = textarea.clientWidth + "px"; shadow.style.width = textarea.clientWidth + "px";
shadow.value = textarea.value; shadow.value = textarea.value;
@ -83,10 +85,7 @@ function openEditor() {
textarea.addEventListener('input', () => autosizeTextarea(textarea, shadow)); textarea.addEventListener('input', () => autosizeTextarea(textarea, shadow));
window.addEventListener('resize', () => autosizeTextarea(textarea, shadow)); window.addEventListener('resize', () => autosizeTextarea(textarea, shadow));
form.addEventListener("submit", function (ev) { function doSave() {
ev.preventDefault();
ev.stopPropagation();
const body = queryArgsFromForm(form); const body = queryArgsFromForm(form);
textarea.disabled = true; textarea.disabled = true;
// TODO Disable other interaction as well: title editor, cancel and OK buttons // TODO Disable other interaction as well: title editor, cancel and OK buttons
@ -153,12 +152,9 @@ function openEditor() {
console.error(err); console.error(err);
return alertAsync(err.toString()); return alertAsync(err.toString());
}); });
}); }
cancel.addEventListener('click', function (ev) {
ev.preventDefault();
ev.stopPropagation();
function doCancel() {
Promise.resolve(!isEdited(form) || confirmDiscard()) Promise.resolve(!isEdited(form) || confirmDiscard())
.then(doReset => { .then(doReset => {
if (doReset) { if (doReset) {
@ -166,6 +162,18 @@ function openEditor() {
form.reset(); 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) { window.addEventListener("beforeunload", function (ev) {
@ -174,6 +182,19 @@ function openEditor() {
return ev.returnValue = "Discard changes?"; 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 document