Better scroll handling when toggling editing.

It will now retain the scroll position relative to the full document height when entering and leaving editing mode. This places the viewport closer to the contents you were watching. This is likely less disorienting
This commit is contained in:
Magnus Hovland Hoff 2018-10-01 09:01:27 +02:00
parent 619ba14b3f
commit 17c23da9bf

View file

@ -82,11 +82,12 @@ function openEditor() {
textarea.style.height = rendered.clientHeight + "px"; textarea.style.height = rendered.clientHeight + "px";
container.classList.add('edit'); retainScrollRatio(() => {
container.classList.add('edit');
autosizeTextarea(textarea, shadow);
});
updateFormEnabledState(); updateFormEnabledState();
autosizeTextarea(textarea, shadow);
textarea.setSelectionRange(textarea.value.length, textarea.value.length); textarea.setSelectionRange(textarea.value.length, textarea.value.length);
textarea.focus(); textarea.focus();
@ -112,6 +113,18 @@ function openEditor() {
// TODO: edit-link in footer? // TODO: edit-link in footer?
} }
function retainScrollRatio(innerFunction) {
const scrollElement = document.body.parentElement;
const savedScrollRatio = scrollElement.scrollTop / (scrollElement.scrollHeight - scrollElement.clientHeight);
innerFunction();
scrollElement.scrollTop = (scrollElement.scrollHeight - scrollElement.clientHeight) * savedScrollRatio;
}
function closeEditor() {
retainScrollRatio(() => container.classList.remove('edit'));
document.activeElement && document.activeElement.blur();
}
function doSave() { function doSave() {
state.saving = true; state.saving = true;
updateFormEnabledState(); updateFormEnabledState();
@ -170,8 +183,7 @@ function openEditor() {
} }
if (!result.conflict) { if (!result.conflict) {
container.classList.remove('edit'); closeEditor();
document.activeElement && document.activeElement.blur();
} }
state.saving = false; state.saving = false;
@ -196,8 +208,7 @@ function openEditor() {
Promise.resolve(!isEdited(form) || confirmDiscard()) Promise.resolve(!isEdited(form) || confirmDiscard())
.then(doReset => { .then(doReset => {
if (doReset) { if (doReset) {
container.classList.remove('edit'); closeEditor();
document.activeElement && document.activeElement.blur();
updateFormEnabledState(); updateFormEnabledState();
form.reset(); form.reset();