@@ -29,6 +29,7 @@ import {
2929 useMemo ,
3030 useRef ,
3131 useState ,
32+ type KeyboardEvent as ReactKeyboardEvent ,
3233} from "react" ;
3334import { EditorContent , useEditor } from "@tiptap/react" ;
3435
@@ -371,13 +372,40 @@ function ComposerCitationNodeView({ node, editor, getPos }: NodeViewProps) {
371372 . run ( ) ;
372373 } , [ editor , nodePos ] ) ;
373374
375+ // Put the caret right after the chip so Enter sends and typing continues the prompt.
376+ const onRestoreFocus = useCallback ( ( ) => {
377+ if ( ! editor . isEditable ) return ;
378+ const pos = nodePos ( ) ;
379+ if ( pos === null ) return ;
380+ const current = editor . state . doc . nodeAt ( pos ) ;
381+ if ( ! current || current . type . name !== "composer-citation" ) return ;
382+ editor . commands . focus ( pos + current . nodeSize ) ;
383+ } , [ editor , nodePos ] ) ;
384+
374385 return (
375386 < NodeViewWrapper
376387 as = "span"
377388 className = "inline-flex min-w-0 max-w-full select-none"
378389 contentEditable = { false }
379390 spellCheck = { false }
380391 data-composer-citation-chip = "true"
392+ onKeyDown = { ( event : ReactKeyboardEvent < HTMLElement > ) => {
393+ // Tab from the comment button returns to the caret after the chip.
394+ if (
395+ ! editor . isEditable ||
396+ event . key !== "Tab" ||
397+ event . shiftKey ||
398+ event . altKey ||
399+ event . metaKey ||
400+ event . ctrlKey ||
401+ ! ( event . target instanceof HTMLElement ) ||
402+ event . target . dataset . citationCommentTrigger === undefined
403+ ) {
404+ return ;
405+ }
406+ event . preventDefault ( ) ;
407+ onRestoreFocus ( ) ;
408+ } }
381409 >
382410 < AssistantCitationChip
383411 citation = { citation }
@@ -396,6 +424,7 @@ function ComposerCitationNodeView({ node, editor, getPos }: NodeViewProps) {
396424 commentContext . onSubmitAndSend ( ) ;
397425 return true ;
398426 } ,
427+ onRestoreFocus,
399428 } }
400429 />
401430 </ NodeViewWrapper >
@@ -856,6 +885,32 @@ function ComposerPromptEditorTiptapInner(props: ComposerPromptEditorProps) {
856885 return true ;
857886 }
858887 }
888+ // Shift+Tab from just after a citation reaches its comment button, which
889+ // native tab order skips because the chip lives inside the editor.
890+ if (
891+ event . key === "Tab" &&
892+ event . shiftKey &&
893+ ! event . altKey &&
894+ ! event . metaKey &&
895+ ! event . ctrlKey &&
896+ view . state . selection . empty
897+ ) {
898+ const { $from } = view . state . selection ;
899+ const citation = $from . nodeBefore ;
900+ if ( citation ?. type . name === "composer-citation" ) {
901+ const chip = view . nodeDOM ( $from . pos - citation . nodeSize ) ;
902+ const commentButton =
903+ chip instanceof HTMLElement
904+ ? chip . querySelector < HTMLElement > ( "[data-citation-comment-trigger]" )
905+ : null ;
906+ if ( commentButton ) {
907+ event . preventDefault ( ) ;
908+ event . stopPropagation ( ) ;
909+ commentButton . focus ( ) ;
910+ return true ;
911+ }
912+ }
913+ }
859914 if ( event . key === "Enter" && ( event . isComposing || event . keyCode === 229 ) ) {
860915 event . stopPropagation ( ) ;
861916 return true ;
0 commit comments