mirror of
https://github.com/yu-i-i/overleaf-cep.git
synced 2025-07-29 23:00:08 +02:00

Create separate window.overleaf.unstable.store based on React context values GitOrigin-RevId: 68f070a6fc5e2965a82720024d91b16fa622b28b
49 lines
1.2 KiB
TypeScript
49 lines
1.2 KiB
TypeScript
import {
|
|
createContext,
|
|
Dispatch,
|
|
FC,
|
|
PropsWithChildren,
|
|
SetStateAction,
|
|
useContext,
|
|
} from 'react'
|
|
import { EditorView } from '@codemirror/view'
|
|
import useExposedState from '@/shared/hooks/use-exposed-state'
|
|
|
|
export type EditorContextValue = {
|
|
view: EditorView | null
|
|
setView: Dispatch<SetStateAction<EditorView | null>>
|
|
}
|
|
|
|
// This provides access to the CodeMirror EditorView instance outside the editor
|
|
// component itself, including external extensions (in particular, Writefull)
|
|
export const EditorViewContext = createContext<EditorContextValue | undefined>(
|
|
undefined
|
|
)
|
|
|
|
export const EditorViewProvider: FC<PropsWithChildren> = ({ children }) => {
|
|
const [view, setView] = useExposedState<EditorView | null>(
|
|
null,
|
|
'editor.view'
|
|
)
|
|
|
|
const value = {
|
|
view,
|
|
setView,
|
|
}
|
|
|
|
return (
|
|
<EditorViewContext.Provider value={value}>
|
|
{children}
|
|
</EditorViewContext.Provider>
|
|
)
|
|
}
|
|
|
|
export const useEditorViewContext = (): EditorContextValue => {
|
|
const context = useContext(EditorViewContext)
|
|
if (!context) {
|
|
throw new Error(
|
|
'useEditorViewContext is only available inside EditorViewProvider'
|
|
)
|
|
}
|
|
return context
|
|
}
|