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

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