Skip to content

Commit 770266d

Browse files
tellahonpub1223z34hd7vtwc6qj4s7flsxkj644nlre2nthu7lrrmkumhu3xddsrx9r6wnpub14vtk7pvazqrq9639qu7e560wnqtl0d53ca4gjuvq6jzf3k2el23qqlwa7f
authored
feat(composer): link editing — inline popover, modal picker, buzz:// in-app nav (#1045)
Signed-off-by: Taylor Ho <[email protected]> Co-authored-by: npub1223z34hd7vtwc6qj4s7flsxkj644nlre2nthu7lrrmkumhu3xddsrx9r6w <52a228d6edf316ec6812ac3c9fc0d696ab59fc7954d77e7be31eedcddf91335b@sprout-oss.stage.blox.sqprod.co> Co-authored-by: npub14vtk7pvazqrq9639qu7e560wnqtl0d53ca4gjuvq6jzf3k2el23qqlwa7f <ab176f059d100602ea25073d9a69ee9817f7b691c76a897180d48498d959faa2@sprout-oss.stage.blox.sqprod.co>
1 parent 48994e0 commit 770266d

15 files changed

Lines changed: 1231 additions & 92 deletions

‎desktop/src/features/forum/ui/ForumComposer.tsx‎

Lines changed: 125 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -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";
1418
import { DropZoneOverlay } from "@/features/messages/ui/ComposerAttachments";
1519
import type { MentionSuggestion } from "@/features/messages/ui/MentionAutocomplete";
1620
import { 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
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import assert from "node:assert/strict";
2+
import test from "node:test";
3+
4+
import { getLinkEditorInitialFocus } from "./linkEditorFocus.ts";
5+
6+
test("focuses URL when adding a link to selected text", () => {
7+
assert.equal(
8+
getLinkEditorInitialFocus({
9+
href: "",
10+
text: "selected text",
11+
from: 3,
12+
to: 16,
13+
}),
14+
"url",
15+
);
16+
});
17+
18+
test("focuses display text when editing an existing link", () => {
19+
assert.equal(
20+
getLinkEditorInitialFocus({
21+
href: "https://example.com",
22+
text: "existing link",
23+
from: 3,
24+
to: 16,
25+
}),
26+
"text",
27+
);
28+
});
29+
30+
test("focuses display text when inserting a link with no selected text", () => {
31+
assert.equal(
32+
getLinkEditorInitialFocus({
33+
href: "",
34+
text: "",
35+
from: 3,
36+
to: 3,
37+
}),
38+
"text",
39+
);
40+
});
41+
42+
test("focuses display text when the selected text is whitespace only", () => {
43+
assert.equal(
44+
getLinkEditorInitialFocus({
45+
href: "",
46+
text: " ",
47+
from: 3,
48+
to: 6,
49+
}),
50+
"text",
51+
);
52+
});
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import type { LinkSelectionInfo } from "./useRichTextEditor";
2+
3+
export type LinkEditorInitialFocus = "text" | "url";
4+
5+
export function getLinkEditorInitialFocus(
6+
info: LinkSelectionInfo,
7+
): LinkEditorInitialFocus {
8+
const isSelectedTextLinkInsert =
9+
info.href.length === 0 &&
10+
info.text.trim().length > 0 &&
11+
info.from !== info.to;
12+
13+
return isSelectedTextLinkInsert ? "url" : "text";
14+
}

0 commit comments

Comments
 (0)