⎗ ✓ 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89diff --git a/javascript/_textAreas.js b/javascript/_textAreas.js index 267d2fb..129f831 100644 --- a/javascript/_textAreas.js +++ b/javascript/_textAreas.js @@ -93,6 +93,14 @@ const thirdParty = { "selectors": [ "Tag Prompt" ] + }, + "json-builder": { + "base": "#txt2img_json_builder, #img2img_json_builder", + "hasIds": true, + "selectors": [ + ".jb-root textarea", + ".jb-root input[type='text']" + ] } } diff --git a/javascript/tagAutocomplete.js b/javascript/tagAutocomplete.js index 61cb7db..a957193 100644 --- a/javascript/tagAutocomplete.js +++ b/javascript/tagAutocomplete.js @@ -365,16 +365,43 @@ function showResults(textArea) { parentDiv.style.display = "flex"; if (TAC_CFG.slidingPopup) { - let caretPosition = getCaretCoordinates(textArea, textArea.selectionEnd); - // Top cursor offset fix for SDNext modern UI, based on code by https://github.com/Nyx01 - let offsetTop = textArea.offsetTop + caretPosition.top - textArea.scrollTop + 10; // Adjust this value for desired distance below cursor - let offsetLeft = Math.min(textArea.offsetLeft - textArea.scrollLeft + caretPosition.left, textArea.offsetWidth - parentDiv.offsetWidth); + const caretPosition = getCaretCoordinates(textArea, textArea.selectionEnd); + let offsetTop, offsetLeft; + + if (textArea.closest(".jb-root")) { + // Measure the popup's real coordinate origin. Setting top/left to 0 + // avoids relying on offsetParent, which differs inside JSON Builder. + parentDiv.style.top = "0px"; + parentDiv.style.left = "0px"; + const origin = parentDiv.getBoundingClientRect(); + const area = textArea.getBoundingClientRect(); + + // Compensate for the canvas transform/zoom. + const popupScaleX = origin.width / parentDiv.offsetWidth || 1; + const popupScaleY = origin.height / parentDiv.offsetHeight || 1; + const areaScaleX = area.width / textArea.offsetWidth || 1; + const areaScaleY = area.height / textArea.offsetHeight || 1; + + offsetLeft = ( + area.left - origin.left + + (caretPosition.left - textArea.scrollLeft) * areaScaleX + ) / popupScaleX; + + offsetTop = ( + area.top - origin.top + + (caretPosition.top - textArea.scrollTop) * areaScaleY + ) / popupScaleY + 10; + } else { + // Keep the existing behavior for core prompts/other third-party areas + offsetTop = textArea.offsetTop + caretPosition.top - textArea.scrollTop + 10; + offsetLeft = Math.min( + textArea.offsetLeft - textArea.scrollLeft + caretPosition.left, + textArea.offsetWidth - parentDiv.offsetWidth + ); + } - parentDiv.style.top = `${offsetTop}px`; // Position below the cursor + parentDiv.style.top = `${offsetTop}px`; parentDiv.style.left = `${offsetLeft}px`; - } else { - if (parentDiv.style.left) - parentDiv.style.removeProperty("left"); } // Reset here too to make absolutely sure the browser registers it parentDiv.scrollTop = 0; @@ -1561,6 +1588,13 @@ async function setup() { // Add mutation observer to accordions inside a base that has onDemand set to true addOnDemandObservers(addAutocompleteToArea); + // JSON Builder (and similar dynamic fields) can be created after setup + const jsonBuilder = thirdParty["json-builder"]; + gradioApp().addEventListener("focusin", (e) => { + if (e.target.matches?.(jsonBuilder.selectors.join(", "))) + addAutocompleteToArea(e.target); + }); + // Add event listener to apply settings button so we can mirror the changes to our internal config let applySettingsButton = gradioApp().querySelector("#tab_settings #settings_submit") || gradioApp().querySelector("#tab_settings > div > .gr-button-primary"); applySettingsButton?.addEventListener("click", () => {
Warning LINK You are about to visit a link which has been flagged with the above content warnings. Do you wish to continue? Continue Cancel