Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(ui): scroll active code line into view #2232

Merged
merged 1 commit into from
May 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions ee/tabby-ui/app/files/components/code-editor-view.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ const CodeEditorView: React.FC<CodeEditorViewProps> = ({ value, language }) => {
const tags: TCodeTag[] = React.useMemo(() => {
return []
}, [])
const initialized = React.useRef(false)
const { copyToClipboard } = useCopyToClipboard({})
const line = searchParams.get('line')?.toString()
const [editorView, setEditorView] = React.useState<EditorView | null>(null)
Expand Down Expand Up @@ -132,11 +131,16 @@ const CodeEditorView: React.FC<CodeEditorViewProps> = ({ value, language }) => {
React.useEffect(() => {
if (line && editorView && value) {
try {
initialized.current = true
const lineNumber = parseInt(line)
const lineObject = editorView?.state?.doc?.line(lineNumber)
if (lineObject) {
setSelectedLines(editorView, lineObject.from)
editorView.dispatch({
effects: EditorView.scrollIntoView(lineObject.from, {
y: 'start',
yMargin: 120
})
})
}
} catch (e) {}
}
Expand Down
18 changes: 17 additions & 1 deletion ee/tabby-ui/app/files/components/file-tree.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import {
resolveFileNameFromPath,
resolveRepositoryInfoFromPath
} from './utils'
import { useInView } from 'react-intersection-observer'

type TFileTreeNode = {
name: string
Expand Down Expand Up @@ -136,7 +137,22 @@ const GridArea: React.FC<{ level: number }> = ({ level }) => {
}

const ActiveViewBar = () => {
return <div className="absolute -left-2 h-8 w-1 rounded-md bg-primary" />
const { ref, entry, inView } = useInView({
trackVisibility: true,
delay: 500
})

React.useEffect(() => {
if (!!entry?.target && !inView) {
entry?.target?.scrollIntoView({
block: 'center'
})
}
}, [entry?.target])

return (
<div ref={ref} className="absolute -left-2 h-8 w-1 rounded-md bg-primary" />
)
}

/**
Expand Down
20 changes: 0 additions & 20 deletions ee/tabby-ui/app/files/components/source-code-browser.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -434,26 +434,6 @@ const SourceCodeBrowserRenderer: React.FC<SourceCodeBrowserProps> = ({
}
}, [chatSideBarVisible])

React.useEffect(() => {
const onMessage = (event: MessageEvent) => {
if (event.origin !== window.origin || !event.data) return

const { data } = event
if (data.action === 'navigateToContext') {
updateSearchParams({
set: {
path: data.path,
line: data.line
}
})
}
}

window.addEventListener('message', onMessage)

return () => window.removeEventListener('message', onMessage)
}, [])

return (
<ResizablePanelGroup
direction="horizontal"
Expand Down