Skip to content

Commit b228d59

Browse files
committed
perf: 优化 swipe-action 代码,删除无用日志
1 parent 4aed3fe commit b228d59

4 files changed

Lines changed: 99 additions & 74 deletions

File tree

‎components/uni-swipe-action/index.wxs‎

Lines changed: 94 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,88 +1,132 @@
1-
function change(newValue, oldValue, ownerInstance, instance) {
1+
/**
2+
* 监听页面内值的变化,主要用于动态开关swipe-action
3+
* @param {Object} newValue
4+
* @param {Object} oldValue
5+
* @param {Object} ownerInstance
6+
* @param {Object} instance
7+
*/
8+
function sizeReady(newValue, oldValue, ownerInstance, instance) {
29
var state = instance.getState()
310
state.position = JSON.parse(instance.getDataset().position)
411
if (state.position.length === 0) return
5-
// if (state.isMove) return
612
var show = state.position[0].show
713
state.left = state.left || state.position[0].left;
14+
// 通过用户变量,开启或关闭
815
if (show) {
9-
open(instance, ownerInstance)
16+
openState(true, instance, ownerInstance)
1017
} else {
11-
close(instance, ownerInstance)
18+
openState(false, instance, ownerInstance)
1219
}
1320
}
1421

22+
/**
23+
* 开始触摸操作
24+
* @param {Object} e
25+
* @param {Object} ins
26+
*/
1527
function touchstart(e, ins) {
1628
var instance = e.instance;
1729
var state = instance.getState();
18-
var pageX = e.touches[0].pageX;
19-
instance.removeClass('ani');
20-
var owner = ins.selectAllComponents('.button-hock')
21-
for (var i = 0; i < owner.length; i++) {
22-
owner[i].removeClass('ani');
30+
var pageX = e.touches[0].pageX;
31+
// 开始触摸时移除动画类
32+
instance.removeClass('ani');
33+
var owner = ins.selectAllComponents('.button-hock')
34+
for (var i = 0; i < owner.length; i++) {
35+
owner[i].removeClass('ani');
2336
}
2437
state.position = JSON.parse(instance.getDataset().position);
2538
state.left = state.left || state.position[0].left;
39+
// 获取最终按钮组的宽度
2640
state.width = pageX - state.left;
2741
ins.callMethod('openSwipe')
2842
}
2943

44+
/**
45+
* 开始滑动操作
46+
* @param {Object} e
47+
* @param {Object} ownerInstance
48+
*/
3049
function touchmove(e, ownerInstance) {
3150
var instance = e.instance;
3251
var disabled = instance.getDataset().disabled
3352
var state = instance.getState()
3453
if (disabled) return
35-
if (state.isMove) return
36-
var pageX = e.touches[0].pageX;
37-
setValue(pageX - state.width, instance, ownerInstance)
38-
54+
var pageX = e.touches[0].pageX;
55+
move(pageX - state.width, instance, ownerInstance)
3956
}
4057

58+
/**
59+
* 结束触摸操作
60+
* @param {Object} e
61+
* @param {Object} ownerInstance
62+
*/
4163
function touchend(e, ownerInstance) {
4264
var instance = e.instance;
4365
var disabled = instance.getDataset().disabled
4466
var state = instance.getState()
4567
if (disabled) return
46-
move(state.left, -40, instance, ownerInstance)
47-
68+
// 滑动过程中触摸结束,通过阙值判断是开启还是关闭
69+
moveDirection(state.left, -40, instance, ownerInstance)
4870
}
4971

50-
function setValue(value, instance, ownerInstance) {
72+
/**
73+
* 设置移动距离
74+
* @param {Object} value
75+
* @param {Object} instance
76+
* @param {Object} ownerInstance
77+
*/
78+
function move(value, instance, ownerInstance) {
5179
var state = instance.getState()
80+
// 获取可滑动范围
5281
var x = Math.max(-state.position[1].width, Math.min((value), 0));
5382
state.left = x;
5483
instance.setStyle({
5584
transform: 'translateX(' + x + 'px)',
5685
'-webkit-transform': 'translateX(' + x + 'px)'
57-
})
58-
setFold(x, instance, ownerInstance)
86+
})
87+
// 折叠按钮动画
88+
buttonFold(x, instance, ownerInstance)
5989
}
6090

61-
function move(left, value, ins, ownerInstance) {
91+
/**
92+
* 移动方向判断
93+
* @param {Object} left
94+
* @param {Object} value
95+
* @param {Object} ownerInstance
96+
* @param {Object} ins
97+
*/
98+
function moveDirection(left, value, ins, ownerInstance) {
6299
var state = ins.getState()
63100
var position = state.position
64101
var isopen = state.isopen
65102
if (!position[1].width) {
66-
close(ins, ownerInstance)
103+
openState(false,ins, ownerInstance)
67104
return
68105
}
106+
// 如果已经是打开状态,进行判断是否关闭,还是保留打开状态
69107
if (isopen) {
70108
if (-left <= position[1].width) {
71-
close(ins, ownerInstance)
109+
openState(false, ins, ownerInstance)
72110
} else {
73-
open(ins, ownerInstance)
111+
openState(true, ins, ownerInstance)
74112
}
75113
return
76114
}
77-
115+
// 如果是关闭状态,进行判断是否打开,还是保留关闭状态
78116
if (left <= value) {
79-
open(ins, ownerInstance)
117+
openState(true, ins, ownerInstance)
80118
} else {
81-
close(ins, ownerInstance)
119+
openState(false, ins, ownerInstance)
82120
}
83121
}
84122

85-
function setFold(value, instance, ownerInstance) {
123+
/**
124+
* 设置按钮移动距离
125+
* @param {Object} value
126+
* @param {Object} instance
127+
* @param {Object} ownerInstance
128+
*/
129+
function buttonFold(value, instance, ownerInstance) {
86130
var ins = ownerInstance.selectAllComponents('.button-hock');
87131
var state = instance.getState();
88132
var position = state.position;
@@ -93,57 +137,46 @@ function setFold(value, instance, ownerInstance) {
93137
var btnData = JSON.parse(ins[i].getDataset().button)
94138
var button = btnData[i] && btnData[i].width || 0
95139
w += button
96-
arr.push(-w)
140+
arr.push(-w)
141+
// 动态计算按钮组每个按钮的折叠动画移动距离
142+
var distance = arr[i - 1] + value * (arr[i - 1] / position[1].width)
97143
if (i != 0) {
98144
ins[i].setStyle({
99-
transform: 'translateX(' + (arr[i - 1] + value * (arr[i - 1] / position[1].width)) + 'px)',
100-
'-webkit-transform': 'translateX(' + (arr[i - 1] + value * (arr[i - 1] / position[1].width)) + 'px)'
145+
transform: 'translateX(' + distance + 'px)',
101146
})
102147
}
103148
}
104149
}
105150

106-
function open(ins, ownerInstance) {
151+
/**
152+
* 开启状态
153+
* @param {Boolean} type
154+
* @param {Object} ins
155+
* @param {Object} ownerInstance
156+
*/
157+
function openState(type, ins, ownerInstance) {
107158
var state = ins.getState()
108159
var position = state.position
109-
state.isopen = true
110-
state.isMove = true
160+
// 设置打开和移动状态
161+
state.isopen = type
111162

163+
// 通知页面,已经打开
112164
ownerInstance.callMethod('change', {
113-
open: true
165+
open: type
114166
})
115-
ins.addClass('ani');
116-
var owner = ownerInstance.selectAllComponents('.button-hock')
117-
for (var i = 0; i < owner.length; i++) {
118-
owner[i].addClass('ani');
119-
}
120-
121-
state.isMove = false
122-
setValue(-position[1].width, ins, ownerInstance)
123-
setFold(-position[1].width,ins, ownerInstance)
124-
}
125-
126-
function close(ins, ownerInstance) {
127-
var state = ins.getState()
128-
var position = state.position
129-
state.isopen = false
130-
state.isMove = true
131-
ownerInstance.callMethod('change', {
132-
open: false
133-
})
134-
135-
ins.addClass('ani');
136-
var owner = ownerInstance.selectAllComponents('.button-hock')
137-
for (var i = 0; i < owner.length; i++) {
138-
owner[i].addClass('ani');
167+
// 添加动画类
168+
ins.addClass('ani');
169+
var owner = ownerInstance.selectAllComponents('.button-hock')
170+
for (var i = 0; i < owner.length; i++) {
171+
owner[i].addClass('ani');
139172
}
140-
setValue(0, ins, ownerInstance)
141-
state.isMove = false
142-
}
173+
// 设置最终移动位置
174+
move(type ? -position[1].width : 0, ins, ownerInstance)
143175

176+
}
144177

145178
module.exports = {
146-
change: change,
179+
sizeReady: sizeReady,
147180
touchstart: touchstart,
148181
touchmove: touchmove,
149182
touchend: touchend

‎components/uni-swipe-action/mp.js‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,14 @@ export default {
2626
mounted () {
2727
this.init()
2828
this.getSize()
29-
this.getBUttonSize()
29+
this.getButtonSize()
3030
},
3131
// #endif
3232
// #ifndef H5
3333
onReady () {
3434
this.init()
3535
this.getSize()
36-
this.getBUttonSize()
36+
this.getButtonSize()
3737
},
3838
// #endif
3939
methods: {
@@ -76,7 +76,7 @@ export default {
7676
})
7777
.exec()
7878
},
79-
getBUttonSize () {
79+
getButtonSize () {
8080
const views = uni.createSelectorQuery().in(this)
8181
views
8282
.selectAll('.button-hock')

‎components/uni-swipe-action/mpother.js‎

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ export default {
3232
},
3333
methods: {
3434
init () {
35-
console.log('混入', this.moveLeft)
3635
uni.$on('__uni__swipe__event', (res) => {
3736
if (res !== this && this.autoClose) {
3837
if (this.left !== 0) {
@@ -51,7 +50,6 @@ export default {
5150
const {
5251
pageX
5352
} = e.touches[0]
54-
console.log('touchstart')
5553
if (this.disabled) return
5654
const left = this.position[0].left
5755
uni.$emit('__uni__swipe__event', this)
@@ -65,14 +63,12 @@ export default {
6563
},
6664
touchmove (e, index) {
6765
if (this.disabled) return
68-
console.log('touchmove')
6966
const {
7067
pageX
7168
} = e.touches[0]
7269
this.setPosition(pageX)
7370
},
7471
touchend () {
75-
console.log('touchend')
7672
if (this.disabled) return
7773
if (this.isopen) {
7874
this.move(this.openleft, 0)
@@ -81,14 +77,11 @@ export default {
8177
this.move(this.left, -40)
8278
},
8379
setPosition (x, y) {
84-
console.log(!this.position[1].width, this.position[1].width)
8580
if (!this.position[1].width) {
8681
return
8782
}
88-
console.log(x, '--')
8983
// const width = this.position[0].width
9084
this.left = x - this.width
91-
console.log(this.left)
9285
this.setValue(x - this.width)
9386
},
9487
setValue (value) {
@@ -128,7 +121,6 @@ export default {
128121
.selectAll('.selector-query-hock')
129122
.boundingClientRect(data => {
130123
this.position = data
131-
console.log(data)
132124
if (this.autoClose) return
133125
if (this.show) {
134126
this.open()

‎components/uni-swipe-action/uni-swipe-action.vue‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<template>
22
<view class="uni-swipe_content">
33
<!-- #ifdef APP-PLUS|| MP-WEIXIN||H5 -->
4-
<view :data-disabled="disabled" :data-position="pos" :change:prop="swipe.change" :prop="pos" class="uni-swipe_move-box selector-query-hock move-hock" @touchstart="swipe.touchstart" @touchmove="swipe.touchmove" @touchend="swipe.touchend" @change="change">
4+
<view :data-disabled="disabled" :data-position="pos" :change:prop="swipe.sizeReady" :prop="pos" class="uni-swipe_move-box selector-query-hock move-hock" @touchstart="swipe.touchstart" @touchmove="swipe.touchmove" @touchend="swipe.touchend" @change="change">
55
<!-- #endif -->
66
<!-- #ifndef APP-PLUS|| MP-WEIXIN||H5 -->
77
<view :class="{'ani':uniShow}" :style="{transform:moveLeft}" class="uni-swipe_move-box selector-query-hock" @touchstart="touchstart" @touchmove="touchmove" @touchend="touchend">
@@ -24,7 +24,7 @@
2424
<!-- #endif -->
2525
</view>
2626
</template>
27-
<wxs src="./index.wxs" module="swipe"></wxs>
27+
<script src="./index.wxs" module="swipe" lang="wxs"></script>
2828
<script>
2929
import mixins from './mpother'
3030
import mp from './mp'

0 commit comments

Comments
 (0)