From f9183c06629d12aa0cbe7324597981c9c70487e2 Mon Sep 17 00:00:00 2001 From: Webifi Date: Fri, 18 Aug 2023 13:06:51 -0500 Subject: [PATCH] Shrink text-areas on profile change --- src/lib/ChatSettingsModal.svelte | 6 +++++- src/lib/Util.svelte | 10 ++++++---- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/src/lib/ChatSettingsModal.svelte b/src/lib/ChatSettingsModal.svelte index 3c6b9db..640b83b 100644 --- a/src/lib/ChatSettingsModal.svelte +++ b/src/lib/ChatSettingsModal.svelte @@ -56,6 +56,7 @@ $: globalStore = $globalStorage let originalProfile:string + let lastProfile:string let originalSettings:ChatSettings onMount(async () => { @@ -193,7 +194,10 @@ // Refresh settings modal showSettingsModal++ - setTimeout(() => sizeTextElements(), 0) + const profileChanged = lastProfile !== chatSettings.profile + lastProfile = chatSettings.profile + + setTimeout(() => sizeTextElements(profileChanged)) } const saveProfile = () => { diff --git a/src/lib/Util.svelte b/src/lib/Util.svelte index 81e1a3f..bb68d4a 100644 --- a/src/lib/Util.svelte +++ b/src/lib/Util.svelte @@ -6,9 +6,11 @@ import { replace } from 'svelte-spa-router' // import PromptConfirm from './PromptConfirm.svelte' import type { ChatSettings } from './Types.svelte' - export const sizeTextElements = () => { + export const sizeTextElements = (force?: boolean) => { const els = document.querySelectorAll('textarea.auto-size') - for (let i:number = 0, l = els.length; i < l; i++) autoGrowInput(els[i] as HTMLTextAreaElement) + for (let i:number = 0, l = els.length; i < l; i++) { + autoGrowInput(els[i] as HTMLTextAreaElement, force) + } } export const autoGrowInputOnEvent = (event: Event) => { @@ -18,9 +20,9 @@ autoGrowInput(event.target as HTMLTextAreaElement) } - export const autoGrowInput = (el: HTMLTextAreaElement) => { + export const autoGrowInput = (el: HTMLTextAreaElement, force?: boolean) => { const anyEl = el as any // Oh how I hate typescript. All the markup of Java with no real payoff.. - if (!anyEl.__didAutoGrow) el.style.height = '38px' // don't use "auto" here. Firefox will over-size. + if (force || !anyEl.__didAutoGrow) el.style.height = '38px' // don't use "auto" here. Firefox will over-size. el.style.height = el.scrollHeight + 'px' setTimeout(() => { if (el.scrollHeight > el.getBoundingClientRect().height + 5) {