2021-03-10 07:19:54 -05:00
|
|
|
import React, { createContext, useCallback, useContext } 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-03-10 07:19:54 -05:00
|
|
|
import { useApplicationContext } from './application-context'
|
|
|
|
import useBrowserWindow from '../../infrastructure/browser-window-hook'
|
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
|
|
|
}),
|
|
|
|
loading: PropTypes.bool,
|
2021-04-19 08:38:03 -04:00
|
|
|
projectRootDocId: PropTypes.string,
|
2021-02-10 05:36:41 -05:00
|
|
|
projectId: PropTypes.string.isRequired,
|
2021-03-10 07:19:54 -05:00
|
|
|
projectName: PropTypes.string.isRequired,
|
|
|
|
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-04-27 03:52:58 -04:00
|
|
|
rootFolder: PropTypes.object,
|
|
|
|
}),
|
2021-02-10 05:36:41 -05:00
|
|
|
}
|
|
|
|
|
2021-03-10 07:19:54 -05:00
|
|
|
export function EditorProvider({ children, ide, settings }) {
|
|
|
|
const {
|
2021-04-27 03:52:58 -04:00
|
|
|
exposedSettings: { appName },
|
2021-03-10 07:19:54 -05:00
|
|
|
} = useApplicationContext({
|
|
|
|
exposedSettings: PropTypes.shape({ appName: PropTypes.string.isRequired })
|
2021-04-27 03:52:58 -04:00
|
|
|
.isRequired,
|
2021-03-10 07:19:54 -05:00
|
|
|
})
|
|
|
|
|
2021-01-27 05:30:55 -05:00
|
|
|
const cobranding = window.brandVariation
|
|
|
|
? {
|
|
|
|
logoImgUrl: window.brandVariation.logo_url,
|
|
|
|
brandVariationName: window.brandVariation.name,
|
2021-04-19 08:38:03 -04:00
|
|
|
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,
|
2021-04-27 03:52:58 -04:00
|
|
|
submitBtnHtml: window.brandVariation.submit_button_html,
|
2021-01-27 05:30:55 -05:00
|
|
|
}
|
|
|
|
: undefined
|
|
|
|
|
2021-01-14 10:16:54 -05:00
|
|
|
const ownerId =
|
2021-03-10 07:19:54 -05:00
|
|
|
ide.$scope.project && ide.$scope.project.owner
|
|
|
|
? ide.$scope.project.owner._id
|
2021-01-14 10:16:54 -05:00
|
|
|
: null
|
|
|
|
|
2021-03-10 07:19:54 -05:00
|
|
|
const [loading] = useScopeValue('state.loading', ide.$scope)
|
|
|
|
|
2021-04-19 08:38:03 -04:00
|
|
|
const [projectRootDocId] = useScopeValue('project.rootDoc_id', ide.$scope)
|
|
|
|
|
2021-03-10 07:19:54 -05:00
|
|
|
const [projectName, setProjectName] = useScopeValue(
|
|
|
|
'project.name',
|
|
|
|
ide.$scope
|
|
|
|
)
|
|
|
|
|
2021-04-19 08:38:03 -04:00
|
|
|
const [rootFolder] = useScopeValue('rootFolder', ide.$scope)
|
|
|
|
|
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()
|
|
|
|
setTitle(
|
|
|
|
`${projectName ? projectName + ' - ' : ''}Online LaTeX Editor ${appName}`
|
|
|
|
)
|
2021-02-09 10:37:48 -05:00
|
|
|
|
2021-01-14 10:16:54 -05:00
|
|
|
const editorContextValue = {
|
2021-01-27 05:30:55 -05:00
|
|
|
cobranding,
|
|
|
|
loading,
|
2021-01-14 10:16:54 -05:00
|
|
|
projectId: window.project_id,
|
2021-04-19 08:38:03 -04:00
|
|
|
projectRootDocId,
|
2021-03-10 07:19:54 -05:00
|
|
|
projectName: projectName || '', // initially might be empty in Angular
|
|
|
|
renameProject,
|
2021-02-09 10:37:48 -05:00
|
|
|
isProjectOwner: ownerId === window.user.id,
|
2021-04-19 08:38:03 -04:00
|
|
|
isRestrictedTokenMember: window.isRestrictedTokenMember,
|
2021-04-27 03:52:58 -04:00
|
|
|
rootFolder,
|
2021-01-14 10:16:54 -05:00
|
|
|
}
|
|
|
|
|
2020-12-14 06:44:10 -05:00
|
|
|
return (
|
2021-03-10 07:19:54 -05:00
|
|
|
<>
|
|
|
|
<EditorContext.Provider value={editorContextValue}>
|
|
|
|
{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-03-10 07:19:54 -05:00
|
|
|
ide: PropTypes.any.isRequired,
|
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) {
|
|
|
|
const data = useContext(EditorContext)
|
|
|
|
PropTypes.checkPropTypes(propTypes, data, 'data', 'EditorContext.Provider')
|
|
|
|
return data
|
2020-12-14 06:44:10 -05:00
|
|
|
}
|