Skip to content

Commit

Permalink
viewer: refresh slide on gobals update (#1194)
Browse files Browse the repository at this point in the history
Signed-off-by: Varun Patil <radialapps@gmail.com>
  • Loading branch information
pulsejet committed May 17, 2024
1 parent c9088d3 commit bb1e861
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
15 changes: 10 additions & 5 deletions src/components/viewer/Viewer.vue
Original file line number Diff line number Diff line change
Expand Up @@ -751,8 +751,11 @@ export default defineComponent({
// If the anchor shifts to the left, we need to shift the index
// by the same amount. This happens synchronously, so update first.
if (globals.anchor < this.globalAnchor) {
// Also check if the current position is invalid here
if (globals.anchor != this.globalAnchor) {
goTo = this.photoswipe.currIndex - (this.globalAnchor - globals.anchor);
} else if (this.photoswipe.currIndex >= globals.count || this.photoswipe.currIndex < 0) {
goTo = this.photoswipe.currIndex; // equivalent to above
}
// Update the global anchor and count
Expand All @@ -762,12 +765,14 @@ export default defineComponent({
// Go to the new index if needed
if (goTo === null) {
// no change
} else if (this.photoswipe.currIndex >= this.globalCount) {
this.photoswipe.goTo(this.globalCount - 1);
} else if (goTo < 0) {
this.photoswipe.goTo(0);
} else {
// Change the index to the new one with clamp
goTo = utils.clamp(goTo, 0, globals.count - 1);
this.photoswipe.goTo(goTo);
// Make sure the slide is current, since this call is deferred
// https://github.com/pulsejet/memories/issues/1194
this.photoswipe.refreshSlideContent(goTo);
}
},
0,
Expand Down
5 changes: 5 additions & 0 deletions src/services/utils/algo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,11 @@ export function isNumber<T>(num: T): boolean {
return !isNaN(cast) && isFinite(cast);
}

/** Clamp number between two numbers */
export function clamp(num: number, min: number, max: number): number {
return Math.min(Math.max(num, min), max);
}

/** Check if a value is truthy */
export function truthy<T>(value: T): value is NonNullable<T> {
return !!value;
Expand Down

0 comments on commit bb1e861

Please sign in to comment.