Skip to content

Commit

Permalink
Keep video selected if it moves position
Browse files Browse the repository at this point in the history
  • Loading branch information
probablykasper committed Jan 21, 2024
1 parent c85d377 commit 077dc76
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 5 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
# Changelog

## Next
- Fix deselection not happening when videos update
- Fix selection staying in the same position when videos update
- Keep video selected if it moves position

## 1.6.2 - 2024 Jan 15
- Fix channel page filtering
Expand Down
24 changes: 20 additions & 4 deletions src/routes/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,32 @@
loading++
const selectedId = videos[selectedIndex]?.id
const oldselectedIndex = selectedIndex
const newVideos = await commands.getVideos(options, null)
allLoaded = newVideos.length < $viewOptions.limit
videos = newVideos
// Remove selection if the video changes position
if (!selectedId || selectedId !== videos[selectedIndex]?.id) {
// Update the selection index if the video moves
const newSelectedIndex = videos.findIndex((v) => v.id === selectedId)
if (newSelectedIndex >= 0) {
selectedIndex = newSelectedIndex
selectionVisible = true
} else {
// Or clear selection if the video disappeared from view
selectedIndex = 0
selectionVisible = false
}
const selectionMoved = selectionVisible && selectedIndex !== oldselectedIndex
await tick()
if (selectionMoved) {
allowScrollToBox = false
await tick()
allowScrollToBox = true
scrollToBox(selectedIndex)
} else {
await tick()
}
await autoloadHandler()
loading--
}
Expand Down Expand Up @@ -263,8 +278,9 @@
let boxes: HTMLDivElement[] = []
$: scrollToBox(selectedIndex)
let allowScrollToBox = true
function scrollToBox(index: number) {
if (scrollDiv && boxes[index]) {
if (scrollDiv && boxes[index] && allowScrollToBox) {
const el = boxes[index].getBoundingClientRect()
const parent = scrollDiv.getBoundingClientRect()
const topOffset = el.top - parent.top
Expand Down

0 comments on commit 077dc76

Please sign in to comment.