2024-08-15 06:07:40 -04:00
|
|
|
import { FC, useCallback, useState } from 'react'
|
|
|
|
import { AnyOperation } from '../../../../../types/change'
|
|
|
|
import {
|
|
|
|
useCodeMirrorStateContext,
|
|
|
|
useCodeMirrorViewContext,
|
2024-09-27 06:31:31 -04:00
|
|
|
} from '@/features/source-editor/components/codemirror-context'
|
2024-08-15 06:07:40 -04:00
|
|
|
import { isSelectionWithinOp } from '../utils/is-selection-within-op'
|
|
|
|
import classNames from 'classnames'
|
2024-09-19 06:08:20 -04:00
|
|
|
import { highlightRanges } from '@/features/source-editor/extensions/ranges'
|
2024-09-26 10:13:54 -04:00
|
|
|
import { useEditorManagerContext } from '@/features/ide-react/context/editor-manager-context'
|
2024-08-15 06:07:40 -04:00
|
|
|
|
|
|
|
export const ReviewPanelEntry: FC<{
|
|
|
|
position: number
|
|
|
|
op: AnyOperation
|
2024-09-26 10:13:54 -04:00
|
|
|
docId: string
|
2024-08-15 06:07:40 -04:00
|
|
|
top?: number
|
|
|
|
className?: string
|
2024-09-16 04:40:59 -04:00
|
|
|
selectLineOnFocus?: boolean
|
2024-09-26 10:13:54 -04:00
|
|
|
hoverRanges?: boolean
|
|
|
|
}> = ({
|
|
|
|
children,
|
|
|
|
position,
|
|
|
|
top,
|
|
|
|
op,
|
|
|
|
className,
|
|
|
|
selectLineOnFocus = true,
|
|
|
|
docId,
|
|
|
|
hoverRanges = true,
|
|
|
|
}) => {
|
2024-08-15 06:07:40 -04:00
|
|
|
const state = useCodeMirrorStateContext()
|
|
|
|
const view = useCodeMirrorViewContext()
|
2024-09-26 10:13:54 -04:00
|
|
|
const { openDocId } = useEditorManagerContext()
|
2024-08-15 06:07:40 -04:00
|
|
|
const [focused, setFocused] = useState(false)
|
|
|
|
|
|
|
|
const highlighted = isSelectionWithinOp(op, state.selection.main)
|
|
|
|
|
|
|
|
const focusHandler = useCallback(() => {
|
2024-09-26 10:13:54 -04:00
|
|
|
if (selectLineOnFocus) {
|
|
|
|
openDocId(docId, { gotoOffset: position, keepCurrentView: true })
|
|
|
|
}
|
2024-08-15 06:07:40 -04:00
|
|
|
setFocused(true)
|
2024-09-26 10:13:54 -04:00
|
|
|
}, [selectLineOnFocus, docId, openDocId, position])
|
2024-08-15 06:07:40 -04:00
|
|
|
|
|
|
|
return (
|
|
|
|
<div
|
|
|
|
onFocus={focusHandler}
|
|
|
|
onBlur={() => setFocused(false)}
|
2024-09-26 10:13:54 -04:00
|
|
|
onMouseEnter={() => {
|
|
|
|
if (hoverRanges) {
|
|
|
|
view.dispatch(highlightRanges(op))
|
|
|
|
}
|
|
|
|
}}
|
|
|
|
onMouseLeave={() => {
|
|
|
|
if (hoverRanges) {
|
|
|
|
view.dispatch(highlightRanges())
|
|
|
|
}
|
|
|
|
}}
|
2024-08-15 06:07:40 -04:00
|
|
|
role="button"
|
|
|
|
tabIndex={position + 1}
|
|
|
|
className={classNames(
|
|
|
|
'review-panel-entry',
|
|
|
|
{
|
|
|
|
'review-panel-entry-focused': focused,
|
|
|
|
'review-panel-entry-highlighted': highlighted,
|
|
|
|
},
|
|
|
|
className
|
|
|
|
)}
|
|
|
|
data-top={top}
|
|
|
|
data-pos={position}
|
|
|
|
style={{
|
|
|
|
position: top === undefined ? 'relative' : 'absolute',
|
|
|
|
visibility: top === undefined ? 'visible' : 'hidden',
|
|
|
|
transition: 'top .3s, left .1s, right .1s',
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
{children}
|
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|