menu-bar.tsx 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  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: ['download-as-source-zip', 'download-pdf', 'export-as-docx'],
  86. },
  87. {
  88. id: 'settings',
  89. children: ['open-settings'],
  90. },
  91. ],
  92. []
  93. )
  94. const editMenuStructure: MenuStructure = useMemo(
  95. () => [
  96. {
  97. id: 'edit-undo-redo',
  98. children: ['undo', 'redo'],
  99. },
  100. {
  101. id: 'edit-search',
  102. children: ['find', 'select-all'],
  103. },
  104. ],
  105. []
  106. )
  107. const insertMenuStructure: MenuStructure = useMemo(
  108. () => [
  109. {
  110. id: 'insert-latex',
  111. children: [
  112. {
  113. id: 'insert-math-group',
  114. title: t('math'),
  115. children: ['insert-inline-math', 'insert-display-math'],
  116. },
  117. 'insert-symbol',
  118. {
  119. id: 'insert-figure-group',
  120. title: t('figure'),
  121. children: [
  122. 'insert-figure-from-computer',
  123. 'insert-figure-from-project-files',
  124. 'insert-figure-from-another-project',
  125. 'insert-figure-from-url',
  126. ],
  127. },
  128. 'insert-table',
  129. 'insert-citation',
  130. 'insert-link',
  131. 'insert-cross-reference',
  132. ],
  133. },
  134. {
  135. id: 'insert-comment',
  136. children: ['comment'],
  137. },
  138. ],
  139. [t]
  140. )
  141. const formatMenuStructure: MenuStructure = useMemo(
  142. () => [
  143. {
  144. id: 'format-text',
  145. children: ['format-bold', 'format-italics'],
  146. },
  147. {
  148. id: 'format-list',
  149. children: [
  150. 'format-bullet-list',
  151. 'format-numbered-list',
  152. 'format-increase-indentation',
  153. 'format-decrease-indentation',
  154. ],
  155. },
  156. {
  157. id: 'format-paragraph',
  158. title: t('paragraph_styles'),
  159. children: [
  160. 'format-style-normal',
  161. 'format-style-section',
  162. 'format-style-subsection',
  163. 'format-style-subsubsection',
  164. 'format-style-paragraph',
  165. 'format-style-subparagraph',
  166. ],
  167. },
  168. ],
  169. [t]
  170. )
  171. const pdfControlsMenuSectionStructure: MenuSectionStructure = useMemo(
  172. () => ({
  173. title: t('pdf_preview'),
  174. id: 'pdf-controls',
  175. children: [
  176. 'view-pdf-presentation-mode',
  177. 'view-pdf-zoom-in',
  178. 'view-pdf-zoom-out',
  179. 'view-pdf-fit-width',
  180. 'view-pdf-fit-height',
  181. ],
  182. }),
  183. [t]
  184. )
  185. const { mathPreview, setMathPreview, breadcrumbs, setBreadcrumbs } =
  186. useProjectSettingsContext()
  187. const toggleMathPreview = useCallback(() => {
  188. setMathPreview(!mathPreview)
  189. }, [setMathPreview, mathPreview])
  190. const toggleBreadcrumbs = useCallback(() => {
  191. setBreadcrumbs(!breadcrumbs)
  192. }, [setBreadcrumbs, breadcrumbs])
  193. const { setActiveModal } = useRailContext()
  194. const openKeyboardShortcutsModal = useCallback(() => {
  195. setActiveModal('keyboard-shortcuts')
  196. }, [setActiveModal])
  197. const openContactUsModal = useCallback(() => {
  198. setActiveModal('contact-us')
  199. }, [setActiveModal])
  200. return (
  201. <>
  202. <MenuBar
  203. className="ide-redesign-toolbar-menu-bar"
  204. id="toolbar-menu-bar-item"
  205. >
  206. <CommandDropdown menu={fileMenuStructure} title={t('file')} id="file" />
  207. <CommandDropdown menu={editMenuStructure} title={t('edit')} id="edit" />
  208. <CommandDropdown
  209. menu={insertMenuStructure}
  210. title={t('insert')}
  211. id="insert"
  212. />
  213. <MenuBarDropdown
  214. title={t('view')}
  215. id="view"
  216. className="ide-redesign-toolbar-dropdown-toggle-subdued ide-redesign-toolbar-button-subdued"
  217. >
  218. <ChangeLayoutOptions />
  219. <DropdownDivider />
  220. <DropdownHeader>Editor settings</DropdownHeader>
  221. <MenuBarOption
  222. eventKey="show_breadcrumbs"
  223. title={t('show_breadcrumbs')}
  224. leadingIcon={
  225. breadcrumbs ? 'check' : <DropdownItem.EmptyLeadingIcon />
  226. }
  227. onClick={toggleBreadcrumbs}
  228. />
  229. <MenuBarOption
  230. eventKey="show_equation_preview"
  231. title={t('show_equation_preview')}
  232. leadingIcon={
  233. mathPreview ? 'check' : <DropdownItem.EmptyLeadingIcon />
  234. }
  235. onClick={toggleMathPreview}
  236. />
  237. <CommandSection
  238. section={pdfControlsMenuSectionStructure}
  239. includeDivider
  240. />
  241. </MenuBarDropdown>
  242. <CommandDropdown
  243. menu={formatMenuStructure}
  244. title={t('format')}
  245. id="format"
  246. />
  247. <MenuBarDropdown
  248. title={t('help')}
  249. id="help"
  250. className="ide-redesign-toolbar-dropdown-toggle-subdued ide-redesign-toolbar-button-subdued"
  251. >
  252. <MenuBarOption
  253. eventKey="keyboard_shortcuts"
  254. title={t('keyboard_shortcuts')}
  255. onClick={openKeyboardShortcutsModal}
  256. />
  257. {showDocumentation && (
  258. <MenuBarOption
  259. title={t('documentation')}
  260. eventKey="documentation"
  261. href="/learn"
  262. target="_blank"
  263. rel="noopener noreferrer"
  264. />
  265. )}
  266. {showSupport && (
  267. <>
  268. <DropdownDivider />
  269. <MenuBarOption
  270. eventKey="contact_us"
  271. title={t('contact_us')}
  272. onClick={openContactUsModal}
  273. />
  274. </>
  275. )}
  276. </MenuBarDropdown>
  277. </MenuBar>
  278. <WordCountModal
  279. show={showWordCountModal}
  280. handleHide={() => setShowWordCountModal(false)}
  281. />
  282. <EditorCloneProjectModalWrapper
  283. show={showCloneProjectModal}
  284. handleHide={() => setShowCloneProjectModal(false)}
  285. openProject={openProject}
  286. />
  287. {menubarExtraComponents.map(
  288. ({ import: { default: Component } }, index) => (
  289. <Component key={index} />
  290. )
  291. )}
  292. </>
  293. )
  294. }