use-context-menu-items.tsx 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. import {
  2. useCodeMirrorStateContext,
  3. useCodeMirrorViewContext,
  4. } from '@/features/source-editor/components/codemirror-context'
  5. import { usePermissionsContext } from '@/features/ide-react/context/permissions-context'
  6. import { useEditorPropertiesContext } from '@/features/ide-react/context/editor-properties-context'
  7. import useSynctex from '@/features/pdf-preview/hooks/use-synctex'
  8. import { useDetachCompileContext } from '@/shared/context/detach-compile-context'
  9. import { useLayoutContext } from '@/shared/context/layout-context'
  10. import { useFeatureFlag } from '@/shared/context/split-test-context'
  11. import { useTranslation } from 'react-i18next'
  12. import { useCallback, useEffect, useState } from 'react'
  13. import {
  14. formatShortcut,
  15. useCommandRegistry,
  16. } from '@/features/ide-react/context/command-registry-context'
  17. import { closeContextMenuEffect } from '../extensions/context-menu'
  18. import * as commands from '../extensions/toolbar/commands'
  19. import {
  20. cutSelection,
  21. copySelection,
  22. pasteWithoutFormatting,
  23. pasteWithFormatting,
  24. } from '../commands/clipboard'
  25. import { isVisual } from '../extensions/visual/visual'
  26. export const useContextMenuItems = () => {
  27. const view = useCodeMirrorViewContext()
  28. const state = useCodeMirrorStateContext()
  29. const permissions = usePermissionsContext()
  30. const { wantTrackChanges } = useEditorPropertiesContext()
  31. const { syncToPdf, syncToPdfInFlight, canSyncToPdf } = useSynctex()
  32. const { pdfUrl, pdfViewer } = useDetachCompileContext()
  33. const { detachRole } = useLayoutContext()
  34. const visualPreviewEnabled = useFeatureFlag('visual-preview')
  35. const { t } = useTranslation()
  36. const { shortcuts } = useCommandRegistry()
  37. const closeMenu = useCallback(() => {
  38. view.dispatch({ effects: closeContextMenuEffect.of(null) })
  39. }, [view])
  40. const [pendingClose, setPendingClose] = useState(false)
  41. // Wait for syncToPdf to finish before closing the menu
  42. useEffect(() => {
  43. if (pendingClose && !syncToPdfInFlight) {
  44. closeMenu()
  45. setPendingClose(false)
  46. }
  47. }, [pendingClose, syncToPdfInFlight, closeMenu])
  48. const hasSelection = !state.selection.main.empty
  49. const canEdit = permissions.write || permissions.trackedWrite
  50. const jumpToLocationInPdfEnabled =
  51. pdfUrl &&
  52. pdfViewer !== 'native' &&
  53. !detachRole &&
  54. !visualPreviewEnabled &&
  55. canSyncToPdf
  56. const wrapForContextMenu = useCallback(
  57. (command: () => Promise<boolean> | boolean) => async () => {
  58. const result = await command()
  59. if (result !== false) {
  60. view.focus()
  61. closeMenu()
  62. }
  63. },
  64. [view, closeMenu]
  65. )
  66. const inVisualMode = isVisual(view)
  67. const handleCut = wrapForContextMenu(() => cutSelection(view))
  68. const handleCopy = wrapForContextMenu(() => copySelection(view))
  69. const handlePaste = wrapForContextMenu(() =>
  70. inVisualMode ? pasteWithFormatting(view) : pasteWithoutFormatting(view)
  71. )
  72. const handlePasteSpecial = wrapForContextMenu(() =>
  73. inVisualMode ? pasteWithoutFormatting(view) : pasteWithFormatting(view)
  74. )
  75. const handleDelete = wrapForContextMenu(() => commands.deleteSelection(view))
  76. const handleToggleTrackChanges = wrapForContextMenu(() => {
  77. window.dispatchEvent(new Event('toggle-track-changes'))
  78. return true
  79. })
  80. const handleComment = wrapForContextMenu(() => {
  81. commands.addComment()
  82. return true
  83. })
  84. // Sync-to-PDF is special: it needs to wait for async completion before closing
  85. const handleSyncToPdf = useCallback(() => {
  86. syncToPdf()
  87. setPendingClose(true)
  88. view.focus()
  89. }, [syncToPdf, view])
  90. const getShortcut = useCallback(
  91. (id: string) => {
  92. const shortcut = shortcuts[id]?.[0]
  93. return shortcut ? formatShortcut(shortcut) : undefined
  94. },
  95. [shortcuts]
  96. )
  97. return [
  98. {
  99. label: t('cut'),
  100. handler: handleCut,
  101. disabled: false,
  102. show: canEdit,
  103. shortcut: getShortcut('cut'),
  104. },
  105. {
  106. label: t('copy'),
  107. handler: handleCopy,
  108. disabled: false,
  109. show: true,
  110. shortcut: getShortcut('copy'),
  111. },
  112. {
  113. label: t('paste'),
  114. handler: handlePaste,
  115. disabled: false,
  116. show: canEdit,
  117. shortcut: getShortcut('paste'),
  118. },
  119. {
  120. label: inVisualMode
  121. ? t('paste_without_formatting')
  122. : t('paste_with_formatting'),
  123. handler: handlePasteSpecial,
  124. disabled: false,
  125. show: canEdit,
  126. shortcut: inVisualMode ? getShortcut('paste-special') : undefined,
  127. },
  128. {
  129. label: t('delete'),
  130. handler: handleDelete,
  131. disabled: !hasSelection,
  132. show: canEdit,
  133. shortcut: undefined,
  134. },
  135. {
  136. label: t('jump_to_location_in_pdf'),
  137. handler: handleSyncToPdf,
  138. disabled: syncToPdfInFlight,
  139. separatorAbove: true,
  140. show: jumpToLocationInPdfEnabled,
  141. shortcut: undefined,
  142. },
  143. {
  144. label: wantTrackChanges ? t('back_to_editing') : t('suggest_edits'),
  145. handler: handleToggleTrackChanges,
  146. disabled: false,
  147. separatorAbove: true,
  148. show: canEdit,
  149. shortcut: getShortcut('toggle-track-changes'),
  150. },
  151. {
  152. label: t('comment'),
  153. handler: handleComment,
  154. disabled: !hasSelection,
  155. show: permissions.comment,
  156. shortcut: getShortcut('insert-comment'),
  157. },
  158. ].filter(item => item.show)
  159. }