2021-06-23 05:37:08 -04:00
|
|
|
import {
|
2021-06-21 06:02:38 -04:00
|
|
|
createContext,
|
|
|
|
useCallback,
|
|
|
|
useContext,
|
|
|
|
useEffect,
|
|
|
|
useMemo,
|
|
|
|
} from 'react'
|
2020-12-14 06:44:10 -05:00
|
|
|
import PropTypes from 'prop-types'
|
2021-02-23 05:17:41 -05:00
|
|
|
import useScopeValue from './util/scope-value-hook'
|
2021-05-18 06:56:56 -04:00
|
|
|
import useBrowserWindow from '../hooks/use-browser-window'
|
2021-06-16 05:32:38 -04:00
|
|
|
import { useIdeContext } from './ide-context'
|
2021-06-25 04:13:17 -04:00
|
|
|
import { useProjectContext } from './project-context'
|
2020-12-14 06:44:10 -05:00
|
|
|
|
|
|
|
export const EditorContext = createContext()
|
|
|
|
|
2021-02-10 05:36:41 -05:00
|
|
|
EditorContext.Provider.propTypes = {
|
|
|
|
value: PropTypes.shape({
|
|
|
|
cobranding: PropTypes.shape({
|
|
|
|
logoImgUrl: PropTypes.string.isRequired,
|
|
|
|
brandVariationName: PropTypes.string.isRequired,
|
2021-04-19 08:38:03 -04:00
|
|
|
brandVariationId: PropTypes.number.isRequired,
|
|
|
|
brandId: PropTypes.number.isRequired,
|
|
|
|
brandVariationHomeUrl: PropTypes.string.isRequired,
|
|
|
|
publishGuideHtml: PropTypes.string,
|
|
|
|
partner: PropTypes.string,
|
|
|
|
brandedMenu: PropTypes.string,
|
2021-04-27 03:52:58 -04:00
|
|
|
submitBtnHtml: PropTypes.string,
|
2021-02-10 05:36:41 -05:00
|
|
|
}),
|
2021-05-14 04:26:20 -04:00
|
|
|
hasPremiumCompile: PropTypes.bool,
|
2021-02-10 05:36:41 -05:00
|
|
|
loading: PropTypes.bool,
|
2021-03-10 07:19:54 -05:00
|
|
|
renameProject: PropTypes.func.isRequired,
|
2021-02-23 05:17:41 -05:00
|
|
|
isProjectOwner: PropTypes.bool,
|
2021-04-19 08:38:03 -04:00
|
|
|
isRestrictedTokenMember: PropTypes.bool,
|
2021-06-25 04:13:17 -04:00
|
|
|
rootFolder: PropTypes.shape({
|
|
|
|
children: PropTypes.arrayOf(PropTypes.shape({ type: PropTypes.string })),
|
|
|
|
}),
|
2021-06-25 04:14:07 -04:00
|
|
|
permissionsLevel: PropTypes.oneOf(['readOnly', 'readAndWrite', 'owner']),
|
2021-04-27 03:52:58 -04:00
|
|
|
}),
|
2021-02-10 05:36:41 -05:00
|
|
|
}
|
|
|
|
|
2021-06-16 05:32:38 -04:00
|
|
|
export function EditorProvider({ children, settings }) {
|
|
|
|
const ide = useIdeContext()
|
|
|
|
|
2021-06-25 04:13:17 -04:00
|
|
|
const { owner, features } = useProjectContext({
|
|
|
|
owner: PropTypes.shape({
|
|
|
|
_id: PropTypes.string.isRequired,
|
|
|
|
}),
|
|
|
|
features: PropTypes.shape({
|
|
|
|
compileGroup: PropTypes.string,
|
|
|
|
}),
|
|
|
|
})
|
|
|
|
|
|
|
|
const cobranding = useMemo(() => {
|
|
|
|
if (window.brandVariation) {
|
|
|
|
return {
|
|
|
|
logoImgUrl: window.brandVariation.logo_url,
|
|
|
|
brandVariationName: window.brandVariation.name,
|
|
|
|
brandVariationId: window.brandVariation.id,
|
|
|
|
brandId: window.brandVariation.brand_id,
|
|
|
|
brandVariationHomeUrl: window.brandVariation.home_url,
|
|
|
|
publishGuideHtml: window.brandVariation.publish_guide_html,
|
|
|
|
partner: window.brandVariation.partner,
|
|
|
|
brandedMenu: window.brandVariation.branded_menu,
|
|
|
|
submitBtnHtml: window.brandVariation.submit_button_html,
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return undefined
|
|
|
|
}
|
|
|
|
}, [])
|
2021-01-27 05:30:55 -05:00
|
|
|
|
2021-06-16 05:32:38 -04:00
|
|
|
const [loading] = useScopeValue('state.loading')
|
|
|
|
const [projectName, setProjectName] = useScopeValue('project.name')
|
|
|
|
const [rootFolder] = useScopeValue('rootFolder')
|
2021-06-25 04:14:07 -04:00
|
|
|
const [permissionsLevel] = useScopeValue('permissionsLevel')
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
if (ide?.socket) {
|
|
|
|
ide.socket.on('projectNameUpdated', setProjectName)
|
|
|
|
return () =>
|
|
|
|
ide.socket.removeListener('projectNameUpdated', setProjectName)
|
|
|
|
}
|
|
|
|
}, [ide?.socket, setProjectName])
|
2021-04-19 08:38:03 -04:00
|
|
|
|
2021-03-10 07:19:54 -05:00
|
|
|
const renameProject = useCallback(
|
|
|
|
newName => {
|
|
|
|
setProjectName(oldName => {
|
|
|
|
if (oldName !== newName) {
|
|
|
|
settings.saveProjectSettings({ name: newName }).catch(response => {
|
|
|
|
setProjectName(oldName)
|
|
|
|
const { data, status } = response
|
|
|
|
if (status === 400) {
|
|
|
|
return ide.showGenericMessageModal('Error renaming project', data)
|
|
|
|
} else {
|
|
|
|
return ide.showGenericMessageModal(
|
|
|
|
'Error renaming project',
|
|
|
|
'Please try again in a moment'
|
|
|
|
)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
return newName
|
|
|
|
})
|
|
|
|
},
|
|
|
|
[settings, ide, setProjectName]
|
|
|
|
)
|
|
|
|
|
|
|
|
const { setTitle } = useBrowserWindow()
|
2021-05-18 09:43:41 -04:00
|
|
|
useEffect(() => {
|
|
|
|
setTitle(
|
2021-05-19 04:29:32 -04:00
|
|
|
`${projectName ? projectName + ' - ' : ''}Online LaTeX Editor ${
|
|
|
|
window.ExposedSettings.appName
|
|
|
|
}`
|
2021-05-18 09:43:41 -04:00
|
|
|
)
|
2021-05-19 04:29:32 -04:00
|
|
|
}, [projectName, setTitle])
|
2021-02-09 10:37:48 -05:00
|
|
|
|
2021-06-21 06:02:38 -04:00
|
|
|
const value = useMemo(
|
|
|
|
() => ({
|
|
|
|
cobranding,
|
2021-06-25 04:13:17 -04:00
|
|
|
hasPremiumCompile: features?.compileGroup === 'priority',
|
2021-06-21 06:02:38 -04:00
|
|
|
loading,
|
|
|
|
renameProject,
|
2021-06-25 04:14:07 -04:00
|
|
|
permissionsLevel,
|
2021-06-25 04:13:17 -04:00
|
|
|
isProjectOwner: owner?._id === window.user.id,
|
2021-06-21 06:02:38 -04:00
|
|
|
isRestrictedTokenMember: window.isRestrictedTokenMember,
|
|
|
|
rootFolder,
|
|
|
|
}),
|
|
|
|
[
|
|
|
|
cobranding,
|
2021-06-25 04:13:17 -04:00
|
|
|
features?.compileGroup,
|
2021-06-21 06:02:38 -04:00
|
|
|
loading,
|
|
|
|
renameProject,
|
2021-06-25 04:14:07 -04:00
|
|
|
permissionsLevel,
|
2021-06-25 04:13:17 -04:00
|
|
|
owner?._id,
|
2021-06-21 06:02:38 -04:00
|
|
|
rootFolder,
|
|
|
|
]
|
|
|
|
)
|
2021-01-14 10:16:54 -05:00
|
|
|
|
2020-12-14 06:44:10 -05:00
|
|
|
return (
|
2021-06-21 06:02:38 -04:00
|
|
|
<EditorContext.Provider value={value}>{children}</EditorContext.Provider>
|
2020-12-14 06:44:10 -05:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
EditorProvider.propTypes = {
|
2021-01-27 05:30:55 -05:00
|
|
|
children: PropTypes.any,
|
2021-04-27 03:52:58 -04:00
|
|
|
settings: PropTypes.any.isRequired,
|
2020-12-14 06:44:10 -05:00
|
|
|
}
|
|
|
|
|
2021-02-10 05:36:41 -05:00
|
|
|
export function useEditorContext(propTypes) {
|
2021-06-16 05:32:38 -04:00
|
|
|
const context = useContext(EditorContext)
|
|
|
|
|
|
|
|
if (!context) {
|
|
|
|
throw new Error('useEditorContext is only available inside EditorProvider')
|
|
|
|
}
|
|
|
|
|
|
|
|
PropTypes.checkPropTypes(propTypes, context, 'data', 'EditorContext.Provider')
|
|
|
|
|
|
|
|
return context
|
2020-12-14 06:44:10 -05:00
|
|
|
}
|