diff options
Diffstat (limited to 'app/src/web/ts')
-rw-r--r-- | app/src/web/ts/auto_resize_textarea.ts | 16 |
1 files changed, 14 insertions, 2 deletions
diff --git a/app/src/web/ts/auto_resize_textarea.ts b/app/src/web/ts/auto_resize_textarea.ts index e170d14e..c71a75a1 100644 --- a/app/src/web/ts/auto_resize_textarea.ts +++ b/app/src/web/ts/auto_resize_textarea.ts @@ -1,6 +1,7 @@ // Credits to https://codepen.io/tomhodgins/pen/KgazaE (function () { - const textareas = <NodeListOf<HTMLTextAreaElement>>document.querySelectorAll('textarea:not(.frostAutoExpand)'); + const classTag = 'frostAutoExpand'; + const textareas = <NodeListOf<HTMLTextAreaElement>>document.querySelectorAll(`textarea:not(.${classTag})`); const dataAttribute = 'data-frost-minHeight'; @@ -11,14 +12,25 @@ // If no height is defined, have min bound to current height; // otherwise we will allow for height decreases in case user deletes text const minHeight = parseInt(el.getAttribute(dataAttribute) ?? '0'); + + // Save scroll position prior to height update + // See https://stackoverflow.com/a/18262927/4407321 + const scrollLeft = window.pageXOffset || + (document.documentElement || document.body.parentNode || document.body).scrollLeft; + const scrollTop = window.pageYOffset || + (document.documentElement || document.body.parentNode || document.body).scrollTop; + el.style.height = 'inherit'; el.style.height = `${Math.max(el.scrollHeight, minHeight)}px`; + + // Go to original scroll position + window.scrollTo(scrollLeft, scrollTop); }; function _frostExpandAll() { textareas.forEach(_frostAutoExpand); } textareas.forEach(el => { - el.classList.add('frostAutoExpand') + el.classList.add(classTag) const __frostAutoExpand = () => { _frostAutoExpand(el) }; |