2023-01-03 12:04:34 -05:00
|
|
|
import { createContext, useContext, useMemo } from 'react'
|
2022-11-29 13:00:10 -05:00
|
|
|
import type { PropsWithChildren } from 'react'
|
2023-01-03 12:29:09 -05:00
|
|
|
import useProjectWideSettings from '../hooks/use-project-wide-settings'
|
2022-12-23 18:43:26 -05:00
|
|
|
import useUserWideSettings from '../hooks/use-user-wide-settings'
|
2022-12-27 15:36:08 -05:00
|
|
|
import useProjectWideSettingsSocketListener from '../hooks/use-project-wide-settings-socket-listener'
|
2023-01-03 12:13:54 -05:00
|
|
|
import type { ProjectSettingsScope, UserSettingsScope } from '../utils/api'
|
2022-11-29 13:00:10 -05:00
|
|
|
|
2023-01-03 12:13:54 -05:00
|
|
|
type ProjectSettingsSetterContextValue = {
|
|
|
|
setCompiler: (compiler: ProjectSettingsScope['compiler']) => void
|
|
|
|
setImageName: (imageName: ProjectSettingsScope['imageName']) => void
|
2023-01-03 17:33:31 -05:00
|
|
|
setRootDocId: (rootDocId: ProjectSettingsScope['rootDocId']) => void
|
2023-01-03 12:13:54 -05:00
|
|
|
setSpellCheckLanguage: (
|
|
|
|
spellCheckLanguage: ProjectSettingsScope['spellCheckLanguage']
|
|
|
|
) => void
|
|
|
|
setAutoComplete: (autoComplete: UserSettingsScope['autoComplete']) => void
|
|
|
|
setAutoPairDelimiters: (
|
|
|
|
autoPairDelimiters: UserSettingsScope['autoPairDelimiters']
|
|
|
|
) => void
|
|
|
|
setSyntaxValidation: (
|
|
|
|
syntaxValidation: UserSettingsScope['syntaxValidation']
|
|
|
|
) => void
|
|
|
|
setMode: (mode: UserSettingsScope['mode']) => void
|
|
|
|
setEditorTheme: (editorTheme: UserSettingsScope['editorTheme']) => void
|
|
|
|
setOverallTheme: (overallTheme: UserSettingsScope['overallTheme']) => void
|
|
|
|
setFontSize: (fontSize: UserSettingsScope['fontSize']) => void
|
|
|
|
setFontFamily: (fontFamily: UserSettingsScope['fontFamily']) => void
|
|
|
|
setLineHeight: (lineHeight: UserSettingsScope['lineHeight']) => void
|
|
|
|
setPdfViewer: (pdfViewer: UserSettingsScope['pdfViewer']) => void
|
2022-11-29 13:00:10 -05:00
|
|
|
}
|
|
|
|
|
2023-01-03 12:13:54 -05:00
|
|
|
type ProjectSettingsContextValue = Partial<ProjectSettingsScope> &
|
|
|
|
Partial<UserSettingsScope> &
|
|
|
|
ProjectSettingsSetterContextValue
|
|
|
|
|
2022-11-29 13:00:10 -05:00
|
|
|
export const ProjectSettingsContext = createContext<
|
|
|
|
ProjectSettingsContextValue | undefined
|
|
|
|
>(undefined)
|
|
|
|
|
|
|
|
export function ProjectSettingsProvider({
|
|
|
|
children,
|
|
|
|
}: PropsWithChildren<Record<string, never>>) {
|
2022-12-22 13:42:28 -05:00
|
|
|
const {
|
|
|
|
compiler,
|
|
|
|
setCompiler,
|
|
|
|
imageName,
|
|
|
|
setImageName,
|
|
|
|
rootDocId,
|
|
|
|
setRootDocId,
|
|
|
|
spellCheckLanguage,
|
|
|
|
setSpellCheckLanguage,
|
2023-01-03 12:29:09 -05:00
|
|
|
} = useProjectWideSettings()
|
2022-12-22 13:42:28 -05:00
|
|
|
|
2022-12-22 19:41:16 -05:00
|
|
|
const {
|
|
|
|
autoComplete,
|
|
|
|
setAutoComplete,
|
|
|
|
autoPairDelimiters,
|
|
|
|
setAutoPairDelimiters,
|
|
|
|
syntaxValidation,
|
|
|
|
setSyntaxValidation,
|
|
|
|
editorTheme,
|
|
|
|
setEditorTheme,
|
|
|
|
overallTheme,
|
|
|
|
setOverallTheme,
|
|
|
|
mode,
|
|
|
|
setMode,
|
|
|
|
fontSize,
|
|
|
|
setFontSize,
|
|
|
|
fontFamily,
|
|
|
|
setFontFamily,
|
|
|
|
lineHeight,
|
|
|
|
setLineHeight,
|
|
|
|
pdfViewer,
|
|
|
|
setPdfViewer,
|
2022-12-23 18:43:26 -05:00
|
|
|
} = useUserWideSettings()
|
2022-11-29 13:00:10 -05:00
|
|
|
|
2023-01-03 12:04:34 -05:00
|
|
|
useProjectWideSettingsSocketListener()
|
2022-12-27 15:36:08 -05:00
|
|
|
|
2022-11-29 13:00:10 -05:00
|
|
|
const value: ProjectSettingsContextValue = useMemo(
|
|
|
|
() => ({
|
|
|
|
compiler,
|
|
|
|
setCompiler,
|
|
|
|
imageName,
|
|
|
|
setImageName,
|
|
|
|
rootDocId,
|
|
|
|
setRootDocId,
|
|
|
|
spellCheckLanguage,
|
|
|
|
setSpellCheckLanguage,
|
|
|
|
autoComplete,
|
|
|
|
setAutoComplete,
|
|
|
|
autoPairDelimiters,
|
|
|
|
setAutoPairDelimiters,
|
|
|
|
syntaxValidation,
|
|
|
|
setSyntaxValidation,
|
|
|
|
editorTheme,
|
|
|
|
setEditorTheme,
|
|
|
|
overallTheme,
|
|
|
|
setOverallTheme,
|
|
|
|
mode,
|
|
|
|
setMode,
|
|
|
|
fontSize,
|
|
|
|
setFontSize,
|
|
|
|
fontFamily,
|
|
|
|
setFontFamily,
|
|
|
|
lineHeight,
|
|
|
|
setLineHeight,
|
|
|
|
pdfViewer,
|
|
|
|
setPdfViewer,
|
|
|
|
}),
|
|
|
|
[
|
|
|
|
compiler,
|
|
|
|
setCompiler,
|
|
|
|
imageName,
|
|
|
|
setImageName,
|
|
|
|
rootDocId,
|
|
|
|
setRootDocId,
|
|
|
|
spellCheckLanguage,
|
|
|
|
setSpellCheckLanguage,
|
|
|
|
autoComplete,
|
|
|
|
setAutoComplete,
|
|
|
|
autoPairDelimiters,
|
|
|
|
setAutoPairDelimiters,
|
|
|
|
syntaxValidation,
|
|
|
|
setSyntaxValidation,
|
|
|
|
editorTheme,
|
|
|
|
setEditorTheme,
|
|
|
|
overallTheme,
|
|
|
|
setOverallTheme,
|
|
|
|
mode,
|
|
|
|
setMode,
|
|
|
|
fontSize,
|
|
|
|
setFontSize,
|
|
|
|
fontFamily,
|
|
|
|
setFontFamily,
|
|
|
|
lineHeight,
|
|
|
|
setLineHeight,
|
|
|
|
pdfViewer,
|
|
|
|
setPdfViewer,
|
|
|
|
]
|
|
|
|
)
|
|
|
|
|
|
|
|
return (
|
|
|
|
<ProjectSettingsContext.Provider value={value}>
|
|
|
|
{children}
|
|
|
|
</ProjectSettingsContext.Provider>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
export function useProjectSettingsContext() {
|
|
|
|
const context = useContext(ProjectSettingsContext)
|
|
|
|
|
|
|
|
if (!context) {
|
|
|
|
throw new Error(
|
|
|
|
'useProjectSettingsContext is only available inside ProjectSettingsProvider'
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
return context
|
|
|
|
}
|