diff --git a/gui/src/components/mainInput/TipTapEditor/useMainEditorWebviewListeners.ts b/gui/src/components/mainInput/TipTapEditor/useMainEditorWebviewListeners.ts index 3b5b66442..26fa47a7b 100644 --- a/gui/src/components/mainInput/TipTapEditor/useMainEditorWebviewListeners.ts +++ b/gui/src/components/mainInput/TipTapEditor/useMainEditorWebviewListeners.ts @@ -3,11 +3,13 @@ import { InputModifiers } from "core"; import { rifWithContentsToContextItem } from "core/commands/util"; import { MutableRefObject } from "react"; import { useWebviewListener } from "../../../hooks/useWebviewListener"; +import { useAppSelector } from "../../../redux/hooks"; import { clearCodeToEdit } from "../../../redux/slices/editModeState"; import { setNewestToolbarPreviewForInput } from "../../../redux/slices/sessionSlice"; import { AppDispatch } from "../../../redux/store"; import { loadSession, saveCurrentSession } from "../../../redux/thunks/session"; -import { CodeBlock, Mention, PromptBlock } from "./extensions"; +import { CodeBlock, PromptBlock } from "./extensions"; +import { insertCurrentFileContextMention } from "./utils/insertContent"; /** * Hook for setting up main editor specific webview listeners @@ -179,27 +181,18 @@ export function useMainEditorWebviewListeners({ [], ); - useWebviewListener( - "configUpdate", - async (data) => { - const currentFileProvider = data.result.config?.contextProviders.find( - (provider) => provider.title === "currentFile", - ); - - if (currentFileProvider && editor) { - const node = editor.schema.nodes[Mention.name].create({ - name: currentFileProvider.displayTitle, - description: currentFileProvider.description, - id: currentFileProvider.title, - label: currentFileProvider.displayTitle, - renderInlineAs: currentFileProvider.renderInlineAs, - type: currentFileProvider.type, - itemType: 'contextProvider' - }); + const activeContextProviders = useAppSelector( + (state) => state.config.config.contextProviders, + ); + const mode = useAppSelector(state=>state.session.mode); - editor.chain().insertContent(node.toJSON()).run(); - } + useWebviewListener( + "newSession", + async () => { + // do not insert current file context mention if we are in edit mode + if(!editor || mode === 'edit') return; + insertCurrentFileContextMention(editor, activeContextProviders); }, - [editor], + [editor, activeContextProviders, mode], ); } diff --git a/gui/src/components/mainInput/TipTapEditor/utils/insertContent.ts b/gui/src/components/mainInput/TipTapEditor/utils/insertContent.ts new file mode 100644 index 000000000..764ea9f9b --- /dev/null +++ b/gui/src/components/mainInput/TipTapEditor/utils/insertContent.ts @@ -0,0 +1,34 @@ +import { Editor } from "@tiptap/core"; +import { ContextProviderDescription } from "core"; +import * as ContinueExtensions from "../extensions"; + +/** + * inserts the current file context as a removable mention in the editor area + * + * uses tiptap's `create` method to create a new node under the hood + */ +export function insertCurrentFileContextMention( + editor: Editor, + contextProviders: ContextProviderDescription[], +) { + const foundCurrentFileProvider = contextProviders.find( + (provider) => provider.title === "currentFile", + ); + + if (foundCurrentFileProvider) { + const node = editor.schema.nodes[ContinueExtensions.Mention.name].create({ + name: foundCurrentFileProvider.displayTitle, + description: foundCurrentFileProvider.description, + id: foundCurrentFileProvider.title, + label: foundCurrentFileProvider.displayTitle, + renderInlineAs: foundCurrentFileProvider.renderInlineAs, + type: foundCurrentFileProvider.type, + itemType: "contextProvider", + }); + + editor + .chain() + .insertContent([node.toJSON(), { type: "text", text: " " }]) // add a space after the mention + .run(); + } +}