Skip to content

Fully autonomous AI Agents system capable of performing complex penetration testing tasks

fix(markdown-editor): honor field ref in both modes; unify to one handle

Sergey Kozyrenko943b2aab010a
diff --git a/frontend/src/components/shared/markdown-editor/index.ts b/frontend/src/components/shared/markdown-editor/index.ts
index 99a50f00..f0d84384 100644
--- a/frontend/src/components/shared/markdown-editor/index.ts
+++ b/frontend/src/components/shared/markdown-editor/index.ts
@@ -3,6 +3,7 @@ export type { MarkdownEditorHandle } from './markdown-editor';
// into any route importing a light util from here. Consume the mode-switching MarkdownEditorField instead (it
// owns the lazy() boundary, so importing it is chunk-free until rich mode renders).
export { MarkdownEditorField } from './markdown-editor-field';
+export type { MarkdownEditorFieldHandle } from './markdown-editor-field';
export { findVariableUseRanges, VARIABLE_RE, variableProbe } from './markdown-editor-variable-syntax';
export { EditorViewModeToggle } from './markdown-editor-view-mode';
export type { EditorViewMode } from './markdown-editor-view-mode';
diff --git a/frontend/src/components/shared/markdown-editor/markdown-editor-field.tsx b/frontend/src/components/shared/markdown-editor/markdown-editor-field.tsx
index 8e9cb810..d1d212df 100644
--- a/frontend/src/components/shared/markdown-editor/markdown-editor-field.tsx
+++ b/frontend/src/components/shared/markdown-editor/markdown-editor-field.tsx
@@ -1,7 +1,9 @@
-import type { AriaAttributes, ReactNode, Ref } from 'react';
+import type { AriaAttributes, Ref } from 'react';
import { Loader2 } from 'lucide-react';
-import { lazy, Suspense } from 'react';
+import { lazy, Suspense, useImperativeHandle, useRef } from 'react';
+
+import type { TextareaRef } from '@/components/ui/textarea';
import { Textarea } from '@/components/ui/textarea';
import { cn } from '@/lib/utils';
@@ -13,18 +15,26 @@ import type { EditorViewMode } from './markdown-editor-view-mode';
// barrel; the lazy() boundary is what lets the barrel re-export this component eagerly.
const MarkdownEditor = lazy(() => import('./markdown-editor').then((module) => ({ default: module.MarkdownEditor })));
+// The imperative handle a consumer gets via `ref`. `focus()` is honored in BOTH modes (so RHF's
+// focus-on-validation-error reaches the field whether it renders a textarea or the rich editor). The
+// variable-panel methods exist only in rich mode — raw has no ProseMirror doc to cycle — so they are optional.
+export interface MarkdownEditorFieldHandle {
+ cycleToVariable?: (variable: string) => boolean;
+ focus: () => void;
+ insertAtCursor?: (text: string) => void;
+}
+
interface MarkdownEditorFieldProps extends Pick<AriaAttributes, 'aria-describedby' | 'aria-invalid'> {
// Sizes the field's outer box in both modes — the rich editor wrapper and the raw <textarea> take the
// same flex/min-height layout. The byte-exact font-mono / no-resize raw config is baked in, not overridable.
className?: string;
disabled?: boolean;
- fallback?: ReactNode;
id?: string;
mode: EditorViewMode;
onBlur?: () => void;
onChange: (value: string) => void;
placeholder?: string;
- ref?: Ref<MarkdownEditorHandle>;
+ ref?: Ref<MarkdownEditorFieldHandle>;
value: string;
}
@@ -33,7 +43,6 @@ export function MarkdownEditorField({
'aria-invalid': ariaInvalid,
className,
disabled,
- fallback,
id,
mode,
onBlur,
@@ -42,6 +51,22 @@ export function MarkdownEditorField({
ref,
value,
}: MarkdownEditorFieldProps) {
+ const rawRef = useRef<TextareaRef>(null);
+ const richRef = useRef<MarkdownEditorHandle>(null);
+
+ useImperativeHandle(
+ ref,
+ () =>
+ mode === 'raw'
+ ? { focus: () => rawRef.current?.focus() }
+ : {
+ cycleToVariable: (variable) => richRef.current?.cycleToVariable(variable) ?? false,
+ focus: () => richRef.current?.focus(),
+ insertAtCursor: (text) => richRef.current?.insertAtCursor(text),
+ },
+ [mode],
+ );
+
if (mode === 'raw') {
return (
<Textarea
@@ -54,6 +79,7 @@ export function MarkdownEditorField({
onBlur={onBlur}
onChange={(event) => onChange(event.target.value)}
placeholder={placeholder}
+ ref={rawRef}
value={value}
/>
);
@@ -62,11 +88,12 @@ export function MarkdownEditorField({
return (
<Suspense
fallback={
- fallback ?? (
- <div className={cn('flex items-center justify-center rounded-md border', className)}>
- <Loader2 className="text-muted-foreground size-5 animate-spin" />
- </div>
- )
+ <div
+ aria-busy="true"
+ className={cn('flex items-center justify-center rounded-md border', className)}
+ >
+ <Loader2 className="text-muted-foreground size-5 animate-spin" />
+ </div>
}
>
<MarkdownEditor
@@ -78,7 +105,7 @@ export function MarkdownEditorField({
onBlur={onBlur}
onChange={onChange}
placeholder={placeholder}
- ref={ref}
+ ref={richRef}
value={value}
/>
</Suspense>
diff --git a/frontend/src/components/shared/markdown-editor/markdown-editor.tsx b/frontend/src/components/shared/markdown-editor/markdown-editor.tsx
index ca87f522..f03daeed 100644
--- a/frontend/src/components/shared/markdown-editor/markdown-editor.tsx
+++ b/frontend/src/components/shared/markdown-editor/markdown-editor.tsx
@@ -14,6 +14,7 @@ import { findVariableOccurrences } from './markdown-editor-variable-highlight';
export interface MarkdownEditorHandle {
cycleToVariable: (variable: string) => boolean;
+ focus: () => void;
insertAtCursor: (text: string) => void;
}
@@ -164,6 +165,9 @@ function MarkdownEditor({
return true;
},
+ focus: () => {
+ editor?.commands.focus();
+ },
insertAtCursor: (text: string) => {
if (!editor) {
return;
diff --git a/frontend/src/components/ui/textarea.tsx b/frontend/src/components/ui/textarea.tsx
index cc8b4295..06f5b72f 100644
--- a/frontend/src/components/ui/textarea.tsx
+++ b/frontend/src/components/ui/textarea.tsx
@@ -3,18 +3,19 @@ import { useImperativeHandle } from 'react';
import { cn } from '@/lib/utils';
+export type TextareaRef = {
+ focus: () => void;
+ maxHeight: number;
+ minHeight: number;
+ textarea: HTMLTextAreaElement;
+};
+
type TextareaProps = React.ComponentProps<'textarea'> & {
autoSize?: boolean;
maxHeight?: number;
minHeight?: number;
};
-type TextareaRef = {
- maxHeight: number;
- minHeight: number;
- textarea: HTMLTextAreaElement;
-};
-
interface UseTextareaProps {
enabled?: boolean;
maxHeight?: number;
@@ -32,7 +33,7 @@ function Textarea({
ref,
value,
...props
-}: TextareaProps & { ref?: React.Ref<TextareaRef> }) {
+}: Omit<TextareaProps, 'ref'> & { ref?: React.Ref<TextareaRef> }) {
const textareaRef = React.useRef<HTMLTextAreaElement | null>(null);
const [triggerAutoSize, setTriggerAutoSize] = React.useState('');
diff --git a/frontend/src/features/knowledges/knowledge-form-controls.tsx b/frontend/src/features/knowledges/knowledge-form-controls.tsx
index 26884c52..49e07939 100644
--- a/frontend/src/features/knowledges/knowledge-form-controls.tsx
+++ b/frontend/src/features/knowledges/knowledge-form-controls.tsx
@@ -105,6 +105,7 @@ export function KnowledgeContentField({
onBlur={field.onBlur}
onChange={field.onChange}
placeholder="Knowledge content (will be embedded into the vector store)"
+ ref={field.ref}
value={field.value}
/>
</FormControl>
diff --git a/frontend/src/lib/compose-refs.ts b/frontend/src/lib/compose-refs.ts
new file mode 100644
index 00000000..18ace0e3
--- /dev/null
+++ b/frontend/src/lib/compose-refs.ts
@@ -0,0 +1,21 @@
+import type { Ref, RefCallback } from 'react';
+
+/**
+ * Merge several refs (callback or object) into one callback ref, so a single element can satisfy every owner.
+ * Wrap in `useMemo(() => composeRefs(a, b), [a, b])` at the call site to keep the composed ref stable.
+ */
+export function composeRefs<T>(...refs: (Ref<T> | undefined)[]): RefCallback<T> {
+ return (node) => {
+ for (const ref of refs) {
+ assignRef(ref, node);
+ }
+ };
+}
+
+function assignRef<T>(ref: Ref<T> | undefined, value: null | T): void {
+ if (typeof ref === 'function') {
+ ref(value);
+ } else if (ref) {
+ (ref as { current: null | T }).current = value;
+ }
+}
diff --git a/frontend/src/pages/settings/settings-prompt.tsx b/frontend/src/pages/settings/settings-prompt.tsx
index fde9d5db..135c21e1 100644
--- a/frontend/src/pages/settings/settings-prompt.tsx
+++ b/frontend/src/pages/settings/settings-prompt.tsx
@@ -42,7 +42,7 @@ import {
EditorViewModeToggle,
findVariableUseRanges,
MarkdownEditorField,
- type MarkdownEditorHandle,
+ type MarkdownEditorFieldHandle,
VARIABLE_RE,
variableProbe,
} from '@/components/shared/markdown-editor';
@@ -83,6 +83,7 @@ import {
ValidatePromptDocument,
} from '@/graphql/types';
import { useBreakpoint } from '@/hooks/use-breakpoint';
+import { composeRefs } from '@/lib/compose-refs';
import { formatPromptId } from '@/lib/route-titles/format-prompt-id';
const systemFormSchema = z.object({
@@ -96,7 +97,7 @@ const humanFormSchema = z.object({
interface FormMarkdownItemProps<T extends FieldValues> {
control: Control<T>;
disabled?: boolean;
- editorRef?: Ref<MarkdownEditorHandle>;
+ editorRef?: Ref<MarkdownEditorFieldHandle>;
mode: EditorViewMode;
name: FieldPathByValue<T, string>;
placeholder?: string;
@@ -215,6 +216,9 @@ function FormMarkdownItem<T extends FieldValues>({
disabled,
name,
});
+ // `field.ref` lets RHF focus this field on a failed submit; `editorRef` drives the variable panel. One
+ // element, two owners → compose so both are honored.
+ const composedRef = useMemo(() => composeRefs(field.ref, editorRef), [field.ref, editorRef]);
return (
<FormItem className="flex min-h-0 flex-1 flex-col">
@@ -222,16 +226,11 @@ function FormMarkdownItem<T extends FieldValues>({
<MarkdownEditorField
className="min-h-0 flex-1"
disabled={disabled}
- fallback={
- <div className="flex min-h-0 flex-1 items-center justify-center rounded-md border">
- <Loader2 className="text-muted-foreground size-5 animate-spin" />
- </div>
- }
mode={mode}
onBlur={field.onBlur}
onChange={field.onChange}
placeholder={placeholder}
- ref={editorRef}
+ ref={composedRef}
value={field.value}
/>
</FormControl>
@@ -332,15 +331,15 @@ function SettingsPrompt() {
// inactive tab (no forceMount here), so exactly one MarkdownEditor is ever mounted and the ref is
// unambiguous. If a forceMount / exit-animation is ever added, both mount and cycleToVariable would race —
// switch to one ref per tab.
- const editorRef = useRef<MarkdownEditorHandle>(null);
+ const editorRef = useRef<MarkdownEditorFieldHandle>(null);
const isLoading = isCreateLoading || isUpdateLoading || isDeleteLoading || isValidateLoading;
const handleVariableClick = useCallback(
(variable: string, field: { onChange: (value: string) => void; value: string }, formId: string) => {
if (viewMode === 'rich') {
- if (!editorRef.current?.cycleToVariable(variable)) {
- editorRef.current?.insertAtCursor(`{{.${variable}}}`);
+ if (!editorRef.current?.cycleToVariable?.(variable)) {
+ editorRef.current?.insertAtCursor?.(`{{.${variable}}}`);
}
return;
diff --git a/frontend/src/pages/templates/template.tsx b/frontend/src/pages/templates/template.tsx
index 2d926092..a7339176 100644
--- a/frontend/src/pages/templates/template.tsx
+++ b/frontend/src/pages/templates/template.tsx
@@ -676,15 +676,11 @@ function Template() {
<MarkdownEditorField
className="min-h-0 flex-1"
disabled={isSaving}
- fallback={
- <div className="flex min-h-0 flex-1 items-center justify-center rounded-md border">
- <Spinner variant="circle" />
- </div>
- }
mode={viewMode}
onBlur={field.onBlur}
onChange={field.onChange}
placeholder="Content"
+ ref={field.ref}
value={field.value}
/>
</FormControl>