Skip to content

Commit

Permalink
fix(ui): select line menu shouldn't affect scrolling (#2300)
Browse files Browse the repository at this point in the history
  • Loading branch information
liangfung authored May 30, 2024
1 parent 4f52ae9 commit 95feb55
Showing 1 changed file with 28 additions and 5 deletions.
33 changes: 28 additions & 5 deletions ee/tabby-ui/app/files/components/code-editor-view.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -132,13 +132,18 @@ const CodeEditorView: React.FC<CodeEditorViewProps> = ({ value, language }) => {
if (line && editorView && value) {
try {
const lineNumber = parseInt(line)
const lineObject = editorView?.state?.doc?.line(lineNumber)
if (lineObject) {
setSelectedLines(editorView, lineObject.from)
const lineInfo = editorView?.state?.doc?.line(lineNumber)

if (lineInfo) {
const pos = lineInfo.from
setSelectedLines(editorView, pos)
if (isPositionInView(editorView, pos, 90)) {
return
}
editorView.dispatch({
effects: EditorView.scrollIntoView(lineObject.from, {
effects: EditorView.scrollIntoView(pos, {
y: 'start',
yMargin: 120
yMargin: 200
})
})
}
Expand All @@ -158,4 +163,22 @@ const CodeEditorView: React.FC<CodeEditorViewProps> = ({ value, language }) => {
)
}

function isPositionInView(
view: EditorView,
pos: number,
offsetTop: number = 0
) {
const node = view.domAtPos(pos).node
const lineElement =
node.nodeType === 3 ? node.parentElement : (node as HTMLElement)
if (lineElement) {
const rect = lineElement.getBoundingClientRect()
const viewportHeight =
window.innerHeight || document.documentElement.clientHeight
return rect.top >= offsetTop && rect.bottom <= viewportHeight
}

return false
}

export default CodeEditorView

0 comments on commit 95feb55

Please sign in to comment.