util.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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)
  41. return;
  42. const index = arr.indexOf(item);
  43. if (index > -1)
  44. return arr.splice(index, 1);
  45. }
  46. function getBestSelectionFromSrcset(el, scale) {
  47. if (el.tagName !== "IMG" || !el.getAttribute("data-srcset"))
  48. return;
  49. let options = el.getAttribute("data-srcset");
  50. const container = el.parentNode;
  51. const containerWidth = container.offsetWidth * scale;
  52. let spaceIndex;
  53. let tmpSrc;
  54. let tmpWidth;
  55. options = options.trim().split(",");
  56. const result = options.map((item) => {
  57. item = item.trim();
  58. spaceIndex = item.lastIndexOf(" ");
  59. if (spaceIndex === -1) {
  60. tmpSrc = item;
  61. tmpWidth = 999998;
  62. } else {
  63. tmpSrc = item.substr(0, spaceIndex);
  64. tmpWidth = parseInt(
  65. item.substr(spaceIndex + 1, item.length - spaceIndex - 2),
  66. 10
  67. );
  68. }
  69. return [tmpWidth, tmpSrc];
  70. });
  71. result.sort((a, b) => {
  72. if (a[0] < b[0]) {
  73. return 1;
  74. }
  75. if (a[0] > b[0]) {
  76. return -1;
  77. }
  78. if (a[0] === b[0]) {
  79. if (b[1].indexOf(".webp", b[1].length - 5) !== -1) {
  80. return 1;
  81. }
  82. if (a[1].indexOf(".webp", a[1].length - 5) !== -1) {
  83. return -1;
  84. }
  85. }
  86. return 0;
  87. });
  88. let bestSelectedSrc = "";
  89. let tmpOption;
  90. for (let i = 0; i < result.length; i++) {
  91. tmpOption = result[i];
  92. bestSelectedSrc = tmpOption[1];
  93. const next = result[i + 1];
  94. if (next && next[0] < containerWidth) {
  95. bestSelectedSrc = tmpOption[1];
  96. break;
  97. } else if (!next) {
  98. bestSelectedSrc = tmpOption[1];
  99. break;
  100. }
  101. }
  102. return bestSelectedSrc;
  103. }
  104. const getDPR = (scale = 1) => import_use.inBrowser ? window.devicePixelRatio || scale : scale;
  105. function supportWebp() {
  106. if (!import_use.inBrowser)
  107. return false;
  108. let support = true;
  109. try {
  110. const elem = document.createElement("canvas");
  111. if (elem.getContext && elem.getContext("2d")) {
  112. support = elem.toDataURL("image/webp").indexOf("data:image/webp") === 0;
  113. }
  114. } catch (err) {
  115. support = false;
  116. }
  117. return support;
  118. }
  119. function throttle(action, delay) {
  120. let timeout = null;
  121. let lastRun = 0;
  122. return function(...args) {
  123. if (timeout) {
  124. return;
  125. }
  126. const elapsed = Date.now() - lastRun;
  127. const runCallback = () => {
  128. lastRun = Date.now();
  129. timeout = false;
  130. action.apply(this, args);
  131. };
  132. if (elapsed >= delay) {
  133. runCallback();
  134. } else {
  135. timeout = setTimeout(runCallback, delay);
  136. }
  137. };
  138. }
  139. function on(el, type, func) {
  140. el.addEventListener(type, func, {
  141. capture: false,
  142. passive: true
  143. });
  144. }
  145. function off(el, type, func) {
  146. el.removeEventListener(type, func, false);
  147. }
  148. const loadImageAsync = (item, resolve, reject) => {
  149. const image = new Image();
  150. if (!item || !item.src) {
  151. return reject(new Error("image src is required"));
  152. }
  153. image.src = item.src;
  154. if (item.cors) {
  155. image.crossOrigin = item.cors;
  156. }
  157. image.onload = () => resolve({
  158. naturalHeight: image.naturalHeight,
  159. naturalWidth: image.naturalWidth,
  160. src: image.src
  161. });
  162. image.onerror = (e) => reject(e);
  163. };
  164. class ImageCache {
  165. constructor({ max }) {
  166. this.options = {
  167. max: max || 100
  168. };
  169. this.caches = [];
  170. }
  171. has(key) {
  172. return this.caches.indexOf(key) > -1;
  173. }
  174. add(key) {
  175. if (this.has(key))
  176. return;
  177. this.caches.push(key);
  178. if (this.caches.length > this.options.max) {
  179. this.free();
  180. }
  181. }
  182. free() {
  183. this.caches.shift();
  184. }
  185. }