Skip to content

Commit 2ee65e3

Browse files
committed
fixed text marking on text change
1 parent 3f96e3a commit 2ee65e3

1 file changed

Lines changed: 24 additions & 20 deletions

File tree

src/components/AutoSuggestion/AutoSuggestion.tsx

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/* eslint-disable @typescript-eslint/no-explicit-any */
12
import React, { useEffect, useMemo, useState } from "react";
23
import { Classes as BlueprintClassNames } from "@blueprintjs/core";
34
import { EditorView, Rect } from "@codemirror/view";
@@ -219,8 +220,8 @@ const AutoSuggestion = ({
219220
CodeAutocompleteFieldSuggestionWithReplacementInfo | undefined
220221
>(undefined);
221222
const [cm, setCM] = React.useState<EditorView>();
222-
const currentCm = React.useRef<EditorView>()
223-
currentCm.current = cm
223+
const currentCm = React.useRef<EditorView>();
224+
currentCm.current = cm;
224225
const isFocused = React.useRef(false);
225226
const autoSuggestionDivRef = React.useRef<HTMLDivElement>(null);
226227
/** Mutable editor state, since this needs to be current in scope of the SingleLineEditorComponent. */
@@ -242,16 +243,16 @@ const AutoSuggestion = ({
242243
changes: { from: 0, to: currentCm.current.state?.doc.length, insert: initialValue },
243244
});
244245
// Validate initial value change
245-
checkValuePathValidity(initialValue)
246+
checkValuePathValidity(initialValue);
246247
}
247248
}, [initialValue, reInitOnInitialValueChange]);
248249

249250
React.useEffect(() => {
250-
if(currentCm.current) {
251+
if (currentCm.current) {
251252
// Validate initial value
252-
checkValuePathValidity(initialValue)
253+
checkValuePathValidity(initialValue);
253254
}
254-
}, [currentCm.current!!])
255+
}, [currentCm.current]);
255256

256257
const setCurrentIndex = (newIndex: number) => {
257258
editorState.index = newIndex;
@@ -263,10 +264,9 @@ const AutoSuggestion = ({
263264
editorState.cm = cm;
264265
}, [cm, editorState]);
265266

266-
const dispatch = // eslint-disable-next-line @typescript-eslint/no-empty-function
267-
(
268-
typeof editorState?.cm?.dispatch === "function" ? editorState?.cm?.dispatch : () => {}
269-
) as EditorView["dispatch"];
267+
const dispatch = (
268+
typeof editorState?.cm?.dispatch === "function" ? editorState?.cm?.dispatch : () => {}
269+
) as EditorView["dispatch"];
270270

271271
React.useEffect(() => {
272272
editorState.dropdownShown = shouldShowDropdown;
@@ -289,7 +289,9 @@ const AutoSuggestion = ({
289289
}
290290
} else {
291291
//remove redundant markers
292-
cm && removeMarkFromText({ view: cm, from: 0, to: cm.state?.doc.length });
292+
if (cm) {
293+
removeMarkFromText({ view: cm, from: 0, to: cm.state?.doc.length });
294+
}
293295
}
294296
return;
295297
}, [highlightedElement, selectedTextRanges, cm]);
@@ -301,18 +303,13 @@ const AutoSuggestion = ({
301303
if (parseError) {
302304
const { message, start, end } = parseError;
303305
const { toOffset, fromOffset } = getOffsetRange(cm, start, end);
304-
const { from, to } = markText({
306+
markText({
305307
view: cm,
306308
from: fromOffset,
307309
to: toOffset,
308310
className: `${eccgui}-autosuggestion__text--highlighted-error`,
309311
title: message,
310312
});
311-
312-
setErrorMarkers((previousMarkers) => {
313-
previousMarkers.forEach((m) => removeMarkFromText({ view: cm, from: m.from, to: m.to }));
314-
return [from, to];
315-
});
316313
} else {
317314
// Valid, clear all error markers
318315
setErrorMarkers((previous) => {
@@ -323,7 +320,7 @@ const AutoSuggestion = ({
323320
}
324321

325322
const isValid = validationResponse?.valid === undefined || validationResponse.valid;
326-
onInputChecked && onInputChecked(isValid);
323+
onInputChecked?.(isValid);
327324
}, [validationResponse?.valid, validationResponse?.parseError, cm, onInputChecked]);
328325

329326
/** generate suggestions and also populate the replacement indexes dict */
@@ -390,6 +387,7 @@ const AutoSuggestion = ({
390387
try {
391388
const result: CodeAutocompleteFieldValidationResult | undefined = await checkInput(inputString);
392389
setValidationResponse(result);
390+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
393391
} catch (e) {
394392
setValidationResponse(undefined);
395393
// TODO: Error handling
@@ -425,6 +423,7 @@ const AutoSuggestion = ({
425423
setSuggestionResponse(result);
426424
}
427425
}
426+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
428427
} catch (e) {
429428
setSuggestionResponse(undefined);
430429
// TODO: Error handling
@@ -534,8 +533,13 @@ const AutoSuggestion = ({
534533
};
535534

536535
const handleInputFocus = (focusState: boolean) => {
537-
onFocusChange && onFocusChange(focusState);
538-
focusState ? setShouldShowDropdown(true) : closeDropDown();
536+
onFocusChange?.(focusState);
537+
if (focusState) {
538+
setShouldShowDropdown(true);
539+
} else {
540+
closeDropDown();
541+
}
542+
539543
if (!isFocused.current && focusState) {
540544
// Just got focus
541545
// Clear suggestions and repeat suggestion request, something else might have changed while this component was not focused

0 commit comments

Comments
 (0)