Skip to content

The agent that grows with you

fix(desktop): composer menu word spans pasted text nodes and astral letters

Teknium290080e19e8e
diff --git a/apps/desktop/electron/spellcheck-dictionary-ipc.test.ts b/apps/desktop/electron/spellcheck-dictionary-ipc.test.ts
index 4bcc3fdef3a..713833e68ba 100644
--- a/apps/desktop/electron/spellcheck-dictionary-ipc.test.ts
+++ b/apps/desktop/electron/spellcheck-dictionary-ipc.test.ts
@@ -43,6 +43,9 @@ describe('normalizeDictionaryWord', () => {
'1234',
'--',
'<script>',
+ '-Hermes',
+ "'Hermes'",
+ 'Hermes-',
'a/b',
'x'.repeat(65),
42,
diff --git a/apps/desktop/electron/spellcheck-dictionary-ipc.ts b/apps/desktop/electron/spellcheck-dictionary-ipc.ts
index 5f64d57cdac..e744899614d 100644
--- a/apps/desktop/electron/spellcheck-dictionary-ipc.ts
+++ b/apps/desktop/electron/spellcheck-dictionary-ipc.ts
@@ -5,8 +5,8 @@
import type { IpcMain, IpcMainInvokeEvent, Session, WebContents } from 'electron'
const WORD_MAX = 64
-// One word: letters, combining marks, digits, inner apostrophes and hyphens.
-const WORD_RE = /^[\p{L}\p{M}\p{N}'\u2019-]+$/u
+// One word: letters, combining marks, digits; apostrophes and hyphens only inside.
+const WORD_RE = /^[\p{L}\p{M}\p{N}](?:[\p{L}\p{M}\p{N}'\u2019-]*[\p{L}\p{M}\p{N}])?$/u
const LETTER_RE = /\p{L}/u
type DictionaryOp = (session: Session, word: string) => boolean
diff --git a/apps/desktop/src/app/context-menu/composer-menu.test.tsx b/apps/desktop/src/app/context-menu/composer-menu.test.tsx
index 684574e8c6e..a9e1ea9af68 100644
--- a/apps/desktop/src/app/context-menu/composer-menu.test.tsx
+++ b/apps/desktop/src/app/context-menu/composer-menu.test.tsx
@@ -10,7 +10,7 @@ import {
type ComposerContextMenuItem
} from '@/app/chat/composer/contrib'
import { useComposerUndo } from '@/app/chat/composer/hooks/use-composer-undo'
-import { RICH_INPUT_SLOT } from '@/app/chat/composer/rich-editor'
+import { insertComposerContentsAtCaret, RICH_INPUT_SLOT } from '@/app/chat/composer/rich-editor'
import { createPluginContext } from '@/contrib/plugin'
import { AppContextMenu } from './app-context-menu'
@@ -155,6 +155,52 @@ describe('composer context-menu contribution area', () => {
expect(seen.at(-1)).toEqual({ selection: 'quick', word: 'quick' })
})
+ it('reads and replaces a whole word that a paste split across text nodes', async () => {
+ let action: ComposerContextMenuAction | null = null
+
+ plugin('alpha', () => [{ label: 'Fix', onSelect: received => void (action = received) }])
+
+ const editor = composer('Her')
+
+ // The composer's real paste path appends the pasted text as its own node.
+ select(editor, 3)
+ insertComposerContentsAtCaret(editor, 'mse rocks')
+ expect(editor.childNodes.length).toBeGreaterThan(1)
+
+ const pasted = [...editor.childNodes].find(node => node.textContent === 'mse rocks')!
+ const range = document.createRange()
+
+ range.setStart(pasted, 1)
+ range.collapse(true)
+ window.getSelection()!.removeAllRanges()
+ window.getSelection()!.addRange(range)
+ fireEvent.contextMenu(editor)
+ fireEvent.click(await screen.findByText('Fix'))
+ await waitFor(() => expect(action).not.toBeNull())
+
+ expect(action!.word).toBe('Hermse')
+ expect(action!.replaceWord('Hermes')).toBe(true)
+ expect(editor.textContent).toBe('Hermes rocks')
+ })
+
+ it('keeps astral-plane letters inside the word', async () => {
+ const seen: ComposerContextMenuContext[] = []
+
+ plugin('alpha', context => {
+ seen.push(context)
+
+ return []
+ })
+
+ const editor = composer('hi 𠮷田 there')
+
+ select(editor, 6)
+ fireEvent.contextMenu(editor)
+ await screen.findByText('Paste')
+
+ expect(seen[0]?.word).toBe('𠮷田')
+ })
+
it('merges plugins in a deterministic order, caps each one, and isolates a throwing provider', async () => {
plugin('zeta', () => Array.from({ length: 8 }, (_, index) => ({ label: `zeta ${index + 1}`, onSelect: vi.fn() })))
plugin('broken', () => {
diff --git a/apps/desktop/src/app/context-menu/composer-menu.ts b/apps/desktop/src/app/context-menu/composer-menu.ts
index 41cb3630d6e..5579b99c954 100644
--- a/apps/desktop/src/app/context-menu/composer-menu.ts
+++ b/apps/desktop/src/app/context-menu/composer-menu.ts
@@ -52,56 +52,116 @@ export interface ComposerMenuRow {
run: () => void
}
-/** The word around a collapsed caret, or at a selection's start. A caret sits
- * on a boundary between characters, so step to the nearest word character
- * first; otherwise the word found depends on which side of a gap the caret
- * happened to land. */
-function wordRangeAt(selected: Range): Range | null {
- const node = selected.startContainer
+/** One whole character (code point) at / before a UTF-16 offset, so letters
+ * outside the BMP (`𠮷`) count as one word character, not two halves. */
+function charAt(text: string, index: number): string {
+ const point = text.codePointAt(index)
- if (node.nodeType !== Node.TEXT_NODE) {
- return null
- }
+ return point === undefined ? '' : String.fromCodePoint(point)
+}
- const text = node.nodeValue ?? ''
- const isWord = (index: number) => WORD_CHAR.test(text[index] ?? '')
- let start = Math.min(selected.startOffset, text.length)
+function charBefore(text: string, index: number): string {
+ const low = text.charCodeAt(index - 1)
- if (!isWord(start)) {
- start = isWord(start - 1) ? start - 1 : start
- }
+ return text.slice(low >= 0xdc00 && low <= 0xdfff && index >= 2 ? index - 2 : Math.max(0, index - 1), index)
+}
- if (!isWord(start)) {
- return null
+const isWordChar = (char: string) => char !== '' && WORD_CHAR.test(char)
+
+/** Word bounds around `caret` in `text`. A caret sits on a boundary between
+ * characters, so step to the nearest word character first; otherwise the
+ * word found depends on which side of a gap the caret happened to land.
+ * Quotes and dashes join a word's inside ("don't", "re-run"), never its edges. */
+function wordBounds(text: string, caret: number): null | { end: number; start: number } {
+ let start = caret
+
+ if (!isWordChar(charAt(text, start))) {
+ const before = charBefore(text, start)
+
+ if (!isWordChar(before)) {
+ return null
+ }
+
+ start -= before.length
}
let end = start
- while (start > 0 && isWord(start - 1)) {
- start--
+ for (let char = charBefore(text, start); start > 0 && isWordChar(char); char = charBefore(text, start)) {
+ start -= char.length
}
- while (end < text.length && isWord(end)) {
- end++
+ for (let char = charAt(text, end); end < text.length && isWordChar(char); char = charAt(text, end)) {
+ end += char.length
}
- // Quotes and dashes join a word's inside ("don't", "re-run"), never its edges.
- while (start < end && WORD_EDGE.test(text[start])) {
+ while (start < end && WORD_EDGE.test(charAt(text, start))) {
start++
}
- while (end > start && WORD_EDGE.test(text[end - 1])) {
+ while (end > start && WORD_EDGE.test(charBefore(text, end))) {
end--
}
- if (end === start || end - start > WORD_MAX) {
+ return end > start && end - start <= WORD_MAX ? { end, start } : null
+}
+
+/** The text nodes directly beside `node` with no element between them: a paste
+ * lands as its own node, so one word can span several. Chips and line breaks
+ * are elements, so a word never crosses them. */
+function textRun(node: Text): Text[] {
+ const run = [node]
+
+ for (let prev = node.previousSibling; prev instanceof Text; prev = prev.previousSibling) {
+ run.unshift(prev)
+ }
+
+ for (let next = node.nextSibling; next instanceof Text; next = next.nextSibling) {
+ run.push(next)
+ }
+
+ return run
+}
+
+/** Map an offset in the joined run back to a (node, offset) boundary point.
+ * `end` boundaries prefer the earlier node at a seam, `start` the later. */
+function pointIn(run: Text[], offset: number, edge: 'end' | 'start'): [Text, number] {
+ let rest = offset
+
+ for (const [index, node] of run.entries()) {
+ const fits = edge === 'end' ? rest <= node.length : rest < node.length
+
+ if (fits || index === run.length - 1) {
+ return [node, Math.min(rest, node.length)]
+ }
+
+ rest -= node.length
+ }
+
+ return [run[0], 0]
+}
+
+/** The word around a collapsed caret, or at a selection's start, as a live
+ * Range over the composer's own text nodes. */
+function wordRangeAt(selected: Range): Range | null {
+ const node = selected.startContainer
+
+ if (!(node instanceof Text)) {
+ return null
+ }
+
+ const run = textRun(node)
+ const before = run.slice(0, run.indexOf(node)).reduce((sum, item) => sum + item.length, 0)
+ const bounds = wordBounds(run.map(item => item.data).join(''), before + Math.min(selected.startOffset, node.length))
+
+ if (!bounds) {
return null
}
const range = document.createRange()
- range.setStart(node, start)
- range.setEnd(node, end)
+ range.setStart(...pointIn(run, bounds.start, 'start'))
+ range.setEnd(...pointIn(run, bounds.end, 'end'))
return range
}