review-panel-entry.tsx 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. import { FC, useCallback, useEffect, useRef, useState } from 'react'
  2. import { AnyOperation } from '../../../../../types/change'
  3. import {
  4. useCodeMirrorStateContext,
  5. useCodeMirrorViewContext,
  6. } from '@/features/source-editor/components/codemirror-context'
  7. import { isSelectionWithinOp } from '../utils/is-selection-within-op'
  8. import classNames from 'classnames'
  9. import {
  10. clearHighlightRanges,
  11. highlightRanges,
  12. } from '@/features/source-editor/extensions/ranges'
  13. import { useEditorManagerContext } from '@/features/ide-react/context/editor-manager-context'
  14. import { EditorSelection } from '@codemirror/state'
  15. import { OFFSET_FOR_ENTRIES_ABOVE } from '../utils/position-items'
  16. import useReviewPanelLayout from '../hooks/use-review-panel-layout'
  17. import { EntryIndicator } from './review-panel-entry-indicator'
  18. import { EditorView } from '@codemirror/view'
  19. export const ReviewPanelEntry: FC<
  20. React.PropsWithChildren<{
  21. position: number
  22. op: AnyOperation
  23. docId: string
  24. top?: number
  25. className?: string
  26. selectLineOnFocus?: boolean
  27. hoverRanges?: boolean
  28. disabled?: boolean
  29. handleEnter?: () => void
  30. handleLeave?: () => void
  31. entryIndicator?: 'comment' | 'edit'
  32. }>
  33. > = ({
  34. children,
  35. position,
  36. top,
  37. op,
  38. className,
  39. selectLineOnFocus = true,
  40. docId,
  41. hoverRanges = true,
  42. disabled,
  43. handleEnter,
  44. handleLeave,
  45. entryIndicator,
  46. }) => {
  47. const state = useCodeMirrorStateContext()
  48. const view = useCodeMirrorViewContext()
  49. const { openDocWithId, getCurrentDocumentId } = useEditorManagerContext()
  50. const [selected, setSelected] = useState(false)
  51. const [focused, setFocused] = useState(false)
  52. const [textareaFocused, setTextareaFocused] = useState(false)
  53. const { openReviewPanel } = useReviewPanelLayout()
  54. const highlighted = isSelectionWithinOp(op, state.selection.main)
  55. const entryRef = useRef<HTMLDivElement>(null)
  56. const mousePressedRef = useRef(false)
  57. const selectEntry = useCallback(
  58. (event: React.FocusEvent | React.MouseEvent) => {
  59. setFocused(true)
  60. if (event.target instanceof HTMLTextAreaElement) {
  61. const entryBottom =
  62. (entryRef.current?.offsetTop || 0) +
  63. (entryRef.current?.offsetHeight || 0)
  64. if (entryBottom > OFFSET_FOR_ENTRIES_ABOVE) {
  65. // if the entry textarea is visible, no need to select the entry
  66. // so that it doesn't scroll out of view as user types
  67. setTextareaFocused(true)
  68. return
  69. }
  70. }
  71. if (mousePressedRef.current) {
  72. return
  73. }
  74. setSelected(true)
  75. if (!selectLineOnFocus) {
  76. return
  77. }
  78. if (getCurrentDocumentId() !== docId) {
  79. openDocWithId(docId, { gotoOffset: position, keepCurrentView: true })
  80. } else {
  81. setTimeout(() => {
  82. const selection = EditorSelection.cursor(position)
  83. // if the position is outside the viewport, so could be estimated,
  84. // use EditorView.scrollIntoView (accurate, not smooth)
  85. if (position < view.viewport.from || view.viewport.to < position) {
  86. view.dispatch({
  87. selection,
  88. effects: EditorView.scrollIntoView(selection, { y: 'center' }),
  89. })
  90. return
  91. }
  92. // if the position is inside the viewport, so accurate,
  93. // use scrollDOM.scrollTo (smooth)
  94. view.dispatch({ selection })
  95. // scroll to line (centered)
  96. const blockInfo = view.lineBlockAt(position)
  97. const coordsAtPos = view.coordsAtPos(position)
  98. const coordsAtLineStart = view.coordsAtPos(blockInfo.from)
  99. let wrappedLineOffset = 0
  100. if (coordsAtPos !== null && coordsAtLineStart !== null) {
  101. wrappedLineOffset = coordsAtPos.top - coordsAtLineStart.top
  102. }
  103. const editorHeight = view.scrollDOM.getBoundingClientRect().height
  104. view.scrollDOM.scrollTo({
  105. top:
  106. blockInfo.top -
  107. editorHeight / 2 +
  108. view.defaultLineHeight +
  109. wrappedLineOffset,
  110. behavior: 'smooth',
  111. })
  112. })
  113. }
  114. },
  115. [
  116. getCurrentDocumentId,
  117. docId,
  118. selectLineOnFocus,
  119. view,
  120. position,
  121. openDocWithId,
  122. ]
  123. )
  124. // Clear op highlight on dismount
  125. useEffect(() => {
  126. return () => {
  127. if (hoverRanges) {
  128. setTimeout(() => {
  129. view.dispatch(clearHighlightRanges(op))
  130. })
  131. }
  132. }
  133. }, []) // eslint-disable-line react-hooks/exhaustive-deps
  134. return (
  135. <div
  136. ref={entryRef}
  137. onMouseDown={() => {
  138. mousePressedRef.current = true
  139. }}
  140. onMouseUp={event => {
  141. mousePressedRef.current = false
  142. const isTextSelected = Boolean(window.getSelection()?.toString())
  143. if (!isTextSelected && !selected) {
  144. selectEntry(event)
  145. }
  146. }}
  147. onFocus={selectEntry}
  148. onBlur={() => {
  149. mousePressedRef.current = false
  150. setSelected(false)
  151. setFocused(false)
  152. setTextareaFocused(false)
  153. }}
  154. onMouseEnter={() => {
  155. if (hoverRanges) {
  156. view.dispatch(highlightRanges(op))
  157. }
  158. }}
  159. onMouseLeave={() => {
  160. if (hoverRanges) {
  161. view.dispatch(clearHighlightRanges(op))
  162. }
  163. }}
  164. role="button"
  165. tabIndex={position + 1}
  166. className={classNames(
  167. 'review-panel-entry',
  168. {
  169. // 'selected' is used to manually select an entry
  170. // useful if the range is within range and you want to show the one outside the viewport
  171. // it is not enough to just check isSelectionWithinOp for that
  172. 'review-panel-entry-selected': selected,
  173. // 'focused' is set even when an entry was clicked but not selected (like clicking on a menu option)
  174. // used to set z-index above other entries (since entries are not ordered the same way visually and in the DOM)
  175. 'review-panel-entry-focused': focused,
  176. // 'highlighted' is set if the selection is within op but that doesn't necessarily mean it should be selected
  177. // multiple entries can be highlighted at the same time
  178. 'review-panel-entry-highlighted': highlighted,
  179. // 'textarea-focused' only changes entry styling (border, shadow etc)
  180. // it doesnt change selected entry because that moves the cursor
  181. // and repositions entries which can cause textarea to be scrolled out of view
  182. 'review-panel-entry-textarea-focused': textareaFocused,
  183. 'review-panel-entry-disabled': disabled,
  184. },
  185. className
  186. )}
  187. data-top={top}
  188. data-pos={position}
  189. style={{
  190. position: top === undefined ? 'relative' : 'absolute',
  191. visibility: top === undefined ? 'visible' : 'hidden',
  192. transition: 'top .3s, left .1s, right .1s',
  193. }}
  194. >
  195. {entryIndicator && (
  196. <EntryIndicator
  197. type={entryIndicator}
  198. handleMouseEnter={handleEnter}
  199. handleMouseLeave={handleLeave}
  200. handleMouseDown={openReviewPanel}
  201. />
  202. )}
  203. {children}
  204. </div>
  205. )
  206. }