Skip to content

Commit

Permalink
scroll to center
Browse files Browse the repository at this point in the history
  • Loading branch information
Kr0nox authored and tsaglam committed Jul 19, 2024
1 parent a8ac0bb commit 666f417
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 18 deletions.
19 changes: 18 additions & 1 deletion report-viewer/src/components/ScrollableComponent.vue
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,26 @@
Offers a unstyled scrollable container
-->
<template>
<div class="overflow-y-auto print:overflow-y-visible">
<div class="overflow-y-auto print:overflow-y-visible" ref="root">
<div class="max-h-0 min-h-full print:max-h-none">
<slot></slot>
</div>
</div>
</template>

<script setup lang="ts">
import { ref, type Ref } from 'vue'

const root: Ref<HTMLElement | null> = ref(null)

function getRoot() {
if (!root.value) {
throw new Error('Root element is not available')
}
return root.value
}

defineExpose({
getRoot
})
</script>
27 changes: 13 additions & 14 deletions report-viewer/src/components/fileDisplaying/CodePanel.vue
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@

<script setup lang="ts">
import type { MatchInSingleFile } from '@/model/MatchInSingleFile'
import { ref, nextTick, type PropType, computed, type Ref } from 'vue'
import { ref, type PropType, computed, type Ref } from 'vue'
import Interactable from '../InteractableComponent.vue'
import type { SubmissionFile } from '@/model/File'
import { highlight } from '@/utils/CodeHighlighter'
Expand Down Expand Up @@ -109,27 +109,26 @@ function matchSelected(match: Match) {
emit('matchSelected', match)
}

/**
* Scrolls to the line number in the file.
* @param lineNumber line number in the file
*/
function scrollTo(lineNumber: number) {
collapsed.value = false
nextTick(function () {
lineRefs.value[lineNumber - 1].scrollIntoView({ block: 'nearest' })
})
}

/**
* Collapses the container.
*/
function collapse() {
collapsed.value = true
}

function expand() {
console.log('expand')
collapsed.value = false
}

function getLineRect(lineNumber: number): DOMRect {
return lineRefs.value[lineNumber - 1].getBoundingClientRect()
}

defineExpose({
scrollTo,
collapse
collapse,
expand,
getLineRect
})

/**
Expand Down
19 changes: 16 additions & 3 deletions report-viewer/src/components/fileDisplaying/FilesContainer.vue
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
>
</div>

<ScrollableComponent class="flex-grow">
<ScrollableComponent class="flex-grow" ref="scrollContainer">
<VueDraggableNext>
<CodePanel
v-for="(file, index) in files"
Expand All @@ -43,7 +43,7 @@ import Container from '../ContainerComponent.vue'
import Button from '../ButtonComponent.vue'
import ScrollableComponent from '../ScrollableComponent.vue'
import { VueDraggableNext } from 'vue-draggable-next'
import { computed, ref, type PropType, type Ref } from 'vue'
import { computed, nextTick, ref, type PropType, type Ref } from 'vue'
import type { MatchInSingleFile } from '@/model/MatchInSingleFile'
import { FontAwesomeIcon } from '@fortawesome/vue-fontawesome'
import { faCompressAlt } from '@fortawesome/free-solid-svg-icons'
Expand Down Expand Up @@ -86,6 +86,7 @@ const props = defineProps({
defineEmits(['matchSelected'])

const codePanels: Ref<(typeof CodePanel)[]> = ref([])
const scrollContainer: Ref<typeof ScrollableComponent | null> = ref(null)

const tokenCount = computed(() => {
return props.files.reduce((acc, file) => (file.tokenCount ?? 0) + acc - 1, 0)
Expand All @@ -99,7 +100,19 @@ const tokenCount = computed(() => {
function scrollTo(file: string, line: number) {
const fileIndex = Array.from(props.files).findIndex((f) => f.fileName === file)
if (fileIndex !== -1) {
codePanels.value[fileIndex].scrollTo(line)
console.log(fileIndex)
codePanels.value[fileIndex].expand()
nextTick(() => {
if (!scrollContainer.value) {
console.log('null')
return
}
const childToScrollTo = codePanels.value[fileIndex].getLineRect(line) as DOMRect
const scrollBox = scrollContainer.value.getRoot() as HTMLElement
scrollBox.scrollTo({
top: childToScrollTo.top + scrollBox.scrollTop - (scrollBox.clientHeight * 2) / 3
})
})
}
}

Expand Down

0 comments on commit 666f417

Please sign in to comment.