From 54634e27976e646ab86459fa7c37d2ad1ee4f7cd Mon Sep 17 00:00:00 2001 From: Alexander Ebert Date: Thu, 23 Jun 2022 15:21:43 +0200 Subject: [PATCH] Preserve the scroll position in CodeMirror on navigation The previous code stopped working after the update of CodeMirror. See https://www.woltlab.com/community/thread/296010-nach-css-%C3%A4nderung-nicht-an-selber-stelle/ --- wcfsetup/install/files/acp/templates/codemirror.tpl | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/wcfsetup/install/files/acp/templates/codemirror.tpl b/wcfsetup/install/files/acp/templates/codemirror.tpl index 1abaf8ffa5..effe606ea3 100644 --- a/wcfsetup/install/files/acp/templates/codemirror.tpl +++ b/wcfsetup/install/files/acp/templates/codemirror.tpl @@ -42,7 +42,6 @@ document.head.appendChild(link); } - var elements = document.querySelectorAll('{@$codemirrorSelector|encodeJS}'); var config = { {if $codemirrorMode|isset} {if $codemirrorMode === 'smartymixed'} @@ -62,7 +61,7 @@ readOnly: {if !$editable|isset || $editable}false{else}true{/if} }; - [].forEach.call(elements, function (element) { + document.querySelectorAll('{@$codemirrorSelector|encodeJS}').forEach((element) => { {event name='javascriptInit'} if (element.codemirror) { @@ -122,10 +121,16 @@ scrollOffsetStorage = scrollOffsetStorage.nextElementSibling; } while (scrollOffsetStorage && !scrollOffsetStorage.classList.contains('codeMirrorScrollOffset')); if (scrollOffsetStorage) { - element.codemirror.scrollTo(null, scrollOffsetStorage.value); + const offset = scrollOffsetStorage.value element.codemirror.on('scroll', function (cm) { scrollOffsetStorage.value = cm.getScrollInfo().top; }); + + // Delay the scrolling to the next event cycle to allow + // CodeMirror to fully initialize itself. + setTimeout(() => { + element.codemirror.scrollTo(null, offset) + }, 0); } }); }); -- 2.20.1