All files / src/core/dom useDOM.ts

60.21% Statements 56/93
40.81% Branches 20/49
48.38% Functions 15/31
61.53% Lines 56/91

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228  1x 1x 1x 1x 1x 1x 1x 11x 11x       1x                   1x   1x                   1x   1x 1x                                 1x                                                                   1x 2x 2x 2x 2x 2x                           1x 1x 2x 2x 2x 2x 2x                           1x 1x 2x 2x 2x 2x 2x                       1x 1x 2x 2x 2x 2x 2x                       1x 1x 1x 1x 1x                   1x 1x 1x 1x 1x 1x 1x                       1x                                                              
import { miniprogramThis } from './global'
import { pxToNumber } from '../util'
import { canUseMP } from './canUseMP'
import { getCurrentPage } from './getCurrentPage'
import { type MPElement, type MPInst } from '../../types'
 
export const useQuery = (dom: MPInst = getCurrentPage()) => {
  if (!canUseMP()) {
    Ereturn null
  }
  return !!dom ? miniprogramThis?.createSelectorQuery?.().in(dom) : miniprogramThis?.createSelectorQuery?.()
}
 
/**
 * 获取匹配指定选择器的第一个元素
 *
 * @export
 * @param {string} selector
 * @param {MPInst} dom
 * @return {*}  {(MPElement | null)}
 */
export function useSelector (selector: string, dom?: MPInst): MPElement | null {
  return canUseMP() ? useQuery(dom).select(selector) : null
}
 
/**
 * 获取匹配指定选择器的所有元素
 *
 * @export
 * @param {string} selector
 * @param {MPInst} dom
 * @return {*}  {(MPElement | null)}
 */
export function useSelectorAll (selector: string, dom?: MPInst): MPElement | null {
  return canUseMP() ? useQuery(dom).selectAll(selector) : null
}
 
const makeFields = () => ({
  id: true,
  dataset: true,
  mark: true,
  rect: true,
  // size: true,
  scrollOffset: true,
  computedStyle: [
      'width',
      'height',
      'borderTopWidth',
      'borderRightWidth',
      'borderBottomWidth',
      'borderLeftWidth',
  ],
  node: true,
})
 
export type NodeRef = {
  borderRightWidth: number;
  borderLeftWidth: number;
  borderTopWidth: number;
  borderBottomWidth: number;
  width: number;
  height: number;
  id: number;
  dataset: Record<string, string>;
  mark: Record<string, string>;
  top: number;
  right: number;
  bottom: number;
  left: number;
  scrollHeight: number;
  scrollLeft: number;
  scrollTop: number;
  scrollWidth: number;
  node: Record<string, any>;
}
 
const makeNodeRef = (node: NodeRef) => {
  const borderRightWidth = pxToNumber(node.borderRightWidth || 0)
  const borderLeftWidth = pxToNumber(node.borderLeftWidth || 0)
  const borderTopWidth = pxToNumber(node.borderTopWidth || 0)
  const borderBottomWidth = pxToNumber(node.borderBottomWidth || 0)
  const clientWidth = pxToNumber(node.width)
  const clientHeight = pxToNumber(node.height)
  const offsetWidth = clientWidth + borderRightWidth + borderLeftWidth
  const offsetHeight = clientHeight + borderTopWidth + borderBottomWidth
 
  return {
      id: node.id,
      dataset: node.dataset,
      mark: node.mark,
 
      top: node.top,
      right: node.right,
      bottom: node.bottom,
      leIft: node.left,
      width: offsetWidth,
      height: offsetHeight,
      x: node.left,
      y: node.top,
  
      offsetWidth,
      offsetHeight,
      clientLeft: borderLeftWidth,
      clientTop: borderTopWidth,
      clientWidth,
      clientHeight,
 
      scrollHeight: node.scrollHeight,
      scrollLeft: node.scrollLeft,
      scrollTop: node.scrollTop,
      scrollWidth: node.scrollWidth,
 
      node: node.node,
  }
}I

export const useRef = (selector: string | string[], dom?: MPInst) => {
  return new Promise((resolve) => {
      const query = useQuery(dom)
      const isArray = Array.isArray(selector)
      const classList = isArray ? selector : [selector]
      if (query) {
        classList.forEach((s) => {
            query
                .select(s)
                .fields(makeFields())
        })
        query.exec((nodes) => {
            resolve(
                isArray
                    ? nodes.map((node) => makeNodeRef(node))
                    : makeNodeRef(nodes[0])
            )
        })
      }I
  })
}
 
export const useRefAll = (selector: string | string[], dom?: MPInst) => {
  return new Promise((resolve) => {
      const query = useQuery(dom)
      const isArray = Array.isArray(selector)
      const classList = isArray ? selector : [selector]
      if (query) {
      classList.forEach((s) => {
          query
              .selectAll(s)
              .fields(makeFields())
      })
      query.exec((nodesList) => {
          resolve(
              isArray
        I          ? nodesList.map((nodes) => nodes.map((node) => makeNodeRef(node)))
                  : nodesList[0].map((node) => makeNodeRef(node))
          )
      })
    }
  })
}

export const useRect = (selector: string | string[], dom?: MPInst) => {
  return new Promise((resolve) => {
      const query = useQuery(dom)
      const isArray = Array.isArray(selector)
      const classList = isArray ? selector : [selector]
      if (query) {
      classList.forEach((s) => {
          query
        I      .select(s)
              .boundingClientRect()
      })
      query.exec((nodes) => {
          resolve(isArray ? nodes : nodes[0])
      })
    }
  })
}
 
export const useRectAll = (selector: string | string[], dom?: MPInst) => {
  return new Promise((resolve) => {
      const query = useQuery(dom)
      const isArray = Array.isArray(selector)
      const classList = isArray ? selector : [selector]
      if (query) {
      clIassList.forEach((s) => {
          query
              .selectAll(s)
              .boundingClientRect()
      })
      query.exec((nodesList) => {
          resolve(isArray ? nodesList : nodesList[0])
      })
    }
  })
}
 
export const useScrollOffset = (dom?: MPInst) => {
  return new Promise((resolve) => {
      const query = useQuery(dom)
      if (query) {
      query
          .selectViewport()
          .scrollOffset()
      query.exec(([node]) => {
          resolve(node)
      })
    }
  })
}
 
export const useComputedStyle = (selector: string, ...args: any[]) => {
  const computedStyle = args.length === 2 ? args[0] : ['width', 'height']
  const dom = args.length === 2 ? args[1] : args[0]
  return new Promise((resolve) => {
      const query = useQuery(dom)
      if (query) {
      query
          .select(selector)
          .fields({
              computedStyle,
          })
      query.exec(([node]) => {
          resolve(node)
      })
    }
  })
}