1
1
<template >
2
- <view : class =" classes " >
2
+ <view class =" nut-tour " >
3
3
<view v-if =" showTour" class =" nut-tour-masked" @click =" handleClickMask" ></view >
4
4
5
5
<view v-for =" (step, i) in steps" :key =" i" style =" height : 0 " >
42
42
@click =" changeStep('prev')"
43
43
>
44
44
{{ prevStepTxt }}
45
- </view
46
- >
45
+ </view >
47
46
</slot >
48
47
<view
49
48
v-if =" steps.length - 1 == active"
50
49
class =" nut-tour-content-bottom-operate-btn active"
51
50
@click =" close"
52
51
>
53
52
{{ completeTxt }}
54
- </view
55
- >
53
+ </view >
56
54
57
55
<slot name =" next-step" >
58
56
<view
61
59
@click =" changeStep('next')"
62
60
>
63
61
{{ nextStepTxt }}
64
- </view
65
- >
62
+ </view >
66
63
</slot >
67
64
</view >
68
65
</view >
80
77
</view >
81
78
</template >
82
79
<script lang="ts">
83
- import { computed , watch , ref , reactive , toRefs , PropType , onMounted } from ' vue'
80
+ import { watch , ref , reactive , toRefs , PropType , onMounted , CSSProperties } from ' vue'
84
81
import { PopoverLocation , PopoverTheme } from ' ../popover/type'
85
82
import { createComponent } from ' @/packages/utils/create'
86
83
import { rectTaro , useTaroRectById } from ' @/packages/utils/useTaroRect'
@@ -179,17 +176,12 @@ export default create({
179
176
180
177
let maskRect: rectTaro [] = []
181
178
182
- let maskStyles = ref <any []>([])
183
-
184
- const classes = computed (() => {
185
- const prefixCls = ' nut-tour'
186
- return ` ${prefixCls } `
187
- })
179
+ let maskStyles = ref <CSSProperties []>([])
188
180
189
181
const maskStyle = (index : number ) => {
190
182
const { offset, maskWidth, maskHeight } = props
191
183
192
- if (! maskRect [index ]) return {}
184
+ if (! maskRect [index ]) return
193
185
const { width, height, left, top } = maskRect [index ]
194
186
195
187
const center = [left + width / 2 , top + height / 2 ] // 中心点 【横,纵】
@@ -209,31 +201,26 @@ export default create({
209
201
const current = state .active
210
202
let next = current
211
203
212
- if (type == ' next' ) {
213
- next = current + 1
214
- } else {
215
- next = current - 1
216
- }
204
+ next = type == ' next' ? current + 1 : current - 1
217
205
showPopup .value [current ] = false
218
206
219
207
setTimeout (() => {
220
- showPopup .value [next ] = true
221
208
state .active = next
209
+ emit (' change' , state .active )
210
+ showPopup .value [state .active ] = true
222
211
}, 300 )
223
-
224
- emit (' change' , state .active )
225
212
}
226
213
227
- const getRootPosition = () => {
228
- props .steps .forEach ( async ( item , i ) => {
229
- useTaroRectById ( item . target ). then (
230
- ( rect : any ) => {
231
- maskRect [i ] = rect
232
- maskStyle (i )
233
- },
234
- () => {}
235
- )
236
- })
214
+ const getRootPosition = async () => {
215
+ for ( const [index, step] of props .steps .entries ()) {
216
+ try {
217
+ const rect = await useTaroRectById ( step . target )
218
+ maskRect [index ] = rect as rectTaro
219
+ maskStyle (index )
220
+ } catch ( error ) {
221
+ console . warn ( ` [NutUI] Failed to get rect for step ${ index }: ` , error )
222
+ }
223
+ }
237
224
}
238
225
239
226
const close = () => {
@@ -248,33 +235,30 @@ export default create({
248
235
}
249
236
250
237
onMounted (() => {
251
- setTimeout (() => {
252
- getRootPosition ()
253
- }, 500 )
238
+ getRootPosition ()
254
239
})
255
240
256
241
watch (
257
242
() => props .modelValue ,
258
- (val ) => {
259
- if (val ) {
260
- state .active = 0
243
+ (visible ) => {
244
+ if (visible ) {
245
+ state .active = props . type === ' step ' ? Math . min ( props . current , props . steps . length - 1 ) : 0
261
246
getRootPosition ()
262
247
}
263
- state .showTour = val
264
- showPopup .value [state .active ] = val
248
+ state .showTour = visible
249
+ showPopup .value [state .active ] = visible
265
250
}
266
251
)
267
252
268
253
const refRandomId = Math .random ().toString (36 ).slice (- 8 )
269
254
270
255
return {
271
256
... toRefs (state ),
272
- classes ,
273
257
maskStyle ,
274
258
changeStep ,
275
- showPopup ,
276
259
close ,
277
260
handleClickMask ,
261
+ showPopup ,
278
262
maskStyles ,
279
263
refRandomId
280
264
}
0 commit comments