Skip to content

Commit ee9e850

Browse files
committed
call props.onChange when text changes
1 parent 3820183 commit ee9e850

3 files changed

Lines changed: 20 additions & 4 deletions

File tree

packages/react-textarea/src/components/copilot-textarea/copilot-textarea.tsx

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import {
1414
defaultAutosuggestionsConfig,
1515
} from "../../types/autosuggestions-config";
1616
import { makeRenderPlaceholderFunction } from "./render-placeholder";
17+
import { getFullEditorTextWithNewlines, getTextAroundCursor } from "../../lib/get-text-around-cursor";
1718

1819
export interface CopilotTextareaProps {
1920
className?: string;
@@ -102,7 +103,15 @@ export function CopilotTextarea(props: CopilotTextareaProps): JSX.Element {
102103
editor={editor}
103104
initialValue={initialValue}
104105
onChange={(value) => {
105-
onChangeHandlerForAutocomplete(editor);
106+
const newEditorState = getTextAroundCursor(editor)
107+
108+
const fullEditorText = newEditorState
109+
? newEditorState.textBeforeCursor + newEditorState.textAfterCursor
110+
: getFullEditorTextWithNewlines(editor); // we don't double-parse the editor. When `newEditorState` is null, we didn't parse the editor yet.
111+
112+
setLastKnownFullEditorText(fullEditorText);
113+
onChangeHandlerForAutocomplete(newEditorState);
114+
props.onChange?.(fullEditorText);
106115
}}
107116
>
108117
<Editable

packages/react-textarea/src/hooks/use-autosuggestions.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import { AutosuggestionState } from "../types/autosuggestion-state";
1313

1414
export interface UseAutosuggestionsResult {
1515
currentAutocompleteSuggestion: AutosuggestionState | null;
16-
onChangeHandler: (editor: CustomEditor) => void;
16+
onChangeHandler: (newEditorState: EditorAutocompleteState | null) => void;
1717
onKeyDownHandler: (event: React.KeyboardEvent<HTMLDivElement>) => void;
1818
}
1919

@@ -71,8 +71,7 @@ export function useAutosuggestions(
7171
);
7272

7373
const onChange = useCallback(
74-
(editor: CustomEditor) => {
75-
const newEditorState = getTextAroundCursor(editor);
74+
(newEditorState: EditorAutocompleteState | null) => {
7675
const editorStateHasChanged = !nullableCompatibleEqualityCheck(
7776
areEqual_autocompleteState,
7877
previousAutocompleteState,

packages/react-textarea/src/lib/get-text-around-cursor.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,14 @@ export function getTextAroundCursor(
3030
};
3131
}
3232

33+
export function getFullEditorTextWithNewlines(editor: Editor): string {
34+
const fullDocumentRange: Range = {
35+
anchor: Editor.start(editor, []),
36+
focus: Editor.end(editor, []),
37+
};
38+
return extractTextWithNewlines(editor, fullDocumentRange);
39+
}
40+
3341
// Helper function to extract text with newlines
3442
export function extractTextWithNewlines(editor: Editor, range: Range): string {
3543
const voids = false;

0 commit comments

Comments
 (0)