menu-bar.tsx 9.3 KB

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