2023-10-26 04:57:00 -04:00
|
|
|
import { createContext, FC, useContext, useMemo } 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-04 07:17:50 -05:00
|
|
|
import type { ProjectSettings, UserSettings } from '../utils/api'
|
2022-11-29 13:00:10 -05:00
|
|
|
|
2023-01-03 12:13:54 -05:00
|
|
|
type ProjectSettingsSetterContextValue = {
|
2023-01-04 07:17:50 -05:00
|
|
|
setCompiler: (compiler: ProjectSettings['compiler']) => void
|
|
|
|
setImageName: (imageName: ProjectSettings['imageName']) => void
|
|
|
|
setRootDocId: (rootDocId: ProjectSettings['rootDocId']) => void
|
2023-01-03 12:13:54 -05:00
|
|
|
setSpellCheckLanguage: (
|
2023-01-04 07:17:50 -05:00
|
|
|
spellCheckLanguage: ProjectSettings['spellCheckLanguage']
|
2023-01-03 12:13:54 -05:00
|
|
|
) => void
|
2023-01-04 07:17:50 -05:00
|
|
|
setAutoComplete: (autoComplete: UserSettings['autoComplete']) => void
|
2023-01-03 12:13:54 -05:00
|
|
|
setAutoPairDelimiters: (
|
2023-01-04 07:17:50 -05:00
|
|
|
autoPairDelimiters: UserSettings['autoPairDelimiters']
|
2023-01-03 12:13:54 -05:00
|
|
|
) => void
|
|
|
|
setSyntaxValidation: (
|
2023-01-04 07:17:50 -05:00
|
|
|
syntaxValidation: UserSettings['syntaxValidation']
|
2023-01-03 12:13:54 -05:00
|
|
|
) => void
|
2023-01-04 07:17:50 -05:00
|
|
|
setMode: (mode: UserSettings['mode']) => void
|
|
|
|
setEditorTheme: (editorTheme: UserSettings['editorTheme']) => void
|
|
|
|
setOverallTheme: (overallTheme: UserSettings['overallTheme']) => void
|
|
|
|
setFontSize: (fontSize: UserSettings['fontSize']) => void
|
|
|
|
setFontFamily: (fontFamily: UserSettings['fontFamily']) => void
|
|
|
|
setLineHeight: (lineHeight: UserSettings['lineHeight']) => void
|
|
|
|
setPdfViewer: (pdfViewer: UserSettings['pdfViewer']) => void
|
2022-11-29 13:00:10 -05:00
|
|
|
}
|
|
|
|
|
2023-01-04 07:17:50 -05:00
|
|
|
type ProjectSettingsContextValue = Partial<ProjectSettings> &
|
|
|
|
Partial<UserSettings> &
|
2023-01-03 12:13:54 -05:00
|
|
|
ProjectSettingsSetterContextValue
|
|
|
|
|
2022-11-29 13:00:10 -05:00
|
|
|
export const ProjectSettingsContext = createContext<
|
|
|
|
ProjectSettingsContextValue | undefined
|
|
|
|
>(undefined)
|
|
|
|
|
2023-10-26 04:57:00 -04:00
|
|
|
export const ProjectSettingsProvider: FC = ({ children }) => {
|
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
|
|
|
|
}
|