Fully autonomous AI Agents system capable of performing complex penetration testing tasks
Go ai-agentsai-security-toolanthropicautonomous-agentsgolanggptgraphqlmulti-agent-systemoffensive-securityopen-sourceopenaipenetration-testingpenetration-testing-toolsreactsecurity-automationsecurity-testingsecurity-toolsself-hosted
refactor(markdown-editor): rename cycleToVariable → selectNextUse
Sergey Kozyrenkod1ed0157784d
diff --git a/frontend/src/components/shared/markdown-editor/markdown-editor-field.test.tsx b/frontend/src/components/shared/markdown-editor/markdown-editor-field.test.tsxindex 47d733ce..40c19f48 100644--- a/frontend/src/components/shared/markdown-editor/markdown-editor-field.test.tsx+++ b/frontend/src/components/shared/markdown-editor/markdown-editor-field.test.tsx@@ -46,8 +46,8 @@ describe('MarkdownEditorField raw-mode handle', () => {ref.current!.focus();expect(document.activeElement).toBe(textarea);- expect(ref.current!.cycleToVariable('Nope')).toBe(false);- expect(ref.current!.cycleToVariable('Foo')).toBe(true);+ expect(ref.current!.selectNextUse('Nope')).toBe(false);+ expect(ref.current!.selectNextUse('Foo')).toBe(true);expect(textarea.value.slice(textarea.selectionStart, textarea.selectionEnd)).toBe('{{.Foo}}');textarea.setSelectionRange(0, 0);@@ -69,13 +69,13 @@ describe('MarkdownEditorField rich-mode handle', () => {);await waitFor(() => expect(container.querySelector('.ProseMirror')?.textContent).toContain('Foo'));- expect(ref.current?.cycleToVariable('Foo')).toBe(true);- expect(ref.current?.cycleToVariable('Nope')).toBe(false);+ expect(ref.current?.selectNextUse('Foo')).toBe(true);+ expect(ref.current?.selectNextUse('Nope')).toBe(false);});});describe('MarkdownEditorField handle contract', () => {- it('exposes focus, cycleToVariable, and insertAtCursor in both modes', async () => {+ it('exposes focus, selectNextUse, and insertAtCursor in both modes', async () => {const rawRef = createRef<MarkdownEditorFieldHandle>();render(<MarkdownEditorField@@ -99,7 +99,7 @@ describe('MarkdownEditorField handle contract', () => {for (const current of [rawRef.current, richRef.current]) {expect(typeof current?.focus).toBe('function');- expect(typeof current?.cycleToVariable).toBe('function');+ expect(typeof current?.selectNextUse).toBe('function');expect(typeof current?.insertAtCursor).toBe('function');}});diff --git a/frontend/src/components/shared/markdown-editor/markdown-editor-field.tsx b/frontend/src/components/shared/markdown-editor/markdown-editor-field.tsxindex b78cd352..721e2388 100644--- a/frontend/src/components/shared/markdown-editor/markdown-editor-field.tsx+++ b/frontend/src/components/shared/markdown-editor/markdown-editor-field.tsx@@ -12,7 +12,7 @@ import type { MarkdownEditorHandle } from './markdown-editor';import type { EditorViewMode } from './markdown-editor-view-mode';import { MARKDOWN_EDITOR_WRAPPER_CLASS } from './markdown-editor-styles';-import { cycleTextareaToVariable, insertTextareaText } from './markdown-editor-textarea';+import { insertTextareaText, selectNextTextareaUse } from './markdown-editor-textarea';// Static-importing MarkdownEditor would merge the tiptap chunk into every route that pulls a util from the barrel.const MarkdownEditor = lazy(() => import('./markdown-editor').then((module) => ({ default: module.MarkdownEditor })));@@ -22,9 +22,9 @@ const MarkdownEditor = lazy(() => import('./markdown-editor').then((module) => (* each implement them — so a consumer drives the field the same way whether it renders raw source or rich.*/export interface MarkdownEditorFieldHandle {- cycleToVariable: (variable: string) => boolean;focus: () => void;insertAtCursor: (text: string) => void;+ selectNextUse: (variable: string) => boolean;}interface MarkdownEditorFieldProps extends Pick<AriaAttributes, 'aria-describedby' | 'aria-invalid'> {@@ -60,8 +60,6 @@ export function MarkdownEditorField({() =>mode === 'raw'? {- cycleToVariable: (variable) =>- rawRef.current ? cycleTextareaToVariable(rawRef.current.textarea, variable) : false,focus: () => rawRef.current?.focus(),// A disabled field must not be mutated through the handle (a variable click mid-save// would splice the value and dirty the form); the rich branch guards on editor.isEditable.@@ -70,11 +68,13 @@ export function MarkdownEditorField({insertTextareaText(rawRef.current.textarea, text, onChange);}},+ selectNextUse: (variable) =>+ rawRef.current ? selectNextTextareaUse(rawRef.current.textarea, variable) : false,}: {- cycleToVariable: (variable) => richRef.current?.cycleToVariable(variable) ?? false,focus: () => richRef.current?.focus(),insertAtCursor: (text) => richRef.current?.insertAtCursor(text),+ selectNextUse: (variable) => richRef.current?.selectNextUse(variable) ?? false,},[mode, onChange, disabled],);diff --git a/frontend/src/components/shared/markdown-editor/markdown-editor-handle.test.tsx b/frontend/src/components/shared/markdown-editor/markdown-editor-handle.test.tsxindex 03dbcf06..82a4e9a2 100644--- a/frontend/src/components/shared/markdown-editor/markdown-editor-handle.test.tsx+++ b/frontend/src/components/shared/markdown-editor/markdown-editor-handle.test.tsx@@ -10,7 +10,7 @@ beforeAll(setupEditorJsdom);const proseMirror = (c: HTMLElement) => c.querySelector('.ProseMirror');describe('MarkdownEditor imperative handle', () => {- it('cycleToVariable: rejects a missing variable, cycles an existing one (with wrap) without throwing', async () => {+ it('selectNextUse: rejects a missing variable, cycles an existing one (with wrap) without throwing', async () => {const ref = createRef<MarkdownEditorHandle>();const { container } = render(<MarkdownEditor@@ -21,15 +21,15 @@ describe('MarkdownEditor imperative handle', () => {);await waitFor(() => expect(proseMirror(container)?.textContent).toContain('Foo'));- expect(ref.current?.cycleToVariable('Nope')).toBe(false);+ expect(ref.current?.selectNextUse('Nope')).toBe(false);// two occurrences → advance, advance (wrap), and a second distinct variable — always found, never throws- expect(ref.current?.cycleToVariable('Foo')).toBe(true);- expect(ref.current?.cycleToVariable('Foo')).toBe(true);- expect(ref.current?.cycleToVariable('Foo')).toBe(true);- expect(ref.current?.cycleToVariable('Bar')).toBe(true);+ expect(ref.current?.selectNextUse('Foo')).toBe(true);+ expect(ref.current?.selectNextUse('Foo')).toBe(true);+ expect(ref.current?.selectNextUse('Foo')).toBe(true);+ expect(ref.current?.selectNextUse('Bar')).toBe(true);});- it('cycleToVariable advances across occurrences (observed: insertAtCursor replaces the targeted one)', async () => {+ it('selectNextUse advances across occurrences (observed: insertAtCursor replaces the targeted one)', async () => {const onChange = vi.fn();const ref = createRef<MarkdownEditorHandle>();const { container } = render(@@ -44,8 +44,8 @@ describe('MarkdownEditor imperative handle', () => {// Two cycles advance the selection from the first {{.Foo}} to the second; jsdom has no layout so the// selection isn't readable directly — insertAtCursor replaces the SELECTED range, making the target// observable. A regression to "always select hits[0]" would replace the first occurrence instead.- ref.current?.cycleToVariable('Foo');- ref.current?.cycleToVariable('Foo');+ ref.current?.selectNextUse('Foo');+ ref.current?.selectNextUse('Foo');ref.current?.insertAtCursor('REPLACED');await waitFor(() => expect(onChange).toHaveBeenCalled());diff --git a/frontend/src/components/shared/markdown-editor/markdown-editor-textarea.ts b/frontend/src/components/shared/markdown-editor/markdown-editor-textarea.tsindex 92e6518d..357ddd1c 100644--- a/frontend/src/components/shared/markdown-editor/markdown-editor-textarea.ts+++ b/frontend/src/components/shared/markdown-editor/markdown-editor-textarea.ts@@ -1,23 +1,5 @@import { findVariableUseRanges, nextVariableRange } from './markdown-editor-variable-syntax';-export function cycleTextareaToVariable(textarea: HTMLTextAreaElement, variable: string): boolean {- const ranges = findVariableUseRanges(textarea.value, variable).map(({ index, length }) => ({- end: index + length,- start: index,- }));- const target = nextVariableRange(ranges, textarea.selectionStart, textarea.selectionEnd);-- if (!target) {- return false;- }-- textarea.focus();- textarea.setSelectionRange(target.start, target.end);- textarea.scrollTop = Math.max(0, caretOffsetTop(textarea, target.start) - textarea.clientHeight / 2);-- return true;-}-export function insertTextareaText(textarea: HTMLTextAreaElement,text: string,@@ -36,6 +18,24 @@ export function insertTextareaText(}, 0);}+export function selectNextTextareaUse(textarea: HTMLTextAreaElement, variable: string): boolean {+ const ranges = findVariableUseRanges(textarea.value, variable).map(({ index, length }) => ({+ end: index + length,+ start: index,+ }));+ const target = nextVariableRange(ranges, textarea.selectionStart, textarea.selectionEnd);++ if (!target) {+ return false;+ }++ textarea.focus();+ textarea.setSelectionRange(target.start, target.end);+ textarea.scrollTop = Math.max(0, caretOffsetTop(textarea, target.start) - textarea.clientHeight / 2);++ return true;+}+// Pixel offset of `position` from the textarea content top, measured via a hidden mirror so soft-wrapped// lines count — a logical-line count undershoots the scroll badly for wrapped templates.function caretOffsetTop(textarea: HTMLTextAreaElement, position: number): number {diff --git a/frontend/src/components/shared/markdown-editor/markdown-editor.tsx b/frontend/src/components/shared/markdown-editor/markdown-editor.tsxindex 39bc95bf..e4acc966 100644--- a/frontend/src/components/shared/markdown-editor/markdown-editor.tsx+++ b/frontend/src/components/shared/markdown-editor/markdown-editor.tsx@@ -16,9 +16,9 @@ import { findVariableOccurrences } from './markdown-editor-variable-highlight';import { nextVariableRange } from './markdown-editor-variable-syntax';export interface MarkdownEditorHandle {- cycleToVariable: (variable: string) => boolean;focus: () => void;insertAtCursor: (text: string) => void;+ selectNextUse: (variable: string) => boolean;}interface MarkdownEditorProps {@@ -133,7 +133,23 @@ function MarkdownEditor({useImperativeHandle(ref,() => ({- cycleToVariable: (variable: string) => {+ focus: () => {+ editor?.commands.focus();+ },+ insertAtCursor: (text: string) => {+ // A disabled field must not be mutated through the handle — `dispatch` bypasses the editable+ // gate (which only blocks user input), so without this a variable click mid-save would edit+ // the doc and dirty the form.+ if (!editor || !editor.isEditable) {+ return;+ }++ const { state, view } = editor;++ view.focus();+ view.dispatch(state.tr.insertText(text).scrollIntoView());+ },+ selectNextUse: (variable: string) => {if (!editor) {return false;}@@ -158,22 +174,6 @@ function MarkdownEditor({return true;},- focus: () => {- editor?.commands.focus();- },- insertAtCursor: (text: string) => {- // A disabled field must not be mutated through the handle — `dispatch` bypasses the editable- // gate (which only blocks user input), so without this a variable click mid-save would edit- // the doc and dirty the form.- if (!editor || !editor.isEditable) {- return;- }-- const { state, view } = editor;-- view.focus();- view.dispatch(state.tr.insertText(text).scrollIntoView());- },}),[editor],);diff --git a/frontend/src/pages/settings/settings-prompt.tsx b/frontend/src/pages/settings/settings-prompt.tsxindex 128ac014..4e3e1c0d 100644--- a/frontend/src/pages/settings/settings-prompt.tsx+++ b/frontend/src/pages/settings/settings-prompt.tsx@@ -290,7 +290,7 @@ function SettingsPrompt() {const [viewMode, setViewMode] = useState<EditorViewMode>('rich');// One ref shared by both tab editors (System + Human): safe only because Radix TabsContent unmounts the// 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 —+ // unambiguous. If a forceMount / exit-animation is ever added, both mount and selectNextUse would race —// switch to one ref per tab.const editorRef = useRef<MarkdownEditorFieldHandle>(null);@@ -300,7 +300,7 @@ function SettingsPrompt() {// jump to its next use, or insert `{{.Name}}` at the caret if it isn't used yet. `editorRef` points at the// active tab's field (Radix unmounts the inactive tab), so this drives whichever prompt is on screen.const handleVariableClick = useCallback((variable: string) => {- if (!editorRef.current?.cycleToVariable(variable)) {+ if (!editorRef.current?.selectNextUse(variable)) {editorRef.current?.insertAtCursor(`{{.${variable}}}`);}}, []);