Implement autofit for edit textarea

2fbe689605beddaa84cfc3b867c4e324901f365e

Cohee <18619528+Cohee1207@users.noreply.github.com>

3 files changed, +27 -30Showing whitespace changes
public/script.js+23 -27
@@ -849,11 +849,9 @@ export let is_send_press = false; //Send generation
849849
850850let this_del_mes = -1;
851851
852852//message editing and chat scroll position persistence
853853var this_edit_mes_chname = '';
854854var this_edit_mes_id;
855-var scroll_holder = 0;
856-var is_use_scroll_holder = false;
857855
858856//settings
859857export let settings;
@@ -9727,25 +9725,7 @@ jQuery(async function () {
97279725 chooseBogusFolder($(this), tagId);
97289726 });
97299727
9730- /**
9728+ const cssAutofit = CSS.supports('field-sizing', 'content');
9731- * Sets the scroll height of the edit textarea to fit the content.
9732- * @param {HTMLTextAreaElement} e Textarea element to auto-fit
9733- */
9734- function autoFitEditTextArea(e) {
9735- scroll_holder = chatElement[0].scrollTop;
9736- e.style.height = '0px';
9737- const newHeight = e.scrollHeight + 4;
9738- e.style.height = `${newHeight}px`;
9739- is_use_scroll_holder = true;
9740- }
9741- const autoFitEditTextAreaDebounced = debounce(autoFitEditTextArea, debounce_timeout.short);
9742- document.addEventListener('input', e => {
9743- if (e.target instanceof HTMLTextAreaElement && e.target.classList.contains('edit_textarea')) {
9744- const scrollbarShown = e.target.clientWidth < e.target.offsetWidth && e.target.offsetHeight >= window.innerHeight * 0.75;
9745- const immediately = (e.target.scrollHeight > e.target.offsetHeight && !scrollbarShown) || e.target.value === '';
9746- immediately ? autoFitEditTextArea(e.target) : autoFitEditTextAreaDebounced(e.target);
9747- }
9748- });
97499729 const chatElementScroll = document.getElementById('chat');
97509730 const chatScrollHandler = function () {
97519731 if (power_user.waifuMode) {
@@ -9767,12 +9747,26 @@ jQuery(async function () {
97679747 };
97689748 chatElementScroll.addEventListener('wheel', chatScrollHandler, { passive: true });
97699749 chatElementScroll.addEventListener('touchmove', chatScrollHandler, { passive: true });
9770- chatElementScroll.addEventListener('scroll', function () {
9750+
97719751 if (is_use_scroll_holder!cssAutofit) {
9772- this.scrollTop = scroll_holder;
9752+ /**
9773- is_use_scroll_holder = false;
9753+ * Sets the scroll height of the edit textarea to fit the content.
9754+ * @param {HTMLTextAreaElement} e Textarea element to auto-fit
9755+ */
9756+ function autoFitEditTextArea(e) {
9757+ e.style.height = '0px';
9758+ const newHeight = e.scrollHeight + 4;
9759+ e.style.height = `${newHeight}px`;
9760+ }
9761+ const autoFitEditTextAreaDebounced = debounce(autoFitEditTextArea, debounce_timeout.short);
9762+ document.addEventListener('input', e => {
9763+ if (e.target instanceof HTMLTextAreaElement && e.target.classList.contains('edit_textarea')) {
9764+ const scrollbarShown = e.target.clientWidth < e.target.offsetWidth && e.target.offsetHeight >= window.innerHeight * 0.75;
9765+ const immediately = (e.target.scrollHeight > e.target.offsetHeight && !scrollbarShown) || e.target.value === '';
9766+ immediately ? autoFitEditTextArea(e.target) : autoFitEditTextAreaDebounced(e.target);
9767+ }
9768+ });
97749769 }
9775- }, { passive: true });
97769770
97779771 $(document).on('click', '.mes', function () {
97789772 //when a 'delete message' parent div is clicked
@@ -10517,8 +10511,10 @@ jQuery(async function () {
1051710511 let edit_textarea = $(this)
1051810512 .closest('.mes_block')
1051910513 .find('.edit_textarea');
10514+ if (!cssAutofit) {
1052010515 edit_textarea.height(0);
1052110516 edit_textarea.height(edit_textarea[0].scrollHeight);
10517+ }
1052210518 edit_textarea.focus();
1052310519 edit_textarea[0].setSelectionRange( //this sets the cursor at the end of the text
1052410520 String(edit_textarea.val()).length,
public/scripts/RossAscends-mods.js+2 -3
@@ -890,9 +890,6 @@ export function initRossMods() {
890890 const cssAutofit = CSS.supports('field-sizing', 'content');
891891
892892 if (cssAutofit) {
893- sendTextArea.style['fieldSizing'] = 'content';
894- sendTextArea.style['height'] = 'auto';
895-
896893 let lastHeight = chatBlock.offsetHeight;
897894 const chatBlockResizeObserver = new ResizeObserver((entries) => {
898895 for (const entry of entries) {
@@ -919,6 +916,8 @@ export function initRossMods() {
919916 saveUserInputDebounced();
920917
921918 if (cssAutofit) {
919+ // Unset modifications made with a manual resize
920+ sendTextArea.style.height = 'auto';
922921 return;
923922 }
924923
public/style.css+2 -0
@@ -1264,6 +1264,7 @@ button {
12641264 text-shadow: 0px 0px calc(var(--shadowWidth) * 1px) var(--SmartThemeShadowColor);
12651265 flex: 1;
12661266 order: 3;
1267+ field-sizing: content;
12671268
12681269 --progColor: rgb(146, 190, 252);
12691270 --progFlashColor: rgb(215, 136, 114);
@@ -4111,6 +4112,7 @@ input[type="range"]::-webkit-slider-thumb {
41114112 line-height: calc(var(--mainFontSize) + .25rem);
41124113 max-height: 75vh;
41134114 max-height: 75dvh;
4115+ field-sizing: content;
41144116}
41154117
41164118#anchor_order {