Просмотр исходного кода

Bug Fix: Rarely the loop carousel rewound itself.

Naotoshi Fujita 3 лет назад
Родитель
Сommit
8aff0998b4

+ 9 - 10
dist/js/splide.cjs.js

@@ -1,6 +1,6 @@
 /*!
 /*!
  * Splide.js
  * Splide.js
- * Version  : 4.0.4
+ * Version  : 4.0.5
  * License  : MIT
  * License  : MIT
  * Copyright: 2022 Naotoshi Fujita
  * Copyright: 2022 Naotoshi Fujita
  */
  */
@@ -1428,11 +1428,15 @@ function Move(Splide2, Components2, options) {
 
 
   function move(dest, index, prev, callback) {
   function move(dest, index, prev, callback) {
     var position = getPosition();
     var position = getPosition();
-    var crossing = sign(dest - prev) * orient(toPosition(dest) - position) < 0;
+    var shifted = shift(position, dest > prev);
+    var oriented = orient(shifted);
+    var destination = toPosition(dest);
+    var shouldShift = dest !== index || abs(shifted - destination) < abs(position - destination);
+    var canShift = dest > prev ? oriented >= 0 : oriented <= list[resolve("scrollWidth")] - rect(track)[resolve("width")];
 
 
-    if ((dest !== index || crossing) && canShift(dest > prev)) {
+    if (shouldShift && canShift) {
       cancel();
       cancel();
-      translate(shift(position, dest > prev), true);
+      translate(shifted, true);
     }
     }
 
 
     set(MOVING);
     set(MOVING);
@@ -1529,11 +1533,6 @@ function Move(Splide2, Components2, options) {
     return toPosition(max ? Components2.Controller.getEnd() : 0, !!options.trimSpace);
     return toPosition(max ? Components2.Controller.getEnd() : 0, !!options.trimSpace);
   }
   }
 
 
-  function canShift(backwards) {
-    var shifted = orient(shift(getPosition(), backwards));
-    return backwards ? shifted >= 0 : shifted <= list[resolve("scrollWidth")] - rect(track)[resolve("width")];
-  }
-
   function exceededLimit(max, position) {
   function exceededLimit(max, position) {
     position = isUndefined(position) ? getPosition() : position;
     position = isUndefined(position) ? getPosition() : position;
     var exceededMin = max !== true && orient(position) < orient(getLimit(false));
     var exceededMin = max !== true && orient(position) < orient(getLimit(false));
@@ -1609,7 +1608,7 @@ function Controller(Splide2, Components2, options) {
 
 
   function scroll(destination, duration, snap, callback) {
   function scroll(destination, duration, snap, callback) {
     Components2.Scroll.scroll(destination, duration, snap, function () {
     Components2.Scroll.scroll(destination, duration, snap, function () {
-      setIndex(loop(Move.toIndex(Move.getPosition())));
+      setIndex(loop(Move.toIndex(getPosition())));
       callback && callback();
       callback && callback();
     });
     });
   }
   }

+ 9 - 10
dist/js/splide.esm.js

@@ -4,7 +4,7 @@ function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _d
 
 
 /*!
 /*!
  * Splide.js
  * Splide.js
- * Version  : 4.0.4
+ * Version  : 4.0.5
  * License  : MIT
  * License  : MIT
  * Copyright: 2022 Naotoshi Fujita
  * Copyright: 2022 Naotoshi Fujita
  */
  */
@@ -1423,11 +1423,15 @@ function Move(Splide2, Components2, options) {
 
 
   function move(dest, index, prev, callback) {
   function move(dest, index, prev, callback) {
     var position = getPosition();
     var position = getPosition();
-    var crossing = sign(dest - prev) * orient(toPosition(dest) - position) < 0;
+    var shifted = shift(position, dest > prev);
+    var oriented = orient(shifted);
+    var destination = toPosition(dest);
+    var shouldShift = dest !== index || abs(shifted - destination) < abs(position - destination);
+    var canShift = dest > prev ? oriented >= 0 : oriented <= list[resolve("scrollWidth")] - rect(track)[resolve("width")];
 
 
-    if ((dest !== index || crossing) && canShift(dest > prev)) {
+    if (shouldShift && canShift) {
       cancel();
       cancel();
-      translate(shift(position, dest > prev), true);
+      translate(shifted, true);
     }
     }
 
 
     set(MOVING);
     set(MOVING);
@@ -1524,11 +1528,6 @@ function Move(Splide2, Components2, options) {
     return toPosition(max ? Components2.Controller.getEnd() : 0, !!options.trimSpace);
     return toPosition(max ? Components2.Controller.getEnd() : 0, !!options.trimSpace);
   }
   }
 
 
-  function canShift(backwards) {
-    var shifted = orient(shift(getPosition(), backwards));
-    return backwards ? shifted >= 0 : shifted <= list[resolve("scrollWidth")] - rect(track)[resolve("width")];
-  }
-
   function exceededLimit(max, position) {
   function exceededLimit(max, position) {
     position = isUndefined(position) ? getPosition() : position;
     position = isUndefined(position) ? getPosition() : position;
     var exceededMin = max !== true && orient(position) < orient(getLimit(false));
     var exceededMin = max !== true && orient(position) < orient(getLimit(false));
@@ -1604,7 +1603,7 @@ function Controller(Splide2, Components2, options) {
 
 
   function scroll(destination, duration, snap, callback) {
   function scroll(destination, duration, snap, callback) {
     Components2.Scroll.scroll(destination, duration, snap, function () {
     Components2.Scroll.scroll(destination, duration, snap, function () {
-      setIndex(loop(Move.toIndex(Move.getPosition())));
+      setIndex(loop(Move.toIndex(getPosition())));
       callback && callback();
       callback && callback();
     });
     });
   }
   }

+ 9 - 10
dist/js/splide.js

@@ -4,7 +4,7 @@ function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _d
 
 
 /*!
 /*!
  * Splide.js
  * Splide.js
- * Version  : 4.0.4
+ * Version  : 4.0.5
  * License  : MIT
  * License  : MIT
  * Copyright: 2022 Naotoshi Fujita
  * Copyright: 2022 Naotoshi Fujita
  */
  */
@@ -1421,11 +1421,15 @@ function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _d
 
 
     function move(dest, index, prev, callback) {
     function move(dest, index, prev, callback) {
       var position = getPosition();
       var position = getPosition();
-      var crossing = sign(dest - prev) * orient(toPosition(dest) - position) < 0;
+      var shifted = shift(position, dest > prev);
+      var oriented = orient(shifted);
+      var destination = toPosition(dest);
+      var shouldShift = dest !== index || abs(shifted - destination) < abs(position - destination);
+      var canShift = dest > prev ? oriented >= 0 : oriented <= list[resolve("scrollWidth")] - rect(track)[resolve("width")];
 
 
-      if ((dest !== index || crossing) && canShift(dest > prev)) {
+      if (shouldShift && canShift) {
         cancel();
         cancel();
-        translate(shift(position, dest > prev), true);
+        translate(shifted, true);
       }
       }
 
 
       set(MOVING);
       set(MOVING);
@@ -1522,11 +1526,6 @@ function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _d
       return toPosition(max ? Components2.Controller.getEnd() : 0, !!options.trimSpace);
       return toPosition(max ? Components2.Controller.getEnd() : 0, !!options.trimSpace);
     }
     }
 
 
-    function canShift(backwards) {
-      var shifted = orient(shift(getPosition(), backwards));
-      return backwards ? shifted >= 0 : shifted <= list[resolve("scrollWidth")] - rect(track)[resolve("width")];
-    }
-
     function exceededLimit(max, position) {
     function exceededLimit(max, position) {
       position = isUndefined(position) ? getPosition() : position;
       position = isUndefined(position) ? getPosition() : position;
       var exceededMin = max !== true && orient(position) < orient(getLimit(false));
       var exceededMin = max !== true && orient(position) < orient(getLimit(false));
@@ -1602,7 +1601,7 @@ function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _d
 
 
     function scroll(destination, duration, snap, callback) {
     function scroll(destination, duration, snap, callback) {
       Components2.Scroll.scroll(destination, duration, snap, function () {
       Components2.Scroll.scroll(destination, duration, snap, function () {
-        setIndex(loop(Move.toIndex(Move.getPosition())));
+        setIndex(loop(Move.toIndex(getPosition())));
         callback && callback();
         callback && callback();
       });
       });
     }
     }

Разница между файлами не показана из-за своего большого размера
+ 0 - 0
dist/js/splide.min.js


BIN
dist/js/splide.min.js.gz


Разница между файлами не показана из-за своего большого размера
+ 0 - 0
dist/js/splide.min.js.map


+ 2 - 1
dist/types/index.d.ts

@@ -111,7 +111,6 @@ interface ControllerComponent extends BaseComponent {
     scroll(destination: number, duration?: number, snap?: boolean, callback?: AnyFunction): void;
     scroll(destination: number, duration?: number, snap?: boolean, callback?: AnyFunction): void;
     getNext(destination?: boolean): number;
     getNext(destination?: boolean): number;
     getPrev(destination?: boolean): number;
     getPrev(destination?: boolean): number;
-    getAdjacent(prev: boolean, destination?: boolean): number;
     getEnd(): number;
     getEnd(): number;
     setIndex(index: number): void;
     setIndex(index: number): void;
     getIndex(prev?: boolean): number;
     getIndex(prev?: boolean): number;
@@ -120,6 +119,8 @@ interface ControllerComponent extends BaseComponent {
     toDest(position: number): number;
     toDest(position: number): number;
     hasFocus(): boolean;
     hasFocus(): boolean;
     isBusy(): boolean;
     isBusy(): boolean;
+    /** @internal */
+    getAdjacent(prev: boolean, destination?: boolean): number;
 }
 }
 
 
 /**
 /**

+ 2 - 2
package-lock.json

@@ -1,12 +1,12 @@
 {
 {
   "name": "@splidejs/splide",
   "name": "@splidejs/splide",
-  "version": "4.0.4",
+  "version": "4.0.5",
   "lockfileVersion": 2,
   "lockfileVersion": 2,
   "requires": true,
   "requires": true,
   "packages": {
   "packages": {
     "": {
     "": {
       "name": "@splidejs/splide",
       "name": "@splidejs/splide",
-      "version": "4.0.4",
+      "version": "4.0.5",
       "license": "MIT",
       "license": "MIT",
       "devDependencies": {
       "devDependencies": {
         "@babel/core": "^7.16.10",
         "@babel/core": "^7.16.10",

+ 1 - 1
package.json

@@ -1,6 +1,6 @@
 {
 {
   "name": "@splidejs/splide",
   "name": "@splidejs/splide",
-  "version": "4.0.4",
+  "version": "4.0.5",
   "description": "Splide is a lightweight, flexible and accessible slider/carousel. No dependencies, no Lighthouse errors.",
   "description": "Splide is a lightweight, flexible and accessible slider/carousel. No dependencies, no Lighthouse errors.",
   "author": "Naotoshi Fujita",
   "author": "Naotoshi Fujita",
   "license": "MIT",
   "license": "MIT",

+ 5 - 3
src/js/components/Controller/Controller.ts

@@ -17,7 +17,6 @@ export interface ControllerComponent extends BaseComponent {
   scroll( destination: number, duration?: number, snap?: boolean, callback?: AnyFunction ): void;
   scroll( destination: number, duration?: number, snap?: boolean, callback?: AnyFunction ): void;
   getNext( destination?: boolean ): number;
   getNext( destination?: boolean ): number;
   getPrev( destination?: boolean ): number;
   getPrev( destination?: boolean ): number;
-  getAdjacent( prev: boolean, destination?: boolean ): number;
   getEnd(): number;
   getEnd(): number;
   setIndex( index: number ): void;
   setIndex( index: number ): void;
   getIndex( prev?: boolean ): number;
   getIndex( prev?: boolean ): number;
@@ -26,6 +25,9 @@ export interface ControllerComponent extends BaseComponent {
   toDest( position: number ): number;
   toDest( position: number ): number;
   hasFocus(): boolean;
   hasFocus(): boolean;
   isBusy(): boolean;
   isBusy(): boolean;
+
+  /** @internal */
+  getAdjacent( prev: boolean, destination?: boolean ): number;
 }
 }
 
 
 /**
 /**
@@ -84,7 +86,7 @@ export function Controller( Splide: Splide, Components: Components, options: Opt
 
 
   /**
   /**
    * Initializes some parameters.
    * Initializes some parameters.
-   * Needs to check the slides length since the current index may be out of the range after refresh.
+   * Needs to check the number of slides since the current index may be out of the range after refresh.
    * The process order must be Elements -> Controller -> Move.
    * The process order must be Elements -> Controller -> Move.
    */
    */
   function init(): void {
   function init(): void {
@@ -131,7 +133,7 @@ export function Controller( Splide: Splide, Components: Components, options: Opt
    */
    */
   function scroll( destination: number, duration?: number, snap?: boolean, callback?: AnyFunction ): void {
   function scroll( destination: number, duration?: number, snap?: boolean, callback?: AnyFunction ): void {
     Components.Scroll.scroll( destination, duration, snap, () => {
     Components.Scroll.scroll( destination, duration, snap, () => {
-      setIndex( loop( Move.toIndex( Move.getPosition() ) ) );
+      setIndex( loop( Move.toIndex( getPosition() ) ) );
       callback && callback();
       callback && callback();
     } );
     } );
   }
   }

+ 12 - 19
src/js/components/Move/Move.ts

@@ -12,7 +12,7 @@ import { FADE, LOOP, SLIDE } from '../../constants/types';
 import { EventInterface } from '../../constructors';
 import { EventInterface } from '../../constructors';
 import { Splide } from '../../core/Splide/Splide';
 import { Splide } from '../../core/Splide/Splide';
 import { AnyFunction, BaseComponent, Components, Options, TransitionComponent } from '../../types';
 import { AnyFunction, BaseComponent, Components, Options, TransitionComponent } from '../../types';
-import { abs, ceil, clamp, isUndefined, rect, sign, style } from '../../utils';
+import { abs, ceil, clamp, isUndefined, rect, style } from '../../utils';
 
 
 
 
 /**
 /**
@@ -90,12 +90,19 @@ export function Move( Splide: Splide, Components: Components, options: Options )
    * @param callback - Optional. A callback function invoked after transition ends.
    * @param callback - Optional. A callback function invoked after transition ends.
    */
    */
   function move( dest: number, index: number, prev: number, callback?: AnyFunction ): void {
   function move( dest: number, index: number, prev: number, callback?: AnyFunction ): void {
-    const position = getPosition();
-    const crossing = sign( dest - prev ) * orient( toPosition( dest ) - position ) < 0;
+    const position    = getPosition();
+    const shifted     = shift( position, dest > prev );
+    const oriented    = orient( shifted );
+    const destination = toPosition( dest );
+    const shouldShift = dest !== index || abs( shifted - destination ) < abs( position - destination );
 
 
-    if ( ( dest !== index || crossing ) && canShift( dest > prev ) ) {
+    const canShift = dest > prev
+      ? oriented >= 0
+      : oriented <= list[ resolve( 'scrollWidth' ) ] - rect( track )[ resolve( 'width' ) ];
+
+    if ( shouldShift && canShift ) {
       cancel();
       cancel();
-      translate( shift( position, dest > prev ), true );
+      translate( shifted, true );
     }
     }
 
 
     set( MOVING );
     set( MOVING );
@@ -260,20 +267,6 @@ export function Move( Splide: Splide, Components: Components, options: Options )
     return toPosition( max ? Components.Controller.getEnd() : 0, !! options.trimSpace );
     return toPosition( max ? Components.Controller.getEnd() : 0, !! options.trimSpace );
   }
   }
 
 
-  /**
-   * Checks if there is enough width to shift the slider.
-   *
-   * @param backwards - `true` for checking backwards, or `false` for doing forwards.
-   *
-   * @return `true` if the slider can be shifted for the specified direction, or otherwise `false`.
-   */
-  function canShift( backwards: boolean ): boolean {
-    const shifted = orient( shift( getPosition(), backwards ) );
-    return backwards
-      ? shifted >= 0
-      : shifted <= list[ resolve( 'scrollWidth' ) ] - rect( track )[ resolve( 'width' ) ];
-  }
-
   /**
   /**
    * Checks if the provided position exceeds the minimum or maximum limit or not.
    * Checks if the provided position exceeds the minimum or maximum limit or not.
    *
    *

Некоторые файлы не были показаны из-за большого количества измененных файлов