2021-06-23 05:37:08 -04:00
|
|
|
import {
|
2021-06-21 06:02:38 -04:00
|
|
|
createContext,
|
|
|
|
useCallback,
|
|
|
|
useContext,
|
|
|
|
useEffect,
|
|
|
|
useMemo,
|
2023-08-30 09:37:07 -04:00
|
|
|
useState,
|
2021-06-21 06:02:38 -04:00
|
|
|
} from 'react'
|
2020-12-14 06:44:10 -05:00
|
|
|
import PropTypes from 'prop-types'
|
2021-10-08 06:09:41 -04:00
|
|
|
import useScopeValue from '../hooks/use-scope-value'
|
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'
|
2022-05-20 04:15:49 -04:00
|
|
|
import { useDetachContext } from './detach-context'
|
2023-03-24 08:09:14 -04:00
|
|
|
import getMeta from '../../utils/meta'
|
|
|
|
import { useUserContext } from './user-context'
|
2023-09-28 12:36:08 -04:00
|
|
|
import { saveProjectSettings } from '@/features/editor-left-menu/utils/api'
|
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,
|
2021-07-26 10:02:41 -04:00
|
|
|
brandedMenu: PropTypes.bool,
|
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-11-10 05:50:01 -05:00
|
|
|
showSymbolPalette: PropTypes.bool,
|
|
|
|
toggleSymbolPalette: PropTypes.func,
|
|
|
|
insertSymbol: PropTypes.func,
|
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: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
|
|
|
}
|
|
|
|
|
2023-09-28 12:36:08 -04:00
|
|
|
export function EditorProvider({ children }) {
|
2021-06-16 05:32:38 -04:00
|
|
|
const ide = useIdeContext()
|
2023-03-24 08:09:14 -04:00
|
|
|
const { id: userId } = useUserContext()
|
2022-05-20 04:15:49 -04:00
|
|
|
const { role } = useDetachContext()
|
|
|
|
|
2023-09-28 12:36:08 -04:00
|
|
|
const {
|
|
|
|
owner,
|
|
|
|
features,
|
|
|
|
_id: projectId,
|
|
|
|
} = useProjectContext({
|
2021-06-25 04:13:17 -04:00
|
|
|
owner: PropTypes.shape({
|
|
|
|
_id: PropTypes.string.isRequired,
|
|
|
|
}),
|
|
|
|
features: PropTypes.shape({
|
|
|
|
compileGroup: PropTypes.string,
|
|
|
|
}),
|
2023-09-28 12:36:08 -04:00
|
|
|
_id: PropTypes.string.isRequired,
|
2021-06-25 04:13:17 -04:00
|
|
|
})
|
|
|
|
|
|
|
|
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')
|
2021-06-25 04:14:07 -04:00
|
|
|
const [permissionsLevel] = useScopeValue('permissionsLevel')
|
2021-11-10 05:50:01 -05:00
|
|
|
const [showSymbolPalette] = useScopeValue('editor.showSymbolPalette')
|
|
|
|
const [toggleSymbolPalette] = useScopeValue('editor.toggleSymbolPalette')
|
2021-06-25 04:14:07 -04:00
|
|
|
|
2023-08-30 09:37:07 -04:00
|
|
|
const [completedTutorials, setCompletedTutorials] = useState(() =>
|
|
|
|
getMeta('ol-completedTutorials')
|
|
|
|
)
|
|
|
|
|
|
|
|
const setCompletedTutorial = useCallback(
|
|
|
|
tutorialKey => {
|
|
|
|
setCompletedTutorials({
|
|
|
|
...completedTutorials,
|
|
|
|
[tutorialKey]: new Date(),
|
|
|
|
})
|
|
|
|
},
|
|
|
|
[completedTutorials]
|
|
|
|
)
|
|
|
|
|
2021-06-25 04:14:07 -04:00
|
|
|
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) {
|
2023-09-28 12:36:08 -04:00
|
|
|
saveProjectSettings(projectId, { name: newName }).catch(response => {
|
2021-03-10 07:19:54 -05:00
|
|
|
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
|
|
|
|
})
|
|
|
|
},
|
2023-09-28 12:36:08 -04:00
|
|
|
[ide, setProjectName, projectId]
|
2021-03-10 07:19:54 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
const { setTitle } = useBrowserWindow()
|
2021-05-18 09:43:41 -04:00
|
|
|
useEffect(() => {
|
2022-05-20 04:15:49 -04:00
|
|
|
const parts = []
|
|
|
|
|
|
|
|
if (role === 'detached') {
|
|
|
|
parts.push('[PDF]')
|
|
|
|
}
|
|
|
|
|
|
|
|
if (projectName) {
|
|
|
|
parts.push(projectName)
|
|
|
|
parts.push('-')
|
|
|
|
}
|
|
|
|
|
|
|
|
parts.push('Online LaTeX Editor')
|
|
|
|
parts.push(window.ExposedSettings.appName)
|
|
|
|
|
|
|
|
const title = parts.join(' ')
|
|
|
|
|
|
|
|
setTitle(title)
|
|
|
|
}, [projectName, setTitle, role])
|
2021-02-09 10:37:48 -05:00
|
|
|
|
2022-01-10 08:56:36 -05:00
|
|
|
const insertSymbol = useCallback(symbol => {
|
|
|
|
window.dispatchEvent(
|
|
|
|
new CustomEvent('editor:insert-symbol', {
|
|
|
|
detail: symbol,
|
|
|
|
})
|
|
|
|
)
|
|
|
|
}, [])
|
|
|
|
|
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,
|
2023-03-24 08:09:14 -04:00
|
|
|
isProjectOwner: owner?._id === userId,
|
|
|
|
isRestrictedTokenMember: getMeta('ol-isRestrictedTokenMember'),
|
2021-11-10 05:50:01 -05:00
|
|
|
showSymbolPalette,
|
|
|
|
toggleSymbolPalette,
|
|
|
|
insertSymbol,
|
2023-08-30 09:37:07 -04:00
|
|
|
completedTutorials,
|
|
|
|
setCompletedTutorial,
|
2021-06-21 06:02:38 -04:00
|
|
|
}),
|
|
|
|
[
|
|
|
|
cobranding,
|
2021-06-25 04:13:17 -04:00
|
|
|
features?.compileGroup,
|
2023-03-24 08:09:14 -04:00
|
|
|
owner,
|
|
|
|
userId,
|
2021-06-21 06:02:38 -04:00
|
|
|
loading,
|
|
|
|
renameProject,
|
2021-06-25 04:14:07 -04:00
|
|
|
permissionsLevel,
|
2021-11-10 05:50:01 -05:00
|
|
|
showSymbolPalette,
|
|
|
|
toggleSymbolPalette,
|
|
|
|
insertSymbol,
|
2023-08-30 09:37:07 -04:00
|
|
|
completedTutorials,
|
|
|
|
setCompletedTutorial,
|
2021-06-21 06:02:38 -04:00
|
|
|
]
|
|
|
|
)
|
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,
|
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
|
|
|
}
|