use-context-menu-items.tsx 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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 { useProjectContext } from '@/shared/context/project-context'
  8. import useSynctex from '@/features/pdf-preview/hooks/use-synctex'
  9. import { useDetachCompileContext } from '@/shared/context/detach-compile-context'
  10. import { useLayoutContext } from '@/shared/context/layout-context'
  11. import { useFeatureFlag } from '@/shared/context/split-test-context'
  12. import { useTranslation } from 'react-i18next'
  13. import { useCallback, useEffect, useRef } from 'react'
  14. import {
  15. formatShortcut,
  16. useCommandRegistry,
  17. } from '@/features/ide-react/context/command-registry-context'
  18. import { closeContextMenuEffect } from '../extensions/context-menu'
  19. import * as commands from '../extensions/toolbar/commands'
  20. import {
  21. cutSelection,
  22. copySelection,
  23. pasteWithoutFormatting,
  24. pasteWithFormatting,
  25. } from '../commands/clipboard'
  26. import { isVisual } from '../extensions/visual/visual'
  27. import { useEditorContext } from '@/shared/context/editor-context'
  28. import { useTrackingChangesMode } from '@/shared/hooks/use-tracking-changes-mode'
  29. export const useContextMenuItems = () => {
  30. const view = useCodeMirrorViewContext()
  31. const state = useCodeMirrorStateContext()
  32. const permissions = usePermissionsContext()
  33. const { wantTrackChanges } = useEditorPropertiesContext()
  34. const { syncToPdf, syncToPdfInFlight, canSyncToPdf } = useSynctex()
  35. const { pdfUrl, pdfViewer } = useDetachCompileContext()
  36. const { detachRole } = useLayoutContext()
  37. const visualPreviewEnabled = useFeatureFlag('visual-preview')
  38. const { t } = useTranslation()
  39. const { shortcuts } = useCommandRegistry()
  40. const { features } = useProjectContext()
  41. const requestedPdfSyncRef = useRef(false)
  42. const { setShowUpgradeModal } = useEditorContext()
  43. const trackingChangesMode = useTrackingChangesMode()
  44. const isReview = trackingChangesMode === 'review'
  45. const closeMenu = useCallback(() => {
  46. view.dispatch({ effects: closeContextMenuEffect.of(null) })
  47. view.focus()
  48. }, [view])
  49. // Handle closing the menu when it loses focus, e.g. click outside the editor
  50. const onToggle = (show: boolean) => {
  51. if (!show) {
  52. // Skip closing if a sync to PDF is in flight
  53. if (requestedPdfSyncRef.current) {
  54. return
  55. }
  56. closeMenu()
  57. }
  58. }
  59. // Wait for syncToPdf to finish before closing the menu
  60. useEffect(() => {
  61. if (requestedPdfSyncRef.current && !syncToPdfInFlight) {
  62. closeMenu()
  63. // Clear the synchronous flag when the close completes
  64. requestedPdfSyncRef.current = false
  65. }
  66. }, [syncToPdfInFlight, closeMenu])
  67. const hasSelection = !state.selection.main.empty
  68. const canEdit = permissions.write || permissions.trackedWrite
  69. const jumpToLocationInPdfEnabled =
  70. pdfUrl &&
  71. pdfViewer !== 'native' &&
  72. !detachRole &&
  73. !visualPreviewEnabled &&
  74. canSyncToPdf
  75. const wrapForContextMenu = useCallback(
  76. (command: () => Promise<boolean> | boolean) => async () => {
  77. const result = await command()
  78. if (result !== false) {
  79. view.focus()
  80. closeMenu()
  81. }
  82. },
  83. [view, closeMenu]
  84. )
  85. const inVisualMode = isVisual(view)
  86. const handleCut = wrapForContextMenu(() => cutSelection(view))
  87. const handleCopy = wrapForContextMenu(() => copySelection(view))
  88. const handlePaste = wrapForContextMenu(() =>
  89. inVisualMode ? pasteWithFormatting(view) : pasteWithoutFormatting(view)
  90. )
  91. const handlePasteSpecial = wrapForContextMenu(() =>
  92. inVisualMode ? pasteWithoutFormatting(view) : pasteWithFormatting(view)
  93. )
  94. const handleDelete = wrapForContextMenu(() => commands.deleteSelection(view))
  95. const handleToggleTrackChanges = wrapForContextMenu(() => {
  96. // Matching the logic in review toggle to ensure consistency for server pro
  97. if (!features.trackChanges && !isReview) {
  98. setShowUpgradeModal(true)
  99. return true
  100. }
  101. window.dispatchEvent(new Event('toggle-track-changes'))
  102. return true
  103. })
  104. const handleComment = wrapForContextMenu(() => {
  105. commands.addComment()
  106. return true
  107. })
  108. // Sync-to-PDF is special: it needs to wait for async completion before closing
  109. const handleSyncToPdf = useCallback(() => {
  110. requestedPdfSyncRef.current = true
  111. syncToPdf()
  112. view.focus()
  113. }, [syncToPdf, view])
  114. const getShortcut = useCallback(
  115. (id: string) => {
  116. const shortcut = shortcuts[id]?.[0]
  117. return shortcut ? formatShortcut(shortcut) : undefined
  118. },
  119. [shortcuts]
  120. )
  121. return {
  122. closeMenu,
  123. onToggle,
  124. menuItems: [
  125. {
  126. label: t('cut'),
  127. handler: handleCut,
  128. disabled: false,
  129. show: canEdit,
  130. shortcut: getShortcut('cut'),
  131. },
  132. {
  133. label: t('copy'),
  134. handler: handleCopy,
  135. disabled: false,
  136. show: true,
  137. shortcut: getShortcut('copy'),
  138. },
  139. {
  140. label: t('paste'),
  141. handler: handlePaste,
  142. disabled: false,
  143. show: canEdit,
  144. shortcut: getShortcut('paste'),
  145. },
  146. {
  147. label: inVisualMode
  148. ? t('paste_without_formatting')
  149. : t('paste_with_formatting'),
  150. handler: handlePasteSpecial,
  151. disabled: false,
  152. show: canEdit,
  153. shortcut: inVisualMode ? getShortcut('paste-special') : undefined,
  154. },
  155. {
  156. label: t('delete'),
  157. handler: handleDelete,
  158. disabled: !hasSelection,
  159. show: canEdit,
  160. shortcut: undefined,
  161. },
  162. {
  163. label: t('jump_to_location_in_pdf'),
  164. handler: handleSyncToPdf,
  165. disabled: syncToPdfInFlight,
  166. separatorAbove: true,
  167. show: jumpToLocationInPdfEnabled,
  168. shortcut: undefined,
  169. },
  170. {
  171. label: wantTrackChanges ? t('back_to_editing') : t('suggest_edits'),
  172. handler: handleToggleTrackChanges,
  173. disabled: false,
  174. separatorAbove: true,
  175. show: canEdit && features.trackChangesVisible,
  176. shortcut: getShortcut('toggle-track-changes'),
  177. },
  178. {
  179. label: t('comment'),
  180. handler: handleComment,
  181. disabled: !hasSelection,
  182. show: permissions.comment,
  183. shortcut: getShortcut('insert-comment'),
  184. },
  185. ].filter(item => item.show),
  186. }
  187. }