Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/) and this p
- improve keyboard navigation, tag removal and focus handling
- improve screen reader labels and selection announcements
- preserve filters and keep the dropdown open when toggling existing options
- `<CodeEditor />`
- preserve the editor height when disabled
- apply callback, extension, and appearance changes in place, including `multiline` changes in `<ExtendedCodeEditor />`

## [26.2.1] - 2026-11-02

Expand Down
53 changes: 19 additions & 34 deletions src/components/AutoSuggestion/AutoSuggestion.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -698,40 +698,25 @@ export const CodeAutocompleteField = ({
effectiveIntent && !["info", "accent", "neutral"].includes(effectiveIntent) ? effectiveIntent : undefined;
const inputIntentClass = effectiveIntent ? ` ${eccgui}-intent--${effectiveIntent}` : "";

const codeEditor = React.useMemo(() => {
return (
<ExtendedCodeEditor
mode={mode}
setCM={setCM}
onChange={handleChange}
onCursorChange={handleCursorChange}
initialValue={initialValue}
onFocusChange={handleInputFocus}
onKeyDown={handleInputEditorKeyPress}
enableTab={useTabForCompletions}
placeholder={placeholder}
onSelection={onSelection}
showScrollBar={showScrollBar}
multiline={multiline}
onMouseDown={handleInputMouseDown}
height={height}
readOnly={readOnly}
/>
);
}, [
mode,
setCM,
handleChange,
initialValue,
useTabForCompletions,
placeholder,
showScrollBar,
multiline,
handleInputMouseDown,
height,
readOnly,
effectiveIntent,
]);
const codeEditor = (
<ExtendedCodeEditor
mode={mode}
setCM={setCM}
onChange={handleChange}
onCursorChange={handleCursorChange}
initialValue={initialValue}
onFocusChange={handleInputFocus}
onKeyDown={handleInputEditorKeyPress}
enableTab={useTabForCompletions}
placeholder={placeholder}
onSelection={onSelection}
showScrollBar={showScrollBar}
multiline={multiline}
onMouseDown={handleInputMouseDown}
height={height}
readOnly={readOnly}
/>
);
const autoSuggestionInput = (
<div
id={id}
Expand Down
17 changes: 9 additions & 8 deletions src/components/AutoSuggestion/ExtendedCodeEditor.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React from "react";
import { Classes as BlueprintClassNames } from "@blueprintjs/core";
import { EditorState } from "@codemirror/state";
import { EditorView, lineNumbers, Rect } from "@codemirror/view";
import { EditorView, Rect } from "@codemirror/view";

import { CLASSPREFIX as eccgui } from "../../configuration/constants";
import { CodeEditor, CodeEditorProps } from "../../extensions/codemirror/CodeMirror";
Expand Down Expand Up @@ -67,6 +67,12 @@ export interface ExtendedCodeEditorProps {
readOnly?: boolean;
}

const singleLineExtensions = [
EditorState.transactionFilter.of((transaction) =>
transaction.newDoc.lines > transaction.startState.doc.lines ? [] : transaction,
),
];

/** Supports single-line and multiline editing. */
export const ExtendedCodeEditor = ({
multiline = false,
Expand All @@ -86,12 +92,6 @@ export const ExtendedCodeEditor = ({
readOnly,
}: ExtendedCodeEditorProps) => {
const initialContent = React.useRef(multiline ? initialValue : initialValue.replace(/[\r\n]/g, " "));
const multilineExtensions = multiline
? [lineNumbers(), EditorView.lineWrapping]
: [
EditorState?.transactionFilter.of((tr) => (tr.newDoc.lines > 1 ? [] : tr)), //prevent multiline,
];

return (
<CodeEditor
defaultValue={initialContent.current}
Expand All @@ -110,9 +110,10 @@ export const ExtendedCodeEditor = ({
readOnly={readOnly}
name=""
enableTab={enableTab}
additionalExtensions={[...multilineExtensions]}
additionalExtensions={multiline ? undefined : singleLineExtensions}
className={multiline ? undefined : `${eccgui}-singlelinecodeeditor ${BlueprintClassNames.INPUT}`}
{...codeEditorProps}
wrapLines={multiline || codeEditorProps?.wrapLines}
/>
);
};
Expand Down
14 changes: 14 additions & 0 deletions src/components/AutoSuggestion/tests/AutoSuggestion.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,20 @@ describe("CodeAutocompleteField", () => {
expect(getByText(props.label!)).toBeTruthy();
});

it("forwards focus events to the latest callback after a rerender", async () => {
const { rerender } = render(<CodeAutocompleteField {...props} />);
const editor = screen.getByRole("combobox");
await act(async () => editor.focus());
expect(props.onFocusChange).toHaveBeenCalledWith(true);

const nextOnFocusChange = jest.fn();
rerender(<CodeAutocompleteField {...props} onFocusChange={nextOnFocusChange} />);
await act(async () => editor.blur());

expect(nextOnFocusChange).toHaveBeenCalledWith(false);
expect(props.onFocusChange).not.toHaveBeenCalledWith(false);
});

it("connects the focused editor to the active suggestion", async () => {
const { container } = render(
<CodeAutocompleteField
Expand Down
72 changes: 71 additions & 1 deletion src/components/AutoSuggestion/tests/ExtendedCodeEditor.test.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React from "react";
import { EditorView } from "@codemirror/view";
import { render } from "@testing-library/react";
import { act, render, screen } from "@testing-library/react";

import "@testing-library/jest-dom";

Expand Down Expand Up @@ -74,4 +74,74 @@ describe("SingleLineCodeEditor", () => {
render(<ExtendedCodeEditor {...{ ...props, initialValue: "1\n2\n3" }} />);
expect(cm?.state.doc.lines).toBe(1);
});

it.each([
{ multiline: true, draft: "unsaved draft" },
{ multiline: false, draft: "unsaved\nsecond line" },
])("preserves the draft and selection when multiline changes to $multiline", ({ multiline, draft }) => {
const editorProps = { ...props, initialValue: "initial" };
const { rerender } = render(<ExtendedCodeEditor {...editorProps} multiline={!multiline} />);
const editor = EditorView.findFromDOM(screen.getByRole("textbox"));

if (!editor) {
throw new Error("Expected the mounted CodeMirror editor");
}
const selection = { anchor: draft.length, head: 2 };
act(() => {
editor.dispatch({ changes: { from: 0, to: editor.state.doc.length, insert: draft }, selection });
});
expect(editor.state.doc.toString()).toBe(draft);
expect(editor.state.selection.main).toMatchObject(selection);

rerender(<ExtendedCodeEditor {...editorProps} multiline={multiline} />);

expect(EditorView.findFromDOM(screen.getByRole("textbox"))).toBe(editor);
expect(editor.state.doc.toString()).toBe(draft);
expect(editor.state.selection.main).toMatchObject(selection);
});

it("allows editing and removing existing lines after switching to single-line mode", () => {
const editorProps = { ...props, initialValue: "first\nsecond" };
const { rerender } = render(<ExtendedCodeEditor {...editorProps} multiline />);
const editor = EditorView.findFromDOM(screen.getByRole("textbox"));

if (!editor) {
throw new Error("Expected the mounted CodeMirror editor");
}
rerender(<ExtendedCodeEditor {...editorProps} multiline={false} />);

act(() => editor.dispatch({ changes: { from: 0, insert: "draft " } }));
expect(editor.state.doc.toString()).toBe("draft first\nsecond");
act(() => editor.dispatch({ changes: { from: 0, insert: "\n" } }));
expect(editor.state.doc.toString()).toBe("draft first\nsecond");
act(() => editor.dispatch({ changes: { from: 11, to: 12, insert: " " } }));
expect(editor.state.doc.toString()).toBe("draft first second");
});

it.each([
{ multiline: true, expectedText: "draft\nnext line" },
{ multiline: false, expectedText: "draft" },
])("updates newline handling and wrapping when multiline changes to $multiline", ({ multiline, expectedText }) => {
const editorProps = { ...props, initialValue: "draft" };
const { rerender } = render(<ExtendedCodeEditor {...editorProps} multiline={!multiline} />);
const content = screen.getByRole("textbox");
const editor = EditorView.findFromDOM(content);

if (!editor) {
throw new Error("Expected the mounted CodeMirror editor");
}
act(() => editor.dispatch({ selection: { anchor: 5 } }));

rerender(<ExtendedCodeEditor {...editorProps} multiline={multiline} />);

expect(screen.getByRole("textbox")).toBe(content);
expect(editor.state.selection.main.head).toBe(5);

act(() => editor.dispatch({ changes: { from: 5, insert: "\nnext line" } }));

expect({
text: editor.state.doc.toString(),
wraps: content.classList.contains("cm-lineWrapping"),
}).toEqual({ text: expectedText, wraps: multiline });
});
});
Loading
Loading