Merge pull request #3210 from SillyTavern/sendtextarea-fieldsizing Use CSS resizing for send textarea
Signed| @@ -849,11 +849,9 @@ export let is_send_press = false; //Send generation | |||
| 849 | 849 | ||
| 850 | let this_del_mes = -1; | 850 | let this_del_mes = -1; |
| 851 | 851 | ||
| 852 | //message editing and chat scroll position persistence | 852 | //message editing |
| 853 | var this_edit_mes_chname = ''; | 853 | var this_edit_mes_chname = ''; |
| 854 | var this_edit_mes_id; | 854 | var this_edit_mes_id; |
| 855 | var scroll_holder = 0; | ||
| 856 | var is_use_scroll_holder = false; | ||
| 857 | 855 | ||
| 858 | //settings | 856 | //settings |
| 859 | export let settings; | 857 | export let settings; |
| @@ -9727,16 +9725,16 @@ jQuery(async function () { | |||
| 9727 | chooseBogusFolder($(this), tagId); | 9725 | chooseBogusFolder($(this), tagId); |
| 9728 | }); | 9726 | }); |
| 9729 | 9727 | ||
| 9728 | const cssAutofit = CSS.supports('field-sizing', 'content'); | ||
| 9729 | if (!cssAutofit) { | ||
| 9730 | /** | 9730 | /** |
| 9731 | * Sets the scroll height of the edit textarea to fit the content. | 9731 | * Sets the scroll height of the edit textarea to fit the content. |
| 9732 | * @param {HTMLTextAreaElement} e Textarea element to auto-fit | 9732 | * @param {HTMLTextAreaElement} e Textarea element to auto-fit |
| 9733 | */ | 9733 | */ |
| 9734 | function autoFitEditTextArea(e) { | 9734 | function autoFitEditTextArea(e) { |
| 9735 | scroll_holder = chatElement[0].scrollTop; | ||
| 9736 | e.style.height = '0px'; | 9735 | e.style.height = '0px'; |
| 9737 | const newHeight = e.scrollHeight + 4; | 9736 | const newHeight = e.scrollHeight + 4; |
| 9738 | e.style.height = `${newHeight}px`; | 9737 | e.style.height = `${newHeight}px`; |
| 9739 | is_use_scroll_holder = true; | ||
| 9740 | } | 9738 | } |
| 9741 | const autoFitEditTextAreaDebounced = debounce(autoFitEditTextArea, debounce_timeout.short); | 9739 | const autoFitEditTextAreaDebounced = debounce(autoFitEditTextArea, debounce_timeout.short); |
| 9742 | document.addEventListener('input', e => { | 9740 | document.addEventListener('input', e => { |
| @@ -9746,6 +9744,8 @@ jQuery(async function () { | |||
| 9746 | immediately ? autoFitEditTextArea(e.target) : autoFitEditTextAreaDebounced(e.target); | 9744 | immediately ? autoFitEditTextArea(e.target) : autoFitEditTextAreaDebounced(e.target); |
| 9747 | } | 9745 | } |
| 9748 | }); | 9746 | }); |
| 9747 | } | ||
| 9748 | |||
| 9749 | const chatElementScroll = document.getElementById('chat'); | 9749 | const chatElementScroll = document.getElementById('chat'); |
| 9750 | const chatScrollHandler = function () { | 9750 | const chatScrollHandler = function () { |
| 9751 | if (power_user.waifuMode) { | 9751 | if (power_user.waifuMode) { |
| @@ -9767,12 +9767,6 @@ jQuery(async function () { | |||
| 9767 | }; | 9767 | }; |
| 9768 | chatElementScroll.addEventListener('wheel', chatScrollHandler, { passive: true }); | 9768 | chatElementScroll.addEventListener('wheel', chatScrollHandler, { passive: true }); |
| 9769 | chatElementScroll.addEventListener('touchmove', chatScrollHandler, { passive: true }); | 9769 | chatElementScroll.addEventListener('touchmove', chatScrollHandler, { passive: true }); |
| 9770 | chatElementScroll.addEventListener('scroll', function () { | ||
| 9771 | if (is_use_scroll_holder) { | ||
| 9772 | this.scrollTop = scroll_holder; | ||
| 9773 | is_use_scroll_holder = false; | ||
| 9774 | } | ||
| 9775 | }, { passive: true }); | ||
| 9776 | 9770 | ||
| 9777 | $(document).on('click', '.mes', function () { | 9771 | $(document).on('click', '.mes', function () { |
| 9778 | //when a 'delete message' parent div is clicked | 9772 | //when a 'delete message' parent div is clicked |
| @@ -10511,14 +10505,16 @@ jQuery(async function () { | |||
| 10511 | .closest('.mes_block') | 10505 | .closest('.mes_block') |
| 10512 | .find('.mes_text') | 10506 | .find('.mes_text') |
| 10513 | .append( | 10507 | .append( |
| 10514 | '<textarea id=\'curEditTextarea\' class=\'edit_textarea mdHotkeys\' style=\'max-width:auto;\'></textarea>', | 10508 | '<textarea id=\'curEditTextarea\' class=\'edit_textarea mdHotkeys\'></textarea>', |
| 10515 | ); | 10509 | ); |
| 10516 | $('#curEditTextarea').val(text); | 10510 | $('#curEditTextarea').val(text); |
| 10517 | let edit_textarea = $(this) | 10511 | let edit_textarea = $(this) |
| 10518 | .closest('.mes_block') | 10512 | .closest('.mes_block') |
| 10519 | .find('.edit_textarea'); | 10513 | .find('.edit_textarea'); |
| 10514 | if (!cssAutofit) { | ||
| 10520 | edit_textarea.height(0); | 10515 | edit_textarea.height(0); |
| 10521 | edit_textarea.height(edit_textarea[0].scrollHeight); | 10516 | edit_textarea.height(edit_textarea[0].scrollHeight); |
| 10517 | } | ||
| 10522 | edit_textarea.focus(); | 10518 | edit_textarea.focus(); |
| 10523 | edit_textarea[0].setSelectionRange( //this sets the cursor at the end of the text | 10519 | edit_textarea[0].setSelectionRange( //this sets the cursor at the end of the text |
| 10524 | String(edit_textarea.val()).length, | 10520 | String(edit_textarea.val()).length, |
| @@ -887,7 +887,40 @@ export function initRossMods() { | |||
| 887 | saveSettingsDebounced(); | 887 | saveSettingsDebounced(); |
| 888 | }); | 888 | }); |
| 889 | 889 | ||
| 890 | const cssAutofit = CSS.supports('field-sizing', 'content'); | ||
| 891 | |||
| 892 | if (cssAutofit) { | ||
| 893 | let lastHeight = chatBlock.offsetHeight; | ||
| 894 | const chatBlockResizeObserver = new ResizeObserver((entries) => { | ||
| 895 | for (const entry of entries) { | ||
| 896 | if (entry.target !== chatBlock) { | ||
| 897 | continue; | ||
| 898 | } | ||
| 899 | |||
| 900 | const threshold = 1; | ||
| 901 | const newHeight = chatBlock.offsetHeight; | ||
| 902 | const deltaHeight = newHeight - lastHeight; | ||
| 903 | const isScrollAtBottom = Math.abs(chatBlock.scrollHeight - chatBlock.scrollTop - newHeight) <= threshold; | ||
| 904 | |||
| 905 | if (!isScrollAtBottom && Math.abs(deltaHeight) > threshold) { | ||
| 906 | chatBlock.scrollTop -= deltaHeight; | ||
| 907 | } | ||
| 908 | lastHeight = newHeight; | ||
| 909 | } | ||
| 910 | }); | ||
| 911 | |||
| 912 | chatBlockResizeObserver.observe(chatBlock); | ||
| 913 | } | ||
| 914 | |||
| 890 | sendTextArea.addEventListener('input', () => { | 915 | sendTextArea.addEventListener('input', () => { |
| 916 | saveUserInputDebounced(); | ||
| 917 | |||
| 918 | if (cssAutofit) { | ||
| 919 | // Unset modifications made with a manual resize | ||
| 920 | sendTextArea.style.height = 'auto'; | ||
| 921 | return; | ||
| 922 | } | ||
| 923 | |||
| 891 | const hasContent = sendTextArea.value !== ''; | 924 | const hasContent = sendTextArea.value !== ''; |
| 892 | const fitsCurrentSize = sendTextArea.scrollHeight <= sendTextArea.offsetHeight; | 925 | const fitsCurrentSize = sendTextArea.scrollHeight <= sendTextArea.offsetHeight; |
| 893 | const isScrollbarShown = sendTextArea.clientWidth < sendTextArea.offsetWidth; | 926 | const isScrollbarShown = sendTextArea.clientWidth < sendTextArea.offsetWidth; |
| @@ -895,7 +928,6 @@ export function initRossMods() { | |||
| 895 | const needsDebounce = hasContent && (fitsCurrentSize || (isScrollbarShown && isHalfScreenHeight)); | 928 | const needsDebounce = hasContent && (fitsCurrentSize || (isScrollbarShown && isHalfScreenHeight)); |
| 896 | if (needsDebounce) autoFitSendTextAreaDebounced(); | 929 | if (needsDebounce) autoFitSendTextAreaDebounced(); |
| 897 | else autoFitSendTextArea(); | 930 | else autoFitSendTextArea(); |
| 898 | saveUserInputDebounced(); | ||
| 899 | }); | 931 | }); |
| 900 | 932 | ||
| 901 | restoreUserInput(); | 933 | restoreUserInput(); |
| @@ -1264,6 +1264,7 @@ button { | |||
| 1264 | text-shadow: 0px 0px calc(var(--shadowWidth) * 1px) var(--SmartThemeShadowColor); | 1264 | text-shadow: 0px 0px calc(var(--shadowWidth) * 1px) var(--SmartThemeShadowColor); |
| 1265 | flex: 1; | 1265 | flex: 1; |
| 1266 | order: 3; | 1266 | order: 3; |
| 1267 | field-sizing: content; | ||
| 1267 | 1268 | ||
| 1268 | --progColor: rgb(146, 190, 252); | 1269 | --progColor: rgb(146, 190, 252); |
| 1269 | --progFlashColor: rgb(215, 136, 114); | 1270 | --progFlashColor: rgb(215, 136, 114); |
| @@ -4111,6 +4112,7 @@ input[type="range"]::-webkit-slider-thumb { | |||
| 4111 | line-height: calc(var(--mainFontSize) + .25rem); | 4112 | line-height: calc(var(--mainFontSize) + .25rem); |
| 4112 | max-height: 75vh; | 4113 | max-height: 75vh; |
| 4113 | max-height: 75dvh; | 4114 | max-height: 75dvh; |
| 4115 | field-sizing: content; | ||
| 4114 | } | 4116 | } |
| 4115 | 4117 | ||
| 4116 | #anchor_order { | 4118 | #anchor_order { |