Skip to content

SmythOS Studio: Open-Source Visual AI Agent Builder and deployable runtime stack from SmythOS. Start with an intuitive drag-and-drop workspace, extend with custom code, and deploy your agents anywhere — local, cloud, or edge — with full governance and control.

Fix: 86ev8zerq :Add bracket selection for variable syntax in textareas

samme-abdul7901531f2228
diff --git a/packages/app/src/builder-ui/ui/form/fields.ts b/packages/app/src/builder-ui/ui/form/fields.ts
index bf97050c..ed5cd483 100644
--- a/packages/app/src/builder-ui/ui/form/fields.ts
+++ b/packages/app/src/builder-ui/ui/form/fields.ts
@@ -4,7 +4,7 @@ import React from 'react';
import { createRoot } from 'react-dom/client';
import { delay } from '../../utils';
import { handleKvFieldEditBtn, handleVaultBtn } from '../../utils/component.utils';
-import { handleTemplateVars } from './misc';
+import { addBracketSelection, handleTemplateVars } from './misc';
declare var Metro;
@@ -291,6 +291,7 @@ interface TextAreaWithEditor extends HTMLTextAreaElement {
getValue: () => string;
setValue: (value: string) => void;
focus: () => void;
+ container?: HTMLElement;
};
}
@@ -678,6 +679,8 @@ function syncTextareaValues(
/**
* Initialize code editor in a textarea element
* Uses requestAnimationFrame for optimal DOM readiness detection
+ * Adds json-editor class to match inline textarea behavior
+ * Sets up Ace editor with bracket selection for variable syntax
*/
async function initializeCodeEditor(
textarea: TextAreaWithEditor,
@@ -702,6 +705,12 @@ async function initializeCodeEditor(
if (textarea._editor) {
textarea._editor.setValue(initialValue);
textarea._editor.focus();
+
+ // Setup bracket selection for Ace editor content
+ const aceContentElement = textarea._editor.container?.querySelector('.ace_content') as HTMLElement | null;
+ if (aceContentElement) {
+ addBracketSelection(aceContentElement);
+ }
}
}
@@ -755,6 +764,26 @@ function copyTextareaAttributes(
}
}
+/**
+ * Setup bracket selection for Metro UI textareas in modal
+ * Waits for Metro UI initialization and attaches handlers to both fake and real textareas
+ */
+async function setupMetroUIBracketSelection(textarea: HTMLTextAreaElement): Promise<void> {
+ // Wait for Metro UI to initialize its wrapper structure
+ await new Promise((resolve) => setTimeout(resolve, 100));
+
+ const textareaWrapper = textarea.closest('.textarea');
+ const fakeTextarea = textareaWrapper?.querySelector('.fake-textarea') as HTMLElement;
+
+ if (fakeTextarea) {
+ // Attach to both elements: fake receives clicks, real handles some focus events
+ addBracketSelection(fakeTextarea);
+ addBracketSelection(textarea);
+ } else {
+ addBracketSelection(textarea);
+ }
+}
+
/**
* Initialize template variables UI inside the modal if enabled on the textarea
*/
@@ -993,6 +1022,12 @@ async function handleExpandTextarea(
modalTextarea.classList.add('form-control', 'flex-1', 'resize-none');
modalTextarea.id = 'expanded-textarea';
+ // Add Metro UI data-role for regular textareas (non-code editor)
+ if (!hasCodeEditor) {
+ modalTextarea.setAttribute('data-role', 'textarea');
+ modalTextarea.setAttribute('data-auto-size', 'false');
+ }
+
// Copy validation attributes from original textarea for Metro UI validation
const validateAttr = originalTextarea.getAttribute('data-validate');
if (validateAttr) {
@@ -1118,6 +1153,9 @@ async function handleExpandTextarea(
);
} else {
initializeRegularTextarea(modalTextareaInDialog);
+
+ // Setup bracket selection for Metro UI textareas
+ await setupMetroUIBracketSelection(modalTextareaInDialog);
}
// Template variables setup (if enabled)
diff --git a/packages/app/src/builder-ui/ui/form/misc.ts b/packages/app/src/builder-ui/ui/form/misc.ts
index 3c42e474..f6e2845d 100644
--- a/packages/app/src/builder-ui/ui/form/misc.ts
+++ b/packages/app/src/builder-ui/ui/form/misc.ts
@@ -246,9 +246,14 @@ export function handleTemplateVars(targetElm, component = null) {
e.preventDefault();
// for some reason clickedElm does not able to get parent elements, so we need to query it again
- const buttonElm = document.querySelector(
+ // Search for the button within the current target element scope (modal or inline)
+ // This ensures we get the correct button when there are multiple forms (inline + modal)
+ const buttonElm = targetElm.querySelector(
`.template-var-button[data-var="${clickedElm.getAttribute('data-var')}"]`,
- );
+ ) as HTMLElement;
+
+ // If button not found in current scope, return early
+ if (!buttonElm) return;
// Prioritize finding json-editor (ACE editor textarea) first, then fall back to regular fields
const formGroup = buttonElm.closest('.form-group');
@@ -460,48 +465,82 @@ export const setTabIndex = (selector: string): void => {
fakeTextarea.forEach((el) => el.setAttribute('tabindex', '-1'));
};
-function _bracketSelectionEvent(e: any) {
- let inputElement: HTMLInputElement = e.target;
-
+/**
+ * Finds and returns the bracket match range for a given cursor position
+ * Only matches if cursor is ON or INSIDE the brackets, not before/after them
+ */
+function findBracketMatch(text: string, cursorPosition: number): { start: number; end: number } | null {
const regex = /{{.*?}}/g;
let match;
- if (inputElement?.classList?.contains('ace_content')) {
- const editor = (<any>(
- inputElement?.closest('.smt-input-textarea')?.querySelector('.json-editor')
- ))?._editor;
+ while ((match = regex.exec(text)) !== null) {
+ const start = match.index;
+ const end = start + match[0].length;
+
+ // Only select if cursor is strictly inside the brackets (start < cursor < end)
+ // This excludes clicking before opening {{ or after closing }}
+ if (start < cursorPosition && cursorPosition < end) {
+ return { start, end };
+ }
+ }
+
+ return null;
+}
- const cursorPosition = editor?.getCursorPosition();
- const rowPosition = cursorPosition?.row;
- const columnPosition = cursorPosition?.column;
+/**
+ * Handles bracket selection for variable syntax (e.g., {{variable}})
+ * Supports: Ace editors, Metro UI textareas, and regular textareas
+ */
+function _bracketSelectionEvent(e: any) {
+ const inputElement: HTMLInputElement = e.target;
+ const aceContentElement = inputElement?.classList?.contains('ace_content')
+ ? inputElement
+ : inputElement?.closest?.('.ace_content');
+
+ if (aceContentElement) {
+ // Handle Ace editor clicks
+ const editorContainer = aceContentElement.closest('.ace-editor');
+ const textarea = editorContainer?.previousElementSibling as any;
+ const editor = textarea?._editor ||
+ (aceContentElement.closest('.form-group')?.querySelector('.json-editor') as any)?._editor;
+
+ if (editor) {
+ setTimeout(() => {
+ const { row, column } = editor.getCursorPosition();
+ const text = editor.session.getLine(row);
+ const match = findBracketMatch(text, column);
+
+ if (match) {
+ editor.selection.setRange({
+ start: { row, column: match.start },
+ end: { row, column: match.end },
+ });
+ }
+ }, 0);
+ }
+ } else if (inputElement?.classList?.contains('fake-textarea')) {
+ // Handle Metro UI fake textarea clicks
+ const textareaWrapper = inputElement.closest('.textarea');
+ const realTextarea = textareaWrapper?.querySelector('textarea:not(.fake-textarea)') as HTMLTextAreaElement;
- const text = editor?.session?.getLine(rowPosition);
+ if (realTextarea) {
+ realTextarea.focus();
- while ((match = regex.exec(text)) !== null) {
- const start = match.index;
- const end = match.index + match[0].length;
+ // Longer delay needed for Metro UI to sync cursor position
+ setTimeout(() => {
+ const match = findBracketMatch(realTextarea.value, realTextarea.selectionStart);
- if (start <= columnPosition && columnPosition <= end) {
- editor.selection.setRange({
- start: { row: rowPosition, column: start },
- end: { row: rowPosition, column: end },
- });
- break;
- }
+ if (match) {
+ realTextarea.setSelectionRange(match.start, match.end);
+ }
+ }, 50);
}
} else {
- const text = inputElement.value;
- const cursorPosition = inputElement.selectionStart;
+ // Handle regular textarea/input clicks
+ const match = findBracketMatch(inputElement.value, inputElement.selectionStart);
- while ((match = regex.exec(text)) !== null) {
- const start = match.index;
- const end = match.index + match[0].length;
-
- if (start <= cursorPosition && cursorPosition <= end) {
- inputElement.selectionStart = start;
- inputElement.selectionEnd = end;
- break;
- }
+ if (match) {
+ inputElement.setSelectionRange(match.start, match.end);
}
}
}