util.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. var __defProp = Object.defineProperty;
  2. var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
  3. var __getOwnPropNames = Object.getOwnPropertyNames;
  4. var __hasOwnProp = Object.prototype.hasOwnProperty;
  5. var __export = (target, all) => {
  6. for (var name in all)
  7. __defProp(target, name, { get: all[name], enumerable: true });
  8. };
  9. var __copyProps = (to, from, except, desc) => {
  10. if (from && typeof from === "object" || typeof from === "function") {
  11. for (let key of __getOwnPropNames(from))
  12. if (!__hasOwnProp.call(to, key) && key !== except)
  13. __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  14. }
  15. return to;
  16. };
  17. var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
  18. var stdin_exports = {};
  19. __export(stdin_exports, {
  20. ImageCache: () => ImageCache,
  21. getBestSelectionFromSrcset: () => getBestSelectionFromSrcset,
  22. getDPR: () => getDPR,
  23. hasIntersectionObserver: () => hasIntersectionObserver,
  24. loadImageAsync: () => loadImageAsync,
  25. modeType: () => modeType,
  26. off: () => off,
  27. on: () => on,
  28. remove: () => remove,
  29. supportWebp: () => supportWebp,
  30. throttle: () => throttle
  31. });
  32. module.exports = __toCommonJS(stdin_exports);
  33. var import_use = require("@vant/use");
  34. const hasIntersectionObserver = import_use.inBrowser && "IntersectionObserver" in window && "IntersectionObserverEntry" in window && "intersectionRatio" in window.IntersectionObserverEntry.prototype;
  35. const modeType = {
  36. event: "event",
  37. observer: "observer"
  38. };
  39. function remove(arr, item) {
  40. if (!arr.length) return;
  41. const index = arr.indexOf(item);
  42. if (index > -1) return arr.splice(index, 1);
  43. }
  44. function getBestSelectionFromSrcset(el, scale) {
  45. if (el.tagName !== "IMG" || !el.getAttribute("data-srcset")) return;
  46. let options = el.getAttribute("data-srcset");
  47. const container = el.parentNode;
  48. const containerWidth = container.offsetWidth * scale;
  49. let spaceIndex;
  50. let tmpSrc;
  51. let tmpWidth;
  52. options = options.trim().split(",");
  53. const result = options.map((item) => {
  54. item = item.trim();
  55. spaceIndex = item.lastIndexOf(" ");
  56. if (spaceIndex === -1) {
  57. tmpSrc = item;
  58. tmpWidth = 999998;
  59. } else {
  60. tmpSrc = item.substr(0, spaceIndex);
  61. tmpWidth = parseInt(
  62. item.substr(spaceIndex + 1, item.length - spaceIndex - 2),
  63. 10
  64. );
  65. }
  66. return [tmpWidth, tmpSrc];
  67. });
  68. result.sort((a, b) => {
  69. if (a[0] < b[0]) {
  70. return 1;
  71. }
  72. if (a[0] > b[0]) {
  73. return -1;
  74. }
  75. if (a[0] === b[0]) {
  76. if (b[1].indexOf(".webp", b[1].length - 5) !== -1) {
  77. return 1;
  78. }
  79. if (a[1].indexOf(".webp", a[1].length - 5) !== -1) {
  80. return -1;
  81. }
  82. }
  83. return 0;
  84. });
  85. let bestSelectedSrc = "";
  86. let tmpOption;
  87. for (let i = 0; i < result.length; i++) {
  88. tmpOption = result[i];
  89. bestSelectedSrc = tmpOption[1];
  90. const next = result[i + 1];
  91. if (next && next[0] < containerWidth) {
  92. bestSelectedSrc = tmpOption[1];
  93. break;
  94. } else if (!next) {
  95. bestSelectedSrc = tmpOption[1];
  96. break;
  97. }
  98. }
  99. return bestSelectedSrc;
  100. }
  101. const getDPR = (scale = 1) => import_use.inBrowser ? window.devicePixelRatio || scale : scale;
  102. function supportWebp() {
  103. if (!import_use.inBrowser) return false;
  104. let support = true;
  105. try {
  106. const elem = document.createElement("canvas");
  107. if (elem.getContext && elem.getContext("2d")) {
  108. support = elem.toDataURL("image/webp").indexOf("data:image/webp") === 0;
  109. }
  110. } catch (err) {
  111. support = false;
  112. }
  113. return support;
  114. }
  115. function throttle(action, delay) {
  116. let timeout = null;
  117. let lastRun = 0;
  118. return function(...args) {
  119. if (timeout) {
  120. return;
  121. }
  122. const elapsed = Date.now() - lastRun;
  123. const runCallback = () => {
  124. lastRun = Date.now();
  125. timeout = false;
  126. action.apply(this, args);
  127. };
  128. if (elapsed >= delay) {
  129. runCallback();
  130. } else {
  131. timeout = setTimeout(runCallback, delay);
  132. }
  133. };
  134. }
  135. function on(el, type, func) {
  136. el.addEventListener(type, func, {
  137. capture: false,
  138. passive: true
  139. });
  140. }
  141. function off(el, type, func) {
  142. el.removeEventListener(type, func, false);
  143. }
  144. const loadImageAsync = (item, resolve, reject) => {
  145. const image = new Image();
  146. if (!item || !item.src) {
  147. return reject(new Error("image src is required"));
  148. }
  149. image.src = item.src;
  150. if (item.cors) {
  151. image.crossOrigin = item.cors;
  152. }
  153. image.onload = () => resolve({
  154. naturalHeight: image.naturalHeight,
  155. naturalWidth: image.naturalWidth,
  156. src: image.src
  157. });
  158. image.onerror = (e) => reject(e);
  159. };
  160. class ImageCache {
  161. constructor({ max }) {
  162. this.options = {
  163. max: max || 100
  164. };
  165. this.caches = [];
  166. }
  167. has(key) {
  168. return this.caches.indexOf(key) > -1;
  169. }
  170. add(key) {
  171. if (this.has(key)) return;
  172. this.caches.push(key);
  173. if (this.caches.length > this.options.max) {
  174. this.free();
  175. }
  176. }
  177. free() {
  178. this.caches.shift();
  179. }
  180. }