index.cjs.js 12 KB

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