From e58557a13e0ce6a8f396924c4c613c4fbb7180ae Mon Sep 17 00:00:00 2001 From: malangfox Date: Fri, 16 Feb 2024 07:15:17 +0900 Subject: [PATCH] fix: add findActiveAnchor fallback for bound flicking --- src/camera/Camera.ts | 6 ++++-- test/unit/control/SnapControl.spec.ts | 25 ++++++++++++++++++++++++- 2 files changed, 28 insertions(+), 3 deletions(-) diff --git a/src/camera/Camera.ts b/src/camera/Camera.ts index c84dbf242..5065d435b 100644 --- a/src/camera/Camera.ts +++ b/src/camera/Camera.ts @@ -404,9 +404,11 @@ class Camera { */ public findActiveAnchor(): AnchorPoint | null { const flicking = getFlickingAttached(this._flicking); - const activeIndex = flicking.control.activeIndex; + const activePanel = flicking.control.activePanel; + + if (!activePanel) return null; - return find(this._anchors, anchor => anchor.panel.index === activeIndex); + return find(this._anchors, anchor => anchor.panel.index === activePanel.index) ?? this.findNearestAnchor(activePanel.position); } /** diff --git a/test/unit/control/SnapControl.spec.ts b/test/unit/control/SnapControl.spec.ts index 9e338ff86..1ab6ab75e 100644 --- a/test/unit/control/SnapControl.spec.ts +++ b/test/unit/control/SnapControl.spec.ts @@ -283,7 +283,7 @@ describe("SnapControl", () => { expect(position).to.equal(flicking.panels[0].position); }); - it("Should move to the nearest panel from the camera, when animation is interrupted by user input", async () => { + it("should move to the nearest panel from the camera, when animation is interrupted by user input", async () => { const flicking = await createFlicking(El.DEFAULT_HORIZONTAL, { moveType: MOVE_TYPE.SNAP, align: "prev", @@ -305,6 +305,29 @@ describe("SnapControl", () => { expect(moveSpy.calledTwice).to.be.true; expect(control.activePanel.index).to.equal(camera.findNearestAnchor(position).panel.index); }); + + it("should use the closest anchor even if there is no anchor for the active panel.", async () => { + const flicking = await createFlicking( + El.viewport().add( + El.camera() + .add(El.panel(`33%`)) + .add(El.panel(`33%`)) + .add(El.panel(`33%`)) + .add(El.panel(`33%`)) + ), { + moveType: MOVE_TYPE.SNAP, + bound: true // when bound is true, there is no anchor for first panel + } + ); + + // Set active panel to first panel + void flicking.moveTo(0, 0); + await simulate(flicking.element, { deltaX: -1000, duration: 100 }); + + const status = flicking.getStatus(); + + expect(status.index).equals(2); + }); }); }); });