menu-bar.tsx 9.5 KB

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