123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117 |
- // @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
- });
- };
|