Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Preview fixes #257

Merged
merged 7 commits into from
Apr 22, 2022
Merged

Preview fixes #257

merged 7 commits into from
Apr 22, 2022

Conversation

locinus
Copy link

@locinus locinus commented Apr 22, 2022

(recreated from latest develop)
(fix #253)
Hi there !

This is quite an important PR about previews. Please rest assured that I've worked on it and tested it quite thoroughly.
Each fixes is accompanied with corresponding tests, which fails on versions before the PR and are green with the PR.
Feel free to review these "specs" first, to check if they're coherent with your vision.

This PR adresses different small quirks:

  1. clicking the right image arrow always shifts the displayed previews, even in cases where it shouldn't, for instance for nbPreviews>=3 and current=0
    That's what causes the flickering. The previews are then re-adjusted when 'images' are detected as changed by ngOnChanges; this was making the bug subtle and difficult to see/catch.

  2. the number of previews oscillate between n (number of requested previews) and n-1/n+1, for example when n=4 or n=5

  3. when opening the modal and navigating to the last preview by clicking on the right preview arrow, it's impossible to then click on the left preview arrow: isPreventSliding() prevents it

  4. in infinite sliding, the left and right preview arrows should be always visible (like the left and right image arrows), except if nbPreviews < images.length
    This is not a clear specification of your tool, but I've imagined it would be consistent.

I'll annotate the .spec file in the PR, so you can check which test addresses which point.
I'll also annotate the code in the PR, so you can understand the reason I had to refactor.

Simplifies and uniformizes the calculation of previews indexes, in the
different navigation cases (changes of current images, uses of previews
arrows).
Provides fixes for:
- clicking the right image arrow always shifts the displayed previews,
even in cases where it shouldn't, for instance for n>=3 and current=0
(root cause of 'clipping' effects when navigating away from first/last
preview)
- the number of displayed previews sometimes oscillate between n
(number of requested previews in config) and n-1/n+1
while navigating (for example for n=4 or n=5)
- when opening the modal and navigating to the last preview by clicking on the
right preview arrow, it's impossible to then click on the left preview
arrow (same from last to first and clicking on the right preview arrow)
In infinite sliding, the left and right preview arrows should be always visible (except if nbPreviews < nbImages)
@@ -738,7 +738,15 @@ describe('PreviewsComponent', () => {

previews = element.queryAll(By.css('img'));

previews[1].nativeElement.click();
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addresses: 1. clicking the right image arrow always shifts the displayed previews, even in cases where it shouldn't, for instance for nbPreviews>=3 and current=0

@@ -853,6 +861,136 @@ describe('PreviewsComponent', () => {
checkPreview(previews[i], previewImages[i], i === 0, DEFAULT_PREVIEW_SIZE);
}
});

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addresses: 2. the number of previews oscillate between n (number of requested previews) and n-1/n+1, for example when n=4 or n=5

}));
});

it('should allow to navigate previews from first to last and back', () => {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addresses: when opening the modal and navigating to the last preview by clicking on the right preview arrow, it's impossible to then click on the left preview arrow: isPreventSliding() prevents it

});

it('should always display previews navigation arrows, in infinite sliding mode (nbPreviews < nbImages)', () => {
const configService = fixture.debugElement.injector.get(ConfigService);
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addresses: 4. in infinite sliding, the left and right preview arrows should be always visible (like the left and right image arrows), except if nbPreviews < images.length

@@ -170,28 +170,12 @@ export class PreviewsComponent extends AccessibleComponent implements OnInit, On
* In particular, it's called when any data-bound property of a directive changes!!!
*/
ngOnChanges(changes: SimpleChanges): void {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In order for the previews calculation to be more consistent and simple, I simplified this part so that the previews are calculated, with any change, by the same process.

*/
private initPreviews(currentImage: InternalLibImage, images: InternalLibImage[]): void {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here: for simplification and edge-case handling, I propose that the whole logic be processed uniformly in a single method (setIndexesPreviews)

throw new Error('Internal library error - images must be defined');
}
// check if nextImage should be blocked
if (this.isPreventSliding(this.images.length - 1)) {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

preventSliding depends whether next() or previous() is called, so I moved the isPreventSliding logic within these two methods for better contextualization.


this.start++;
this.end = Math.min(this.end + 1, this.images.length);
this.end = this.start + Math.min((this.previewConfig.number as number), this.images.length);
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, start and end are calculated in a co-dependant way, like in setIndexesPreviews().


this.start = Math.max(this.start - 1, 0);
this.end = Math.min(this.end - 1, this.images.length);
this.start = this.end - Math.min((this.previewConfig.number as number), this.images.length);
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, start and end are calculated in a co-dependant way, like in setIndexesPreviews().

throw new Error('Internal library error - images must be defined');
}
// check if prevImage should be blocked
if (this.isPreventSliding(0)) {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

preventSliding depends whether next() or previous() is called, so I moved the isPreventSliding logic within these two methods for better contextualization.

* @param number boundaryIndex is the first or the last index of `images` input array
* @returns boolean if true block sliding, otherwise not
*/
private isPreventSliding(boundaryIndex: number): boolean {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

preventSliding depends whether next() or previous() is called, so I moved the isPreventSliding logic within these two methods for better contextualization.

/**
* Private method to handle navigation changing the previews array and other variables.
*/
private updatePreviews(prev: InternalLibImage, current: InternalLibImage): void {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

all the calculation is now done in initPreviews to avoid divergence of logics

@Ks89
Copy link
Owner

Ks89 commented Apr 22, 2022

Very cool, I reviewed everything and merged.
Thank you very much.

@Ks89 Ks89 merged commit 135c2ab into Ks89:develop Apr 22, 2022
@Ks89
Copy link
Owner

Ks89 commented Apr 22, 2022

released as 9.1.0-beta.2
https://github.com/Ks89/angular-modal-gallery/releases/tag/v.9.1.0-beta.2

I made some tests and it seems to be ok. The only missing thing before the stable release is to update doc website with previews template feature.

@locinus
Copy link
Author

locinus commented Apr 23, 2022

Awesome !
I've got time this week end to work on the documentation project.
Also, I just started to use the @beta version on my project. I'll release it on beta production next week, and check out for normal behavior.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Small clipping on previews
2 participants