Elffuss BraidHub

kikocis/continue public

6fe634474097 · 3.3 KiBRawPermalink
1import { Editor } from "@tiptap/react";
2import { InputModifiers } from "core";
3import React, {
4 createContext,
5 useContext,
6 useEffect,
7 useRef,
8 useState,
9} from "react";
10import { useAppDispatch, useAppSelector } from "../../../redux/hooks";
11import { setMainEditorContentTrigger } from "../../../redux/slices/sessionSlice";
12import { useMainEditorWebviewListeners } from "./useMainEditorWebviewListeners";
13
14/**
15 * Context for accessing the main editor instance
16 */
17interface MainEditorContextType {
18 /** The current main editor instance */
19 mainEditor: Editor | null;
20 /** Set the current main editor instance */
21 setMainEditor: (editor: Editor | null) => void;
22 /** The current input ID */
23 inputId: string | null;
24 /** Set the current input ID */
25 setInputId: (id: string) => void;
26 /** Reference to the execute content function */
27 onEnterRef: React.MutableRefObject<(modifiers: InputModifiers) => void>;
28}
29
30const MainEditorContext = createContext<MainEditorContextType | undefined>(
31 undefined,
32);
33
34/**
35 * Provider component that maintains a reference to the main editor
36 */
37export const MainEditorProvider: React.FC<{ children: React.ReactNode }> = ({
38 children,
39}) => {
40 const dispatch = useAppDispatch();
41 const [mainEditor, setMainEditorInternal] = useState<Editor | null>(null);
42 const [inputId, setInputId] = useState<string | null>(null);
43 const onEnterRef = useRef<(modifiers: InputModifiers) => void>(() => {});
44 const editorFocusedRef = useRef<boolean>(false);
45 const historyLength = useAppSelector((store) => store.session.history.length);
46
47 // Listen for changes to mainEditorContentTrigger in Redux
48 const mainEditorContentTrigger = useAppSelector(
49 (store) => store.session.mainEditorContentTrigger,
50 );
51
52 useEffect(() => {
53 if (mainEditor && mainEditorContentTrigger) {
54 queueMicrotask(() => {
55 mainEditor.commands.setContent(mainEditorContentTrigger);
56 });
57 // Clear the trigger after using it
58 dispatch(setMainEditorContentTrigger(undefined));
59 }
60 }, [mainEditor, mainEditorContentTrigger, dispatch]);
61
62 // Update focused ref when editor focus state changes
63 useEffect(() => {
64 if (mainEditor) {
65 const updateFocus = () => {
66 editorFocusedRef.current = mainEditor.isFocused || false;
67 };
68
69 mainEditor.on("focus", updateFocus);
70 mainEditor.on("blur", updateFocus);
71
72 return () => {
73 mainEditor.off("focus", updateFocus);
74 mainEditor.off("blur", updateFocus);
75 };
76 }
77 }, [mainEditor]);
78
79 // Set up main editor webview listeners when we have a valid editor and input ID
80 useMainEditorWebviewListeners({
81 editor: mainEditor,
82 onEnterRef,
83 dispatch,
84 historyLength,
85 inputId: inputId || "",
86 editorFocusedRef,
87 });
88
89 const setMainEditor = (newEditor: Editor | null) => {
90 setMainEditorInternal(newEditor);
91 };
92
93 const value: MainEditorContextType = {
94 mainEditor,
95 setMainEditor,
96 inputId,
97 setInputId,
98 onEnterRef,
99 };
100
101 return (
102 <MainEditorContext.Provider value={value}>
103 {children}
104 </MainEditorContext.Provider>
105 );
106};
107
108/**
109 * Hook to access the main editor context
110 */
111export const useMainEditor = (): MainEditorContextType => {
112 const context = useContext(MainEditorContext);
113 if (context === undefined) {
114 throw new Error("useMainEditor must be used within a MainEditorProvider");
115 }
116 return context;
117};