diff --git a/gui/src/App.tsx b/gui/src/App.tsx index deb1e376f..257160320 100644 --- a/gui/src/App.tsx +++ b/gui/src/App.tsx @@ -1,5 +1,6 @@ import { RouterProvider, createMemoryRouter } from "react-router-dom"; import Layout from "./components/Layout"; +import { MainEditorProvider } from "./components/mainInput/TipTapEditor"; import { SubmenuContextProvidersProvider } from "./context/SubmenuContextProviders"; import { VscThemeProvider } from "./context/VscTheme"; import useSetup from "./hooks/useSetup"; @@ -66,9 +67,11 @@ function SetupListeners() { function App() { return ( - - - + + + + + ); diff --git a/gui/src/components/ConversationStarters/ConversationStarterCards.tsx b/gui/src/components/ConversationStarters/ConversationStarterCards.tsx index b8004b472..07d0b0cdb 100644 --- a/gui/src/components/ConversationStarters/ConversationStarterCards.tsx +++ b/gui/src/components/ConversationStarters/ConversationStarterCards.tsx @@ -3,10 +3,8 @@ import { useState } from "react"; import { useBookmarkedSlashCommands } from "../../hooks/useBookmarkedSlashCommands"; import { MAIN_EDITOR_INPUT_ID } from "../../pages/gui/Chat"; import { useAppDispatch, useAppSelector } from "../../redux/hooks"; -import { - setMainEditorContentTrigger, - setNewestCodeblocksForInput, -} from "../../redux/slices/sessionSlice"; +import { setNewestCodeblocksForInput } from "../../redux/slices/sessionSlice"; +import { useMainEditor } from "../mainInput/TipTapEditor"; import { createParagraphNodeFromSlashCmdDescription, createPromptBlockNodeFromSlashCmdDescription, @@ -17,6 +15,7 @@ const NUM_CARDS_TO_RENDER = 5; export function ConversationStarterCards() { const dispatch = useAppDispatch(); + const { mainEditor } = useMainEditor(); const [showAll, setShowAll] = useState(false); const { isCommandBookmarked } = useBookmarkedSlashCommands(); const slashCommands = useAppSelector( @@ -35,15 +34,13 @@ export function ConversationStarterCards() { }), ); - dispatch( - setMainEditorContentTrigger({ - type: "doc", - content: [ - createPromptBlockNodeFromSlashCmdDescription(command), - createParagraphNodeFromSlashCmdDescription(command), - ], - }), - ); + mainEditor?.commands.setContent({ + type: "doc", + content: [ + createPromptBlockNodeFromSlashCmdDescription(command), + createParagraphNodeFromSlashCmdDescription(command), + ], + }); } if (bookmarkedSlashCommands.length === 0) { diff --git a/gui/src/components/mainInput/TipTapEditor/MainEditorProvider.tsx b/gui/src/components/mainInput/TipTapEditor/MainEditorProvider.tsx new file mode 100644 index 000000000..874640aa5 --- /dev/null +++ b/gui/src/components/mainInput/TipTapEditor/MainEditorProvider.tsx @@ -0,0 +1,117 @@ +import { Editor } from "@tiptap/react"; +import { InputModifiers } from "core"; +import React, { + createContext, + useContext, + useEffect, + useRef, + useState, +} from "react"; +import { useAppDispatch, useAppSelector } from "../../../redux/hooks"; +import { setMainEditorContentTrigger } from "../../../redux/slices/sessionSlice"; +import { useMainEditorWebviewListeners } from "./useMainEditorWebviewListeners"; + +/** + * Context for accessing the main editor instance + */ +interface MainEditorContextType { + /** The current main editor instance */ + mainEditor: Editor | null; + /** Set the current main editor instance */ + setMainEditor: (editor: Editor | null) => void; + /** The current input ID */ + inputId: string | null; + /** Set the current input ID */ + setInputId: (id: string) => void; + /** Reference to the execute content function */ + onEnterRef: React.MutableRefObject<(modifiers: InputModifiers) => void>; +} + +const MainEditorContext = createContext( + undefined, +); + +/** + * Provider component that maintains a reference to the main editor + */ +export const MainEditorProvider: React.FC<{ children: React.ReactNode }> = ({ + children, +}) => { + const dispatch = useAppDispatch(); + const [mainEditor, setMainEditorInternal] = useState(null); + const [inputId, setInputId] = useState(null); + const onEnterRef = useRef<(modifiers: InputModifiers) => void>(() => {}); + const editorFocusedRef = useRef(false); + const historyLength = useAppSelector((store) => store.session.history.length); + + // Listen for changes to mainEditorContentTrigger in Redux + const mainEditorContentTrigger = useAppSelector( + (store) => store.session.mainEditorContentTrigger, + ); + + useEffect(() => { + if (mainEditor && mainEditorContentTrigger) { + queueMicrotask(() => { + mainEditor.commands.setContent(mainEditorContentTrigger); + }); + // Clear the trigger after using it + dispatch(setMainEditorContentTrigger(undefined)); + } + }, [mainEditor, mainEditorContentTrigger, dispatch]); + + // Update focused ref when editor focus state changes + useEffect(() => { + if (mainEditor) { + const updateFocus = () => { + editorFocusedRef.current = mainEditor.isFocused || false; + }; + + mainEditor.on("focus", updateFocus); + mainEditor.on("blur", updateFocus); + + return () => { + mainEditor.off("focus", updateFocus); + mainEditor.off("blur", updateFocus); + }; + } + }, [mainEditor]); + + // Set up main editor webview listeners when we have a valid editor and input ID + useMainEditorWebviewListeners({ + editor: mainEditor, + onEnterRef, + dispatch, + historyLength, + inputId: inputId || "", + editorFocusedRef, + }); + + const setMainEditor = (newEditor: Editor | null) => { + setMainEditorInternal(newEditor); + }; + + const value: MainEditorContextType = { + mainEditor, + setMainEditor, + inputId, + setInputId, + onEnterRef, + }; + + return ( + + {children} + + ); +}; + +/** + * Hook to access the main editor context + */ +export const useMainEditor = (): MainEditorContextType => { + const context = useContext(MainEditorContext); + if (context === undefined) { + throw new Error("useMainEditor must be used within a MainEditorProvider"); + } + return context; +}; diff --git a/gui/src/components/mainInput/TipTapEditor/TipTapEditor.tsx b/gui/src/components/mainInput/TipTapEditor/TipTapEditor.tsx index 6d20b2f1c..24a9d4aa6 100644 --- a/gui/src/components/mainInput/TipTapEditor/TipTapEditor.tsx +++ b/gui/src/components/mainInput/TipTapEditor/TipTapEditor.tsx @@ -7,16 +7,13 @@ import useIsOSREnabled from "../../../hooks/useIsOSREnabled"; import useUpdatingRef from "../../../hooks/useUpdatingRef"; import { useAppDispatch, useAppSelector } from "../../../redux/hooks"; import { selectDefaultModel } from "../../../redux/slices/configSlice"; -import { - selectIsInEditMode, - setMainEditorContentTrigger, -} from "../../../redux/slices/sessionSlice"; +import { selectIsInEditMode } from "../../../redux/slices/sessionSlice"; import InputToolbar, { ToolbarOptions } from "../InputToolbar"; import { ComboBoxItem } from "../types"; import { DragOverlay } from "./DragOverlay"; +import { useMainEditor } from "./MainEditorProvider"; import { InputBoxDiv } from "./StyledComponents"; import "./TipTapEditor.css"; -import { useWebviewListeners } from "./useWebviewListeners"; import { handleImageFile } from "./utils"; import { createEditorConfig, getPlaceholderText } from "./utils/editorConfig"; import { useEditorEventHandlers } from "./utils/keyHandlers"; @@ -41,6 +38,7 @@ export const TIPPY_DIV_ID = "tippy-js-div"; function TipTapEditor(props: TipTapEditorProps) { const dispatch = useAppDispatch(); + const mainEditorContext = useMainEditor(); const ideMessenger = useContext(IdeMessengerContext); @@ -57,6 +55,15 @@ function TipTapEditor(props: TipTapEditorProps) { dispatch, }); + // Register the main editor with the provider + useEffect(() => { + if (props.isMainInput && editor) { + mainEditorContext.setMainEditor(editor); + mainEditorContext.setInputId(props.inputId); + mainEditorContext.onEnterRef.current = onEnterRef.current; + } + }, [editor, props.isMainInput, props.inputId, mainEditorContext, onEnterRef]); + const [shouldHideToolbar, setShouldHideToolbar] = useState(true); useEffect(() => { @@ -101,30 +108,6 @@ function TipTapEditor(props: TipTapEditorProps) { } }, [props.isMainInput, isStreaming, editor]); - // This allows anywhere in the app to set the content of the main input - const mainInputContentTrigger = useAppSelector( - (store) => store.session.mainEditorContentTrigger, - ); - useEffect(() => { - if (!props.isMainInput || !mainInputContentTrigger) { - return; - } - queueMicrotask(() => { - editor?.commands.setContent(mainInputContentTrigger); - }); - dispatch(setMainEditorContentTrigger(undefined)); - }, [editor, props.isMainInput, mainInputContentTrigger]); - - // IDE event listeners - useWebviewListeners({ - editor, - onEnterRef, - dispatch, - historyLength, - props, - editorFocusedRef, - }); - const [showDragOverMsg, setShowDragOverMsg] = useState(false); const [activeKey, setActiveKey] = useState(null); @@ -186,9 +169,6 @@ function TipTapEditor(props: TipTapEditorProps) { setShouldHideToolbar(false); }, [cancelBlurTimeout]); - // TODO pass clear blur timeout to model and mode selectors - // Seems like unnecessary for now? - return ( void>; dispatch: AppDispatch; historyLength: number; - props: TipTapEditorProps; + inputId: string; editorFocusedRef: MutableRefObject; }) { - const { - editor, - onEnterRef, - dispatch, - historyLength, - props, - editorFocusedRef, - } = options; + useWebviewListener( + "isContinueInputFocused", + async () => { + return !!editorFocusedRef.current; + }, + [editorFocusedRef], + ); useWebviewListener( "userInput", async (data) => { - if (!props.isMainInput) { - return; - } - editor?.commands.insertContent(data.input); + if (!editor) return; + editor.commands.insertContent(data.input); onEnterRef.current({ useCodebase: false, noContext: true }); }, - [editor, onEnterRef.current, props.isMainInput], + [editor, onEnterRef.current], ); useWebviewListener("jetbrains/editorInsetRefresh", async () => { @@ -47,11 +53,7 @@ export function useWebviewListeners(options: { useWebviewListener( "focusContinueInput", - async (data) => { - if (!props.isMainInput) { - return; - } - + async () => { dispatch(clearCodeToEdit()); if (historyLength > 0) { @@ -62,52 +64,46 @@ export function useWebviewListeners(options: { }), ); } + setTimeout(() => { editor?.commands.blur(); editor?.commands.focus("end"); }, 20); }, - [historyLength, editor, props.isMainInput], + [historyLength, editor, dispatch], ); useWebviewListener( "focusContinueInputWithoutClear", async () => { - if (!props.isMainInput) { - return; - } setTimeout(() => { editor?.commands.focus("end"); }, 20); }, - [editor, props.isMainInput], + [editor], ); useWebviewListener( "focusContinueInputWithNewSession", async () => { - if (!props.isMainInput) { - return; - } await dispatch( saveCurrentSession({ openNewSession: true, generateTitle: true, }), ); + setTimeout(() => { editor?.commands.focus("end"); }, 20); }, - [editor, props.isMainInput], + [editor, dispatch], ); useWebviewListener( "highlightedCode", async (data) => { - if (!props.isMainInput || !editor) { - return; - } + if (!editor) return; const contextItem = rifWithContentsToContextItem( data.rangeInFileWithContents, @@ -124,23 +120,25 @@ export function useWebviewListeners(options: { break; } } + editor .chain() .insertContentAt(index, { type: CodeBlockExtension.name, attrs: { item: contextItem, - inputId: props.inputId, + inputId, }, }) .run(); dispatch( setNewestCodeblocksForInput({ - inputId: props.inputId, + inputId, contextItemId: contextItem.id.itemId, }), ); + if (data.prompt) { editor.commands.focus("end"); editor.commands.insertContent(data.prompt); @@ -155,52 +153,42 @@ export function useWebviewListeners(options: { editor.commands.focus("end"); }, 20); }, - [editor, props.isMainInput, historyLength, onEnterRef.current], + [editor, inputId, onEnterRef.current], ); useWebviewListener( "focusEdit", async () => { - if (!props.isMainInput) { - return; - } - setTimeout(() => { editor?.commands.focus("end"); }, 20); }, - [editor, props.isMainInput], + [editor], ); useWebviewListener( "focusEditWithoutClear", async () => { - if (!props.isMainInput) { - return; - } - setTimeout(() => { editor?.commands.focus("end"); }, 2000); }, - [editor, props.isMainInput], + [editor], ); useWebviewListener( "isContinueInputFocused", async () => { - return props.isMainInput && !!editorFocusedRef.current; + return !!editorFocusedRef.current; }, - [editorFocusedRef, props.isMainInput], - !props.isMainInput, + [editorFocusedRef], ); useWebviewListener( "focusContinueSessionId", async (data) => { - if (!props.isMainInput || !data.sessionId) { - return; - } + if (!data.sessionId) return; + await dispatch( loadSession({ sessionId: data.sessionId, @@ -208,6 +196,6 @@ export function useWebviewListeners(options: { }), ); }, - [props.isMainInput], + [], ); }