use-toolbar-menu-editor-commands.tsx 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. import { useCommandProvider } from '@/features/ide-react/hooks/use-command-provider'
  2. import {
  3. useCodeMirrorStateContext,
  4. useCodeMirrorViewContext,
  5. } from '@/features/source-editor/components/codemirror-context'
  6. import { FigureModalSource } from '@/features/source-editor/components/figure-modal/figure-modal-context'
  7. import * as commands from '@/features/source-editor/extensions/toolbar/commands'
  8. import { setSectionHeadingLevel } from '@/features/source-editor/extensions/toolbar/sections'
  9. import { useEditorPropertiesContext } from '@/features/ide-react/context/editor-properties-context'
  10. import { useLayoutContext } from '@/shared/context/layout-context'
  11. import getMeta from '@/utils/meta'
  12. import { redo, selectAll, undo } from '@codemirror/commands'
  13. import { openSearchPanel } from '@codemirror/search'
  14. import { useCallback } from 'react'
  15. import { useTranslation } from 'react-i18next'
  16. import { usePermissionsContext } from '@/features/ide-react/context/permissions-context'
  17. import { language } from '@codemirror/language'
  18. import { isCursorOnEmptyLine } from '@/features/source-editor/utils/is-cursor-on-empty-line'
  19. export const useToolbarMenuBarEditorCommands = () => {
  20. const view = useCodeMirrorViewContext()
  21. const state = useCodeMirrorStateContext()
  22. const { t } = useTranslation()
  23. const { view: layoutView } = useLayoutContext()
  24. const editorIsVisible = layoutView === 'editor'
  25. const { trackedWrite, comment } = usePermissionsContext()
  26. const languageName = state.facet(language)?.name
  27. const isTeXFile = languageName === 'latex'
  28. const canComment = !isCursorOnEmptyLine(state)
  29. const openFigureModal = useCallback((source: FigureModalSource) => {
  30. window.dispatchEvent(
  31. new CustomEvent('figure-modal:open', {
  32. detail: { source },
  33. })
  34. )
  35. }, [])
  36. const {
  37. hasLinkedProjectFileFeature,
  38. hasLinkedProjectOutputFileFeature,
  39. hasLinkUrlFeature,
  40. } = getMeta('ol-ExposedSettings')
  41. useCommandProvider(() => {
  42. if (!editorIsVisible) {
  43. return
  44. }
  45. return [
  46. /************************************
  47. * Edit menu
  48. ************************************/
  49. {
  50. id: 'undo',
  51. label: t('undo'),
  52. handler: () => {
  53. undo(view)
  54. view.focus()
  55. },
  56. disabled: !trackedWrite,
  57. },
  58. {
  59. id: 'redo',
  60. label: t('redo'),
  61. handler: () => {
  62. redo(view)
  63. view.focus()
  64. },
  65. disabled: !trackedWrite,
  66. },
  67. {
  68. id: 'find',
  69. label: t('find'),
  70. handler: () => {
  71. openSearchPanel(view)
  72. },
  73. },
  74. {
  75. id: 'select-all',
  76. label: t('select_all'),
  77. handler: () => {
  78. selectAll(view)
  79. view.focus()
  80. },
  81. },
  82. ]
  83. }, [editorIsVisible, t, view, trackedWrite])
  84. // LaTeX commands
  85. useCommandProvider(() => {
  86. if (!editorIsVisible) {
  87. return
  88. }
  89. if (!isTeXFile || !trackedWrite) {
  90. return
  91. }
  92. return [
  93. /************************************
  94. * Insert menu
  95. ************************************/
  96. {
  97. id: 'insert-inline-math',
  98. menuLabel: t('inline_math'),
  99. label: t('insert_inline_math'),
  100. handler: () => {
  101. commands.wrapInInlineMath(view)
  102. view.focus()
  103. },
  104. },
  105. {
  106. id: 'insert-display-math',
  107. menuLabel: t('display_math'),
  108. label: t('insert_display_math'),
  109. handler: () => {
  110. commands.wrapInDisplayMath(view)
  111. view.focus()
  112. },
  113. },
  114. {
  115. menuLabel: t('upload_from_computer'),
  116. label: t('insert_figure_from_computer'),
  117. id: 'insert-figure-from-computer',
  118. handler: () => {
  119. openFigureModal(FigureModalSource.FILE_UPLOAD)
  120. },
  121. },
  122. {
  123. menuLabel: t('from_project_files'),
  124. label: t('insert_figure_from_project_files'),
  125. id: 'insert-figure-from-project-files',
  126. handler: () => {
  127. openFigureModal(FigureModalSource.FILE_TREE)
  128. },
  129. },
  130. ]
  131. }, [view, t, editorIsVisible, openFigureModal, trackedWrite, isTeXFile])
  132. useCommandProvider(() => {
  133. if (!editorIsVisible) {
  134. return
  135. }
  136. if (!isTeXFile || !trackedWrite) {
  137. return
  138. }
  139. if (!hasLinkedProjectFileFeature || !hasLinkedProjectOutputFileFeature) {
  140. return
  141. }
  142. return [
  143. {
  144. menuLabel: t('from_another_project'),
  145. label: t('insert_figure_from_another_project'),
  146. id: 'insert-figure-from-another-project',
  147. handler: () => {
  148. openFigureModal(FigureModalSource.OTHER_PROJECT)
  149. },
  150. },
  151. ]
  152. }, [
  153. t,
  154. editorIsVisible,
  155. openFigureModal,
  156. trackedWrite,
  157. isTeXFile,
  158. hasLinkedProjectFileFeature,
  159. hasLinkedProjectOutputFileFeature,
  160. ])
  161. useCommandProvider(() => {
  162. if (!editorIsVisible) {
  163. return
  164. }
  165. if (!isTeXFile || !trackedWrite) {
  166. return
  167. }
  168. if (!hasLinkUrlFeature) {
  169. return
  170. }
  171. return [
  172. {
  173. menuLabel: t('from_url'),
  174. label: t('insert_figure_from_url'),
  175. id: 'insert-figure-from-url',
  176. handler: () => {
  177. openFigureModal(FigureModalSource.FROM_URL)
  178. },
  179. },
  180. ]
  181. }, [
  182. t,
  183. editorIsVisible,
  184. openFigureModal,
  185. trackedWrite,
  186. isTeXFile,
  187. hasLinkUrlFeature,
  188. ])
  189. useCommandProvider(() => {
  190. if (!editorIsVisible) {
  191. return
  192. }
  193. if (!isTeXFile || !trackedWrite) {
  194. return
  195. }
  196. return [
  197. {
  198. id: 'insert-table',
  199. menuLabel: t('table'),
  200. label: t('insert_table'),
  201. handler: () => {
  202. commands.insertTable(view, 3, 3)
  203. view.focus()
  204. },
  205. },
  206. {
  207. id: 'insert-citation',
  208. menuLabel: t('citation'),
  209. label: t('insert_citation'),
  210. handler: () => {
  211. commands.insertCite(view)
  212. view.focus()
  213. },
  214. },
  215. {
  216. id: 'insert-link',
  217. menuLabel: t('link'),
  218. label: t('insert_link'),
  219. handler: () => {
  220. commands.wrapInHref(view)
  221. view.focus()
  222. },
  223. },
  224. {
  225. id: 'insert-cross-reference',
  226. menuLabel: t('cross_reference'),
  227. label: t('insert_cross_reference'),
  228. handler: () => {
  229. commands.insertRef(view)
  230. view.focus()
  231. },
  232. },
  233. {
  234. id: 'comment',
  235. menuLabel: t('comment'),
  236. label: t('add_comment'),
  237. handler: () => {
  238. commands.addComment('toolbar')
  239. },
  240. disabled: !comment || !canComment,
  241. },
  242. /************************************
  243. * Format menu
  244. ************************************/
  245. {
  246. id: 'format-bold',
  247. label: t('bold'),
  248. handler: () => {
  249. commands.toggleBold(view)
  250. view.focus()
  251. },
  252. },
  253. {
  254. id: 'format-italics',
  255. label: t('italics'),
  256. handler: () => {
  257. commands.toggleItalic(view)
  258. view.focus()
  259. },
  260. },
  261. {
  262. id: 'format-bullet-list',
  263. label: t('bullet_list'),
  264. handler: () => {
  265. commands.toggleBulletList(view)
  266. view.focus()
  267. },
  268. },
  269. {
  270. id: 'format-numbered-list',
  271. label: t('numbered_list'),
  272. handler: () => {
  273. commands.toggleNumberedList(view)
  274. view.focus()
  275. },
  276. },
  277. {
  278. id: 'format-increase-indentation',
  279. label: t('increase_indent'),
  280. handler: () => {
  281. commands.indentIncrease(view)
  282. view.focus()
  283. },
  284. },
  285. {
  286. id: 'format-decrease-indentation',
  287. label: t('decrease_indent'),
  288. handler: () => {
  289. commands.indentDecrease(view)
  290. view.focus()
  291. },
  292. },
  293. {
  294. id: 'format-style-normal',
  295. label: t('normal'),
  296. handler: () => {
  297. setSectionHeadingLevel(view, 'text')
  298. view.focus()
  299. },
  300. },
  301. {
  302. id: 'format-style-section',
  303. label: 'Section',
  304. handler: () => {
  305. setSectionHeadingLevel(view, 'section')
  306. view.focus()
  307. },
  308. },
  309. {
  310. id: 'format-style-subsection',
  311. label: 'Subsection',
  312. handler: () => {
  313. setSectionHeadingLevel(view, 'subsection')
  314. view.focus()
  315. },
  316. },
  317. {
  318. id: 'format-style-subsubsection',
  319. label: 'Subsubsection',
  320. handler: () => {
  321. setSectionHeadingLevel(view, 'subsubsection')
  322. view.focus()
  323. },
  324. },
  325. {
  326. id: 'format-style-paragraph',
  327. label: 'Paragraph',
  328. handler: () => {
  329. setSectionHeadingLevel(view, 'paragraph')
  330. view.focus()
  331. },
  332. },
  333. {
  334. id: 'format-style-subparagraph',
  335. label: 'Subparagraph',
  336. handler: () => {
  337. setSectionHeadingLevel(view, 'subparagraph')
  338. view.focus()
  339. },
  340. },
  341. ]
  342. }, [view, t, editorIsVisible, trackedWrite, isTeXFile, canComment, comment])
  343. const { toggleSymbolPalette } = useEditorPropertiesContext()
  344. const symbolPaletteAvailable = getMeta('ol-symbolPaletteAvailable')
  345. useCommandProvider(() => {
  346. if (!editorIsVisible) {
  347. return
  348. }
  349. if (!symbolPaletteAvailable) {
  350. return
  351. }
  352. if (!isTeXFile || !trackedWrite) {
  353. return
  354. }
  355. return [
  356. {
  357. id: 'insert-symbol',
  358. label: t('symbol'),
  359. handler: () => {
  360. toggleSymbolPalette?.()
  361. },
  362. },
  363. ]
  364. }, [
  365. symbolPaletteAvailable,
  366. t,
  367. toggleSymbolPalette,
  368. editorIsVisible,
  369. isTeXFile,
  370. trackedWrite,
  371. ])
  372. }