vue.ts 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. // @ts-nocheck
  2. // #ifdef APP-NVUE
  3. // 当编译环境是 APP-NVUE 时,引入 uni.requireNativePlugin('dom'),具体插件用途未知
  4. const dom = uni.requireNativePlugin('dom')
  5. // #endif
  6. /**
  7. * 获取节点信息
  8. * @param selector 选择器字符串
  9. * @param context ComponentInternalInstance 对象
  10. * @param node 是否获取node
  11. * @returns 包含节点信息的 Promise 对象
  12. */
  13. export function getRect(selector : string, context : ComponentInternalInstance, node: boolean = false) {
  14. // 之前是个对象,现在改成实例,防止旧版会报错
  15. if(context== null) {
  16. return Promise.reject('context is null')
  17. }
  18. if(context.context){
  19. context = context.context
  20. }
  21. // #ifdef MP || VUE2
  22. if (context.proxy) context = context.proxy
  23. // #endif
  24. return new Promise<UniNamespace.NodeInfo>((resolve, reject) => {
  25. // #ifndef APP-NVUE
  26. const dom = uni.createSelectorQuery().in(context).select(selector);
  27. const result = (rect: UniNamespace.NodeInfo) => {
  28. if (rect) {
  29. resolve(rect)
  30. } else {
  31. reject('no rect')
  32. }
  33. }
  34. if (!node) {
  35. dom.boundingClientRect(result).exec()
  36. } else {
  37. dom.fields({
  38. node: true,
  39. size: true,
  40. rect: true
  41. }, result).exec()
  42. }
  43. // #endif
  44. // #ifdef APP-NVUE
  45. let { context } = options
  46. if (/#|\./.test(selector) && context.refs) {
  47. selector = selector.replace(/#|\./, '')
  48. if (context.refs[selector]) {
  49. selector = context.refs[selector]
  50. if(Array.isArray(selector)) {
  51. selector = selector[0]
  52. }
  53. }
  54. }
  55. dom.getComponentRect(selector, (res) => {
  56. if (res.size) {
  57. resolve(res.size)
  58. } else {
  59. reject('no rect')
  60. }
  61. })
  62. // #endif
  63. });
  64. };
  65. export function getAllRect(selector : string, context: ComponentInternalInstance, node:boolean = false) {
  66. if(context== null) {
  67. return Promise.reject('context is null')
  68. }
  69. // #ifdef MP || VUE2
  70. if (context.proxy) context = context.proxy
  71. // #endif
  72. return new Promise<UniNamespace.NodeInfo>((resolve, reject) => {
  73. // #ifndef APP-NVUE
  74. const dom = uni.createSelectorQuery().in(context).selectAll(selector);
  75. const result = (rect: UniNamespace.NodeInfo[]) => {
  76. if (rect) {
  77. resolve(rect)
  78. } else {
  79. reject('no rect')
  80. }
  81. }
  82. if (!node) {
  83. dom.boundingClientRect(result).exec()
  84. } else {
  85. dom.fields({
  86. node: true,
  87. size: true,
  88. rect: true
  89. }, result).exec()
  90. }
  91. // #endif
  92. // #ifdef APP-NVUE
  93. let { context } = options
  94. if (/#|\./.test(selector) && context.refs) {
  95. selector = selector.replace(/#|\./, '')
  96. if (context.refs[selector]) {
  97. selector = context.refs[selector]
  98. if(Array.isArray(selector)) {
  99. selector = selector[0]
  100. }
  101. }
  102. }
  103. dom.getComponentRect(selector, (res) => {
  104. if (res.size) {
  105. resolve([res.size])
  106. } else {
  107. reject('no rect')
  108. }
  109. })
  110. // #endif
  111. });
  112. };