useDOM.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. import { miniprogramThis } from '../internals/global'
  2. import { pxToNumber } from '../shared/pxToNumber'
  3. export const useQuery = (vm) => {
  4. return !!vm ? miniprogramThis.createSelectorQuery().in(vm) : miniprogramThis.createSelectorQuery()
  5. }
  6. const makeFields = () => ({
  7. id: true,
  8. dataset: true,
  9. mark: true,
  10. rect: true,
  11. // size: true,
  12. scrollOffset: true,
  13. computedStyle: [
  14. 'width',
  15. 'height',
  16. 'borderTopWidth',
  17. 'borderRightWidth',
  18. 'borderBottomWidth',
  19. 'borderLeftWidth',
  20. ],
  21. node: true,
  22. })
  23. const makeNodeRef = (node) => {
  24. const borderRightWidth = pxToNumber(node.borderRightWidth || 0)
  25. const borderLeftWidth = pxToNumber(node.borderLeftWidth || 0)
  26. const borderTopWidth = pxToNumber(node.borderTopWidth || 0)
  27. const borderBottomWidth = pxToNumber(node.borderBottomWidth || 0)
  28. const clientWidth = pxToNumber(node.width)
  29. const clientHeight = pxToNumber(node.height)
  30. const offsetWidth = clientWidth + borderRightWidth + borderLeftWidth
  31. const offsetHeight = clientHeight + borderTopWidth + borderBottomWidth
  32. return {
  33. id: node.id,
  34. dataset: node.dataset,
  35. mark: node.mark,
  36. top: node.top,
  37. right: node.right,
  38. bottom: node.bottom,
  39. left: node.left,
  40. width: offsetWidth,
  41. height: offsetHeight,
  42. x: node.left,
  43. y: node.top,
  44. offsetWidth,
  45. offsetHeight,
  46. clientLeft: borderLeftWidth,
  47. clientTop: borderTopWidth,
  48. clientWidth,
  49. clientHeight,
  50. scrollHeight: node.scrollHeight,
  51. scrollLeft: node.scrollLeft,
  52. scrollTop: node.scrollTop,
  53. scrollWidth: node.scrollWidth,
  54. node: node.node,
  55. }
  56. }
  57. export const useRef = (selector, vm) => {
  58. return new Promise((resolve) => {
  59. const query = useQuery(vm)
  60. const isArray = Array.isArray(selector)
  61. const classList = isArray ? selector : [selector]
  62. classList.forEach((s) => {
  63. query
  64. .select(s)
  65. .fields(makeFields())
  66. })
  67. query.exec((nodes) => {
  68. resolve(
  69. isArray
  70. ? nodes.map((node) => makeNodeRef(node))
  71. : makeNodeRef(nodes[0])
  72. )
  73. })
  74. })
  75. }
  76. export const useRefAll = (selector, vm) => {
  77. return new Promise((resolve) => {
  78. const query = useQuery(vm)
  79. const isArray = Array.isArray(selector)
  80. const classList = isArray ? selector : [selector]
  81. classList.forEach((s) => {
  82. query
  83. .selectAll(s)
  84. .fields(makeFields())
  85. })
  86. query.exec((nodesList) => {
  87. resolve(
  88. isArray
  89. ? nodesList.map((nodes) => nodes.map((node) => makeNodeRef(node)))
  90. : nodesList[0].map((node) => makeNodeRef(node))
  91. )
  92. })
  93. })
  94. }
  95. export const useRect = (selector, vm) => {
  96. return new Promise((resolve) => {
  97. const query = useQuery(vm)
  98. const isArray = Array.isArray(selector)
  99. const classList = isArray ? selector : [selector]
  100. classList.forEach((s) => {
  101. query
  102. .select(s)
  103. .boundingClientRect()
  104. })
  105. query.exec((nodes) => {
  106. resolve(isArray ? nodes : nodes[0])
  107. })
  108. })
  109. }
  110. export const useRectAll = (selector, vm) => {
  111. return new Promise((resolve) => {
  112. const query = useQuery(vm)
  113. const isArray = Array.isArray(selector)
  114. const classList = isArray ? selector : [selector]
  115. classList.forEach((s) => {
  116. query
  117. .selectAll(s)
  118. .boundingClientRect()
  119. })
  120. query.exec((nodesList) => {
  121. resolve(isArray ? nodesList : nodesList[0])
  122. })
  123. })
  124. }
  125. export const useScrollOffset = (vm) => {
  126. return new Promise((resolve) => {
  127. const query = useQuery(vm)
  128. query
  129. .selectViewport()
  130. .scrollOffset()
  131. query.exec(([node]) => {
  132. resolve(node)
  133. })
  134. })
  135. }
  136. export const useComputedStyle = (selector, ...args) => {
  137. const computedStyle = args.length === 2 ? args[0] : ['width', 'height']
  138. const vm = args.length === 2 ? args[1] : args[0]
  139. return new Promise((resolve) => {
  140. const query = useQuery(vm)
  141. query
  142. .select(selector)
  143. .fields({
  144. computedStyle,
  145. })
  146. query.exec(([node]) => {
  147. resolve(node)
  148. })
  149. })
  150. }