web
8 小时以前 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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
// @ts-nocheck
 
// #ifdef APP-NVUE
// 当编译环境是 APP-NVUE 时,引入 uni.requireNativePlugin('dom'),具体插件用途未知
const dom = uni.requireNativePlugin('dom')
// #endif
 
/** 
 * 获取节点信息
 * @param selector 选择器字符串
 * @param context ComponentInternalInstance 对象
 * @param node 是否获取node
 * @returns 包含节点信息的 Promise 对象
 */
export function getRect(selector : string, context : ComponentInternalInstance, node: boolean = false) {
    // 之前是个对象,现在改成实例,防止旧版会报错
    if(context== null) {
        return Promise.reject('context is null')
    }
    if(context.context){
        context = context.context
    }
    // #ifdef MP || VUE2
    if (context.proxy) context = context.proxy
    // #endif
    return new Promise<UniNamespace.NodeInfo>((resolve, reject) => {
        // #ifndef APP-NVUE
        const dom = uni.createSelectorQuery().in(context).select(selector);
        const result = (rect: UniNamespace.NodeInfo) => {
            if (rect) {
                resolve(rect)
            } else {
                reject('no rect')
            }
        }
        
        if (!node) {
            dom.boundingClientRect(result).exec()
        } else {
            dom.fields({
                node: true,
                size: true,
                rect: true
            }, result).exec()
        }
        // #endif
        // #ifdef APP-NVUE
        let { context } = options
        if (/#|\./.test(selector) && context.refs) {
            selector = selector.replace(/#|\./, '')
            if (context.refs[selector]) {
                selector = context.refs[selector]
                if(Array.isArray(selector)) {
                    selector = selector[0]
                }
            }
        }
        dom.getComponentRect(selector, (res) => {
            if (res.size) {
                resolve(res.size)
            } else {
                reject('no rect')
            }
        })
        // #endif
    });
};
 
 
export function getAllRect(selector : string, context: ComponentInternalInstance, node:boolean = false) {
    if(context== null) {
        return Promise.reject('context is null')
    }
    // #ifdef MP || VUE2
    if (context.proxy) context = context.proxy
    // #endif
    return new Promise<UniNamespace.NodeInfo>((resolve, reject) => {
        // #ifndef APP-NVUE
        const dom = uni.createSelectorQuery().in(context).selectAll(selector);
        const result = (rect: UniNamespace.NodeInfo[]) => {
            if (rect) {
                resolve(rect)
            } else {
                reject('no rect')
            }
        }
        if (!node) {
            dom.boundingClientRect(result).exec()
        } else {
            dom.fields({
                node: true,
                size: true,
                rect: true
            }, result).exec()
        }
        // #endif
        // #ifdef APP-NVUE
        let { context } = options
        if (/#|\./.test(selector) && context.refs) {
            selector = selector.replace(/#|\./, '')
            if (context.refs[selector]) {
                selector = context.refs[selector]
                if(Array.isArray(selector)) {
                    selector = selector[0]
                }
            }
        }
        dom.getComponentRect(selector, (res) => {
            if (res.size) {
                resolve([res.size])
            } else {
                reject('no rect')
            }
        })
        // #endif
    });
};