123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185 |
- var __defProp = Object.defineProperty;
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
- var __getOwnPropNames = Object.getOwnPropertyNames;
- var __hasOwnProp = Object.prototype.hasOwnProperty;
- var __export = (target, all) => {
- for (var name in all)
- __defProp(target, name, { get: all[name], enumerable: true });
- };
- var __copyProps = (to, from, except, desc) => {
- if (from && typeof from === "object" || typeof from === "function") {
- for (let key of __getOwnPropNames(from))
- if (!__hasOwnProp.call(to, key) && key !== except)
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
- }
- return to;
- };
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
- var stdin_exports = {};
- __export(stdin_exports, {
- ImageCache: () => ImageCache,
- getBestSelectionFromSrcset: () => getBestSelectionFromSrcset,
- getDPR: () => getDPR,
- hasIntersectionObserver: () => hasIntersectionObserver,
- loadImageAsync: () => loadImageAsync,
- modeType: () => modeType,
- off: () => off,
- on: () => on,
- remove: () => remove,
- supportWebp: () => supportWebp,
- throttle: () => throttle
- });
- module.exports = __toCommonJS(stdin_exports);
- var import_use = require("@vant/use");
- const hasIntersectionObserver = import_use.inBrowser && "IntersectionObserver" in window && "IntersectionObserverEntry" in window && "intersectionRatio" in window.IntersectionObserverEntry.prototype;
- const modeType = {
- event: "event",
- observer: "observer"
- };
- function remove(arr, item) {
- if (!arr.length)
- return;
- const index = arr.indexOf(item);
- if (index > -1)
- return arr.splice(index, 1);
- }
- function getBestSelectionFromSrcset(el, scale) {
- if (el.tagName !== "IMG" || !el.getAttribute("data-srcset"))
- return;
- let options = el.getAttribute("data-srcset");
- const container = el.parentNode;
- const containerWidth = container.offsetWidth * scale;
- let spaceIndex;
- let tmpSrc;
- let tmpWidth;
- options = options.trim().split(",");
- const result = options.map((item) => {
- item = item.trim();
- spaceIndex = item.lastIndexOf(" ");
- if (spaceIndex === -1) {
- tmpSrc = item;
- tmpWidth = 999998;
- } else {
- tmpSrc = item.substr(0, spaceIndex);
- tmpWidth = parseInt(
- item.substr(spaceIndex + 1, item.length - spaceIndex - 2),
- 10
- );
- }
- return [tmpWidth, tmpSrc];
- });
- result.sort((a, b) => {
- if (a[0] < b[0]) {
- return 1;
- }
- if (a[0] > b[0]) {
- return -1;
- }
- if (a[0] === b[0]) {
- if (b[1].indexOf(".webp", b[1].length - 5) !== -1) {
- return 1;
- }
- if (a[1].indexOf(".webp", a[1].length - 5) !== -1) {
- return -1;
- }
- }
- return 0;
- });
- let bestSelectedSrc = "";
- let tmpOption;
- for (let i = 0; i < result.length; i++) {
- tmpOption = result[i];
- bestSelectedSrc = tmpOption[1];
- const next = result[i + 1];
- if (next && next[0] < containerWidth) {
- bestSelectedSrc = tmpOption[1];
- break;
- } else if (!next) {
- bestSelectedSrc = tmpOption[1];
- break;
- }
- }
- return bestSelectedSrc;
- }
- const getDPR = (scale = 1) => import_use.inBrowser ? window.devicePixelRatio || scale : scale;
- function supportWebp() {
- if (!import_use.inBrowser)
- return false;
- let support = true;
- try {
- const elem = document.createElement("canvas");
- if (elem.getContext && elem.getContext("2d")) {
- support = elem.toDataURL("image/webp").indexOf("data:image/webp") === 0;
- }
- } catch (err) {
- support = false;
- }
- return support;
- }
- function throttle(action, delay) {
- let timeout = null;
- let lastRun = 0;
- return function(...args) {
- if (timeout) {
- return;
- }
- const elapsed = Date.now() - lastRun;
- const runCallback = () => {
- lastRun = Date.now();
- timeout = false;
- action.apply(this, args);
- };
- if (elapsed >= delay) {
- runCallback();
- } else {
- timeout = setTimeout(runCallback, delay);
- }
- };
- }
- function on(el, type, func) {
- el.addEventListener(type, func, {
- capture: false,
- passive: true
- });
- }
- function off(el, type, func) {
- el.removeEventListener(type, func, false);
- }
- const loadImageAsync = (item, resolve, reject) => {
- const image = new Image();
- if (!item || !item.src) {
- return reject(new Error("image src is required"));
- }
- image.src = item.src;
- if (item.cors) {
- image.crossOrigin = item.cors;
- }
- image.onload = () => resolve({
- naturalHeight: image.naturalHeight,
- naturalWidth: image.naturalWidth,
- src: image.src
- });
- image.onerror = (e) => reject(e);
- };
- class ImageCache {
- constructor({ max }) {
- this.options = {
- max: max || 100
- };
- this.caches = [];
- }
- has(key) {
- return this.caches.indexOf(key) > -1;
- }
- add(key) {
- if (this.has(key))
- return;
- this.caches.push(key);
- if (this.caches.length > this.options.max) {
- this.free();
- }
- }
- free() {
- this.caches.shift();
- }
- }
|