Skip to content

Commit

Permalink
chore: handle the case that refContents is undefined
Browse files Browse the repository at this point in the history
  • Loading branch information
josdejong committed Sep 28, 2023
1 parent 0a87b9b commit f00acd6
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 42 deletions.
43 changes: 23 additions & 20 deletions src/lib/components/modes/tablemode/TableMode.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@
})
let refJsonEditor: HTMLDivElement
let refContents: HTMLDivElement
let refContents: HTMLDivElement | undefined
let refHiddenInput: HTMLInputElement
createFocusTracker({
Expand Down Expand Up @@ -241,15 +241,8 @@
defaultItemHeight
)
// $: debug('visibleSection', visibleSection, { viewPortHeight }) // TODO: cleanup
$: refreshScrollTop(json)
// TODO: cleanup
// $: {
// debug('scrollTop', scrollTop, refContents?.scrollTop, refContents?.scrollHeight)
// }
// eslint-disable-next-line @typescript-eslint/no-unused-vars
function refreshScrollTop(_json: JSONValue | undefined) {
// When the contents go from lots of items and scrollable contents to only a few items and
Expand Down Expand Up @@ -804,12 +797,16 @@
debug('scrollTo', { path, top, scrollTop, elem })
if (!refContents) {
return Promise.resolve()
}
const viewPortRect = refContents.getBoundingClientRect()
if (elem && !scrollToWhenVisible) {
const elemRect = elem.getBoundingClientRect()
if (elemRect.bottom > viewPortRect.top && elemRect.top < viewPortRect.bottom) {
// element is fully or partially visible, don't scroll to it
return
return Promise.resolve()
}
}
Expand Down Expand Up @@ -862,6 +859,10 @@
}
function scrollToVertical(path: JSONPath) {
if (!refContents) {
return
}
const { rowIndex } = toTableCellPosition(path, columns)
const top = calculateAbsolutePosition(path, columns, itemHeightsCache, defaultItemHeight)
const bottom = top + (itemHeightsCache[rowIndex] || defaultItemHeight)
Expand All @@ -884,19 +885,21 @@
function scrollToHorizontal(path: JSONPath) {
const elem = findElement(path)
if (elem) {
const viewPortRect = refContents.getBoundingClientRect()
const elemRect = elem.getBoundingClientRect() // TODO: scroll to column instead of item (is always rendered)
if (!elem || !refContents) {
return
}
if (elemRect.right > viewPortRect.right) {
const diff = elemRect.right - viewPortRect.right
refContents.scrollLeft += diff
}
const viewPortRect = refContents.getBoundingClientRect()
const elemRect = elem.getBoundingClientRect() // TODO: scroll to column instead of item (is always rendered)
if (elemRect.left < viewPortRect.left) {
const diff = viewPortRect.left - elemRect.left
refContents.scrollLeft -= diff
}
if (elemRect.right > viewPortRect.right) {
const diff = elemRect.right - viewPortRect.right
refContents.scrollLeft += diff
}
if (elemRect.left < viewPortRect.left) {
const diff = viewPortRect.left - elemRect.left
refContents.scrollLeft -= diff
}
}
Expand Down
45 changes: 23 additions & 22 deletions src/lib/components/modes/treemode/TreeMode.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@
const { openAbsolutePopup, closeAbsolutePopup } =
getContext<AbsolutePopupContext>('absolute-popup')
let refContents: HTMLDivElement
let refContents: HTMLDivElement | undefined
let refHiddenInput: HTMLInputElement
let refJsonEditor: HTMLDivElement
let hasFocus = false
Expand Down Expand Up @@ -1337,31 +1337,32 @@
await tick() // await rerender
const elem = findElement(path)
if (elem) {
debug('scrollTo', { path, elem, refContents })
const viewPortRect = refContents.getBoundingClientRect()
const elemRect = elem.getBoundingClientRect()
if (!scrollToWhenVisible) {
if (elemRect.bottom > viewPortRect.top && elemRect.top < viewPortRect.bottom) {
// element is fully or partially visible, don't scroll to it
return Promise.resolve()
}
}
const offset = -(viewPortRect.height / 4)
debug('scrollTo', { path, elem, refContents })
return new Promise<void>((resolve) => {
jump(elem, {
container: refContents,
offset,
duration: SCROLL_DURATION,
callback: () => resolve()
})
})
} else {
if (!elem || !refContents) {
return Promise.resolve()
}
const viewPortRect = refContents.getBoundingClientRect()
const elemRect = elem.getBoundingClientRect()
if (!scrollToWhenVisible) {
if (elemRect.bottom > viewPortRect.top && elemRect.top < viewPortRect.bottom) {
// element is fully or partially visible, don't scroll to it
return Promise.resolve()
}
}
const offset = -(viewPortRect.height / 4)
return new Promise<void>((resolve) => {
jump(elem, {
container: refContents,
offset,
duration: SCROLL_DURATION,
callback: () => resolve()
})
})
}
/**
Expand Down

0 comments on commit f00acd6

Please sign in to comment.