Skip to content

Commit 6d9859a

Browse files
Fix CodeEditor Enter key handling using wrong offset; Fix first drop down item not active
1 parent 82d50cd commit 6d9859a

3 files changed

Lines changed: 20 additions & 14 deletions

File tree

CHANGELOG.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,17 @@ This is a major release, and it might be not compatible with your current usage
1111
### Added
1212

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

17+
### Fixed
18+
19+
- `<CodeEditor />`:
20+
- Enter key handling (adding new line) broken when `onKeyDown` is defined.
21+
- `<CodeAutocompleteField />`:
22+
- First auto-completion item not marked as active when drop down first shown.
23+
24+
1725
## [24.3.0] - 2025-06-05
1826

1927
### Added

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
};
@@ -715,7 +715,7 @@ const AutoSuggestion = ({
715715
{codeEditor}
716716
</ContextOverlay>
717717
{!!value.current && (
718-
<span className={BlueprintClassNames.INPUT_ACTION} ref={inputactionsDisplayed}>
718+
<span className={BlueprintClassNames.INPUT_ACTION} ref={inputActionsDisplayed}>
719719
<IconButton
720720
data-test-id={"value-path-clear-btn"}
721721
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

@@ -242,15 +242,13 @@ export const CodeEditor = ({
242242
if (onKeyDown && !onKeyDown(event)) {
243243
if (event.key === "Enter") {
244244
const cursor = view.state.selection.main.head;
245-
const cursorLine = view.state.doc.lineAt(cursor).number;
246-
const offsetFromFirstLine = view.state.doc.line(cursorLine).to;
247245
view.dispatch({
248246
changes: {
249-
from: offsetFromFirstLine,
247+
from: cursor,
250248
insert: "\n",
251249
},
252250
selection: {
253-
anchor: offsetFromFirstLine + 1,
251+
anchor: cursor + 1,
254252
},
255253
});
256254
}

0 commit comments

Comments
 (0)