mirror of
https://github.com/overleaf/overleaf.git
synced 2024-11-30 01:25:31 -05:00
2c6aa3d4a7
Add "settings" object to global scope store GitOrigin-RevId: 1d5c7c3a1b417be0726c4a5e95e611ded47c13c4
83 lines
2 KiB
TypeScript
83 lines
2 KiB
TypeScript
import {
|
|
createContext,
|
|
useContext,
|
|
useMemo,
|
|
Dispatch,
|
|
SetStateAction,
|
|
FC,
|
|
useState,
|
|
useEffect,
|
|
} from 'react'
|
|
|
|
import { UserSettings, Keybindings } from '../../../../types/user-settings'
|
|
import getMeta from '@/utils/meta'
|
|
import useScopeValue from '@/shared/hooks/use-scope-value'
|
|
|
|
const defaultSettings: UserSettings = {
|
|
pdfViewer: 'pdfjs',
|
|
autoComplete: true,
|
|
autoPairDelimiters: true,
|
|
syntaxValidation: false,
|
|
editorTheme: 'textmate',
|
|
overallTheme: '',
|
|
mode: 'default',
|
|
fontSize: 12,
|
|
fontFamily: 'monaco',
|
|
lineHeight: 'normal',
|
|
mathPreview: true,
|
|
}
|
|
|
|
type UserSettingsContextValue = {
|
|
userSettings: UserSettings
|
|
setUserSettings: Dispatch<
|
|
SetStateAction<UserSettingsContextValue['userSettings']>
|
|
>
|
|
}
|
|
|
|
type ScopeSettings = {
|
|
overallTheme: 'light' | 'dark'
|
|
keybindings: Keybindings
|
|
}
|
|
|
|
export const UserSettingsContext = createContext<
|
|
UserSettingsContextValue | undefined
|
|
>(undefined)
|
|
|
|
export const UserSettingsProvider: FC = ({ children }) => {
|
|
const [userSettings, setUserSettings] = useState<UserSettings>(
|
|
() => getMeta('ol-userSettings') || defaultSettings
|
|
)
|
|
|
|
// update the global scope 'settings' value, for extensions
|
|
const [, setScopeSettings] = useScopeValue<ScopeSettings>('settings')
|
|
useEffect(() => {
|
|
setScopeSettings({
|
|
overallTheme: userSettings.overallTheme === 'light-' ? 'light' : 'dark',
|
|
keybindings: userSettings.mode === 'none' ? 'default' : userSettings.mode,
|
|
})
|
|
}, [setScopeSettings, userSettings])
|
|
|
|
const value = useMemo<UserSettingsContextValue>(
|
|
() => ({
|
|
userSettings,
|
|
setUserSettings,
|
|
}),
|
|
[userSettings, setUserSettings]
|
|
)
|
|
|
|
return (
|
|
<UserSettingsContext.Provider value={value}>
|
|
{children}
|
|
</UserSettingsContext.Provider>
|
|
)
|
|
}
|
|
|
|
export function useUserSettingsContext() {
|
|
const context = useContext(UserSettingsContext)
|
|
if (!context) {
|
|
throw new Error(
|
|
'useUserSettingsContext is only available inside UserSettingsProvider'
|
|
)
|
|
}
|
|
return context
|
|
}
|