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