Skip to content

Commit a2a556c

Browse files
committed
Fix CodeEditor Enter key handling using wrong offset; Fix first drop down item not active
1 parent 2705c54 commit a2a556c

3 files changed

Lines changed: 16 additions & 14 deletions

File tree

CHANGELOG.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/) and this p
99
### Added
1010

1111
- Extended existing height and readOnly props from `CodeEditorProps` to `AutoSuggestionProps` & `ExtendedCodeEditorProps` to be configurable from `<CodeAutocompleteField />`
12-
- `<CodeAutocompleteField />:
12+
- `<CodeAutocompleteField />`:
1313
- outerDivAttributes parameter: Allows to set parameter of the container div element of the code complete field.
1414

1515
### Fixed
@@ -18,6 +18,10 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/) and this p
1818
- Editor is re-created after certain property changes and is reset, i.e. loses it current state.
1919
- <CodeAutocompleteField />:
2020
- Read-only mode does not work correctly. It is still possible to change the value via pressing Enter (in multiline mode) or clicking the clear button.
21+
- `<CodeEditor />`:
22+
- Enter key handling (adding new line) broken when `onKeyDown` is defined.
23+
- `<CodeAutocompleteField />`:
24+
- First auto-completion item not marked as active when drop down first shown.
2125

2226

2327
## [24.3.0] - 2025-06-05

src/components/AutoSuggestion/AutoSuggestion.tsx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -363,7 +363,7 @@ const AutoSuggestion = ({
363363
editorState.suggestions = [];
364364
setSuggestions([]);
365365
}
366-
editorState.index = 0;
366+
setCurrentIndex(0);
367367
}, [suggestionResponse, editorState]);
368368

369369
const getOffsetRange = (cm: EditorView, from: number, to: number) => {
@@ -377,7 +377,7 @@ const AutoSuggestion = ({
377377
return { fromOffset, toOffset };
378378
};
379379

380-
const inputactionsDisplayed = React.useCallback((node) => {
380+
const inputActionsDisplayed = React.useCallback((node) => {
381381
if (!node) return;
382382
const width = node.offsetWidth;
383383
const slCodeEditor = node.parentElement.getElementsByClassName(`${eccgui}-singlelinecodeeditor`);
@@ -491,8 +491,7 @@ const AutoSuggestion = ({
491491
}, 1);
492492
};
493493

494-
//todo check out typings for event type
495-
const handleInputEditorKeyPress = (event: any) => {
494+
const handleInputEditorKeyPress = (event: KeyboardEvent) => {
496495
const overWrittenKeys: Array<string> = Object.values(OVERWRITTEN_KEYS);
497496
if (overWrittenKeys.includes(event.key) && (useTabForCompletions || event.key !== OVERWRITTEN_KEYS.Tab)) {
498497
//don't prevent when enter should create new line (multiline config) and dropdown isn't shown
@@ -628,6 +627,7 @@ const AutoSuggestion = ({
628627
break;
629628
default:
630629
//do nothing
630+
closeDropDown();
631631
}
632632
}
633633
};
@@ -716,7 +716,7 @@ const AutoSuggestion = ({
716716
{codeEditor}
717717
</ContextOverlay>
718718
{!!value.current && (
719-
<span className={BlueprintClassNames.INPUT_ACTION} ref={inputactionsDisplayed}>
719+
<span className={BlueprintClassNames.INPUT_ACTION} ref={inputActionsDisplayed}>
720720
<IconButton
721721
data-test-id={"value-path-clear-btn"}
722722
name="operation-clear"

src/extensions/codemirror/CodeMirror.tsx

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ export interface CodeEditorProps extends TestableComponent {
5454
* Handler method to receive onChange events.
5555
* As input the new value is given.
5656
*/
57-
onChange?: (v: any) => void;
57+
onChange?: (v: string) => void;
5858
/**
5959
* Called when the focus status changes
6060
*/
@@ -74,7 +74,7 @@ export interface CodeEditorProps extends TestableComponent {
7474
/**
7575
* Called when the cursor position changes
7676
*/
77-
onCursorChange?: (pos: number, coords: Rect, scrollinfo: HTMLElement, cm: EditorView) => any;
77+
onCursorChange?: (pos: number, coords: Rect, scrollinfo: HTMLElement, cm: EditorView) => void;
7878

7979
/**
8080
* Syntax mode of the code editor.
@@ -83,7 +83,7 @@ export interface CodeEditorProps extends TestableComponent {
8383
/**
8484
* Default value used first when the editor is instanciated.
8585
*/
86-
defaultValue?: any;
86+
defaultValue?: string;
8787
/**
8888
* If enabled the code editor won't show numbers before each line.
8989
*/
@@ -169,7 +169,7 @@ export interface CodeEditorProps extends TestableComponent {
169169
}
170170

171171
const addExtensionsFor = (flag: boolean, ...extensions: Extension[]) => (flag ? [...extensions] : []);
172-
const addToKeyMapConfigFor = (flag: boolean, ...keys: any) => (flag ? [...keys] : []);
172+
const addToKeyMapConfigFor = (flag: boolean, ...keys: KeyBinding[]) => (flag ? [...keys] : []);
173173
const addHandlersFor = (flag: boolean, handlerName: string, handler: any) =>
174174
flag ? ({ [handlerName]: handler } as DOMEventHandlers<any>) : {};
175175

@@ -259,15 +259,13 @@ export const CodeEditor = ({
259259
if (onKeyDown && !onKeyDown(event)) {
260260
if (event.key === "Enter" && !currentReadOnly.current) {
261261
const cursor = view.state.selection.main.head;
262-
const cursorLine = view.state.doc.lineAt(cursor).number;
263-
const offsetFromFirstLine = view.state.doc.line(cursorLine).to;
264262
view.dispatch({
265263
changes: {
266-
from: offsetFromFirstLine,
264+
from: cursor,
267265
insert: "\n",
268266
},
269267
selection: {
270-
anchor: offsetFromFirstLine + 1,
268+
anchor: cursor + 1,
271269
},
272270
});
273271
}

0 commit comments

Comments
 (0)