File tree Expand file tree Collapse file tree
packages/react-textarea/src
components/copilot-textarea Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -14,6 +14,7 @@ import {
1414 defaultAutosuggestionsConfig ,
1515} from "../../types/autosuggestions-config" ;
1616import { makeRenderPlaceholderFunction } from "./render-placeholder" ;
17+ import { getFullEditorTextWithNewlines , getTextAroundCursor } from "../../lib/get-text-around-cursor" ;
1718
1819export 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
Original file line number Diff line number Diff line change @@ -13,7 +13,7 @@ import { AutosuggestionState } from "../types/autosuggestion-state";
1313
1414export 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 ,
Original file line number Diff line number Diff 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
3442export function extractTextWithNewlines ( editor : Editor , range : Range ) : string {
3543 const voids = false ;
You can’t perform that action at this time.
0 commit comments