diff --git a/src/components/chat-view/CommandsView.tsx b/src/components/chat-view/CommandsView.tsx index 61995b7..4c10b55 100644 --- a/src/components/chat-view/CommandsView.tsx +++ b/src/components/chat-view/CommandsView.tsx @@ -4,7 +4,7 @@ import { InitialEditorStateType } from '@lexical/react/LexicalComposer' import { $getRoot, $insertNodes, LexicalEditor } from 'lexical' import { Pencil, Search, Trash2 } from 'lucide-react' import { Notice } from 'obsidian' -import { useCallback, useEffect, useRef, useState } from 'react' +import React, { useCallback, useEffect, useRef, useState } from 'react' // import { v4 as uuidv4 } from 'uuid' import { lexicalNodeToPlainText } from '../../components/chat-view/chat-input/utils/editor-state-to-plain-text' @@ -65,7 +65,44 @@ const CommandsView = ( const nameInputRefs = useRef>(new Map()) const contentEditorRefs = useRef>(new Map()) - const contentEditableRefs = useRef>(new Map()) + + // 为每个正在编辑的命令创建refs + const commandEditRefs = useRef, + contentEditableRef: React.RefObject + }>>(new Map()); + + // 获取或创建命令编辑refs + const getCommandEditRefs = useCallback((id: string) => { + if (!commandEditRefs.current.has(id)) { + commandEditRefs.current.set(id, { + editorRef: React.createRef(), + contentEditableRef: React.createRef() + }); + } + // 由于之前的if语句确保了值存在,所以这里不会返回undefined + const refs = commandEditRefs.current.get(id); + if (!refs) { + // 添加保险逻辑,创建一个新的refs对象 + const newRefs = { + editorRef: React.createRef(), + contentEditableRef: React.createRef() + }; + commandEditRefs.current.set(id, newRefs); + return newRefs; + } + return refs; + }, []); + + // 当编辑状态改变时更新refs + useEffect(() => { + if (editingCommandId) { + const refs = getCommandEditRefs(editingCommandId); + if (refs.editorRef.current) { + contentEditorRefs.current.set(editingCommandId, refs.editorRef.current); + } + } + }, [editingCommandId, getCommandEditRefs]); // new command content's editor state const initialEditorState: InitialEditorStateType = ( @@ -80,9 +117,9 @@ const CommandsView = ( }) } // new command content's editor - const editorRef = useRef(null) + const editorRef = useRef(null) // new command content's editable - const contentEditableRef = useRef(null) + const contentEditableRef = useRef(null) // Create new command const handleAddCommand = async () => { @@ -233,12 +270,8 @@ const CommandsView = (
{ - if (editor) contentEditorRefs.current.set(command.id, editor) - }} - contentEditableRef={(el: HTMLDivElement) => { - if (el) contentEditableRefs.current.set(command.id, el) - }} + editorRef={getCommandEditRefs(command.id).editorRef} + contentEditableRef={getCommandEditRefs(command.id).contentEditableRef} />
diff --git a/src/database/modules/command/command-manager.ts b/src/database/modules/command/command-manager.ts index 59ec7f8..c49aa7f 100644 --- a/src/database/modules/command/command-manager.ts +++ b/src/database/modules/command/command-manager.ts @@ -50,8 +50,8 @@ export class CommandManager { } async searchCommands(query: string): Promise { - const templates = await this.findAllCommands() - const results = fuzzysort.go(query, templates, { + const commands = await this.findAllCommands() + const results = fuzzysort.go(query, commands, { keys: ['name'], threshold: 0.2, limit: 20, diff --git a/src/database/modules/command/command-repository.ts b/src/database/modules/command/command-repository.ts index 83d812a..b4a870d 100644 --- a/src/database/modules/command/command-repository.ts +++ b/src/database/modules/command/command-repository.ts @@ -31,7 +31,7 @@ export class CommandRepository { if (!this.db) { throw new DatabaseNotInitializedException() } - const result = await this.db.liveQuery( + const result = await this.db.query( `SELECT * FROM "template"` ) return result.rows diff --git a/src/types/settings.test.ts b/src/types/settings.test.ts index 9e97743..3858423 100644 --- a/src/types/settings.test.ts +++ b/src/types/settings.test.ts @@ -121,6 +121,12 @@ describe('parseSmartCopilotSettings', () => { baseUrl: '', useCustomUrl: false, }, + grokProvider: { + name: 'Grok', + apiKey: '', + baseUrl: '', + useCustomUrl: false, + }, infioProvider: { name: 'Infio', apiKey: '',