editor-context.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. import {
  2. createContext,
  3. useCallback,
  4. useContext,
  5. useEffect,
  6. useMemo,
  7. } from 'react'
  8. import PropTypes from 'prop-types'
  9. import useScopeValue from '../hooks/use-scope-value'
  10. import useBrowserWindow from '../hooks/use-browser-window'
  11. import { useIdeContext } from './ide-context'
  12. import { useProjectContext } from './project-context'
  13. import { useDetachContext } from './detach-context'
  14. export const EditorContext = createContext()
  15. EditorContext.Provider.propTypes = {
  16. value: PropTypes.shape({
  17. cobranding: PropTypes.shape({
  18. logoImgUrl: PropTypes.string.isRequired,
  19. brandVariationName: PropTypes.string.isRequired,
  20. brandVariationId: PropTypes.number.isRequired,
  21. brandId: PropTypes.number.isRequired,
  22. brandVariationHomeUrl: PropTypes.string.isRequired,
  23. publishGuideHtml: PropTypes.string,
  24. partner: PropTypes.string,
  25. brandedMenu: PropTypes.bool,
  26. submitBtnHtml: PropTypes.string,
  27. }),
  28. hasPremiumCompile: PropTypes.bool,
  29. loading: PropTypes.bool,
  30. renameProject: PropTypes.func.isRequired,
  31. showSymbolPalette: PropTypes.bool,
  32. toggleSymbolPalette: PropTypes.func,
  33. insertSymbol: PropTypes.func,
  34. showGalileo: PropTypes.bool,
  35. toggleGalileo: PropTypes.func,
  36. insertGalileoAutocomplete: PropTypes.func,
  37. insertGalileoCiteAutocomplete: PropTypes.func,
  38. isProjectOwner: PropTypes.bool,
  39. isRestrictedTokenMember: PropTypes.bool,
  40. permissionsLevel: PropTypes.oneOf(['readOnly', 'readAndWrite', 'owner']),
  41. }),
  42. }
  43. export function EditorProvider({ children, settings }) {
  44. const ide = useIdeContext()
  45. const { role } = useDetachContext()
  46. const { owner, features } = useProjectContext({
  47. owner: PropTypes.shape({
  48. _id: PropTypes.string.isRequired,
  49. }),
  50. features: PropTypes.shape({
  51. compileGroup: PropTypes.string,
  52. }),
  53. })
  54. const cobranding = useMemo(() => {
  55. if (window.brandVariation) {
  56. return {
  57. logoImgUrl: window.brandVariation.logo_url,
  58. brandVariationName: window.brandVariation.name,
  59. brandVariationId: window.brandVariation.id,
  60. brandId: window.brandVariation.brand_id,
  61. brandVariationHomeUrl: window.brandVariation.home_url,
  62. publishGuideHtml: window.brandVariation.publish_guide_html,
  63. partner: window.brandVariation.partner,
  64. brandedMenu: window.brandVariation.branded_menu,
  65. submitBtnHtml: window.brandVariation.submit_button_html,
  66. }
  67. } else {
  68. return undefined
  69. }
  70. }, [])
  71. const [loading] = useScopeValue('state.loading')
  72. const [projectName, setProjectName] = useScopeValue('project.name')
  73. const [permissionsLevel] = useScopeValue('permissionsLevel')
  74. const [showSymbolPalette] = useScopeValue('editor.showSymbolPalette')
  75. const [toggleSymbolPalette] = useScopeValue('editor.toggleSymbolPalette')
  76. const [showGalileo] = useScopeValue('editor.showGalileo')
  77. const [toggleGalileo] = useScopeValue('editor.toggleGalileo')
  78. useEffect(() => {
  79. if (ide?.socket) {
  80. ide.socket.on('projectNameUpdated', setProjectName)
  81. return () =>
  82. ide.socket.removeListener('projectNameUpdated', setProjectName)
  83. }
  84. }, [ide?.socket, setProjectName])
  85. const renameProject = useCallback(
  86. newName => {
  87. setProjectName(oldName => {
  88. if (oldName !== newName) {
  89. settings.saveProjectSettings({ name: newName }).catch(response => {
  90. setProjectName(oldName)
  91. const { data, status } = response
  92. if (status === 400) {
  93. return ide.showGenericMessageModal('Error renaming project', data)
  94. } else {
  95. return ide.showGenericMessageModal(
  96. 'Error renaming project',
  97. 'Please try again in a moment'
  98. )
  99. }
  100. })
  101. }
  102. return newName
  103. })
  104. },
  105. [settings, ide, setProjectName]
  106. )
  107. const { setTitle } = useBrowserWindow()
  108. useEffect(() => {
  109. const parts = []
  110. if (role === 'detached') {
  111. parts.push('[PDF]')
  112. }
  113. if (projectName) {
  114. parts.push(projectName)
  115. parts.push('-')
  116. }
  117. parts.push('Online LaTeX Editor')
  118. parts.push(window.ExposedSettings.appName)
  119. const title = parts.join(' ')
  120. setTitle(title)
  121. }, [projectName, setTitle, role])
  122. const insertSymbol = useCallback(symbol => {
  123. window.dispatchEvent(
  124. new CustomEvent('editor:insert-symbol', {
  125. detail: symbol,
  126. })
  127. )
  128. }, [])
  129. const insertGalileoAutocomplete = useCallback(text => {
  130. window.dispatchEvent(
  131. new CustomEvent('editor:insert-galileo-completion', {
  132. detail: { text, cite: false },
  133. })
  134. )
  135. }, [])
  136. const insertGalileoCiteAutocomplete = useCallback(text => {
  137. window.dispatchEvent(
  138. new CustomEvent('editor:insert-galileo-completion', {
  139. detail: { text, cite: true },
  140. })
  141. )
  142. }, [])
  143. const value = useMemo(
  144. () => ({
  145. cobranding,
  146. hasPremiumCompile: features?.compileGroup === 'priority',
  147. loading,
  148. renameProject,
  149. permissionsLevel,
  150. isProjectOwner: owner?._id === window.user.id,
  151. isRestrictedTokenMember: window.isRestrictedTokenMember,
  152. showSymbolPalette,
  153. toggleSymbolPalette,
  154. insertSymbol,
  155. showGalileo,
  156. toggleGalileo,
  157. insertGalileoAutocomplete,
  158. insertGalileoCiteAutocomplete,
  159. }),
  160. [
  161. cobranding,
  162. features?.compileGroup,
  163. loading,
  164. renameProject,
  165. permissionsLevel,
  166. owner?._id,
  167. showSymbolPalette,
  168. toggleSymbolPalette,
  169. insertSymbol,
  170. showGalileo,
  171. toggleGalileo,
  172. insertGalileoAutocomplete,
  173. insertGalileoCiteAutocomplete,
  174. ]
  175. )
  176. return (
  177. <EditorContext.Provider value={value}>{children}</EditorContext.Provider>
  178. )
  179. }
  180. EditorProvider.propTypes = {
  181. children: PropTypes.any,
  182. settings: PropTypes.object,
  183. }
  184. export function useEditorContext(propTypes) {
  185. const context = useContext(EditorContext)
  186. if (!context) {
  187. throw new Error('useEditorContext is only available inside EditorProvider')
  188. }
  189. PropTypes.checkPropTypes(propTypes, context, 'data', 'EditorContext.Provider')
  190. return context
  191. }