index.ts 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. // @ts-nocheck
  2. // #ifdef APP-NVUE
  3. const dom = uni.requireNativePlugin('dom')
  4. // #endif
  5. interface RectOptions {
  6. /**
  7. * 上下文
  8. */
  9. context ?: any //ComponentInternalInstance
  10. /**
  11. * 所有节点 nvue不支持
  12. */
  13. needAll ?: Boolean,
  14. nodes ?: UniNamespace.NodesRef
  15. type ?: keyof UniNamespace.NodesRef
  16. }
  17. /** 获取节点信息 */
  18. export function getRect(selector : string, options : RectOptions = {}) {
  19. // #ifndef APP-NVUE
  20. const typeDefault = 'boundingClientRect'
  21. let { context, needAll, type = typeDefault } = options
  22. // #endif
  23. // #ifdef MP || VUE2
  24. if (context.proxy) context = context.proxy
  25. // #endif
  26. return new Promise<any>((resolve, reject) => {
  27. // #ifndef APP-NVUE
  28. const dom = uni.createSelectorQuery().in(context)[needAll ? 'selectAll' : 'select'](selector);
  29. const result = (rect) => {
  30. if (rect) {
  31. resolve(rect)
  32. } else {
  33. reject('no rect')
  34. }
  35. }
  36. if (type == typeDefault) {
  37. dom[type](result).exec()
  38. } else {
  39. dom[type]({
  40. node: true,
  41. size: true,
  42. rect: true
  43. }, result).exec()
  44. }
  45. // #endif
  46. // #ifdef APP-NVUE
  47. let { context } = options
  48. if (/#|\./.test(selector) && context.refs) {
  49. selector = selector.replace(/#|\./, '')
  50. if (context.refs[selector]) {
  51. selector = context.refs[selector]
  52. if(Array.isArray(selector)) {
  53. selector = selector[0]
  54. }
  55. }
  56. }
  57. dom.getComponentRect(selector, (res) => {
  58. if (res.size) {
  59. resolve(res.size)
  60. } else {
  61. reject('no rect')
  62. }
  63. })
  64. // #endif
  65. });
  66. };