index.cjs.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512
  1. "use strict";
  2. var __defProp = Object.defineProperty;
  3. var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
  4. var __getOwnPropNames = Object.getOwnPropertyNames;
  5. var __hasOwnProp = Object.prototype.hasOwnProperty;
  6. var __export = (target, all) => {
  7. for (var name in all)
  8. __defProp(target, name, { get: all[name], enumerable: true });
  9. };
  10. var __copyProps = (to, from, except, desc) => {
  11. if (from && typeof from === "object" || typeof from === "function") {
  12. for (let key of __getOwnPropNames(from))
  13. if (!__hasOwnProp.call(to, key) && key !== except)
  14. __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  15. }
  16. return to;
  17. };
  18. var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
  19. // src/index.ts
  20. var src_exports = {};
  21. __export(src_exports, {
  22. CUSTOM_FIELD_INJECTION_KEY: () => CUSTOM_FIELD_INJECTION_KEY,
  23. cancelRaf: () => cancelRaf,
  24. doubleRaf: () => doubleRaf,
  25. flattenVNodes: () => flattenVNodes,
  26. getScrollParent: () => getScrollParent,
  27. inBrowser: () => inBrowser,
  28. onMountedOrActivated: () => onMountedOrActivated,
  29. raf: () => raf,
  30. sortChildren: () => sortChildren,
  31. supportsPassive: () => supportsPassive,
  32. useChildren: () => useChildren,
  33. useClickAway: () => useClickAway,
  34. useCountDown: () => useCountDown,
  35. useCustomFieldValue: () => useCustomFieldValue,
  36. useEventListener: () => useEventListener,
  37. usePageVisibility: () => usePageVisibility,
  38. useParent: () => useParent,
  39. useRaf: () => useRaf,
  40. useRect: () => useRect,
  41. useScrollParent: () => useScrollParent,
  42. useToggle: () => useToggle,
  43. useWindowSize: () => useWindowSize
  44. });
  45. module.exports = __toCommonJS(src_exports);
  46. // src/utils.ts
  47. var inBrowser = typeof window !== "undefined";
  48. var supportsPassive = true;
  49. function raf(fn) {
  50. return inBrowser ? requestAnimationFrame(fn) : -1;
  51. }
  52. function cancelRaf(id) {
  53. if (inBrowser) {
  54. cancelAnimationFrame(id);
  55. }
  56. }
  57. function doubleRaf(fn) {
  58. raf(() => raf(fn));
  59. }
  60. // src/useRect/index.ts
  61. var import_vue = require("vue");
  62. var isWindow = (val) => val === window;
  63. var makeDOMRect = (width2, height2) => ({
  64. top: 0,
  65. left: 0,
  66. right: width2,
  67. bottom: height2,
  68. width: width2,
  69. height: height2
  70. });
  71. var useRect = (elementOrRef) => {
  72. const element = (0, import_vue.unref)(elementOrRef);
  73. if (isWindow(element)) {
  74. const width2 = element.innerWidth;
  75. const height2 = element.innerHeight;
  76. return makeDOMRect(width2, height2);
  77. }
  78. if (element == null ? void 0 : element.getBoundingClientRect) {
  79. return element.getBoundingClientRect();
  80. }
  81. return makeDOMRect(0, 0);
  82. };
  83. // src/useToggle/index.ts
  84. var import_vue2 = require("vue");
  85. function useToggle(defaultValue = false) {
  86. const state = (0, import_vue2.ref)(defaultValue);
  87. const toggle = (value = !state.value) => {
  88. state.value = value;
  89. };
  90. return [state, toggle];
  91. }
  92. // src/useRelation/useParent.ts
  93. var import_vue3 = require("vue");
  94. function useParent(key) {
  95. const parent = (0, import_vue3.inject)(key, null);
  96. if (parent) {
  97. const instance = (0, import_vue3.getCurrentInstance)();
  98. const { link, unlink, internalChildren } = parent;
  99. link(instance);
  100. (0, import_vue3.onUnmounted)(() => unlink(instance));
  101. const index = (0, import_vue3.computed)(() => internalChildren.indexOf(instance));
  102. return {
  103. parent,
  104. index
  105. };
  106. }
  107. return {
  108. parent: null,
  109. index: (0, import_vue3.ref)(-1)
  110. };
  111. }
  112. // src/useRelation/useChildren.ts
  113. var import_vue4 = require("vue");
  114. function flattenVNodes(children) {
  115. const result = [];
  116. const traverse = (children2) => {
  117. if (Array.isArray(children2)) {
  118. children2.forEach((child) => {
  119. var _a;
  120. if ((0, import_vue4.isVNode)(child)) {
  121. result.push(child);
  122. if ((_a = child.component) == null ? void 0 : _a.subTree) {
  123. result.push(child.component.subTree);
  124. traverse(child.component.subTree.children);
  125. }
  126. if (child.children) {
  127. traverse(child.children);
  128. }
  129. }
  130. });
  131. }
  132. };
  133. traverse(children);
  134. return result;
  135. }
  136. var findVNodeIndex = (vnodes, vnode) => {
  137. const index = vnodes.indexOf(vnode);
  138. if (index === -1) {
  139. return vnodes.findIndex(
  140. (item) => vnode.key !== void 0 && vnode.key !== null && item.type === vnode.type && item.key === vnode.key
  141. );
  142. }
  143. return index;
  144. };
  145. function sortChildren(parent, publicChildren, internalChildren) {
  146. const vnodes = flattenVNodes(parent.subTree.children);
  147. internalChildren.sort(
  148. (a, b) => findVNodeIndex(vnodes, a.vnode) - findVNodeIndex(vnodes, b.vnode)
  149. );
  150. const orderedPublicChildren = internalChildren.map((item) => item.proxy);
  151. publicChildren.sort((a, b) => {
  152. const indexA = orderedPublicChildren.indexOf(a);
  153. const indexB = orderedPublicChildren.indexOf(b);
  154. return indexA - indexB;
  155. });
  156. }
  157. function useChildren(key) {
  158. const publicChildren = (0, import_vue4.reactive)([]);
  159. const internalChildren = (0, import_vue4.reactive)([]);
  160. const parent = (0, import_vue4.getCurrentInstance)();
  161. const linkChildren = (value) => {
  162. const link = (child) => {
  163. if (child.proxy) {
  164. internalChildren.push(child);
  165. publicChildren.push(child.proxy);
  166. sortChildren(parent, publicChildren, internalChildren);
  167. }
  168. };
  169. const unlink = (child) => {
  170. const index = internalChildren.indexOf(child);
  171. publicChildren.splice(index, 1);
  172. internalChildren.splice(index, 1);
  173. };
  174. (0, import_vue4.provide)(
  175. key,
  176. Object.assign(
  177. {
  178. link,
  179. unlink,
  180. children: publicChildren,
  181. internalChildren
  182. },
  183. value
  184. )
  185. );
  186. };
  187. return {
  188. children: publicChildren,
  189. linkChildren
  190. };
  191. }
  192. // src/useCountDown/index.ts
  193. var import_vue5 = require("vue");
  194. var SECOND = 1e3;
  195. var MINUTE = 60 * SECOND;
  196. var HOUR = 60 * MINUTE;
  197. var DAY = 24 * HOUR;
  198. function parseTime(time) {
  199. const days = Math.floor(time / DAY);
  200. const hours = Math.floor(time % DAY / HOUR);
  201. const minutes = Math.floor(time % HOUR / MINUTE);
  202. const seconds = Math.floor(time % MINUTE / SECOND);
  203. const milliseconds = Math.floor(time % SECOND);
  204. return {
  205. total: time,
  206. days,
  207. hours,
  208. minutes,
  209. seconds,
  210. milliseconds
  211. };
  212. }
  213. function isSameSecond(time1, time2) {
  214. return Math.floor(time1 / 1e3) === Math.floor(time2 / 1e3);
  215. }
  216. function useCountDown(options) {
  217. let rafId;
  218. let endTime;
  219. let counting;
  220. let deactivated;
  221. const remain = (0, import_vue5.ref)(options.time);
  222. const current = (0, import_vue5.computed)(() => parseTime(remain.value));
  223. const pause = () => {
  224. counting = false;
  225. cancelRaf(rafId);
  226. };
  227. const getCurrentRemain = () => Math.max(endTime - Date.now(), 0);
  228. const setRemain = (value) => {
  229. var _a, _b;
  230. remain.value = value;
  231. (_a = options.onChange) == null ? void 0 : _a.call(options, current.value);
  232. if (value === 0) {
  233. pause();
  234. (_b = options.onFinish) == null ? void 0 : _b.call(options);
  235. }
  236. };
  237. const microTick = () => {
  238. rafId = raf(() => {
  239. if (counting) {
  240. setRemain(getCurrentRemain());
  241. if (remain.value > 0) {
  242. microTick();
  243. }
  244. }
  245. });
  246. };
  247. const macroTick = () => {
  248. rafId = raf(() => {
  249. if (counting) {
  250. const remainRemain = getCurrentRemain();
  251. if (!isSameSecond(remainRemain, remain.value) || remainRemain === 0) {
  252. setRemain(remainRemain);
  253. }
  254. if (remain.value > 0) {
  255. macroTick();
  256. }
  257. }
  258. });
  259. };
  260. const tick = () => {
  261. if (!inBrowser) {
  262. return;
  263. }
  264. if (options.millisecond) {
  265. microTick();
  266. } else {
  267. macroTick();
  268. }
  269. };
  270. const start = () => {
  271. if (!counting) {
  272. endTime = Date.now() + remain.value;
  273. counting = true;
  274. tick();
  275. }
  276. };
  277. const reset = (totalTime = options.time) => {
  278. pause();
  279. remain.value = totalTime;
  280. };
  281. (0, import_vue5.onBeforeUnmount)(pause);
  282. (0, import_vue5.onActivated)(() => {
  283. if (deactivated) {
  284. counting = true;
  285. deactivated = false;
  286. tick();
  287. }
  288. });
  289. (0, import_vue5.onDeactivated)(() => {
  290. if (counting) {
  291. pause();
  292. deactivated = true;
  293. }
  294. });
  295. return {
  296. start,
  297. pause,
  298. reset,
  299. current
  300. };
  301. }
  302. // src/useClickAway/index.ts
  303. var import_vue8 = require("vue");
  304. // src/useEventListener/index.ts
  305. var import_vue7 = require("vue");
  306. // src/onMountedOrActivated/index.ts
  307. var import_vue6 = require("vue");
  308. function onMountedOrActivated(hook) {
  309. let mounted;
  310. (0, import_vue6.onMounted)(() => {
  311. hook();
  312. (0, import_vue6.nextTick)(() => {
  313. mounted = true;
  314. });
  315. });
  316. (0, import_vue6.onActivated)(() => {
  317. if (mounted) {
  318. hook();
  319. }
  320. });
  321. }
  322. // src/useEventListener/index.ts
  323. function useEventListener(type, listener, options = {}) {
  324. if (!inBrowser) {
  325. return;
  326. }
  327. const { target = window, passive = false, capture = false } = options;
  328. let cleaned = false;
  329. let attached;
  330. const add = (target2) => {
  331. if (cleaned) {
  332. return;
  333. }
  334. const element = (0, import_vue7.unref)(target2);
  335. if (element && !attached) {
  336. element.addEventListener(type, listener, {
  337. capture,
  338. passive
  339. });
  340. attached = true;
  341. }
  342. };
  343. const remove = (target2) => {
  344. if (cleaned) {
  345. return;
  346. }
  347. const element = (0, import_vue7.unref)(target2);
  348. if (element && attached) {
  349. element.removeEventListener(type, listener, capture);
  350. attached = false;
  351. }
  352. };
  353. (0, import_vue7.onUnmounted)(() => remove(target));
  354. (0, import_vue7.onDeactivated)(() => remove(target));
  355. onMountedOrActivated(() => add(target));
  356. let stopWatch;
  357. if ((0, import_vue7.isRef)(target)) {
  358. stopWatch = (0, import_vue7.watch)(target, (val, oldVal) => {
  359. remove(oldVal);
  360. add(val);
  361. });
  362. }
  363. return () => {
  364. stopWatch == null ? void 0 : stopWatch();
  365. remove(target);
  366. cleaned = true;
  367. };
  368. }
  369. // src/useClickAway/index.ts
  370. function useClickAway(target, listener, options = {}) {
  371. if (!inBrowser) {
  372. return;
  373. }
  374. const { eventName = "click" } = options;
  375. const onClick = (event) => {
  376. const targets = Array.isArray(target) ? target : [target];
  377. const isClickAway = targets.every((item) => {
  378. const element = (0, import_vue8.unref)(item);
  379. return element && !element.contains(event.target);
  380. });
  381. if (isClickAway) {
  382. listener(event);
  383. }
  384. };
  385. useEventListener(eventName, onClick, { target: document });
  386. }
  387. // src/useWindowSize/index.ts
  388. var import_vue9 = require("vue");
  389. var width;
  390. var height;
  391. function useWindowSize() {
  392. if (!width) {
  393. width = (0, import_vue9.ref)(0);
  394. height = (0, import_vue9.ref)(0);
  395. if (inBrowser) {
  396. const update = () => {
  397. width.value = window.innerWidth;
  398. height.value = window.innerHeight;
  399. };
  400. update();
  401. window.addEventListener("resize", update, { passive: true });
  402. window.addEventListener("orientationchange", update, { passive: true });
  403. }
  404. }
  405. return { width, height };
  406. }
  407. // src/useScrollParent/index.ts
  408. var import_vue10 = require("vue");
  409. var overflowScrollReg = /scroll|auto|overlay/i;
  410. var defaultRoot = inBrowser ? window : void 0;
  411. function isElement(node) {
  412. const ELEMENT_NODE_TYPE = 1;
  413. return node.tagName !== "HTML" && node.tagName !== "BODY" && node.nodeType === ELEMENT_NODE_TYPE;
  414. }
  415. function getScrollParent(el, root = defaultRoot) {
  416. let node = el;
  417. while (node && node !== root && isElement(node)) {
  418. const { overflowY } = window.getComputedStyle(node);
  419. if (overflowScrollReg.test(overflowY)) {
  420. return node;
  421. }
  422. node = node.parentNode;
  423. }
  424. return root;
  425. }
  426. function useScrollParent(el, root = defaultRoot) {
  427. const scrollParent = (0, import_vue10.ref)();
  428. (0, import_vue10.onMounted)(() => {
  429. if (el.value) {
  430. scrollParent.value = getScrollParent(el.value, root);
  431. }
  432. });
  433. return scrollParent;
  434. }
  435. // src/usePageVisibility/index.ts
  436. var import_vue11 = require("vue");
  437. var visibility;
  438. function usePageVisibility() {
  439. if (!visibility) {
  440. visibility = (0, import_vue11.ref)("visible");
  441. if (inBrowser) {
  442. const update = () => {
  443. visibility.value = document.hidden ? "hidden" : "visible";
  444. };
  445. update();
  446. window.addEventListener("visibilitychange", update);
  447. }
  448. }
  449. return visibility;
  450. }
  451. // src/useCustomFieldValue/index.ts
  452. var import_vue12 = require("vue");
  453. var CUSTOM_FIELD_INJECTION_KEY = Symbol("van-field");
  454. function useCustomFieldValue(customValue) {
  455. const field = (0, import_vue12.inject)(CUSTOM_FIELD_INJECTION_KEY, null);
  456. if (field && !field.customValue.value) {
  457. field.customValue.value = customValue;
  458. (0, import_vue12.watch)(customValue, () => {
  459. field.resetValidation();
  460. field.validateWithTrigger("onChange");
  461. });
  462. }
  463. }
  464. // src/useRaf/index.ts
  465. function useRaf(fn, options) {
  466. if (inBrowser) {
  467. const { interval = 0, isLoop = false } = options || {};
  468. let start;
  469. let isStopped = false;
  470. let rafId;
  471. const stop = () => {
  472. isStopped = true;
  473. cancelAnimationFrame(rafId);
  474. };
  475. const frameWrapper = (timestamp) => {
  476. if (isStopped)
  477. return;
  478. if (start === void 0) {
  479. start = timestamp;
  480. } else if (timestamp - start > interval) {
  481. fn(timestamp);
  482. start = timestamp;
  483. if (!isLoop) {
  484. stop();
  485. return;
  486. }
  487. }
  488. rafId = requestAnimationFrame(frameWrapper);
  489. };
  490. rafId = requestAnimationFrame(frameWrapper);
  491. return stop;
  492. }
  493. return () => {
  494. };
  495. }