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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  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. export const useToolbarMenuBarEditorCommands = () => {
  19. const view = useCodeMirrorViewContext()
  20. const state = useCodeMirrorStateContext()
  21. const { t } = useTranslation()
  22. const { view: layoutView } = useLayoutContext()
  23. const editorIsVisible = layoutView === 'editor'
  24. const { trackedWrite, comment } = usePermissionsContext()
  25. const languageName = state.facet(language)?.name
  26. const isTeXFile = languageName === 'latex'
  27. const openFigureModal = useCallback((source: FigureModalSource) => {
  28. window.dispatchEvent(
  29. new CustomEvent('figure-modal:open', {
  30. detail: { source },
  31. })
  32. )
  33. }, [])
  34. useCommandProvider(() => {
  35. if (!editorIsVisible) {
  36. return
  37. }
  38. return [
  39. /************************************
  40. * Edit menu
  41. ************************************/
  42. {
  43. id: 'undo',
  44. label: t('undo'),
  45. handler: () => {
  46. undo(view)
  47. view.focus()
  48. },
  49. disabled: !trackedWrite,
  50. },
  51. {
  52. id: 'redo',
  53. label: t('redo'),
  54. handler: () => {
  55. redo(view)
  56. view.focus()
  57. },
  58. disabled: !trackedWrite,
  59. },
  60. {
  61. id: 'find',
  62. label: t('find'),
  63. handler: () => {
  64. openSearchPanel(view)
  65. },
  66. },
  67. {
  68. id: 'select-all',
  69. label: t('select_all'),
  70. handler: () => {
  71. selectAll(view)
  72. view.focus()
  73. },
  74. },
  75. ]
  76. }, [editorIsVisible, t, view, trackedWrite])
  77. // LaTeX commands
  78. useCommandProvider(() => {
  79. if (!editorIsVisible) {
  80. return
  81. }
  82. if (!isTeXFile || !trackedWrite) {
  83. return
  84. }
  85. return [
  86. /************************************
  87. * Insert menu
  88. ************************************/
  89. {
  90. id: 'insert-inline-math',
  91. label: t('inline_math'),
  92. handler: () => {
  93. commands.wrapInInlineMath(view)
  94. view.focus()
  95. },
  96. },
  97. {
  98. id: 'insert-display-math',
  99. label: t('display_math'),
  100. handler: () => {
  101. commands.wrapInDisplayMath(view)
  102. view.focus()
  103. },
  104. },
  105. {
  106. label: t('upload_from_computer'),
  107. id: 'insert-figure-from-computer',
  108. handler: () => {
  109. openFigureModal(FigureModalSource.FILE_UPLOAD)
  110. },
  111. },
  112. {
  113. label: t('from_project_files'),
  114. id: 'insert-figure-from-project-files',
  115. handler: () => {
  116. openFigureModal(FigureModalSource.FILE_TREE)
  117. },
  118. },
  119. {
  120. label: t('from_another_project'),
  121. id: 'insert-figure-from-another-project',
  122. handler: () => {
  123. openFigureModal(FigureModalSource.OTHER_PROJECT)
  124. },
  125. },
  126. {
  127. label: t('from_url'),
  128. id: 'insert-figure-from-url',
  129. handler: () => {
  130. openFigureModal(FigureModalSource.FROM_URL)
  131. },
  132. },
  133. {
  134. id: 'insert-table',
  135. label: t('table'),
  136. handler: () => {
  137. commands.insertTable(view, 3, 3)
  138. view.focus()
  139. },
  140. },
  141. {
  142. id: 'insert-citation',
  143. label: t('citation'),
  144. handler: () => {
  145. commands.insertCite(view)
  146. view.focus()
  147. },
  148. },
  149. {
  150. id: 'insert-link',
  151. label: t('link'),
  152. handler: () => {
  153. commands.wrapInHref(view)
  154. view.focus()
  155. },
  156. },
  157. {
  158. id: 'insert-cross-reference',
  159. label: t('cross_reference'),
  160. handler: () => {
  161. commands.insertRef(view)
  162. view.focus()
  163. },
  164. },
  165. {
  166. id: 'comment',
  167. label: t('comment'),
  168. handler: () => {
  169. commands.addComment('toolbar')
  170. },
  171. disabled: !comment || state.selection.main.empty,
  172. },
  173. /************************************
  174. * Format menu
  175. ************************************/
  176. {
  177. id: 'format-bold',
  178. label: t('bold'),
  179. handler: () => {
  180. commands.toggleBold(view)
  181. view.focus()
  182. },
  183. },
  184. {
  185. id: 'format-italics',
  186. label: t('italics'),
  187. handler: () => {
  188. commands.toggleItalic(view)
  189. view.focus()
  190. },
  191. },
  192. {
  193. id: 'format-bullet-list',
  194. label: t('bullet_list'),
  195. handler: () => {
  196. commands.toggleBulletList(view)
  197. view.focus()
  198. },
  199. },
  200. {
  201. id: 'format-numbered-list',
  202. label: t('numbered_list'),
  203. handler: () => {
  204. commands.toggleNumberedList(view)
  205. view.focus()
  206. },
  207. },
  208. {
  209. id: 'format-increase-indentation',
  210. label: t('increase_indent'),
  211. handler: () => {
  212. commands.indentIncrease(view)
  213. view.focus()
  214. },
  215. },
  216. {
  217. id: 'format-decrease-indentation',
  218. label: t('decrease_indent'),
  219. handler: () => {
  220. commands.indentDecrease(view)
  221. view.focus()
  222. },
  223. },
  224. {
  225. id: 'format-style-normal',
  226. label: t('normal'),
  227. handler: () => {
  228. setSectionHeadingLevel(view, 'text')
  229. view.focus()
  230. },
  231. },
  232. {
  233. id: 'format-style-section',
  234. label: 'Section',
  235. handler: () => {
  236. setSectionHeadingLevel(view, 'section')
  237. view.focus()
  238. },
  239. },
  240. {
  241. id: 'format-style-subsection',
  242. label: 'Subsection',
  243. handler: () => {
  244. setSectionHeadingLevel(view, 'subsection')
  245. view.focus()
  246. },
  247. },
  248. {
  249. id: 'format-style-subsubsection',
  250. label: 'Subsubsection',
  251. handler: () => {
  252. setSectionHeadingLevel(view, 'subsubsection')
  253. view.focus()
  254. },
  255. },
  256. {
  257. id: 'format-style-paragraph',
  258. label: 'Paragraph',
  259. handler: () => {
  260. setSectionHeadingLevel(view, 'paragraph')
  261. view.focus()
  262. },
  263. },
  264. {
  265. id: 'format-style-subparagraph',
  266. label: 'Subparagraph',
  267. handler: () => {
  268. setSectionHeadingLevel(view, 'subparagraph')
  269. view.focus()
  270. },
  271. },
  272. ]
  273. }, [
  274. view,
  275. t,
  276. editorIsVisible,
  277. openFigureModal,
  278. trackedWrite,
  279. isTeXFile,
  280. state.selection.main.empty,
  281. comment,
  282. ])
  283. const { toggleSymbolPalette } = useEditorPropertiesContext()
  284. const symbolPaletteAvailable = getMeta('ol-symbolPaletteAvailable')
  285. useCommandProvider(() => {
  286. if (!editorIsVisible) {
  287. return
  288. }
  289. if (!symbolPaletteAvailable) {
  290. return
  291. }
  292. if (!isTeXFile || !trackedWrite) {
  293. return
  294. }
  295. return [
  296. {
  297. id: 'insert-symbol',
  298. label: t('symbol'),
  299. handler: () => {
  300. toggleSymbolPalette?.()
  301. },
  302. },
  303. ]
  304. }, [
  305. symbolPaletteAvailable,
  306. t,
  307. toggleSymbolPalette,
  308. editorIsVisible,
  309. isTeXFile,
  310. trackedWrite,
  311. ])
  312. }