UNPKG

@nutui/nutui-react-taro

Version:

京东风格的轻量级移动端 React 组件库,支持一套代码生成 H5 和小程序

97 lines (96 loc) 2.2 kB
import { a as __awaiter } from "./tslib.es6-iWu3F_1J.js"; import { createSelectorQuery } from "@tarojs/taro"; class MiniLru { constructor(capacity) { if (capacity <= 0) { throw new Error("Cache capacity must be a positive number"); } this.cache = /* @__PURE__ */ new Map(); this.capacity = capacity; } get(key) { if (this.cache.has(key)) { const value = this.cache.get(key); this.cache.delete(key); this.cache.set(key, value); return value; } return null; } set(key, value) { if (this.cache.has(key)) { this.cache.delete(key); } else if (this.cache.size >= this.capacity) { this.cache.delete(this.cache.keys().next().value); } this.cache.set(key, value); } has(key) { return this.cache.has(key); } } const inBrowser = typeof document !== "undefined" && !!document.scripts; function isWindow(val) { return val === window; } const getRect = (elementRef) => { const element = elementRef; if (isWindow(element)) { const width = element.innerWidth; const height = element.innerHeight; return { top: 0, left: 0, right: width, bottom: height, width, height }; } if (element && element.getBoundingClientRect) { return element.getBoundingClientRect(); } return { top: 0, left: 0, right: 0, bottom: 0, width: 0, height: 0 }; }; const lru = new MiniLru(10); function makeRect(width, height) { return { top: 0, left: 0, right: width, bottom: height, width, height }; } const getRectByTaro = (element) => __awaiter(void 0, void 0, void 0, function* () { if (element) { if (inBrowser) { return Promise.resolve(getRect(element)); } return new Promise((resolve, reject) => { if (lru.has(element)) { resolve(lru.get(element)); return; } createSelectorQuery().select(`#${element.uid}`).boundingClientRect().exec(([rects]) => { if (rects) { lru.set(element, rects); } resolve(rects); }); }); } return Promise.resolve(makeRect(0, 0)); }); export { getRect as a, getRectByTaro as g };