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 <tukon479@gmail.com>
This commit is contained in:
Innei
2025-08-05 22:35:53 +08:00
parent a91078af08
commit 96d751f180
7 changed files with 24 additions and 28 deletions
@@ -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<React.JSX.Element> {
__mentionData: MentionData
@@ -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
@@ -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 (
<span key={uniqueKey} className={isMatch ? "text-text-vibrant font-semibold" : ""}>
<span key={mention.id} className={isMatch ? "text-text-vibrant font-semibold" : ""}>
{part}
</span>
)
@@ -11,6 +11,6 @@ export const MENTION_TYPEAHEAD_COMMAND = createCommand<string>("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-]*)$/
@@ -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[]> | MentionData[]
onSearch?: (
query: string,
type: MentionType | undefined,
) => Promise<MentionData[]> | 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) {
@@ -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<MentionData[]> => {
return async (query: string, type?: MentionType | undefined): Promise<MentionData[]> => {
const searchResults = search(query, type, 10)
// Convert to MentionData format
@@ -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 => {