@@ -38,17 +38,14 @@ import {
3838} from "lexical" ;
3939import {
4040 createContext ,
41- forwardRef ,
41+ use ,
4242 useCallback ,
43- useContext ,
4443 useEffect ,
44+ useEffectEvent ,
4545 useImperativeHandle ,
4646 useLayoutEffect ,
4747 useMemo ,
4848 useRef ,
49- type ClipboardEventHandler ,
50- type ReactElement ,
51- type Ref ,
5249} from "react" ;
5350
5451import {
@@ -160,7 +157,7 @@ function ComposerMentionDecorator(props: { path: string }) {
160157 ) ;
161158}
162159
163- class ComposerMentionNode extends DecoratorNode < ReactElement > {
160+ class ComposerMentionNode extends DecoratorNode < React . ReactElement > {
164161 __path : string ;
165162
166163 static override getType ( ) : string {
@@ -208,7 +205,7 @@ class ComposerMentionNode extends DecoratorNode<ReactElement> {
208205 return true ;
209206 }
210207
211- override decorate ( ) : ReactElement {
208+ override decorate ( ) : React . ReactElement {
212209 return < ComposerMentionDecorator path = { this . __path } /> ;
213210 }
214211}
@@ -278,7 +275,7 @@ function ComposerSkillDecorator(props: { skillLabel: string; skillDescription: s
278275 ) ;
279276}
280277
281- class ComposerSkillNode extends DecoratorNode < ReactElement > {
278+ class ComposerSkillNode extends DecoratorNode < React . ReactElement > {
282279 __skillName : string ;
283280 __skillLabel : string ;
284281 __skillDescription : string | null ;
@@ -346,7 +343,7 @@ class ComposerSkillNode extends DecoratorNode<ReactElement> {
346343 return true ;
347344 }
348345
349- override decorate ( ) : ReactElement {
346+ override decorate ( ) : React . ReactElement {
350347 return (
351348 < ComposerSkillDecorator
352349 skillLabel = { this . __skillLabel }
@@ -368,7 +365,7 @@ function ComposerTerminalContextDecorator(props: { context: TerminalContextDraft
368365 return < ComposerPendingTerminalContextChip context = { props . context } /> ;
369366}
370367
371- class ComposerTerminalContextNode extends DecoratorNode < ReactElement > {
368+ class ComposerTerminalContextNode extends DecoratorNode < React . ReactElement > {
372369 __context : TerminalContextDraft ;
373370
374371 static override getType ( ) : string {
@@ -417,7 +414,7 @@ class ComposerTerminalContextNode extends DecoratorNode<ReactElement> {
417414 return true ;
418415 }
419416
420- override decorate ( ) : ReactElement {
417+ override decorate ( ) : React . ReactElement {
421418 return < ComposerTerminalContextDecorator context = { this . __context } /> ;
422419 }
423420}
@@ -900,11 +897,8 @@ interface ComposerPromptEditorProps {
900897 key : "ArrowDown" | "ArrowUp" | "Enter" | "Tab" ,
901898 event : KeyboardEvent ,
902899 ) => boolean ;
903- onPaste : ClipboardEventHandler < HTMLElement > ;
904- }
905-
906- interface ComposerPromptEditorInnerProps extends ComposerPromptEditorProps {
907- editorRef : Ref < ComposerPromptEditorHandle > ;
900+ onPaste : React . ClipboardEventHandler < HTMLElement > ;
901+ editorRef : React . RefObject < ComposerPromptEditorHandle | null > ;
908902}
909903
910904function ComposerCommandKeyPlugin ( props : {
@@ -1059,7 +1053,7 @@ function ComposerInlineTokenSelectionNormalizePlugin() {
10591053
10601054function ComposerInlineTokenBackspacePlugin ( ) {
10611055 const [ editor ] = useLexicalComposerContext ( ) ;
1062- const { onRemoveTerminalContext } = useContext ( ComposerTerminalContextActionsContext ) ;
1056+ const { onRemoveTerminalContext } = use ( ComposerTerminalContextActionsContext ) ;
10631057
10641058 useEffect ( ( ) => {
10651059 return editor . registerCommand (
@@ -1150,68 +1144,65 @@ function ComposerSurroundSelectionPlugin(props: {
11501144 skillMetadataRef . current = skillMetadataByName ( props . skills ) ;
11511145 } , [ props . skills ] ) ;
11521146
1153- const applySurroundInsertion = useCallback (
1154- ( inputData : string ) : boolean => {
1155- const surroundCloseSymbol = SURROUND_SYMBOLS_MAP . get ( inputData ) ;
1156- const pendingSurroundSelection = pendingSurroundSelectionRef . current ;
1157- if ( ! surroundCloseSymbol ) {
1158- pendingSurroundSelectionRef . current = null ;
1159- return false ;
1160- }
1147+ const applySurroundInsertion = useEffectEvent ( ( inputData : string ) : boolean => {
1148+ const surroundCloseSymbol = SURROUND_SYMBOLS_MAP . get ( inputData ) ;
1149+ const pendingSurroundSelection = pendingSurroundSelectionRef . current ;
1150+ if ( ! surroundCloseSymbol ) {
1151+ pendingSurroundSelectionRef . current = null ;
1152+ return false ;
1153+ }
11611154
1162- let handled = false ;
1163- editor . update ( ( ) => {
1164- const selectionSnapshot =
1165- pendingSurroundSelection ??
1166- ( ( ) => {
1167- const selection = $getSelection ( ) ;
1168- if ( ! $isRangeSelection ( selection ) || selection . isCollapsed ( ) ) {
1169- return null ;
1170- }
1171- if ( $selectionTouchesInlineToken ( selection ) ) {
1172- return null ;
1173- }
1174- const range = getSelectionRangeForExpandedComposerOffsets ( selection ) ;
1175- if ( ! range || range . start === range . end ) {
1176- return null ;
1177- }
1178- const value = $getRoot ( ) . getTextContent ( ) ;
1179- if ( selectionTouchesMentionBoundary ( value , range . start , range . end ) ) {
1180- return null ;
1181- }
1182- return {
1183- value,
1184- expandedStart : range . start ,
1185- expandedEnd : range . end ,
1186- } ;
1187- } ) ( ) ;
1188-
1189- if ( ! selectionSnapshot || ! surroundCloseSymbol ) {
1190- return ;
1191- }
1155+ let handled = false ;
1156+ editor . update ( ( ) => {
1157+ const selectionSnapshot =
1158+ pendingSurroundSelection ??
1159+ ( ( ) => {
1160+ const selection = $getSelection ( ) ;
1161+ if ( ! $isRangeSelection ( selection ) || selection . isCollapsed ( ) ) {
1162+ return null ;
1163+ }
1164+ if ( $selectionTouchesInlineToken ( selection ) ) {
1165+ return null ;
1166+ }
1167+ const range = getSelectionRangeForExpandedComposerOffsets ( selection ) ;
1168+ if ( ! range || range . start === range . end ) {
1169+ return null ;
1170+ }
1171+ const value = $getRoot ( ) . getTextContent ( ) ;
1172+ if ( selectionTouchesMentionBoundary ( value , range . start , range . end ) ) {
1173+ return null ;
1174+ }
1175+ return {
1176+ value,
1177+ expandedStart : range . start ,
1178+ expandedEnd : range . end ,
1179+ } ;
1180+ } ) ( ) ;
1181+
1182+ if ( ! selectionSnapshot || ! surroundCloseSymbol ) {
1183+ return ;
1184+ }
11921185
1193- const selectedText = selectionSnapshot . value . slice (
1194- selectionSnapshot . expandedStart ,
1195- selectionSnapshot . expandedEnd ,
1196- ) ;
1197- const nextValue = `${ selectionSnapshot . value . slice ( 0 , selectionSnapshot . expandedStart ) } ${ inputData } ${ selectedText } ${ surroundCloseSymbol } ${ selectionSnapshot . value . slice ( selectionSnapshot . expandedEnd ) } ` ;
1198- $setComposerEditorPrompt ( nextValue , terminalContextsRef . current , skillMetadataRef . current ) ;
1199- const selectionStart = collapseExpandedComposerCursor (
1200- nextValue ,
1201- selectionSnapshot . expandedStart ,
1202- ) ;
1203- $setSelectionRangeAtComposerOffsets (
1204- selectionStart + inputData . length ,
1205- selectionStart + inputData . length + selectedText . length ,
1206- ) ;
1207- handled = true ;
1208- pendingSurroundSelectionRef . current = null ;
1209- } ) ;
1186+ const selectedText = selectionSnapshot . value . slice (
1187+ selectionSnapshot . expandedStart ,
1188+ selectionSnapshot . expandedEnd ,
1189+ ) ;
1190+ const nextValue = `${ selectionSnapshot . value . slice ( 0 , selectionSnapshot . expandedStart ) } ${ inputData } ${ selectedText } ${ surroundCloseSymbol } ${ selectionSnapshot . value . slice ( selectionSnapshot . expandedEnd ) } ` ;
1191+ $setComposerEditorPrompt ( nextValue , terminalContextsRef . current , skillMetadataRef . current ) ;
1192+ const selectionStart = collapseExpandedComposerCursor (
1193+ nextValue ,
1194+ selectionSnapshot . expandedStart ,
1195+ ) ;
1196+ $setSelectionRangeAtComposerOffsets (
1197+ selectionStart + inputData . length ,
1198+ selectionStart + inputData . length + selectedText . length ,
1199+ ) ;
1200+ handled = true ;
1201+ pendingSurroundSelectionRef . current = null ;
1202+ } ) ;
12101203
1211- return handled ;
1212- } ,
1213- [ editor ] ,
1214- ) ;
1204+ return handled ;
1205+ } ) ;
12151206
12161207 useEffect ( ( ) => {
12171208 const onKeyDown = ( event : KeyboardEvent ) => {
@@ -1388,7 +1379,7 @@ function ComposerSurroundSelectionPlugin(props: {
13881379 }
13891380 unregisterRootListener ( ) ;
13901381 } ;
1391- } , [ applySurroundInsertion , editor ] ) ;
1382+ } , [ editor ] ) ;
13921383
13931384 return null ;
13941385}
@@ -1406,7 +1397,7 @@ function ComposerPromptEditorInner({
14061397 onCommandKeyDown,
14071398 onPaste,
14081399 editorRef,
1409- } : ComposerPromptEditorInnerProps ) {
1400+ } : ComposerPromptEditorProps ) {
14101401 const [ editor ] = useLexicalComposerContext ( ) ;
14111402 const onChangeRef = useRef ( onChange ) ;
14121403 const initialCursor = clampCollapsedComposerCursor ( value , cursor ) ;
@@ -1614,13 +1605,13 @@ function ComposerPromptEditorInner({
16141605 } , [ ] ) ;
16151606
16161607 return (
1617- < ComposerTerminalContextActionsContext . Provider value = { terminalContextActions } >
1608+ < ComposerTerminalContextActionsContext value = { terminalContextActions } >
16181609 < div className = "relative" >
16191610 < PlainTextPlugin
16201611 contentEditable = {
16211612 < ContentEditable
16221613 className = { cn (
1623- "block max-h-[200px] min-h-17.5 w-full overflow-y-auto whitespace-pre-wrap wrap-break-word bg-transparent text-[16px] leading-relaxed text-foreground focus:outline-none sm:text-[14px]" ,
1614+ "block max-h-50 min-h-17.5 w-full overflow-y-auto whitespace-pre-wrap wrap-break-word bg-transparent text-[16px] leading-relaxed text-foreground focus:outline-none sm:text-[14px]" ,
16241615 className ,
16251616 ) }
16261617 data-testid = "composer-editor"
@@ -1646,29 +1637,24 @@ function ComposerPromptEditorInner({
16461637 < ComposerInlineTokenBackspacePlugin />
16471638 < HistoryPlugin />
16481639 </ div >
1649- </ ComposerTerminalContextActionsContext . Provider >
1640+ </ ComposerTerminalContextActionsContext >
16501641 ) ;
16511642}
16521643
1653- export const ComposerPromptEditor = forwardRef <
1654- ComposerPromptEditorHandle ,
1655- ComposerPromptEditorProps
1656- > ( function ComposerPromptEditor (
1657- {
1658- value,
1659- cursor,
1660- terminalContexts,
1661- skills,
1662- disabled,
1663- placeholder,
1664- className,
1665- onRemoveTerminalContext,
1666- onChange,
1667- onCommandKeyDown,
1668- onPaste,
1669- } ,
1670- ref ,
1671- ) {
1644+ export function ComposerPromptEditor ( {
1645+ value,
1646+ cursor,
1647+ terminalContexts,
1648+ skills,
1649+ disabled,
1650+ placeholder,
1651+ className,
1652+ onRemoveTerminalContext,
1653+ onChange,
1654+ onCommandKeyDown,
1655+ onPaste,
1656+ editorRef,
1657+ } : ComposerPromptEditorProps ) {
16721658 const initialValueRef = useRef ( value ) ;
16731659 const initialTerminalContextsRef = useRef ( terminalContexts ) ;
16741660 const initialSkillMetadataRef = useRef ( skillMetadataByName ( skills ) ) ;
@@ -1703,10 +1689,10 @@ export const ComposerPromptEditor = forwardRef<
17031689 onRemoveTerminalContext = { onRemoveTerminalContext }
17041690 onChange = { onChange }
17051691 onPaste = { onPaste }
1706- editorRef = { ref }
1692+ editorRef = { editorRef }
17071693 { ...( onCommandKeyDown ? { onCommandKeyDown } : { } ) }
17081694 { ...( className ? { className } : { } ) }
17091695 />
17101696 </ LexicalComposer >
17111697 ) ;
1712- } ) ;
1698+ }
0 commit comments