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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  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. label: t('inline_math'),
  99. handler: () => {
  100. commands.wrapInInlineMath(view)
  101. view.focus()
  102. },
  103. },
  104. {
  105. id: 'insert-display-math',
  106. label: t('display_math'),
  107. handler: () => {
  108. commands.wrapInDisplayMath(view)
  109. view.focus()
  110. },
  111. },
  112. {
  113. label: t('upload_from_computer'),
  114. id: 'insert-figure-from-computer',
  115. handler: () => {
  116. openFigureModal(FigureModalSource.FILE_UPLOAD)
  117. },
  118. },
  119. {
  120. label: t('from_project_files'),
  121. id: 'insert-figure-from-project-files',
  122. handler: () => {
  123. openFigureModal(FigureModalSource.FILE_TREE)
  124. },
  125. },
  126. ]
  127. }, [view, t, editorIsVisible, openFigureModal, trackedWrite, isTeXFile])
  128. useCommandProvider(() => {
  129. if (!editorIsVisible) {
  130. return
  131. }
  132. if (!isTeXFile || !trackedWrite) {
  133. return
  134. }
  135. if (!hasLinkedProjectFileFeature || !hasLinkedProjectOutputFileFeature) {
  136. return
  137. }
  138. return [
  139. {
  140. label: t('from_another_project'),
  141. id: 'insert-figure-from-another-project',
  142. handler: () => {
  143. openFigureModal(FigureModalSource.OTHER_PROJECT)
  144. },
  145. },
  146. ]
  147. }, [
  148. t,
  149. editorIsVisible,
  150. openFigureModal,
  151. trackedWrite,
  152. isTeXFile,
  153. hasLinkedProjectFileFeature,
  154. hasLinkedProjectOutputFileFeature,
  155. ])
  156. useCommandProvider(() => {
  157. if (!editorIsVisible) {
  158. return
  159. }
  160. if (!isTeXFile || !trackedWrite) {
  161. return
  162. }
  163. if (!hasLinkUrlFeature) {
  164. return
  165. }
  166. return [
  167. {
  168. label: t('from_url'),
  169. id: 'insert-figure-from-url',
  170. handler: () => {
  171. openFigureModal(FigureModalSource.FROM_URL)
  172. },
  173. },
  174. ]
  175. }, [
  176. t,
  177. editorIsVisible,
  178. openFigureModal,
  179. trackedWrite,
  180. isTeXFile,
  181. hasLinkUrlFeature,
  182. ])
  183. useCommandProvider(() => {
  184. if (!editorIsVisible) {
  185. return
  186. }
  187. if (!isTeXFile || !trackedWrite) {
  188. return
  189. }
  190. return [
  191. {
  192. id: 'insert-table',
  193. label: t('table'),
  194. handler: () => {
  195. commands.insertTable(view, 3, 3)
  196. view.focus()
  197. },
  198. },
  199. {
  200. id: 'insert-citation',
  201. label: t('citation'),
  202. handler: () => {
  203. commands.insertCite(view)
  204. view.focus()
  205. },
  206. },
  207. {
  208. id: 'insert-link',
  209. label: t('link'),
  210. handler: () => {
  211. commands.wrapInHref(view)
  212. view.focus()
  213. },
  214. },
  215. {
  216. id: 'insert-cross-reference',
  217. label: t('cross_reference'),
  218. handler: () => {
  219. commands.insertRef(view)
  220. view.focus()
  221. },
  222. },
  223. {
  224. id: 'comment',
  225. label: t('comment'),
  226. handler: () => {
  227. commands.addComment('toolbar')
  228. },
  229. disabled: !comment || !canComment,
  230. },
  231. /************************************
  232. * Format menu
  233. ************************************/
  234. {
  235. id: 'format-bold',
  236. label: t('bold'),
  237. handler: () => {
  238. commands.toggleBold(view)
  239. view.focus()
  240. },
  241. },
  242. {
  243. id: 'format-italics',
  244. label: t('italics'),
  245. handler: () => {
  246. commands.toggleItalic(view)
  247. view.focus()
  248. },
  249. },
  250. {
  251. id: 'format-bullet-list',
  252. label: t('bullet_list'),
  253. handler: () => {
  254. commands.toggleBulletList(view)
  255. view.focus()
  256. },
  257. },
  258. {
  259. id: 'format-numbered-list',
  260. label: t('numbered_list'),
  261. handler: () => {
  262. commands.toggleNumberedList(view)
  263. view.focus()
  264. },
  265. },
  266. {
  267. id: 'format-increase-indentation',
  268. label: t('increase_indent'),
  269. handler: () => {
  270. commands.indentIncrease(view)
  271. view.focus()
  272. },
  273. },
  274. {
  275. id: 'format-decrease-indentation',
  276. label: t('decrease_indent'),
  277. handler: () => {
  278. commands.indentDecrease(view)
  279. view.focus()
  280. },
  281. },
  282. {
  283. id: 'format-style-normal',
  284. label: t('normal'),
  285. handler: () => {
  286. setSectionHeadingLevel(view, 'text')
  287. view.focus()
  288. },
  289. },
  290. {
  291. id: 'format-style-section',
  292. label: 'Section',
  293. handler: () => {
  294. setSectionHeadingLevel(view, 'section')
  295. view.focus()
  296. },
  297. },
  298. {
  299. id: 'format-style-subsection',
  300. label: 'Subsection',
  301. handler: () => {
  302. setSectionHeadingLevel(view, 'subsection')
  303. view.focus()
  304. },
  305. },
  306. {
  307. id: 'format-style-subsubsection',
  308. label: 'Subsubsection',
  309. handler: () => {
  310. setSectionHeadingLevel(view, 'subsubsection')
  311. view.focus()
  312. },
  313. },
  314. {
  315. id: 'format-style-paragraph',
  316. label: 'Paragraph',
  317. handler: () => {
  318. setSectionHeadingLevel(view, 'paragraph')
  319. view.focus()
  320. },
  321. },
  322. {
  323. id: 'format-style-subparagraph',
  324. label: 'Subparagraph',
  325. handler: () => {
  326. setSectionHeadingLevel(view, 'subparagraph')
  327. view.focus()
  328. },
  329. },
  330. ]
  331. }, [view, t, editorIsVisible, trackedWrite, isTeXFile, canComment, comment])
  332. const { toggleSymbolPalette } = useEditorPropertiesContext()
  333. const symbolPaletteAvailable = getMeta('ol-symbolPaletteAvailable')
  334. useCommandProvider(() => {
  335. if (!editorIsVisible) {
  336. return
  337. }
  338. if (!symbolPaletteAvailable) {
  339. return
  340. }
  341. if (!isTeXFile || !trackedWrite) {
  342. return
  343. }
  344. return [
  345. {
  346. id: 'insert-symbol',
  347. label: t('symbol'),
  348. handler: () => {
  349. toggleSymbolPalette?.()
  350. },
  351. },
  352. ]
  353. }, [
  354. symbolPaletteAvailable,
  355. t,
  356. toggleSymbolPalette,
  357. editorIsVisible,
  358. isTeXFile,
  359. trackedWrite,
  360. ])
  361. }