Skip to content

Commit

Permalink
Merge pull request #13 from briefercloud/fix-scroll
Browse files Browse the repository at this point in the history
 fix: Unnecessary scrolling after code block insert
  • Loading branch information
vieiralucas authored Sep 11, 2024
2 parents 8b7d0a9 + efd991c commit 1baba90
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 9 deletions.
12 changes: 9 additions & 3 deletions apps/web/src/components/v2Editor/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -890,7 +890,6 @@ file`
}, [blocks, layout, tabRefs, environmentStartedAt])

const runBelowBlock = useCallback(() => {
debugger
const currentBlockGroupIndex = layout.value.toArray().findIndex((bg) => {
return bg.getAttribute('id') === props.id
})
Expand Down Expand Up @@ -1247,9 +1246,16 @@ const V2Editor = (props: V2EditorProps) => {
behavior: 'smooth',
})
} else {
el.scrollIntoView({
// scroll el so that it's center is at the center of the scroll view
const top =
elRect.top -
scrollRect.top -
scrollRect.height / 2 +
elRect.height / 2

scrollViewRef.current.scrollBy({
top,
behavior: 'smooth',
block: 'center',
})
}

Expand Down
23 changes: 17 additions & 6 deletions apps/web/src/hooks/useV2CodeEditor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,19 +113,30 @@ function useCodeEditor(
!interactionState.scrollIntoView
) {
const scrollView = document.getElementById('editor-scrollview')
const editorRect = editor.getDomNode()?.getBoundingClientRect()
editor.focus()
setIsEditorFocused(true)

if (!scrollView) {
if (!scrollView || !editorRect) {
return
}

const currentLine = editor.getPosition()?.lineNumber ?? 0
const top = editor.getTopForLineNumber(currentLine)
scrollView.scrollBy({
top: top - scrollView.getBoundingClientRect().top - 80,
behavior: 'smooth',
})
const scrollViewRect = scrollView.getBoundingClientRect()
const top = editorRect.top + editor.getTopForLineNumber(currentLine)

// scroll to make sure we can see cursor in screen
if (top < scrollViewRect.top) {
scrollView.scrollBy({
top: top - scrollViewRect.top - 80,
behavior: 'smooth',
})
} else if (top + 20 > scrollViewRect.bottom) {
scrollView.scrollBy({
top: top - scrollViewRect.bottom + 80,
behavior: 'smooth',
})
}
}
}, [interactionState, blockId, editor])

Expand Down

0 comments on commit 1baba90

Please sign in to comment.