瀏覽代碼

Compute the valid dest index in the `computeDestIndex` method.

NaotoshiFujita 3 年之前
父節點
當前提交
3995064102
共有 6 個文件被更改,包括 49 次插入33 次删除
  1. 0 0
      dist/js/splide-renderer.min.js
  2. 18 11
      dist/js/splide.js
  3. 0 0
      dist/js/splide.min.js
  4. 二進制
      dist/js/splide.min.js.gz
  5. 0 0
      dist/js/splide.min.js.map
  6. 31 22
      src/js/components/Controller/Controller.ts

文件差異過大導致無法顯示
+ 0 - 0
dist/js/splide-renderer.min.js


+ 18 - 11
dist/js/splide.js

@@ -1579,7 +1579,7 @@ function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _d
     function go(control, allowSameIndex, callback) {
       if (!isBusy()) {
         var dest = parse(control);
-        var index = validate(dest);
+        var index = loop(dest);
 
         if (index > -1 && (allowSameIndex || index !== currIndex)) {
           setIndex(index);
@@ -1604,7 +1604,7 @@ function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _d
             number = _ref[2];
 
         if (indicator === "+" || indicator === "-") {
-          index = computeDestIndex(currIndex + +("" + indicator + (+number || 1)), currIndex, true);
+          index = computeDestIndex(currIndex + +("" + indicator + (+number || 1)), currIndex);
         } else if (indicator === ">") {
           index = number ? toIndex(+number) : getNext(true);
         } else if (indicator === "<") {
@@ -1619,7 +1619,7 @@ function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _d
 
     function getAdjacent(prev, destination) {
       var number = perMove || (hasFocus() ? 1 : perPage);
-      var dest = computeDestIndex(currIndex + number * (prev ? -1 : 1), currIndex);
+      var dest = computeDestIndex(currIndex + number * (prev ? -1 : 1), currIndex, !(perMove || hasFocus()));
 
       if (dest === -1 && isSlide) {
         if (!approximatelyEqual(getPosition(), getLimit(!prev), 1)) {
@@ -1630,16 +1630,23 @@ function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _d
       return destination ? dest : loop(dest);
     }
 
-    function computeDestIndex(dest, from, incremental) {
+    function computeDestIndex(dest, from, snapPage) {
       if (isEnough()) {
         var end = getEnd();
+        var movable = computeMovableDestIndex(dest);
+
+        if (movable !== dest) {
+          from = dest;
+          dest = movable;
+          snapPage = false;
+        }
 
         if (dest < 0 || dest > end) {
           if (between(0, dest, from, true) || between(end, from, dest, true)) {
             dest = toIndex(toPage(dest));
           } else {
             if (isLoop) {
-              dest = perMove || hasFocus() ? dest : dest < 0 ? -(slideCount % perPage || perPage) : slideCount;
+              dest = snapPage ? dest < 0 ? -(slideCount % perPage || perPage) : slideCount : dest;
             } else if (options.rewind) {
               dest = dest < 0 ? end : 0;
             } else {
@@ -1647,8 +1654,8 @@ function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _d
             }
           }
         } else {
-          if (!incremental && dest !== from) {
-            dest = perMove ? dest : toIndex(toPage(from) + (dest < from ? -1 : 1));
+          if (snapPage && dest !== from) {
+            dest = toIndex(toPage(from) + (dest < from ? -1 : 1));
           }
         }
       } else {
@@ -1658,16 +1665,16 @@ function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _d
       return dest;
     }
 
-    function validate(dest) {
-      if (options.trimSpace === "move" && dest !== currIndex) {
+    function computeMovableDestIndex(dest) {
+      if (isSlide && options.trimSpace === "move" && dest !== currIndex) {
         var position = getPosition();
 
-        while (position === toPosition(dest, true) && between(dest, 0, Splide2.length - 1, true)) {
+        while (position === toPosition(dest, true) && between(dest, 0, Splide2.length - 1)) {
           dest < currIndex ? --dest : ++dest;
         }
       }
 
-      return loop(dest);
+      return dest;
     }
 
     function loop(index) {

文件差異過大導致無法顯示
+ 0 - 0
dist/js/splide.min.js


二進制
dist/js/splide.min.js.gz


文件差異過大導致無法顯示
+ 0 - 0
dist/js/splide.min.js.map


+ 31 - 22
src/js/components/Controller/Controller.ts

@@ -113,7 +113,7 @@ export function Controller( Splide: Splide, Components: Components, options: Opt
   function go( control: number | string, allowSameIndex?: boolean, callback?: AnyFunction ): void {
     if ( ! isBusy() ) {
       const dest  = parse( control );
-      const index = validate( dest );
+      const index = loop( dest );
 
       if ( index > -1 && ( allowSameIndex || index !== currIndex ) ) {
         setIndex( index );
@@ -162,7 +162,7 @@ export function Controller( Splide: Splide, Components: Components, options: Opt
       const [ , indicator, number ] = control.match( /([+\-<>])(\d+)?/ ) || [];
 
       if ( indicator === '+' || indicator === '-' ) {
-        index = computeDestIndex( currIndex + +`${ indicator }${ +number || 1 }`, currIndex, true );
+        index = computeDestIndex( currIndex + +`${ indicator }${ +number || 1 }`, currIndex );
       } else if ( indicator === '>' ) {
         index = number ? toIndex( +number ) : getNext( true );
       } else if ( indicator === '<' ) {
@@ -187,7 +187,7 @@ export function Controller( Splide: Splide, Components: Components, options: Opt
    */
   function getAdjacent( prev: boolean, destination?: boolean ): number {
     const number = perMove || ( hasFocus() ? 1 : perPage );
-    const dest   = computeDestIndex( currIndex + number * ( prev ? -1 : 1 ), currIndex );
+    const dest   = computeDestIndex( currIndex + number * ( prev ? -1 : 1 ), currIndex, ! ( perMove || hasFocus() ) );
 
     if ( dest === -1 && isSlide ) {
       if ( ! approximatelyEqual( getPosition(), getLimit( ! prev ), 1 ) ) {
@@ -200,29 +200,36 @@ export function Controller( Splide: Splide, Components: Components, options: Opt
 
   /**
    * Converts the desired destination index to the valid one.
+   * - If the `move` option is `true`, finds the dest index whose position is different with the current one.
    * - This may return clone indices if the editor is the loop mode,
    *   or `-1` if there is no slide to go.
    * - There are still slides where the slider can go if borders are between `from` and `dest`.
    *
-   * @param dest        - The desired destination.
-   * @param from        - A base index.
-   * @param incremental - Optional. Whether the control is incremental or not.
+   * @param dest     - The desired destination.
+   * @param from     - A base index.
+   * @param snapPage - Optional. Whether to snap a page or not.
    *
    * @return A converted destination index, including clones.
    */
-  function computeDestIndex( dest: number, from: number, incremental?: boolean ): number {
+  function computeDestIndex( dest: number, from: number, snapPage?: boolean ): number {
     if ( isEnough() ) {
-      const end = getEnd();
+      const end         = getEnd();
+      const movable     = computeMovableDestIndex( dest );
+
+      if ( movable !== dest ) {
+        from     = dest;
+        dest     = movable;
+        snapPage = false;
+      }
 
-      // Will overrun:
       if ( dest < 0 || dest > end ) {
         if ( between( 0, dest, from, true ) || between( end, from, dest, true ) ) {
           dest = toIndex( toPage( dest ) );
         } else {
           if ( isLoop ) {
-            dest = perMove || hasFocus()
-              ? dest
-              : dest < 0 ? - ( slideCount % perPage || perPage ) : slideCount;
+            dest = snapPage
+              ? dest < 0 ? - ( slideCount % perPage || perPage ) : slideCount
+              : dest;
           } else if ( options.rewind ) {
             dest = dest < 0 ? end : 0;
           } else {
@@ -230,8 +237,8 @@ export function Controller( Splide: Splide, Components: Components, options: Opt
           }
         }
       } else {
-        if ( ! incremental && dest !== from ) {
-          dest = perMove ? dest : toIndex( toPage( from ) + ( dest < from ? -1 : 1 ) );
+        if ( snapPage && dest !== from ) {
+          dest = toIndex( toPage( from ) + ( dest < from ? -1 : 1 ) );
         }
       }
     } else {
@@ -242,23 +249,23 @@ export function Controller( Splide: Splide, Components: Components, options: Opt
   }
 
   /**
-   * Finalizes the dest index.
-   * If the `trim` option is `move`, needs to find the dest index where the slider actually moves.
+   * Finds the dest index whose position is different with the current one.
+   * This can be negative or greater than `length - 1`.
    *
-   * @todo
+   * @param dest - A dest index.
    *
-   * @param dest - A validated dest index.
+   * @return A dest index.
    */
-  function validate( dest: number ): number {
-    if ( options.trimSpace === 'move' && dest !== currIndex ) {
+  function computeMovableDestIndex( dest: number ): number {
+    if ( isSlide && options.trimSpace === 'move' && dest !== currIndex ) {
       const position = getPosition();
 
-      while ( position === toPosition( dest, true ) && between( dest, 0, Splide.length - 1, true ) ) {
+      while ( position === toPosition( dest, true ) && between( dest, 0, Splide.length - 1 ) ) {
         dest < currIndex ? --dest : ++dest;
       }
     }
 
-    return loop( dest );
+    return dest;
   }
 
   /**
@@ -298,6 +305,8 @@ export function Controller( Splide: Splide, Components: Components, options: Opt
    * Converts the slide index to the page index.
    *
    * @param index - An index to convert.
+   *
+   * @return A page index.
    */
   function toPage( index: number ): number {
     return hasFocus()

部分文件因文件數量過多而無法顯示