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 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 | 1x 1x 1x 1x 1x 1x 11x 11x 1x 1x 1x 1x 1x 1x 1x 2x 2x 2x 2x 2x 1x 2x 2x 2x 2x 2x 1x 2x 2x 2x 2x 2x 1x 2x 2x 2x 2x 2x 1x 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 { MiniprogramDOMRect, MiniprogramElement, MiniprogramPublicInstance } from '../../types' /** * 查询节点信息的对象 * * @param {MiniprogramPublicInstance} [instance=getCurrentPage()] 小程序页面或组件的实例对象 * @return {*} */ function useQuery (instance: MiniprogramPublicInstance = getCurrentPage()): WechatMiniprogram.SelectorQuery { if (!canUseMP()) { Ereturn null } return !!instance ? miniprogramThis?.createSelectorQuery?.().in(instance) : miniprogramThis?.createSelectorQuery?.() } /** * 获取匹配指定选择器的第一个元素 * * @see https://developers.weixin.qq.com/miniprogram/dev/api/wxml/SelectorQuery.select.html * @param {string} selector 在当前页面下选择第一个匹配选择器 selector 的节点 * @param {MiniprogramPublicInstance} instance 小程序页面或组件的实例对象 * @return {*} {(MiniprogramElement | null)} */ function useSelector (selector: string, instance?: MiniprogramPublicInstance): MiniprogramElement | null { return canUseMP() ? useQuery(instance).select(selector) : null } /** * 获取匹配指定选择器的所有元素 * * @see https://developers.weixin.qq.com/miniprogram/dev/api/wxml/SelectorQuery.selectAll.html * @param {string} selector 在当前页面下选择匹配选择器 selector 的所有节点。 * @param {MiniprogramPublicInstance} instance 小程序页面或组件的实例对象 * @return {*} {(MiniprogramElement | null)} */ function useSelectorAll (selector: string, instance?: MiniprogramPublicInstance): MiniprogramElement | null { return canUseMP() ? useQuery(instance).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: string dataset: Record<string, any> mark: Record<string, any> top: number right: number bottom: number left: number scrollHeight: number scrollLeft: number scrollTop: number scrollWidth: number node: Record<string, any> } export interface MiniprogramScrollOffset { /** 节点的 ID */ id: string /** 节点的 dataset */ dataset: Record<string, any> scrollLeft: number scrollTop: number } export interface MiniprogramNodeRef extends MiniprogramDOMRect, MiniprogramScrollOffset { mark: Record<string, any> offsetWidth: number offsetHeight: number clientLeft: number clientTop: number clientWidth: number clientHeight: number scrollHeight: number // scrollLeft: number // scrollTop: number scrollWidth: number node: Record<string, any> } I const makeNodeRef = (node: NodeRef): MiniprogramNodeRef => { 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, left: node.left, width: offsetWidth, height: offsetHeight, x: node.left, y: node.top, offsetWidth, offsetHeight, clientLeft: borderLeftWidth, clIientTop: borderTopWidth, clientWidth, clientHeight, scrollHeight: node.scrollHeight, scrollLeft: node.scrollLeft, scrollTop: node.scrollTop, scrollWidth: node.scrollWidth, node: node.node, } } function useRef(selector: string, instance?: MiniprogramPublicInstance): Promise<MiniprogramNodeRef> function useRef(selector: string[], instance?: MiniprogramPublicInstance): Promise<MiniprogramNodeRef[]> /** * 获取第一个节点的相关信息 * * @see https://developers.weixin.qq.com/miniprogram/dev/api/wxml/NodesRef.fields.html * @param {(string | string[])} selector 在当前页面下选择第一个匹配选择器 selector 的节点。 * @param {MiniprogramPublicInstance} [instance] 小程序页面或组件的实例对象 * @return {*} {(Promise<MiniprogramNodeRef | MiniprogramNodeRef[]>)} */ function useRef (selector: string | string[], instance?: MiniprogramPublicInstance): Promise<MiniprogramNodeRef | MiniprogramNodeRef[]> { return new Promise((resolve) => { const query = useQuery(instance) const isArray = Array.isArray(selector) coInst 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]) ) }) } }) } function useRefAll(selector: string, instance?: MiniprogramPublicInstance): Promise<MiniprogramNodeRef[]> function useRefAll(selector: string[], instance?: MiniprogramPublicInstance): Promise<MiniprogramNodeRef[][]> /** * 获取所有节点的相关信息 * * @see https://developers.weixin.qq.com/miniprogram/dev/api/wxml/NodesRef.fields.html * @paraIm {(string | string[])} selector 在当前页面下选择匹配选择器 selector 的所有节点。 * @param {MiniprogramPublicInstance} [instance] 小程序页面或组件的实例对象 * @return {*} {(Promise<MiniprogramNodeRef[] | MiniprogramNodeRef[][]>)} */ function useRefAll (selector: string | string[], instance?: MiniprogramPublicInstance): Promise<MiniprogramNodeRef[] | MiniprogramNodeRef[][]> { return new Promise((resolve) => { const query = useQuery(instance) 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 ? nodesList.map((nodes) => nodes.map((node) => makeNodeRef(node))) : nodesList[0].map((node) => makeNodeRef(node)) ) }) } })I } function useRect(selector: string, instance?: MiniprogramPublicInstance): Promise<MiniprogramDOMRect> function useRect(selector: string[], instance?: MiniprogramPublicInstance): Promise<MiniprogramDOMRect[]> /** * 添加第一个节点的布局位置的查询请求 * * @see https://developers.weixin.qq.com/miniprogram/dev/api/wxml/NodesRef.boundingClientRect.html * @param {(string | string[])} selector 在当前页面下选择第一个匹配选择器 selector 的节点。 * @param {MiniprogramPublicInstance} [instance] 小程序页面或组件的实例对象 * @return {*} {(Promise<MiniprogramDOMRect | MiniprogramDOMRect[]>)} */ function useRect (selector: string | string[], instance?: MiniprogramPublicInstance): Promise<MiniprogramDOMRect | MiniprogramDOMRect[]> { return new Promise((resolve) => { const query = useQuery(instance) const isArray = Array.isArray(selector) const classList = isArray ? selector : [selector] if (query) { classList.forEach((s) => { query .select(s) .boundingClientRect() }) E query.exec((nodes) => { resolve(isArray ? nodes : nodes[0]) }) } }) } I function useRectAll(selector: string, instance?: MiniprogramPublicInstance): Promise<MiniprogramDOMRect[]> function useRectAll(selector: string[], instance?: MiniprogramPublicInstance): Promise<MiniprogramDOMRect[][]> /** * 添加所有节点的布局位置的查询请求 * * @param {(string | string[])} selector 在当前页面下选择匹配选择器 selector 的所有节点。 * @param {MiniprogramPublicInstance} [instance] 小程序页面或组件的实例对象 * @return {*} {(Promise<MiniprogramDOMRect[] | MiniprogramDOMRect[][]>)} */ function useRectAll (selector: string | string[], instance?: MiniprogramPublicInstance): Promise<MiniprogramDOMRect[] | MiniprogramDOMRect[][]> { return new Promise((resolve) => { const query = useQuery(instance) const isArray = Array.isArray(selector) const classList = isArray ? selector : [selector] if (query) { classList.forEach((s) => { query .selectAll(s) .boundingClientRect() }) query.exec((nodesList) => { resolve(isArray ? nodesList : nodesList[0]) }) } }) } /** * 添加节点的滚动位置查询请求。以像素为单位。节点必须是 scroll-view 或者 viewport,返回 NodesRef 对应的 SelectorQuery。 * * @see https://developers.weixin.qq.com/miniprogram/dev/api/wxml/NodesRef.scrollOffset.html * @param {MiniprogramPublicInstance} [instance] 小程序页面或组件的实例对象 * @return {*} {Promise<MiniprogramScrollOffset>} */ function useScrollOffset (instance?: MiniprogramPublicInstance): Promise<MiniprogramScrollOffset> { return new Promise((resolve) => { const query = useQuery(instance) if (query) { query .selectViewport() .scrollOffset() query.exec(([node]) => { resolve(node) }) } }) } function useComputedStyle (selector: string, instance?: MiniprogramPublicInstance): Promise<{ [key in keyof Partial<CSSStyleDeclaration>]: any }> /** * 指定样式名列表,返回节点对应样式名的当前值 * * @see https://developers.weixin.qq.com/miniprogram/dev/api/wxml/NodesRef.fields.html * @param {string} selector 在当前页面下选择第一个匹配选择器 selector 的节点。 * @param {...any[]} args * @return {*} {Promise<{ [key in keyof Partial<CSSStyleDeclaration>]: any }>} */ function useComputedStyle (selector: string, ...args: any[]): Promise<{ [key in keyof Partial<CSSStyleDeclaration>]: any }> { const [ computedStyle, instance ] = args const opts: { computedStyle: (keyof Partial<CSSStyleDeclaration>)[] instance: MiniprogramPublicInstance } = { computedStyle, instance, } if (instance === undefined) { opts.computedStyle = ['width', 'height'] opts.instance = computedStyle as unknown as MiniprogramPublicInstance } return new Promise((resolve) => { const query = useQuery(opts.instance) if (query) { query .select(selector) .fields({ computedStyle: opts.computedStyle as unknown as string[], }) query.exec(([node]) => { resolve(node) }) } }) } export { useQuery, useSelector, useSelectorAll, useRef, useRefAll, useRect, useRectAll, useScrollOffset, useComputedStyle, } |