@@ -50,22 +50,18 @@ declare module "slate" {
5050 }
5151}
5252
53- export interface CopilotTextareaProps { }
53+ export interface CopilotTextareaProps {
54+ className ?: string ;
55+ placeholder ?: string ;
56+ value ?: string ;
57+ onChange ?: ( value : string ) => void ;
58+ }
5459
5560export function CopilotTextarea ( props : CopilotTextareaProps ) : JSX . Element {
5661 const initialValue : Descendant [ ] = [
5762 {
5863 type : "paragraph" ,
59- children : [
60- { text : "A line of text in a paragraph." , isSuggestion : false } ,
61- ] ,
62- } ,
63- {
64- type : "copilot-suggestion" ,
65- children : [
66- { text : "A line of text in a paragraph." , isSuggestion : false } ,
67- { text : "A line of text in a paragraph." , isSuggestion : true } ,
68- ] ,
64+ children : [ { text : props . value || "" , isSuggestion : false } ] ,
6965 } ,
7066 ] ;
7167
@@ -93,13 +89,38 @@ export function CopilotTextarea(props: CopilotTextareaProps): JSX.Element {
9389 ) ;
9490 } , [ ] ) ;
9591
96- const [ editor ] = useState ( ( ) => withReact ( createEditor ( ) ) ) ;
92+ const [ editor ] = useState ( ( ) => {
93+ const editor = withReact ( createEditor ( ) ) ;
94+ editor . onChange = ( ) => {
95+ const suggestionAwareTextComponents : SuggestionAwareText [ ] [ ] =
96+ editor . children . map ( ( node ) => {
97+ if ( Element . isElement ( node ) ) {
98+ return node . children . map ( ( child ) => {
99+ return child ;
100+ } ) ;
101+ } else {
102+ return [ node ] ;
103+ }
104+ } ) ;
105+
106+ const flattened = suggestionAwareTextComponents . reduce (
107+ ( acc , val ) => acc . concat ( val ) ,
108+ [ ]
109+ ) ;
110+ const text = flattened
111+ . map ( ( textComponent ) => textComponent . text )
112+ . join ( "\n" ) ;
113+
114+ props . onChange ?.( text ) ;
115+ } ;
116+ return editor ;
117+ } ) ;
97118
98119 return (
99120 // Add the editable component inside the context.
100121 < Slate editor = { editor } initialValue = { initialValue } >
101122 < Editable
102- className = "p-4"
123+ className = { props . className }
103124 renderElement = { renderElement }
104125 renderLeaf = { renderLeaf }
105126 onKeyDown = { ( event ) => {
0 commit comments