|
|
@@ -1,6 +1,8 @@
|
|
|
-import React, { createContext, useContext } from 'react'
|
|
|
+import React, { createContext, useCallback, useContext } from 'react'
|
|
|
import PropTypes from 'prop-types'
|
|
|
import useScopeValue from './util/scope-value-hook'
|
|
|
+import { useApplicationContext } from './application-context'
|
|
|
+import useBrowserWindow from '../../infrastructure/browser-window-hook'
|
|
|
|
|
|
export const EditorContext = createContext()
|
|
|
|
|
|
@@ -13,12 +15,21 @@ EditorContext.Provider.propTypes = {
|
|
|
}),
|
|
|
loading: PropTypes.bool,
|
|
|
projectId: PropTypes.string.isRequired,
|
|
|
+ projectName: PropTypes.string.isRequired,
|
|
|
+ renameProject: PropTypes.func.isRequired,
|
|
|
isProjectOwner: PropTypes.bool,
|
|
|
isRestrictedTokenMember: PropTypes.bool
|
|
|
})
|
|
|
}
|
|
|
|
|
|
-export function EditorProvider({ children, $scope }) {
|
|
|
+export function EditorProvider({ children, ide, settings }) {
|
|
|
+ const {
|
|
|
+ exposedSettings: { appName }
|
|
|
+ } = useApplicationContext({
|
|
|
+ exposedSettings: PropTypes.shape({ appName: PropTypes.string.isRequired })
|
|
|
+ .isRequired
|
|
|
+ })
|
|
|
+
|
|
|
const cobranding = window.brandVariation
|
|
|
? {
|
|
|
logoImgUrl: window.brandVariation.logo_url,
|
|
|
@@ -28,30 +39,68 @@ export function EditorProvider({ children, $scope }) {
|
|
|
: undefined
|
|
|
|
|
|
const ownerId =
|
|
|
- window._ide.$scope.project && window._ide.$scope.project.owner
|
|
|
- ? window._ide.$scope.project.owner._id
|
|
|
+ ide.$scope.project && ide.$scope.project.owner
|
|
|
+ ? ide.$scope.project.owner._id
|
|
|
: null
|
|
|
|
|
|
- const [loading] = useScopeValue('state.loading', $scope)
|
|
|
+ const [loading] = useScopeValue('state.loading', ide.$scope)
|
|
|
+
|
|
|
+ const [projectName, setProjectName] = useScopeValue(
|
|
|
+ 'project.name',
|
|
|
+ ide.$scope
|
|
|
+ )
|
|
|
+
|
|
|
+ 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}`
|
|
|
+ )
|
|
|
|
|
|
const editorContextValue = {
|
|
|
cobranding,
|
|
|
loading,
|
|
|
projectId: window.project_id,
|
|
|
+ projectName: projectName || '', // initially might be empty in Angular
|
|
|
+ renameProject,
|
|
|
isProjectOwner: ownerId === window.user.id,
|
|
|
isRestrictedTokenMember: window.isRestrictedTokenMember
|
|
|
}
|
|
|
|
|
|
return (
|
|
|
- <EditorContext.Provider value={editorContextValue}>
|
|
|
- {children}
|
|
|
- </EditorContext.Provider>
|
|
|
+ <>
|
|
|
+ <EditorContext.Provider value={editorContextValue}>
|
|
|
+ {children}
|
|
|
+ </EditorContext.Provider>
|
|
|
+ </>
|
|
|
)
|
|
|
}
|
|
|
|
|
|
EditorProvider.propTypes = {
|
|
|
children: PropTypes.any,
|
|
|
- $scope: PropTypes.any.isRequired
|
|
|
+ ide: PropTypes.any.isRequired,
|
|
|
+ settings: PropTypes.any.isRequired
|
|
|
}
|
|
|
|
|
|
export function useEditorContext(propTypes) {
|