From 96d751f1803f7eb7fc00d5531b41bf1242f2f918 Mon Sep 17 00:00:00 2001 From: Innei Date: Tue, 5 Aug 2025 22:35:53 +0800 Subject: [PATCH] feat(ai-chat): enhance mention functionality to support CJK characters - Updated MENTION_TRIGGER_PATTERN to include support for Chinese, Japanese, and Korean characters, improving the mention feature's accessibility for a broader audience. - Refactored getMentionType to return both mention type and cleaned query for better handling in search functionalities. - Adjusted mention search hooks and dropdown components to utilize the updated patterns and improve user experience during mention input. These changes enhance the overall functionality and inclusivity of the mention feature in the chat interface. Signed-off-by: Innei --- .../ai-chat/editor/plugins/mention/MentionNode.tsx | 7 +------ .../ai-chat/editor/plugins/mention/MentionPlugin.tsx | 8 ++------ .../plugins/mention/components/MentionDropdown.tsx | 10 +++++----- .../ai-chat/editor/plugins/mention/constants.ts | 6 +++--- .../editor/plugins/mention/hooks/useMentionSearch.ts | 10 +++++++--- .../plugins/mention/hooks/useMentionSearchService.ts | 2 +- .../editor/plugins/mention/utils/triggerDetection.ts | 9 +++++---- 7 files changed, 24 insertions(+), 28 deletions(-) diff --git a/apps/desktop/layer/renderer/src/modules/ai-chat/editor/plugins/mention/MentionNode.tsx b/apps/desktop/layer/renderer/src/modules/ai-chat/editor/plugins/mention/MentionNode.tsx index c19a679bd..adddc587a 100644 --- a/apps/desktop/layer/renderer/src/modules/ai-chat/editor/plugins/mention/MentionNode.tsx +++ b/apps/desktop/layer/renderer/src/modules/ai-chat/editor/plugins/mention/MentionNode.tsx @@ -12,6 +12,7 @@ import type { import { $applyNodeReplacement, DecoratorNode } from "lexical" import * as React from "react" +import { MentionComponent } from "./components/MentionComponent" import type { MentionData } from "./types" export type SerializedMentionNode = Spread< @@ -21,12 +22,6 @@ export type SerializedMentionNode = Spread< SerializedLexicalNode > -const MentionComponent = React.lazy(() => - import("./components/MentionComponent").then((module) => ({ - default: module.MentionComponent, - })), -) - export class MentionNode extends DecoratorNode { __mentionData: MentionData diff --git a/apps/desktop/layer/renderer/src/modules/ai-chat/editor/plugins/mention/MentionPlugin.tsx b/apps/desktop/layer/renderer/src/modules/ai-chat/editor/plugins/mention/MentionPlugin.tsx index a2b14a3e6..a89816920 100644 --- a/apps/desktop/layer/renderer/src/modules/ai-chat/editor/plugins/mention/MentionPlugin.tsx +++ b/apps/desktop/layer/renderer/src/modules/ai-chat/editor/plugins/mention/MentionPlugin.tsx @@ -15,13 +15,9 @@ export function MentionPlugin() { // Get integrated search and context block handling const { searchMentions, handleMentionInsert } = useMentionIntegration() - const { maxSuggestions, triggerFn } = { - maxSuggestions: DEFAULT_MAX_SUGGESTIONS, - triggerFn: defaultTriggerFn, - } // Hook for detecting mention triggers const { mentionMatch, isActive, clearMentionMatch } = useMentionTrigger({ - triggerFn, + triggerFn: defaultTriggerFn, }) // Hook for searching mentions @@ -35,7 +31,7 @@ export function MentionPlugin() { hasResults, } = useMentionSearch({ onSearch: searchMentions, - maxSuggestions, + maxSuggestions: DEFAULT_MAX_SUGGESTIONS, }) // Hook for handling mention selection diff --git a/apps/desktop/layer/renderer/src/modules/ai-chat/editor/plugins/mention/components/MentionDropdown.tsx b/apps/desktop/layer/renderer/src/modules/ai-chat/editor/plugins/mention/components/MentionDropdown.tsx index 77f486337..b49fbaccf 100644 --- a/apps/desktop/layer/renderer/src/modules/ai-chat/editor/plugins/mention/components/MentionDropdown.tsx +++ b/apps/desktop/layer/renderer/src/modules/ai-chat/editor/plugins/mention/components/MentionDropdown.tsx @@ -14,6 +14,7 @@ import { useLexicalComposerContext } from "@lexical/react/LexicalComposerContext import * as React from "react" import { useCallback, useEffect, useRef, useState } from "react" +import { MENTION_TRIGGER_PATTERN } from "../constants" import type { MentionData } from "../types" import { calculateDropdownPosition } from "../utils/positioning" import { MentionTypeIcon } from "./shared/MentionTypeIcon" @@ -47,16 +48,15 @@ const MentionSuggestionItem = React.memo( // Highlight matching text const highlightText = (text: string, query: string) => { - const cleanQuery = query.replace("@", "").toLowerCase() + const cleanQuery = query.replace(MENTION_TRIGGER_PATTERN, "").toLowerCase() if (!cleanQuery) return text const parts = text.split(new RegExp(`(${cleanQuery})`, "gi")) - return parts.map((part, partIndex) => { + return parts.map((part) => { const isMatch = part.toLowerCase() === cleanQuery - // Create a more unique key that doesn't rely solely on index - const uniqueKey = `${mention.id}-${mention.type}-${part}-${partIndex}-${part.length}` + return ( - + {part} ) diff --git a/apps/desktop/layer/renderer/src/modules/ai-chat/editor/plugins/mention/constants.ts b/apps/desktop/layer/renderer/src/modules/ai-chat/editor/plugins/mention/constants.ts index 546c37f03..238ded1ab 100644 --- a/apps/desktop/layer/renderer/src/modules/ai-chat/editor/plugins/mention/constants.ts +++ b/apps/desktop/layer/renderer/src/modules/ai-chat/editor/plugins/mention/constants.ts @@ -11,6 +11,6 @@ export const MENTION_TYPEAHEAD_COMMAND = createCommand("MENTION_TYPEAHEA export const DEFAULT_MAX_SUGGESTIONS = 10 // Trigger patterns -export const MENTION_TRIGGER_PATTERN = /(?:^|\s)(@[\w-]*)$/ -export const FEED_MENTION_PATTERN = /(?:^|\s)(@#[\w-]*)$/ -export const ENTRY_MENTION_PATTERN = /(?:^|\s)(@\+[\w-]*)$/ +// Support CJK characters (Chinese, Japanese, Korean) in addition to ASCII +export const MENTION_TRIGGER_PATTERN = + /(?:^|\s)(@[#+]?[\w\u4e00-\u9fff\u3400-\u4dbf\u3040-\u309f\u30a0-\u30ff\uac00-\ud7af-]*)$/ diff --git a/apps/desktop/layer/renderer/src/modules/ai-chat/editor/plugins/mention/hooks/useMentionSearch.ts b/apps/desktop/layer/renderer/src/modules/ai-chat/editor/plugins/mention/hooks/useMentionSearch.ts index 40cc95451..685bc3125 100644 --- a/apps/desktop/layer/renderer/src/modules/ai-chat/editor/plugins/mention/hooks/useMentionSearch.ts +++ b/apps/desktop/layer/renderer/src/modules/ai-chat/editor/plugins/mention/hooks/useMentionSearch.ts @@ -5,7 +5,10 @@ import type { MentionData, MentionSearchState, MentionType } from "../types" import { getMentionType, shouldTriggerMention } from "../utils/triggerDetection" interface UseMentionSearchOptions { - onSearch?: (query: string, type: MentionType) => Promise | MentionData[] + onSearch?: ( + query: string, + type: MentionType | undefined, + ) => Promise | MentionData[] maxSuggestions?: number } @@ -58,8 +61,9 @@ export const useMentionSearch = ({ }) try { - const mentionType = getMentionType(query) - const results = await onSearchRef.current(query, mentionType) + const [mentionType, cleanQuery] = getMentionType(query) + + const results = await onSearchRef.current(cleanQuery, mentionType) // Check if this search was aborted if (abortController.signal.aborted) { diff --git a/apps/desktop/layer/renderer/src/modules/ai-chat/editor/plugins/mention/hooks/useMentionSearchService.ts b/apps/desktop/layer/renderer/src/modules/ai-chat/editor/plugins/mention/hooks/useMentionSearchService.ts index 74ef3ce8e..91603a943 100644 --- a/apps/desktop/layer/renderer/src/modules/ai-chat/editor/plugins/mention/hooks/useMentionSearchService.ts +++ b/apps/desktop/layer/renderer/src/modules/ai-chat/editor/plugins/mention/hooks/useMentionSearchService.ts @@ -15,7 +15,7 @@ export const useMentionSearchService = () => { // Search function that converts search results to MentionData format const searchMentions = useMemo(() => { - return async (query: string, type?: MentionType): Promise => { + return async (query: string, type?: MentionType | undefined): Promise => { const searchResults = search(query, type, 10) // Convert to MentionData format diff --git a/apps/desktop/layer/renderer/src/modules/ai-chat/editor/plugins/mention/utils/triggerDetection.ts b/apps/desktop/layer/renderer/src/modules/ai-chat/editor/plugins/mention/utils/triggerDetection.ts index 1239c84a9..9edf14ed5 100644 --- a/apps/desktop/layer/renderer/src/modules/ai-chat/editor/plugins/mention/utils/triggerDetection.ts +++ b/apps/desktop/layer/renderer/src/modules/ai-chat/editor/plugins/mention/utils/triggerDetection.ts @@ -39,11 +39,12 @@ export const defaultTriggerFn = (): MentionMatch | null => { } } -export const getMentionType = (query: string): MentionType => { +export const getMentionType = (query: string): [MentionType | undefined, string] => { // Simple heuristic - could be enhanced with more sophisticated detection - if (query.startsWith("@#")) return "feed" - if (query.startsWith("@+")) return "entry" - return "feed" + if (query.startsWith("@#")) return ["feed", query.slice(2)] + if (query.startsWith("@+")) return ["entry", query.slice(2)] + // Return undefined for general @ trigger to search both types + return [undefined, query.slice(1)] } export const cleanQuery = (query: string): string => {