@@ -10,7 +10,11 @@ import {
1010 hasMentionClipboardHtml ,
1111 normalizeMentionClipboardHtml ,
1212} from "@/features/messages/lib/normalizeMentionClipboard" ;
13- import { useRichTextEditor } from "@/features/messages/lib/useRichTextEditor" ;
13+ import {
14+ type LinkSelectionInfo ,
15+ useRichTextEditor ,
16+ } from "@/features/messages/lib/useRichTextEditor" ;
17+ import { useLinkEditor } from "@/features/messages/lib/useLinkEditor" ;
1418import { DropZoneOverlay } from "@/features/messages/ui/ComposerAttachments" ;
1519import type { MentionSuggestion } from "@/features/messages/ui/MentionAutocomplete" ;
1620import { MessageComposerToolbar } from "@/features/messages/ui/MessageComposerToolbar" ;
@@ -77,13 +81,24 @@ export function ForumComposer({
7781
7882 const submitMessageRef = React . useRef < ( ) => void > ( ( ) => { } ) ;
7983
84+ // Set after `useLinkEditor` exists; the editor's link-click handler
85+ // delegates through this ref to break the hook ordering cycle.
86+ const onEditLinkRef = React . useRef <
87+ ( ( info : LinkSelectionInfo ) => void ) | null
88+ > ( null ) ;
89+ const onLinkSelectionChangeRef = React . useRef <
90+ ( ( info : LinkSelectionInfo | null ) => void ) | null
91+ > ( null ) ;
92+
8093 const richText = useRichTextEditor ( {
8194 placeholder,
8295 editable : ! disabled ,
8396 mentionNames : mentions . knownNames ,
8497 channelNames : channelLinks . knownChannelNames ,
8598 onSubmit : ( ) => submitMessageRef . current ( ) ,
8699 isAutocompleteOpen : isAutocompleteOpenRef ,
100+ onEditLink : ( info ) => onEditLinkRef . current ?.( info ) ,
101+ onLinkSelectionChange : ( info ) => onLinkSelectionChangeRef . current ?.( info ) ,
87102 onUpdate : ( { markdown, text } ) => {
88103 setContent ( markdown ) ;
89104 contentRef . current = markdown ;
@@ -94,6 +109,10 @@ export function ForumComposer({
94109 } ,
95110 } ) ;
96111
112+ const linkEditor = useLinkEditor ( richText ) ;
113+ onEditLinkRef . current = linkEditor . openFromClick ;
114+ onLinkSelectionChangeRef . current = linkEditor . showFromCursor ;
115+
97116 // ── Mention / channel autocomplete insertion ────────────────────────
98117 // Native ProseMirror transactions — no markdown round-trip.
99118 const applyMentionInsert = React . useCallback (
@@ -263,12 +282,22 @@ export function ForumComposer({
263282 }
264283 return ;
265284 }
285+
286+ if ( event . key === "Tab" && ! event . shiftKey && linkEditor . isCardOpen ) {
287+ event . preventDefault ( ) ;
288+ if ( ! linkEditor . focusCardFirstControl ( ) ) {
289+ requestAnimationFrame ( linkEditor . focusCardFirstControl ) ;
290+ }
291+ return ;
292+ }
266293 } ,
267294 [
268295 channelLinks . handleChannelKeyDown ,
269296 applyChannelInsert ,
270297 mentions . handleMentionKeyDown ,
271298 applyMentionInsert ,
299+ linkEditor . isCardOpen ,
300+ linkEditor . focusCardFirstControl ,
272301 ] ,
273302 ) ;
274303
@@ -379,97 +408,104 @@ export function ForumComposer({
379408 } , [ compact , isCompactExpanded , richText . focus ] ) ;
380409 const autocompletePosition = autocompleteBelow ? "below" : "above" ;
381410 return (
382- < form
383- className = { cn (
384- "relative rounded-2xl border border-input bg-card px-3 py-2 sm:px-4" ,
385- className ,
386- ) }
387- onBlurCapture = { handleFormBlur }
388- onDragEnter = { ( event ) => {
389- expandCompactComposer ( ) ;
390- media . handleDragEnter ( event ) ;
391- } }
392- onDragLeave = { media . handleDragLeave }
393- onDragOver = { media . handleDragOver }
394- onDrop = { ( e ) => {
395- void media . handleDrop ( e ) ;
396- } }
397- onFocusCapture = { expandCompactComposer }
398- onSubmit = { handleSubmit }
399- >
400- { media . isDragOver && < DropZoneOverlay /> }
401- { isCompactLayout ? (
402- < ForumComposerCompactLayout
403- editor = { richText . editor }
404- header = { header }
405- isSending = { isSending }
406- onEditorKeyDown = { handleEditorKeyDown }
407- sendDisabled = { sendDisabled }
408- />
409- ) : (
410- < >
411- { header ? (
411+ < >
412+ < form
413+ className = { cn (
414+ "relative rounded-2xl border border-input bg-card px-3 py-2 sm:px-4" ,
415+ className ,
416+ ) }
417+ onBlurCapture = { handleFormBlur }
418+ onDragEnter = { ( event ) => {
419+ expandCompactComposer ( ) ;
420+ media . handleDragEnter ( event ) ;
421+ } }
422+ onDragLeave = { media . handleDragLeave }
423+ onDragOver = { media . handleDragOver }
424+ onDrop = { ( e ) => {
425+ void media . handleDrop ( e ) ;
426+ } }
427+ onFocusCapture = { expandCompactComposer }
428+ onSubmit = { handleSubmit }
429+ >
430+ { media . isDragOver && < DropZoneOverlay /> }
431+ { isCompactLayout ? (
432+ < ForumComposerCompactLayout
433+ editor = { richText . editor }
434+ header = { header }
435+ isSending = { isSending }
436+ onEditorKeyDown = { handleEditorKeyDown }
437+ sendDisabled = { sendDisabled }
438+ />
439+ ) : (
440+ < >
441+ { header ? (
442+ < div
443+ className = { cn ( "mb-2" , compact && "flex min-h-10 items-center" ) }
444+ >
445+ { header }
446+ </ div >
447+ ) : null }
448+ < ForumComposerAutocompletes
449+ channelSelectedIndex = { channelLinks . channelSelectedIndex }
450+ channelSuggestions = {
451+ channelLinks . isChannelOpen
452+ ? channelLinks . channelSuggestions
453+ : [ ]
454+ }
455+ mentionSelectedIndex = { mentions . mentionSelectedIndex }
456+ mentionSuggestions = {
457+ mentions . isMentionOpen ? mentions . suggestions : [ ]
458+ }
459+ onChannelSelect = { applyChannelInsert }
460+ onMentionSelect = { applyMentionInsert }
461+ position = { autocompletePosition }
462+ />
463+
464+ < ForumComposerMediaStatus media = { media } />
465+
466+ { /* biome-ignore lint/a11y/noStaticElementInteractions: keydown handler bridges Tiptap editor to autocomplete and submit */ }
412467 < div
413- className = { cn ( "mb-2" , compact && "flex min-h-10 items-center" ) }
468+ className = "rich-text-composer max-h-32 overflow-y-auto"
469+ onKeyDown = { handleEditorKeyDown }
414470 >
415- { header }
471+ < EditorContent editor = { richText . editor } />
416472 </ div >
417- ) : null }
418- < ForumComposerAutocompletes
419- channelSelectedIndex = { channelLinks . channelSelectedIndex }
420- channelSuggestions = {
421- channelLinks . isChannelOpen ? channelLinks . channelSuggestions : [ ]
422- }
423- mentionSelectedIndex = { mentions . mentionSelectedIndex }
424- mentionSuggestions = {
425- mentions . isMentionOpen ? mentions . suggestions : [ ]
426- }
427- onChannelSelect = { applyChannelInsert }
428- onMentionSelect = { applyMentionInsert }
429- position = { autocompletePosition }
430- />
431-
432- < ForumComposerMediaStatus media = { media } />
433-
434- { /* biome-ignore lint/a11y/noStaticElementInteractions: keydown handler bridges Tiptap editor to autocomplete and submit */ }
435- < div
436- className = "rich-text-composer max-h-32 overflow-y-auto"
437- onKeyDown = { handleEditorKeyDown }
438- >
439- < EditorContent editor = { richText . editor } />
440- </ div >
441473
442- < MessageComposerToolbar
443- composerDisabled = { disabled ?? false }
444- editor = { richText . editor }
445- extraActions = {
446- onCancel ? (
447- < Button
448- disabled = { isSending }
449- onClick = { onCancel }
450- size = "sm"
451- type = "button"
452- variant = "ghost"
453- >
454- Cancel
455- </ Button >
456- ) : undefined
457- }
458- formattingDisabled = { disabled ?? false }
459- isEmojiPickerOpen = { isEmojiPickerOpen }
460- isFormattingOpen = { isFormattingOpen }
461- isSending = { isSending ?? false }
462- isUploading = { media . isUploading }
463- onCaptureSelection = { handleToolbarMouseDown }
464- onEmojiPickerOpenChange = { setIsEmojiPickerOpen }
465- onEmojiSelect = { insertEmoji }
466- onFormattingToggle = { handleFormattingToggle }
467- onOpenMentionPicker = { openMentionPicker }
468- onPaperclip = { handlePaperclipClick }
469- sendDisabled = { sendDisabled }
470- />
471- </ >
472- ) }
473- </ form >
474+ < MessageComposerToolbar
475+ composerDisabled = { disabled ?? false }
476+ editor = { richText . editor }
477+ extraActions = {
478+ onCancel ? (
479+ < Button
480+ disabled = { isSending }
481+ onClick = { onCancel }
482+ size = "sm"
483+ type = "button"
484+ variant = "ghost"
485+ >
486+ Cancel
487+ </ Button >
488+ ) : undefined
489+ }
490+ formattingDisabled = { disabled ?? false }
491+ isEmojiPickerOpen = { isEmojiPickerOpen }
492+ isFormattingOpen = { isFormattingOpen }
493+ isSending = { isSending ?? false }
494+ isUploading = { media . isUploading }
495+ onCaptureSelection = { handleToolbarMouseDown }
496+ onEmojiPickerOpenChange = { setIsEmojiPickerOpen }
497+ onEmojiSelect = { insertEmoji }
498+ onFormattingToggle = { handleFormattingToggle }
499+ onLinkButton = { linkEditor . openFromToolbar }
500+ onOpenMentionPicker = { openMentionPicker }
501+ onPaperclip = { handlePaperclipClick }
502+ sendDisabled = { sendDisabled }
503+ />
504+ </ >
505+ ) }
506+ </ form >
507+ { linkEditor . card }
508+ { linkEditor . dialog }
509+ </ >
474510 ) ;
475511}
0 commit comments