From 1a2950ae342eb4be1d8b715426b48de5b3dcf420 Mon Sep 17 00:00:00 2001 From: Abe Jellinek Date: Fri, 16 Jun 2023 12:16:35 -0700 Subject: [PATCH] Automatically adjust height of multiline/long fields in edit mode Fixes #3170 --- chrome/content/zotero/elements/itemBox.js | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/chrome/content/zotero/elements/itemBox.js b/chrome/content/zotero/elements/itemBox.js index a83da1800b..902010a035 100644 --- a/chrome/content/zotero/elements/itemBox.js +++ b/chrome/content/zotero/elements/itemBox.js @@ -1563,7 +1563,6 @@ var t; if (Zotero.ItemFields.isMultiline(fieldName) || Zotero.ItemFields.isLong(fieldName)) { t = document.createElement("textarea"); - t.setAttribute('rows', 8); } // Add auto-complete for certain fields else if (field == 'creator' || Zotero.ItemFields.isAutocompleteField(fieldName)) { @@ -1732,6 +1731,18 @@ }); t.addEventListener('keypress', event => this.handleKeyPress(event)); + if (t instanceof HTMLTextAreaElement) { + let updateHeight = () => { + // Reset height before getting scrollHeight + // Prevents field from growing slightly each time + // https://stackoverflow.com/a/58073583 + t.style.height = 'auto'; + t.style.height = `calc(max(6em, ${t.scrollHeight}px))`; + }; + t.addEventListener('input', updateHeight); + updateHeight(); + } + return t; }