diff --git a/docs/buzz-help.md b/docs/buzz-help.md index fd35b457de..aee1fe8795 100644 --- a/docs/buzz-help.md +++ b/docs/buzz-help.md @@ -754,6 +754,8 @@ and are asked to sign in again in Buzz. is shortened with an ellipsis, hover over it or use the arrow keys to select it and see the full name in a tooltip. A chosen or picked emoji is followed by a space, unless one already follows, so you can keep typing. +- **Mention, channel and emoji suggestions** also open when you type `@`, `#` + or `:` right after CJK punctuation such as `,` or `(`, without a space. - **Reminder counts** show how many reminders are due in a small capsule on the app tile's corner. It shows `99+` above 99; its accessible label keeps the exact count. diff --git a/src/bundled/channels/channel-query.test.ts b/src/bundled/channels/channel-query.test.ts index 0d5cda1de1..092344f212 100644 --- a/src/bundled/channels/channel-query.test.ts +++ b/src/bundled/channels/channel-query.test.ts @@ -43,3 +43,10 @@ it("uses the caret, rejects invalid offsets, and bounds the query", () => { const text = `#${"a".repeat(160)}`; expect(channelQuery(text, text.length)).toBeNull(); }); +it("matches after fullwidth and CJK punctuation, also at the scan bound", () => { + for (const text of ["看看,#ge", "好。#ge", "(#ge", "「#ge"]) { + expect(channelQuery(text, text.length)?.start).toBe(text.lastIndexOf("#")); + } + const bound = `,#${"a".repeat(159)}`; + expect(channelQuery(bound, bound.length)?.start).toBe(1); +}); diff --git a/src/bundled/channels/channel-query.ts b/src/bundled/channels/channel-query.ts index a084e927e1..e3e82bbfcf 100644 --- a/src/bundled/channels/channel-query.ts +++ b/src/bundled/channels/channel-query.ts @@ -1,12 +1,14 @@ +import { TRIGGER_BOUNDARY } from "../mentions/mention-query"; + /** Syntax only: no channel reads or work for ordinary typing. A query may * contain spaces; completion admits it only while a channel name continues it. */ export function channelQuery(text: string, caret: number) { if (!Number.isInteger(caret) || caret < 0 || caret > text.length) return null; const before = text.slice(Math.max(0, caret - 160), caret); - const match = before.match(/(?:^|[\s([{])#([^\n\r\t#[\](){}`<>\\]*)$/u); + const match = before.match(/#([^\n\r\t#[\](){}`<>\\]*)$/u); if (!match) return null; const query = match[1] ?? ""; const start = caret - query.length - 1; - if (start && !/[\s([{]/u.test(text[start - 1] ?? "")) return null; + if (start && !TRIGGER_BOUNDARY.test(text[start - 1] ?? "")) return null; return { start, end: caret, query }; } diff --git a/src/bundled/emoji/emoji-query.test.ts b/src/bundled/emoji/emoji-query.test.ts index 0a0c4b7322..aab0835faa 100644 --- a/src/bundled/emoji/emoji-query.test.ts +++ b/src/bundled/emoji/emoji-query.test.ts @@ -18,6 +18,12 @@ it("opens after two shortcode characters, at the real caret and word boundaries" } }); +it("opens after fullwidth and CJK punctuation", () => { + for (const text of ["好的,:sm", "(:sm", "「:sm"]) { + expect(emojiQuery(text, text.length)?.start).toBe(text.lastIndexOf(":")); + } +}); + it("opens right after a converted emoji or a completed shortcode, without a space", () => { // `:smile:` converts to its glyph on the closing colon; the next shortcode starts at once. expect(emojiQuery("😄:wa", 5)).toEqual({ start: 2, end: 5, query: "wa" }); diff --git a/src/bundled/emoji/emoji-query.ts b/src/bundled/emoji/emoji-query.ts index 0c2170a29d..2a776353e4 100644 --- a/src/bundled/emoji/emoji-query.ts +++ b/src/bundled/emoji/emoji-query.ts @@ -1,3 +1,5 @@ +import { TRIGGER_BOUNDARY } from "../mentions/mention-query"; + const SHORTCODE = "[a-zA-Z0-9_+-]{2,64}"; const QUERY = new RegExp(`:(${SHORTCODE})$`, "u"); const CLOSED = new RegExp(`:${SHORTCODE}:$`, "u"); @@ -7,7 +9,7 @@ const EMOJI = /(?:\p{Extended_Pictographic}|\p{Emoji_Modifier}|\p{Regional_Indicator}|\u20E3|\u{E007F})[\uFE0E\uFE0F]?$/u; /** - * Start/whitespace/opening-bracket boundaries exclude URLs, times and closed codes. + * The start of text and `TRIGGER_BOUNDARY` exclude URLs, times and closed codes. * A converted emoji or a completed shortcode at its own boundary also counts, so * `😄:wave` and `:bufo::wave` chain without a space while `std::vector` stays text. */ @@ -23,7 +25,7 @@ export function emojiQuery(text: string, caret: number) { } function atBoundary(prefix: string): boolean { - if (prefix === "" || /[\s([{]$/u.test(prefix) || EMOJI.test(prefix)) + if (!prefix || TRIGGER_BOUNDARY.test(prefix.slice(-1)) || EMOJI.test(prefix)) return true; const closed = prefix.match(CLOSED); return closed !== null && atBoundary(prefix.slice(0, -closed[0].length)); diff --git a/src/bundled/mentions/README.md b/src/bundled/mentions/README.md index 2b03f9c8cb..742f8114b6 100644 --- a/src/bundled/mentions/README.md +++ b/src/bundled/mentions/README.md @@ -108,7 +108,9 @@ namesakes get qualified labels. ## 2. Query syntax Inline completion opens on `@` at the start of the text or after whitespace, -`(`, `[`, or `{`. The query is the text from after `@` up to the caret. It MUST +`(`, `[`, `{`, or one of these punctuation marks, which CJK prose does not +follow with a space: `,。、.!?;:"'()[]{}「」『』《》【】〈〉〔〕〖〗〝〞〟。「」、“”‘’…~〜`. +The query is the text from after `@` up to the caret. It MUST NOT contain `@`, a line break, or a tab. It MAY contain spaces. A client MAY look back only 160 characters from the caret. A caret outside the text gives no query. diff --git a/src/bundled/mentions/mention-query.test.ts b/src/bundled/mentions/mention-query.test.ts index 82b9126f64..5cc7102166 100644 --- a/src/bundled/mentions/mention-query.test.ts +++ b/src/bundled/mentions/mention-query.test.ts @@ -16,6 +16,14 @@ it("opens at word/open-bracket boundaries without treating email/URLs as mention expect(mentionQuery(text, text.length)).toBeNull(); } }); +it("opens after fullwidth and CJK punctuation, which CJK prose does not follow with a space", () => { + for (const text of ["好的。@Ho", "(@Ho", "「@Ho", "“@Ho"]) { + expect(mentionQuery(text, text.length)?.start).toBe(text.lastIndexOf("@")); + } + // At the scan bound, the character before `@` still comes from the full text. + const bound = `,@${"x".repeat(159)}`; + expect(mentionQuery(bound, bound.length)?.start).toBe(1); +}); it("uses UTF-16 ranges at the actual caret and respects the scan bound", () => { expect(mentionQuery("🧠 @Ho tail", 7)).toEqual({ start: 3, diff --git a/src/bundled/mentions/mention-query.ts b/src/bundled/mentions/mention-query.ts index e961c9eecb..b87142bbe8 100644 --- a/src/bundled/mentions/mention-query.ts +++ b/src/bundled/mentions/mention-query.ts @@ -1,11 +1,17 @@ import { profileKey } from "../../features/profiles/target"; import { foldName, normalizeName } from "../../features/search/person-match"; +/** What may precede `@`, `#` or `:` for a completion to open: whitespace, an + * opening bracket, or fullwidth/CJK punctuation, which CJK prose does not + * follow with a space. */ +export const TRIGGER_BOUNDARY = + /[\s([{,。、.!?;:"'()[]{}「」『』《》【】〈〉〔〕〖〗〝〞〟。「」、“”‘’…~〜]/u; + /** Syntax only. Multi-word queries are admitted separately against current names. */ export function mentionQuery(text: string, caret: number) { if (!Number.isInteger(caret) || caret < 0 || caret > text.length) return null; const before = text.slice(Math.max(0, caret - 160), caret); - const match = before.match(/(?:^|[\s([{])@([^@\n\r\t]*)$/u); + const match = before.match(/@([^@\n\r\t]*)$/u); if (!match) return null; const query = match[1] ?? ""; const start = caret - query.length - 1; @@ -14,8 +20,8 @@ export function mentionQuery(text: string, caret: number) { /\]\((nostr:npub1[023456789acdefghjklmnpqrstuvwxyz]+)\)$/u.exec(query); if (text[start - 1] === "[" && completed && profileKey(completed[1] ?? "")) return null; - // A bounded slice beginning in the middle of a word is not a boundary. - if (start && !/[\s([{]/u.test(text[start - 1] ?? "")) return null; + // Check the full text: a bounded slice can begin in the middle of a word. + if (start && !TRIGGER_BOUNDARY.test(text[start - 1] ?? "")) return null; return { start, end: caret, query }; } diff --git a/src/bundled/mentions/mention-rules.fixtures.json b/src/bundled/mentions/mention-rules.fixtures.json index ae10c5c567..cab24701b9 100644 --- a/src/bundled/mentions/mention-rules.fixtures.json +++ b/src/bundled/mentions/mention-rules.fixtures.json @@ -1655,6 +1655,16 @@ "query": "Ho" } }, + { + "name": "after_fullwidth_punctuation", + "text": "你好,@Ho", + "caret": 6, + "expected": { + "start": 3, + "end": 6, + "query": "Ho" + } + }, { "name": "after_newline", "text": "@x\n@y", diff --git a/src/bundled/workflows/WorkflowMessageText.test.tsx b/src/bundled/workflows/WorkflowMessageText.test.tsx index 1aaf1bc33a..834e939c73 100644 --- a/src/bundled/workflows/WorkflowMessageText.test.tsx +++ b/src/bundled/workflows/WorkflowMessageText.test.tsx @@ -103,6 +103,26 @@ it("closes on Escape until a new mention starts, and accepts a pointer pick", as expect(text).toHaveValue("@Bob \n@Bob "); }); +it("does not suggest after CJK punctuation, where the relay matches no name", async () => { + const user = userEvent.setup(); + render(); + const text = screen.getByRole("textbox", { + name: "Message text", + }); + const replace = async (value: string) => { + text.setSelectionRange(0, text.value.length); + await user.paste(value); + }; + await user.type(text, "hi @bo"); + expect(await screen.findByRole("listbox")).toBeInTheDocument(); + await user.keyboard("{Escape}"); + await replace("你好,@bo"); + expect(screen.queryByRole("listbox")).not.toBeInTheDocument(); + // The Escape held only the `@` it closed, so a later real query there opens. + await replace("你好 @bo"); + expect(await screen.findByRole("listbox")).toBeInTheDocument(); +}); + it("shows the composer's 50 rows and asks to narrow past them", async () => { const user = userEvent.setup(); const people = Array.from({ length: 51 }, (_, index) => { diff --git a/src/bundled/workflows/WorkflowMessageText.tsx b/src/bundled/workflows/WorkflowMessageText.tsx index 304be90ee8..3d0bceabd3 100644 --- a/src/bundled/workflows/WorkflowMessageText.tsx +++ b/src/bundled/workflows/WorkflowMessageText.tsx @@ -131,6 +131,16 @@ function mentionChoice(member: WorkflowMentionMember): MentionChoice { }; } +// The relay reads a workflow `@Name` as plain text and does not start a name +// after CJK punctuation, so here a query opens only at the start or after +// whitespace, `(`, `[` or `{`. +function workflowQuery(text: string, caret: number) { + const query = mentionQuery(text, caret); + return query?.start && !/[\s([{]/u.test(text[query.start - 1] ?? "") + ? null + : query; +} + /** * Message text whose `@` suggests the workflow channel's members as the * composer does, inserting the plain `@Name` the relay resolves. @@ -160,7 +170,7 @@ export function WorkflowMessageText({ // Only navigation pins a member; otherwise the first result is selected. const [pinned, setPinned] = useState<{ start: number; pubkey: string }>(); const query = - caret === null || !choices.length ? null : mentionQuery(value, caret); + caret === null || !choices.length ? null : workflowQuery(value, caret); const search = query?.query; const matches = useMemo( () => @@ -251,7 +261,7 @@ export function WorkflowMessageText({ onValueChange(element.value); readCaret(element); // Once that `@` is gone, a new one in its place suggests again. - if (!mentionQuery(element.value, element.selectionStart)) + if (!workflowQuery(element.value, element.selectionStart)) setDismissed(undefined); }} onSelect={(event) => readCaret(event.currentTarget)} diff --git a/src/features/messages/MessageComposer.test.tsx b/src/features/messages/MessageComposer.test.tsx index 38c1ab098c..5133895b45 100644 --- a/src/features/messages/MessageComposer.test.tsx +++ b/src/features/messages/MessageComposer.test.tsx @@ -1568,6 +1568,23 @@ it("reopens a query at the same offset after Escape with no carried rows", () => expect(h.messages.send).not.toHaveBeenCalled(); }); +it("opens an @ query after CJK punctuation and sends the pick as a recipient", async () => { + const h = mountTypeahead(); + const input = h.input(); + input.focus(); + h.fill("你好,@honey"); + expect(h.rows()).toEqual(["Honey honey"]); + expect(fireEvent.keyDown(input, { key: " " })).toBe(false); + expect(input).toHaveValue("你好,@Honey "); + fireEvent.click(screen.getByRole("button", { name: "Send message" })); + await waitFor(() => expect(h.messages.send).toHaveBeenCalledOnce()); + expect(h.messages.send.mock.calls[0]?.slice(0, 3)).toEqual([ + "channel", + "你好,@Honey ", + [first.pubkey], + ]); +}); + it("still closes the popup when the query closes, on Escape, and on a Space that selects", () => { const h = mountTypeahead(); const input = h.input();