2024-01-08 07:07:48 -05:00
|
|
|
import { createContext, FC, useContext, useEffect, useMemo } from 'react'
|
2023-10-24 10:57:17 -04:00
|
|
|
import { ScopeValueStore } from '../../../../types/ide/scope-value-store'
|
|
|
|
import { ScopeEventEmitter } from '../../../../types/ide/scope-event-emitter'
|
2022-05-25 03:58:26 -04:00
|
|
|
|
2023-10-24 10:57:17 -04:00
|
|
|
export type Ide = {
|
2022-05-25 03:58:26 -04:00
|
|
|
[key: string]: any // TODO: define the rest of the `ide` and `$scope` properties
|
2024-06-05 04:33:11 -04:00
|
|
|
$scope: Record<string, any>
|
2022-05-25 03:58:26 -04:00
|
|
|
}
|
|
|
|
|
2023-10-24 10:57:17 -04:00
|
|
|
type IdeContextValue = Ide & {
|
|
|
|
scopeStore: ScopeValueStore
|
|
|
|
scopeEventEmitter: ScopeEventEmitter
|
|
|
|
}
|
|
|
|
|
2024-06-05 04:33:11 -04:00
|
|
|
export const IdeContext = createContext<IdeContextValue | undefined>(undefined)
|
2022-05-25 03:58:26 -04:00
|
|
|
|
2023-10-24 10:57:17 -04:00
|
|
|
export const IdeProvider: FC<{
|
|
|
|
ide: Ide
|
|
|
|
scopeStore: ScopeValueStore
|
|
|
|
scopeEventEmitter: ScopeEventEmitter
|
|
|
|
}> = ({ ide, scopeStore, scopeEventEmitter, children }) => {
|
2024-01-08 07:07:48 -05:00
|
|
|
/**
|
|
|
|
* Expose scopeStore via `window.overleaf.unstable.store`, so it can be accessed by external extensions.
|
|
|
|
*
|
|
|
|
* These properties are expected to be available:
|
|
|
|
* - `editor.view`
|
|
|
|
* - `project.spellcheckLanguage`
|
|
|
|
* - `editor.open_doc_name`,
|
|
|
|
* - `editor.open_doc_id`,
|
2024-10-02 05:34:00 -04:00
|
|
|
* - `settings.theme`
|
|
|
|
* - `settings.keybindings`
|
2024-01-08 07:07:48 -05:00
|
|
|
*/
|
|
|
|
useEffect(() => {
|
|
|
|
window.overleaf = {
|
|
|
|
...window.overleaf,
|
|
|
|
unstable: {
|
|
|
|
...window.overleaf?.unstable,
|
|
|
|
store: scopeStore,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}, [scopeStore])
|
|
|
|
|
2023-10-24 10:57:17 -04:00
|
|
|
const value = useMemo<IdeContextValue>(() => {
|
|
|
|
return {
|
|
|
|
...ide,
|
|
|
|
scopeStore,
|
|
|
|
scopeEventEmitter,
|
|
|
|
}
|
|
|
|
}, [ide, scopeStore, scopeEventEmitter])
|
2022-05-25 03:58:26 -04:00
|
|
|
|
|
|
|
return <IdeContext.Provider value={value}>{children}</IdeContext.Provider>
|
|
|
|
}
|
|
|
|
|
2023-10-24 10:57:17 -04:00
|
|
|
export function useIdeContext(): IdeContextValue {
|
2022-05-25 03:58:26 -04:00
|
|
|
const context = useContext(IdeContext)
|
|
|
|
|
|
|
|
if (!context) {
|
|
|
|
throw new Error('useIdeContext is only available inside IdeProvider')
|
|
|
|
}
|
|
|
|
|
|
|
|
return context
|
|
|
|
}
|