|
@@ -9,23 +9,11 @@
|
|
|
@touchcancel="onTouchEnd"
|
|
@touchcancel="onTouchEnd"
|
|
|
:catch-move="isPreventDefault"
|
|
:catch-move="isPreventDefault"
|
|
|
>
|
|
>
|
|
|
- <view
|
|
|
|
|
- :class="{
|
|
|
|
|
- [`${componentName}-inner`]: true,
|
|
|
|
|
- [`${componentName}-vertical`]: isVertical
|
|
|
|
|
- }"
|
|
|
|
|
- :style="state.style"
|
|
|
|
|
- >
|
|
|
|
|
|
|
+ <view :class="classesInner" :style="state.style">
|
|
|
<slot></slot>
|
|
<slot></slot>
|
|
|
</view>
|
|
</view>
|
|
|
<slot name="page"></slot>
|
|
<slot name="page"></slot>
|
|
|
- <view
|
|
|
|
|
- :class="{
|
|
|
|
|
- [`${componentName}-pagination`]: true,
|
|
|
|
|
- [`${componentName}-pagination-vertical`]: isVertical
|
|
|
|
|
- }"
|
|
|
|
|
- v-if="paginationVisible && !slots.page"
|
|
|
|
|
- >
|
|
|
|
|
|
|
+ <view :class="classesPagination" v-if="paginationVisible && !$slots.page">
|
|
|
<i
|
|
<i
|
|
|
:style="{
|
|
:style="{
|
|
|
backgroundColor: activePagination === index ? paginationColor : '#ddd'
|
|
backgroundColor: activePagination === index ? paginationColor : '#ddd'
|
|
@@ -52,9 +40,11 @@ import {
|
|
|
VNode
|
|
VNode
|
|
|
} from 'vue';
|
|
} from 'vue';
|
|
|
import { createComponent } from '@/packages/utils/create';
|
|
import { createComponent } from '@/packages/utils/create';
|
|
|
-import { useTouch } from './use-touch';
|
|
|
|
|
|
|
+import { useTouch } from '@/packages/utils/useTouch/index';
|
|
|
import { useTaroRect } from '@/packages/utils/useTaroRect';
|
|
import { useTaroRect } from '@/packages/utils/useTaroRect';
|
|
|
import { useExpose } from '@/packages/utils/useExpose/index';
|
|
import { useExpose } from '@/packages/utils/useExpose/index';
|
|
|
|
|
+import requestAniFrame from '@/packages/utils/raf';
|
|
|
|
|
+import { clamp } from '@/packages/utils/util';
|
|
|
import Taro, { eventCenter, getCurrentInstance } from '@tarojs/taro';
|
|
import Taro, { eventCenter, getCurrentInstance } from '@tarojs/taro';
|
|
|
const { create, componentName } = createComponent('swiper');
|
|
const { create, componentName } = createComponent('swiper');
|
|
|
export default create({
|
|
export default create({
|
|
@@ -68,7 +58,7 @@ export default create({
|
|
|
default: 0
|
|
default: 0
|
|
|
},
|
|
},
|
|
|
direction: {
|
|
direction: {
|
|
|
- type: [String],
|
|
|
|
|
|
|
+ type: String,
|
|
|
default: 'horizontal' //horizontal and vertical
|
|
default: 'horizontal' //horizontal and vertical
|
|
|
},
|
|
},
|
|
|
paginationVisible: {
|
|
paginationVisible: {
|
|
@@ -143,12 +133,28 @@ export default create({
|
|
|
|
|
|
|
|
const isVertical = computed(() => props.direction === 'vertical');
|
|
const isVertical = computed(() => props.direction === 'vertical');
|
|
|
|
|
|
|
|
|
|
+ const classesInner = computed(() => {
|
|
|
|
|
+ const prefixCls = componentName;
|
|
|
|
|
+ return {
|
|
|
|
|
+ [`${prefixCls}-inner`]: true,
|
|
|
|
|
+ [`${prefixCls}-vertical`]: isVertical.value
|
|
|
|
|
+ };
|
|
|
|
|
+ });
|
|
|
|
|
+
|
|
|
|
|
+ const classesPagination = computed(() => {
|
|
|
|
|
+ const prefixCls = componentName;
|
|
|
|
|
+ return {
|
|
|
|
|
+ [`${prefixCls}-pagination`]: true,
|
|
|
|
|
+ [`${prefixCls}-pagination-vertical`]: isVertical.value
|
|
|
|
|
+ };
|
|
|
|
|
+ });
|
|
|
|
|
+
|
|
|
const delTa = computed(() => {
|
|
const delTa = computed(() => {
|
|
|
- return isVertical.value ? touch.state.deltaY : touch.state.deltaX;
|
|
|
|
|
|
|
+ return isVertical.value ? touch.deltaY.value : touch.deltaX.value;
|
|
|
});
|
|
});
|
|
|
|
|
|
|
|
const isCorrectDirection = computed(() => {
|
|
const isCorrectDirection = computed(() => {
|
|
|
- return touch.state.direction === props.direction;
|
|
|
|
|
|
|
+ return touch.direction.value === props.direction;
|
|
|
});
|
|
});
|
|
|
|
|
|
|
|
const childCount = computed(() => state.children.length);
|
|
const childCount = computed(() => state.children.length);
|
|
@@ -187,26 +193,27 @@ export default create({
|
|
|
|
|
|
|
|
const relation = (child: ComponentInternalInstance) => {
|
|
const relation = (child: ComponentInternalInstance) => {
|
|
|
let children = [] as VNode[];
|
|
let children = [] as VNode[];
|
|
|
|
|
+ const childrenVNodeLen = state.childrenVNode.length;
|
|
|
let slot = slots?.default?.() as VNode[];
|
|
let slot = slots?.default?.() as VNode[];
|
|
|
slot = slot.filter((item: VNode) => item.children && Array.isArray(item.children));
|
|
slot = slot.filter((item: VNode) => item.children && Array.isArray(item.children));
|
|
|
slot.forEach((item: VNode) => {
|
|
slot.forEach((item: VNode) => {
|
|
|
children = children.concat(item.children as VNode[]);
|
|
children = children.concat(item.children as VNode[]);
|
|
|
});
|
|
});
|
|
|
- if (!state.childrenVNode.length) {
|
|
|
|
|
|
|
+ if (!childrenVNodeLen) {
|
|
|
state.childrenVNode = children.slice();
|
|
state.childrenVNode = children.slice();
|
|
|
child.proxy && state.children.push(child.proxy);
|
|
child.proxy && state.children.push(child.proxy);
|
|
|
} else {
|
|
} else {
|
|
|
- if (state.childrenVNode.length > children.length) {
|
|
|
|
|
|
|
+ if (childrenVNodeLen > children.length) {
|
|
|
state.children = state.children.filter((item: ComponentPublicInstance) => child.proxy !== item);
|
|
state.children = state.children.filter((item: ComponentPublicInstance) => child.proxy !== item);
|
|
|
- } else if (state.childrenVNode.length < children.length) {
|
|
|
|
|
- for (let i = 0; i < state.childrenVNode.length; i++) {
|
|
|
|
|
|
|
+ } else if (childrenVNodeLen < children.length) {
|
|
|
|
|
+ for (let i = 0; i < childrenVNodeLen; i++) {
|
|
|
if ((children[i] as VNode).key !== (state.childrenVNode[i] as VNode).key) {
|
|
if ((children[i] as VNode).key !== (state.childrenVNode[i] as VNode).key) {
|
|
|
child.proxy && state.children.splice(i, 0, child.proxy);
|
|
child.proxy && state.children.splice(i, 0, child.proxy);
|
|
|
child.vnode && state.childrenVNode.splice(i, 0, child.vnode);
|
|
child.vnode && state.childrenVNode.splice(i, 0, child.vnode);
|
|
|
break;
|
|
break;
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
- if (state.childrenVNode.length !== children.length) {
|
|
|
|
|
|
|
+ if (childrenVNodeLen !== children.length) {
|
|
|
child.proxy && state.children.push(child.proxy);
|
|
child.proxy && state.children.push(child.proxy);
|
|
|
child.vnode && state.childrenVNode.push(child.vnode);
|
|
child.vnode && state.childrenVNode.push(child.vnode);
|
|
|
}
|
|
}
|
|
@@ -217,14 +224,6 @@ export default create({
|
|
|
}
|
|
}
|
|
|
};
|
|
};
|
|
|
|
|
|
|
|
- const range = (num: number, min: number, max: number) => {
|
|
|
|
|
- return Math.min(Math.max(num, min), max);
|
|
|
|
|
- };
|
|
|
|
|
-
|
|
|
|
|
- const requestFrame = (fn: FrameRequestCallback) => {
|
|
|
|
|
- requestAnimationFrame.call(null, fn);
|
|
|
|
|
- };
|
|
|
|
|
-
|
|
|
|
|
const getOffset = (active: number, offset = 0) => {
|
|
const getOffset = (active: number, offset = 0) => {
|
|
|
let currentPosition = active * size.value;
|
|
let currentPosition = active * size.value;
|
|
|
if (!props.loop) {
|
|
if (!props.loop) {
|
|
@@ -233,7 +232,7 @@ export default create({
|
|
|
|
|
|
|
|
let targetOffset = offset - currentPosition;
|
|
let targetOffset = offset - currentPosition;
|
|
|
if (!props.loop) {
|
|
if (!props.loop) {
|
|
|
- targetOffset = range(targetOffset, minOffset.value, 0);
|
|
|
|
|
|
|
+ targetOffset = clamp(targetOffset, minOffset.value, 0);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
return targetOffset;
|
|
return targetOffset;
|
|
@@ -243,9 +242,9 @@ export default create({
|
|
|
const { active } = state;
|
|
const { active } = state;
|
|
|
if (pace) {
|
|
if (pace) {
|
|
|
if (props.loop) {
|
|
if (props.loop) {
|
|
|
- return range(active + pace, -1, childCount.value);
|
|
|
|
|
|
|
+ return clamp(active + pace, -1, childCount.value);
|
|
|
}
|
|
}
|
|
|
- return range(active + pace, 0, childCount.value - 1);
|
|
|
|
|
|
|
+ return clamp(active + pace, 0, childCount.value - 1);
|
|
|
}
|
|
}
|
|
|
return active;
|
|
return active;
|
|
|
};
|
|
};
|
|
@@ -298,13 +297,11 @@ export default create({
|
|
|
resettPosition();
|
|
resettPosition();
|
|
|
touch.reset();
|
|
touch.reset();
|
|
|
|
|
|
|
|
- requestFrame(() => {
|
|
|
|
|
- requestFrame(() => {
|
|
|
|
|
- state.moving = false;
|
|
|
|
|
- move({
|
|
|
|
|
- pace: -1,
|
|
|
|
|
- isEmit: true
|
|
|
|
|
- });
|
|
|
|
|
|
|
+ requestAniFrame(() => {
|
|
|
|
|
+ state.moving = false;
|
|
|
|
|
+ move({
|
|
|
|
|
+ pace: -1,
|
|
|
|
|
+ isEmit: true
|
|
|
});
|
|
});
|
|
|
});
|
|
});
|
|
|
};
|
|
};
|
|
@@ -313,13 +310,11 @@ export default create({
|
|
|
resettPosition();
|
|
resettPosition();
|
|
|
touch.reset();
|
|
touch.reset();
|
|
|
|
|
|
|
|
- requestFrame(() => {
|
|
|
|
|
- requestFrame(() => {
|
|
|
|
|
- state.moving = false;
|
|
|
|
|
- move({
|
|
|
|
|
- pace: 1,
|
|
|
|
|
- isEmit: true
|
|
|
|
|
- });
|
|
|
|
|
|
|
+ requestAniFrame(() => {
|
|
|
|
|
+ state.moving = false;
|
|
|
|
|
+ move({
|
|
|
|
|
+ pace: 1,
|
|
|
|
|
+ isEmit: true
|
|
|
});
|
|
});
|
|
|
});
|
|
});
|
|
|
};
|
|
};
|
|
@@ -329,19 +324,17 @@ export default create({
|
|
|
|
|
|
|
|
touch.reset();
|
|
touch.reset();
|
|
|
|
|
|
|
|
- requestFrame(() => {
|
|
|
|
|
- requestFrame(() => {
|
|
|
|
|
- state.moving = false;
|
|
|
|
|
- let targetIndex;
|
|
|
|
|
- if (props.loop && childCount.value === index) {
|
|
|
|
|
- targetIndex = state.active === 0 ? 0 : index;
|
|
|
|
|
- } else {
|
|
|
|
|
- targetIndex = index % childCount.value;
|
|
|
|
|
- }
|
|
|
|
|
- move({
|
|
|
|
|
- pace: targetIndex - state.active,
|
|
|
|
|
- isEmit: true
|
|
|
|
|
- });
|
|
|
|
|
|
|
+ requestAniFrame(() => {
|
|
|
|
|
+ state.moving = false;
|
|
|
|
|
+ let targetIndex;
|
|
|
|
|
+ if (props.loop && childCount.value === index) {
|
|
|
|
|
+ targetIndex = state.active === 0 ? 0 : index;
|
|
|
|
|
+ } else {
|
|
|
|
|
+ targetIndex = index % childCount.value;
|
|
|
|
|
+ }
|
|
|
|
|
+ move({
|
|
|
|
|
+ pace: targetIndex - state.active,
|
|
|
|
|
+ isEmit: true
|
|
|
});
|
|
});
|
|
|
});
|
|
});
|
|
|
};
|
|
};
|
|
@@ -400,7 +393,7 @@ export default create({
|
|
|
|
|
|
|
|
if (isShouldMove && isCorrectDirection.value) {
|
|
if (isShouldMove && isCorrectDirection.value) {
|
|
|
let pace = 0;
|
|
let pace = 0;
|
|
|
- const offset = isVertical.value ? touch.state.offsetY : touch.state.offsetX;
|
|
|
|
|
|
|
+ const offset = isVertical.value ? touch.offsetY.value : touch.offsetX.value;
|
|
|
if (props.loop) {
|
|
if (props.loop) {
|
|
|
pace = offset > 0 ? (delTa.value > 0 ? -1 : 1) : 0;
|
|
pace = offset > 0 ? (delTa.value > 0 ? -1 : 1) : 0;
|
|
|
} else {
|
|
} else {
|
|
@@ -475,10 +468,9 @@ export default create({
|
|
|
state,
|
|
state,
|
|
|
refRandomId,
|
|
refRandomId,
|
|
|
classes,
|
|
classes,
|
|
|
|
|
+ classesPagination,
|
|
|
|
|
+ classesInner,
|
|
|
container,
|
|
container,
|
|
|
- componentName,
|
|
|
|
|
- isVertical,
|
|
|
|
|
- slots,
|
|
|
|
|
activePagination,
|
|
activePagination,
|
|
|
onTouchStart,
|
|
onTouchStart,
|
|
|
onTouchMove,
|
|
onTouchMove,
|