feat(QuickPanel): enhance pinyin filtering and improve input handling in QuickPanel

This commit is contained in:
Teo 2025-04-15 15:36:47 +08:00 committed by Asurada
parent 18e99dee67
commit 9ad40b9219
2 changed files with 15 additions and 5 deletions

View File

@ -82,14 +82,19 @@ export const QuickPanelView: React.FC<Props> = ({ setInputText }) => {
return true return true
} }
const pattern = lowerSearchText.split('').join('.*')
if (tinyPinyin.isSupported() && /[\u4e00-\u9fa5]/.test(filterText)) { if (tinyPinyin.isSupported() && /[\u4e00-\u9fa5]/.test(filterText)) {
const pinyinText = tinyPinyin.convertToPinyin(filterText, '', true) try {
if (pinyinText.toLowerCase().includes(lowerSearchText)) { const pinyinText = tinyPinyin.convertToPinyin(filterText, '', true).toLowerCase()
const regex = new RegExp(pattern, 'ig')
return regex.test(pinyinText)
} catch (error) {
return true return true
} }
} else {
const regex = new RegExp(pattern, 'ig')
return regex.test(filterText.toLowerCase())
} }
return false
}) })
setIndex(newList.length > 0 ? ctx.defaultIndex || 0 : -1) setIndex(newList.length > 0 ? ctx.defaultIndex || 0 : -1)
@ -206,6 +211,8 @@ export const QuickPanelView: React.FC<Props> = ({ setInputText }) => {
const textArea = document.querySelector('.inputbar textarea') as HTMLTextAreaElement const textArea = document.querySelector('.inputbar textarea') as HTMLTextAreaElement
const handleInput = (e: Event) => { const handleInput = (e: Event) => {
if (isComposing.current) return
const target = e.target as HTMLTextAreaElement const target = e.target as HTMLTextAreaElement
const cursorPosition = target.selectionStart const cursorPosition = target.selectionStart
const textBeforeCursor = target.value.slice(0, cursorPosition) const textBeforeCursor = target.value.slice(0, cursorPosition)
@ -225,8 +232,9 @@ export const QuickPanelView: React.FC<Props> = ({ setInputText }) => {
isComposing.current = true isComposing.current = true
} }
const handleCompositionEnd = () => { const handleCompositionEnd = (e: CompositionEvent) => {
isComposing.current = false isComposing.current = false
handleInput(e)
} }
textArea.addEventListener('input', handleInput) textArea.addEventListener('input', handleInput)

View File

@ -62,6 +62,7 @@ const MentionModelsButton: FC<Props> = ({ ref, mentionModels, onMentionModel, To
{first(m.name)} {first(m.name)}
</Avatar> </Avatar>
), ),
filterText: (p.isSystem ? t(`provider.${p.id}`) : p.name) + m.name,
action: () => onMentionModel(m), action: () => onMentionModel(m),
isSelected: mentionModels.some((selected) => getModelUniqId(selected) === getModelUniqId(m)) isSelected: mentionModels.some((selected) => getModelUniqId(selected) === getModelUniqId(m))
})) }))
@ -89,6 +90,7 @@ const MentionModelsButton: FC<Props> = ({ ref, mentionModels, onMentionModel, To
{first(m.name)} {first(m.name)}
</Avatar> </Avatar>
), ),
filterText: (p.isSystem ? t(`provider.${p.id}`) : p.name) + m.name,
action: () => onMentionModel(m), action: () => onMentionModel(m),
isSelected: mentionModels.some((selected) => getModelUniqId(selected) === getModelUniqId(m)) isSelected: mentionModels.some((selected) => getModelUniqId(selected) === getModelUniqId(m))
})) }))