Skip to content

Commit e550dce

Browse files
committed
Port from block/buzz#6684: hyperlink selected composer text on link paste
Pasting exactly one http(s) link while composer text is selected now turns the selection into a markdown link ([selected text](url)) instead of replacing it — the behavior every rich text editor ships. - resolveExactLinkPaste(): recognizes a clipboard payload that is exactly one supported link (bare or <...>-wrapped, host required, no prose or trailing punctuation) and returns the href. - selectionLinkLabel(): the selected composer text eligible for linking; rejects collapsed selections, selections spanning ref chips or line breaks, and whitespace-only selections. - markdownLinkFor(): builds the markdown link, escaping square brackets. - handlePaste wires the three together ahead of the @url: chip path; any non-qualifying paste falls through to existing behavior. Adapted from Buzz's TipTap link-mark approach to Hermes' contenteditable composer: we emit a markdown link (the composer's native rich construct) rather than a ProseMirror mark.
1 parent 93b9880 commit e550dce

3 files changed

Lines changed: 175 additions & 3 deletions

File tree

‎apps/desktop/src/app/chat/composer/index.tsx‎

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ import { ComposerTriggerPopover } from './trigger-popover'
8888
import type { ChatBarProps } from './types'
8989
import { isRedoShortcut, isUndoShortcut } from './undo-history'
9090
import { UrlDialog } from './url-dialog'
91-
import { chipTypedUrlOnSpace, linkifyUrls } from './url-refs'
91+
import { chipTypedUrlOnSpace, linkifyUrls, markdownLinkFor, resolveExactLinkPaste, selectionLinkLabel } from './url-refs'
9292
import { VoiceActivity, VoicePlaybackActivity } from './voice-activity'
9393

9494
export function ChatBar({
@@ -564,6 +564,24 @@ export function ChatBar({
564564

565565
event.preventDefault()
566566

567+
// Pasting exactly one link while composer text is selected turns that text
568+
// into a markdown link instead of replacing it — the behavior every rich
569+
// editor ships (ported from block/buzz#6684). Selections that span chips
570+
// or lines fall through to the normal replace-with-chip path.
571+
const exactLink = resolveExactLinkPaste(pastedText)
572+
573+
if (exactLink) {
574+
const label = selectionLinkLabel(event.currentTarget)
575+
576+
if (label) {
577+
recordUndoPoint()
578+
insertComposerContentsAtCaret(event.currentTarget, markdownLinkFor(label, exactLink))
579+
scheduleFlushEditorToDraft(event.currentTarget)
580+
581+
return
582+
}
583+
}
584+
567585
// A paste past the large-paste threshold becomes a `.txt` attachment chip
568586
// instead of flooding the composer.
569587
// The instruction the user types stays in the input; the pasted source

‎apps/desktop/src/app/chat/composer/url-refs.test.ts‎

Lines changed: 91 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import type { KeyboardEvent } from 'react'
22
import { describe, expect, it } from 'vitest'
33

4-
import { composerPlainText, RICH_INPUT_SLOT } from './rich-editor'
5-
import { chipTypedUrlOnSpace, linkifyUrls } from './url-refs'
4+
import { composerPlainText, refChipElement, RICH_INPUT_SLOT } from './rich-editor'
5+
import { chipTypedUrlOnSpace, linkifyUrls, markdownLinkFor, resolveExactLinkPaste, selectionLinkLabel } from './url-refs'
66

77
/** An editor holding `text` with a collapsed caret at `caret`, plus the space
88
* keydown the composer would hand `chipTypedUrlOnSpace`. */
@@ -51,6 +51,95 @@ describe('linkifyUrls', () => {
5151
})
5252
})
5353

54+
describe('resolveExactLinkPaste', () => {
55+
it('accepts a lone bare link', () => {
56+
expect(resolveExactLinkPaste('https://example.dev/a/b')).toBe('https://example.dev/a/b')
57+
})
58+
59+
it('accepts a wrapped <link> and surrounding whitespace', () => {
60+
expect(resolveExactLinkPaste(' <https://example.dev/a> ')).toBe('https://example.dev/a')
61+
})
62+
63+
it('rejects prose around the link', () => {
64+
expect(resolveExactLinkPaste('see https://example.dev')).toBeNull()
65+
expect(resolveExactLinkPaste('https://example.dev is nice')).toBeNull()
66+
})
67+
68+
it('rejects multiple links', () => {
69+
expect(resolveExactLinkPaste('https://a.dev https://b.dev')).toBeNull()
70+
})
71+
72+
it('rejects trailing sentence punctuation and hostless schemes', () => {
73+
expect(resolveExactLinkPaste('https://example.dev.')).toBeNull()
74+
expect(resolveExactLinkPaste('https://')).toBeNull()
75+
})
76+
})
77+
78+
describe('selectionLinkLabel', () => {
79+
const selectAll = (build: (editor: HTMLElement) => void) => {
80+
const editor = document.createElement('div')
81+
editor.dataset.slot = RICH_INPUT_SLOT
82+
build(editor)
83+
document.body.append(editor)
84+
85+
const selection = window.getSelection()!
86+
const range = document.createRange()
87+
88+
range.selectNodeContents(editor)
89+
selection.removeAllRanges()
90+
selection.addRange(range)
91+
92+
return editor
93+
}
94+
95+
it('returns the selected text', () => {
96+
const editor = selectAll(node => {
97+
node.textContent = 'the docs'
98+
})
99+
100+
expect(selectionLinkLabel(editor)).toBe('the docs')
101+
editor.remove()
102+
})
103+
104+
it('rejects a collapsed selection', () => {
105+
const editor = document.createElement('div')
106+
editor.textContent = 'text'
107+
document.body.append(editor)
108+
window.getSelection()?.removeAllRanges()
109+
110+
expect(selectionLinkLabel(editor)).toBeNull()
111+
editor.remove()
112+
})
113+
114+
it('rejects a selection containing a chip', () => {
115+
const editor = selectAll(node => {
116+
node.append(document.createTextNode('see '), refChipElement('url', '`https://a.dev`'))
117+
})
118+
119+
expect(selectionLinkLabel(editor)).toBeNull()
120+
editor.remove()
121+
})
122+
123+
it('rejects a multi-line selection', () => {
124+
const editor = selectAll(node => {
125+
node.append(document.createTextNode('one'), document.createElement('br'), document.createTextNode('two'))
126+
})
127+
128+
expect(selectionLinkLabel(editor)).toBeNull()
129+
editor.remove()
130+
})
131+
})
132+
133+
describe('markdownLinkFor', () => {
134+
it('builds a markdown link', () => {
135+
expect(markdownLinkFor('the docs', 'https://example.dev')).toBe('[the docs](https://example.dev)')
136+
})
137+
138+
it('escapes square brackets in the label', () => {
139+
expect(markdownLinkFor('a [b] c', 'https://example.dev')).toBe('[a \\[b\\] c](https://example.dev)')
140+
})
141+
})
142+
54143
describe('chipTypedUrlOnSpace', () => {
55144
it('chips a link typed right before the caret and adds the space', () => {
56145
const { editor, event } = spaceOn('see https://example.dev/a', 25)

‎apps/desktop/src/app/chat/composer/url-refs.ts‎

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,71 @@ export function linkifyUrls(text: string) {
5959
return out + text.slice(cursor)
6060
}
6161

62+
/** The href to apply when a clipboard payload is exactly ONE supported link —
63+
* a bare or `<…>`-wrapped `http(s)` URL with a host and nothing else. Null for
64+
* anything that isn't a lone link (prose, multiple links, trailing text), so
65+
* callers fall through to the normal paste pipeline. Ported from
66+
* block/buzz#6684's `resolveExactLinkPaste`. */
67+
export function resolveExactLinkPaste(raw: string): string | null {
68+
const text = raw.trim()
69+
const unwrapped = text.startsWith('<') && text.endsWith('>') && text.length > 2 ? text.slice(1, -1).trim() : text
70+
71+
URL_RE.lastIndex = 0
72+
73+
const match = URL_RE.exec(unwrapped)
74+
75+
if (!match || match.index !== 0 || match[0].length !== unwrapped.length) {
76+
return null
77+
}
78+
79+
const { trailing, url } = splitUrlTail(unwrapped)
80+
81+
// Trailing sentence punctuation means the user copied prose, not a link.
82+
if (trailing || !hasHost(url)) {
83+
return null
84+
}
85+
86+
return url
87+
}
88+
89+
/** The selected composer text a link paste should hyperlink, or null when the
90+
* selection can't take a link mark: collapsed, outside `editor`, spanning
91+
* chips or line breaks, or whitespace-only. */
92+
export function selectionLinkLabel(editor: HTMLElement): string | null {
93+
const selection = window.getSelection()
94+
95+
if (!selection || selection.rangeCount === 0 || selection.isCollapsed) {
96+
return null
97+
}
98+
99+
const range = selection.getRangeAt(0)
100+
101+
if (!editor.contains(range.commonAncestorContainer)) {
102+
return null
103+
}
104+
105+
const probe = document.createElement('div')
106+
107+
probe.append(range.cloneContents())
108+
109+
// A chip inside the selection is a directive, not prose — linking over it
110+
// would destroy the reference. Multi-line selections don't read as a label.
111+
if (probe.querySelector('[data-ref-text], br')) {
112+
return null
113+
}
114+
115+
const label = probe.textContent?.replace(/\s+/g, ' ').trim() ?? ''
116+
117+
return label || null
118+
}
119+
120+
/** Markdown link for a paste-over-selection: the label the user selected, the
121+
* URL they pasted. Square brackets in the label are escaped so the link
122+
* survives markdown parsing downstream. */
123+
export function markdownLinkFor(label: string, url: string): string {
124+
return `[${label.replace(/([[\]])/g, '\\$1')}](${url})`
125+
}
126+
62127
/** A plain space finishing a typed link commits it as a chip (followed by
63128
* whatever punctuation ended it, then the space). Returns whether it ran, so a
64129
* keydown handler can fall through on anything else. */

0 commit comments

Comments
 (0)