web
9 小时以前 49fa0d82a40345342966e810b44429aec0480ef3
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
// @ts-nocheck
import { ComponentPublicInstance } from 'vue'
import { ease, linear } from './ease';
import { Timeline, Animation } from './';
export type UseTransitionOptions = {
    duration ?: number
    immediate ?: boolean
    context ?: ComponentPublicInstance
}
// #ifndef UNI-APP-X && APP
import { ref, watch, Ref } from '@/uni_modules/lime-shared/vue'
 
export function useTransition(percent : Ref<number>|(() => number), options : UseTransitionOptions) : Ref<number> {
    const current = ref(0)
    const { immediate, duration = 300 } = options
    let tl:Timeline|null = null;
    let timer = -1
    const isFunction = typeof percent === 'function'
    watch(isFunction ? percent : () => percent.value, (v) => {
        if(tl == null){
            tl = new Timeline()
        }
        tl.start();
        tl.add(
            new Animation(
                current.value,
                v,
                duration,
                0,
                ease,
                nowValue => {
                    current.value = nowValue
                    clearTimeout(timer)
                    if(current.value == v){
                        timer = setTimeout(()=>{
                            tl?.pause();
                            tl = null
                        }, duration)
                    }
                }
            )
        );
    }, { immediate })
 
    return current
}
 
// #endif
 
// #ifdef UNI-APP-X && APP
type UseTransitionReturnType = Ref<number>
// export function useTransition<T>(source : any, options : UseTransitionOptions) : UseTransitionReturnType
export function useTransition(source : any, options : UseTransitionOptions) : UseTransitionReturnType {
    const outputRef : Ref<number> = ref(0)
    const immediate = options.immediate ?? false
    const duration = options.duration ?? 300
    const context = options.context //as ComponentPublicInstance | null
    let tl:Timeline|null = null;
    let timer = -1
    const watchFunc = (v : number) => {
        if(tl == null){
            tl = new Timeline()
        }
        tl!.start();
        tl!.add(
            new Animation(
                outputRef.value,
                v,
                duration,
                0,
                ease,
                nowValue => {
                    outputRef.value = nowValue //nowValue < 0.0001 ? 0 : Math.abs(v - nowValue) < 0.00001 ? v : nowValue;
                    clearTimeout(timer)
                    if(outputRef.value == v){
                        timer = setTimeout(()=>{
                            tl?.pause();
                            tl = null
                        }, duration)
                    }
                }
            ), null
        );
    }
 
    if (context != null && typeof source == 'string') {
        context.$watch(source, watchFunc, { immediate } as WatchOptions)
    } else {
        watch(source, watchFunc, { immediate } as WatchOptions)
    }
    
    const stop = ()=>{
        
    }
    return outputRef //as UseTransitionReturnType
}
 
// #endif