menu-bar.tsx 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. import {
  2. DropdownDivider,
  3. DropdownHeader,
  4. DropdownItem,
  5. } from '@/shared/components/dropdown/dropdown-menu'
  6. import { MenuBar } from '@/shared/components/menu-bar/menu-bar'
  7. import { MenuBarDropdown } from '@/shared/components/menu-bar/menu-bar-dropdown'
  8. import { MenuBarOption } from '@/shared/components/menu-bar/menu-bar-option'
  9. import { useTranslation } from 'react-i18next'
  10. import ChangeLayoutOptions from './change-layout-options'
  11. import { ElementType, useCallback, useMemo, useState } from 'react'
  12. import { useLayoutContext } from '@/shared/context/layout-context'
  13. import { useCommandProvider } from '@/features/ide-react/hooks/use-command-provider'
  14. import CommandDropdown, {
  15. CommandSection,
  16. MenuSectionStructure,
  17. MenuStructure,
  18. } from './command-dropdown'
  19. import { useRailContext } from '../../context/rail-context'
  20. import WordCountModal from '@/features/word-count-modal/components/word-count-modal'
  21. import { isSplitTestEnabled } from '@/utils/splitTestUtils'
  22. import { useDetachCompileContext as useCompileContext } from '@/shared/context/detach-compile-context'
  23. import { useProjectSettingsContext } from '@/features/editor-left-menu/context/project-settings-context'
  24. import getMeta from '@/utils/meta'
  25. import EditorCloneProjectModalWrapper from '@/features/clone-project-modal/components/editor-clone-project-modal-wrapper'
  26. import useOpenProject from '@/shared/hooks/use-open-project'
  27. import importOverleafModules from '../../../../../macros/import-overleaf-module.macro'
  28. const menubarExtraComponents = importOverleafModules(
  29. 'menubarExtraComponents'
  30. ) as {
  31. import: { default: ElementType }
  32. }[]
  33. export const ToolbarMenuBar = () => {
  34. const { t } = useTranslation()
  35. const { setView, view } = useLayoutContext()
  36. const { pdfUrl } = useCompileContext()
  37. const wordCountEnabled = pdfUrl || isSplitTestEnabled('word-count-client')
  38. const [showWordCountModal, setShowWordCountModal] = useState(false)
  39. const [showCloneProjectModal, setShowCloneProjectModal] = useState(false)
  40. const openProject = useOpenProject()
  41. const anonymous = getMeta('ol-anonymous')
  42. const showSupport = getMeta('ol-showSupport')
  43. const showDocumentation = getMeta('ol-wikiEnabled')
  44. useCommandProvider(
  45. () => [
  46. {
  47. type: 'command',
  48. label: t('show_version_history'),
  49. handler: () => {
  50. setView(view === 'history' ? 'editor' : 'history')
  51. },
  52. id: 'show_version_history',
  53. },
  54. {
  55. type: 'command',
  56. label: t('word_count_lower'),
  57. disabled: !wordCountEnabled,
  58. handler: () => {
  59. setShowWordCountModal(true)
  60. },
  61. id: 'word_count',
  62. },
  63. {
  64. type: 'command',
  65. label: t('make_a_copy'),
  66. disabled: anonymous,
  67. handler: () => {
  68. setShowCloneProjectModal(true)
  69. },
  70. id: 'copy_project',
  71. },
  72. ],
  73. [t, setView, view, wordCountEnabled, anonymous]
  74. )
  75. const fileMenuStructure: MenuStructure = useMemo(
  76. () => [
  77. {
  78. id: 'file-file-tree',
  79. children: ['new_file', 'new_folder', 'upload_file', 'copy_project'],
  80. },
  81. { id: 'file-tools', children: ['show_version_history', 'word_count'] },
  82. { id: 'submit', children: ['submit-project', 'manage-template'] },
  83. {
  84. id: 'file-download',
  85. children: [
  86. {
  87. id: 'file-download-group',
  88. title: t('download'),
  89. children: [
  90. 'download-as-source-zip',
  91. 'download-pdf',
  92. 'export-as-docx',
  93. ],
  94. },
  95. ],
  96. },
  97. {
  98. id: 'settings',
  99. children: ['open-settings'],
  100. },
  101. ],
  102. [t]
  103. )
  104. const editMenuStructure: MenuStructure = useMemo(
  105. () => [
  106. {
  107. id: 'edit-undo-redo',
  108. children: ['undo', 'redo'],
  109. },
  110. {
  111. id: 'edit-search',
  112. children: ['find', 'select-all'],
  113. },
  114. ],
  115. []
  116. )
  117. const insertMenuStructure: MenuStructure = useMemo(
  118. () => [
  119. {
  120. id: 'insert-latex',
  121. children: [
  122. {
  123. id: 'insert-math-group',
  124. title: t('math'),
  125. children: ['insert-inline-math', 'insert-display-math'],
  126. },
  127. 'insert-symbol',
  128. {
  129. id: 'insert-figure-group',
  130. title: t('figure'),
  131. children: [
  132. 'insert-figure-from-computer',
  133. 'insert-figure-from-project-files',
  134. 'insert-figure-from-another-project',
  135. 'insert-figure-from-url',
  136. ],
  137. },
  138. 'insert-table',
  139. 'insert-citation',
  140. 'insert-link',
  141. 'insert-cross-reference',
  142. ],
  143. },
  144. {
  145. id: 'insert-comment',
  146. children: ['comment'],
  147. },
  148. ],
  149. [t]
  150. )
  151. const formatMenuStructure: MenuStructure = useMemo(
  152. () => [
  153. {
  154. id: 'format-text',
  155. children: ['format-bold', 'format-italics'],
  156. },
  157. {
  158. id: 'format-list',
  159. children: [
  160. 'format-bullet-list',
  161. 'format-numbered-list',
  162. 'format-increase-indentation',
  163. 'format-decrease-indentation',
  164. ],
  165. },
  166. {
  167. id: 'format-paragraph',
  168. title: t('paragraph_styles'),
  169. children: [
  170. 'format-style-normal',
  171. 'format-style-section',
  172. 'format-style-subsection',
  173. 'format-style-subsubsection',
  174. 'format-style-paragraph',
  175. 'format-style-subparagraph',
  176. ],
  177. },
  178. ],
  179. [t]
  180. )
  181. const pdfControlsMenuSectionStructure: MenuSectionStructure = useMemo(
  182. () => ({
  183. title: t('pdf_preview'),
  184. id: 'pdf-controls',
  185. children: [
  186. 'view-pdf-presentation-mode',
  187. 'view-pdf-zoom-in',
  188. 'view-pdf-zoom-out',
  189. 'view-pdf-fit-width',
  190. 'view-pdf-fit-height',
  191. ],
  192. }),
  193. [t]
  194. )
  195. const { mathPreview, setMathPreview, breadcrumbs, setBreadcrumbs } =
  196. useProjectSettingsContext()
  197. const toggleMathPreview = useCallback(() => {
  198. setMathPreview(!mathPreview)
  199. }, [setMathPreview, mathPreview])
  200. const toggleBreadcrumbs = useCallback(() => {
  201. setBreadcrumbs(!breadcrumbs)
  202. }, [setBreadcrumbs, breadcrumbs])
  203. const { setActiveModal } = useRailContext()
  204. const openKeyboardShortcutsModal = useCallback(() => {
  205. setActiveModal('keyboard-shortcuts')
  206. }, [setActiveModal])
  207. const openContactUsModal = useCallback(() => {
  208. setActiveModal('contact-us')
  209. }, [setActiveModal])
  210. return (
  211. <>
  212. <MenuBar
  213. className="ide-redesign-toolbar-menu-bar"
  214. id="toolbar-menu-bar-item"
  215. >
  216. <CommandDropdown menu={fileMenuStructure} title={t('file')} id="file" />
  217. <CommandDropdown menu={editMenuStructure} title={t('edit')} id="edit" />
  218. <CommandDropdown
  219. menu={insertMenuStructure}
  220. title={t('insert')}
  221. id="insert"
  222. />
  223. <MenuBarDropdown
  224. title={t('view')}
  225. id="view"
  226. className="ide-redesign-toolbar-dropdown-toggle-subdued ide-redesign-toolbar-button-subdued"
  227. >
  228. <ChangeLayoutOptions />
  229. <DropdownDivider />
  230. <DropdownHeader>Editor settings</DropdownHeader>
  231. <MenuBarOption
  232. eventKey="show_breadcrumbs"
  233. title={t('show_breadcrumbs')}
  234. leadingIcon={
  235. breadcrumbs ? 'check' : <DropdownItem.EmptyLeadingIcon />
  236. }
  237. onClick={toggleBreadcrumbs}
  238. />
  239. <MenuBarOption
  240. eventKey="show_equation_preview"
  241. title={t('show_equation_preview')}
  242. leadingIcon={
  243. mathPreview ? 'check' : <DropdownItem.EmptyLeadingIcon />
  244. }
  245. onClick={toggleMathPreview}
  246. />
  247. <CommandSection
  248. section={pdfControlsMenuSectionStructure}
  249. includeDivider
  250. />
  251. </MenuBarDropdown>
  252. <CommandDropdown
  253. menu={formatMenuStructure}
  254. title={t('format')}
  255. id="format"
  256. />
  257. <MenuBarDropdown
  258. title={t('help')}
  259. id="help"
  260. className="ide-redesign-toolbar-dropdown-toggle-subdued ide-redesign-toolbar-button-subdued"
  261. >
  262. <MenuBarOption
  263. eventKey="keyboard_shortcuts"
  264. title={t('keyboard_shortcuts')}
  265. onClick={openKeyboardShortcutsModal}
  266. />
  267. {showDocumentation && (
  268. <MenuBarOption
  269. title={t('documentation')}
  270. eventKey="documentation"
  271. href="/learn"
  272. target="_blank"
  273. rel="noopener noreferrer"
  274. />
  275. )}
  276. {showSupport && (
  277. <>
  278. <DropdownDivider />
  279. <MenuBarOption
  280. eventKey="contact_us"
  281. title={t('contact_us')}
  282. onClick={openContactUsModal}
  283. />
  284. </>
  285. )}
  286. </MenuBarDropdown>
  287. </MenuBar>
  288. <WordCountModal
  289. show={showWordCountModal}
  290. handleHide={() => setShowWordCountModal(false)}
  291. />
  292. <EditorCloneProjectModalWrapper
  293. show={showCloneProjectModal}
  294. handleHide={() => setShowCloneProjectModal(false)}
  295. openProject={openProject}
  296. />
  297. {menubarExtraComponents.map(
  298. ({ import: { default: Component } }, index) => (
  299. <Component key={index} />
  300. )
  301. )}
  302. </>
  303. )
  304. }