@maple-design/use 0.0.0-stage → 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +55 -2
- package/lib/index.cjs +871 -0
- package/lib/index.d.cts +161 -0
- package/lib/index.d.ts +161 -0
- package/lib/index.js +846 -0
- package/package.json +51 -3
package/lib/index.js
ADDED
|
@@ -0,0 +1,846 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __defProps = Object.defineProperties;
|
|
3
|
+
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
|
|
4
|
+
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
|
7
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
8
|
+
var __spreadValues = (a, b) => {
|
|
9
|
+
for (var prop in b || (b = {}))
|
|
10
|
+
if (__hasOwnProp.call(b, prop))
|
|
11
|
+
__defNormalProp(a, prop, b[prop]);
|
|
12
|
+
if (__getOwnPropSymbols)
|
|
13
|
+
for (var prop of __getOwnPropSymbols(b)) {
|
|
14
|
+
if (__propIsEnum.call(b, prop))
|
|
15
|
+
__defNormalProp(a, prop, b[prop]);
|
|
16
|
+
}
|
|
17
|
+
return a;
|
|
18
|
+
};
|
|
19
|
+
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
|
|
20
|
+
var __objRest = (source, exclude) => {
|
|
21
|
+
var target = {};
|
|
22
|
+
for (var prop in source)
|
|
23
|
+
if (__hasOwnProp.call(source, prop) && exclude.indexOf(prop) < 0)
|
|
24
|
+
target[prop] = source[prop];
|
|
25
|
+
if (source != null && __getOwnPropSymbols)
|
|
26
|
+
for (var prop of __getOwnPropSymbols(source)) {
|
|
27
|
+
if (exclude.indexOf(prop) < 0 && __propIsEnum.call(source, prop))
|
|
28
|
+
target[prop] = source[prop];
|
|
29
|
+
}
|
|
30
|
+
return target;
|
|
31
|
+
};
|
|
32
|
+
var __async = (__this, __arguments, generator) => {
|
|
33
|
+
return new Promise((resolve, reject) => {
|
|
34
|
+
var fulfilled = (value) => {
|
|
35
|
+
try {
|
|
36
|
+
step(generator.next(value));
|
|
37
|
+
} catch (e) {
|
|
38
|
+
reject(e);
|
|
39
|
+
}
|
|
40
|
+
};
|
|
41
|
+
var rejected = (value) => {
|
|
42
|
+
try {
|
|
43
|
+
step(generator.throw(value));
|
|
44
|
+
} catch (e) {
|
|
45
|
+
reject(e);
|
|
46
|
+
}
|
|
47
|
+
};
|
|
48
|
+
var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
|
|
49
|
+
step((generator = generator.apply(__this, __arguments)).next());
|
|
50
|
+
});
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
// src/useEventListener.ts
|
|
54
|
+
import { isRef, onBeforeUnmount, onDeactivated, unref, watch } from "vue";
|
|
55
|
+
import { inBrowser, isFunction } from "@maple-design/shared";
|
|
56
|
+
|
|
57
|
+
// src/onSmartMounted.ts
|
|
58
|
+
import { nextTick, onActivated, onMounted } from "vue";
|
|
59
|
+
function onSmartMounted(hook) {
|
|
60
|
+
let isMounted = false;
|
|
61
|
+
onMounted(() => {
|
|
62
|
+
hook();
|
|
63
|
+
nextTick(() => {
|
|
64
|
+
isMounted = true;
|
|
65
|
+
});
|
|
66
|
+
});
|
|
67
|
+
onActivated(() => {
|
|
68
|
+
if (!isMounted) {
|
|
69
|
+
return;
|
|
70
|
+
}
|
|
71
|
+
hook();
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
// src/useEventListener.ts
|
|
76
|
+
function useEventListener(target, type, listener, options = {}) {
|
|
77
|
+
if (!inBrowser()) {
|
|
78
|
+
return () => {
|
|
79
|
+
};
|
|
80
|
+
}
|
|
81
|
+
const { passive = false, capture = false } = options;
|
|
82
|
+
let boundTarget;
|
|
83
|
+
let active = false;
|
|
84
|
+
let cleaned = false;
|
|
85
|
+
let stop;
|
|
86
|
+
const resolveTarget = () => {
|
|
87
|
+
var _a;
|
|
88
|
+
return (_a = isFunction(target) ? target() : unref(target)) != null ? _a : void 0;
|
|
89
|
+
};
|
|
90
|
+
const detach = () => {
|
|
91
|
+
boundTarget == null ? void 0 : boundTarget.removeEventListener(type, listener, { capture });
|
|
92
|
+
boundTarget = void 0;
|
|
93
|
+
};
|
|
94
|
+
const bind = () => {
|
|
95
|
+
if (!active || cleaned) {
|
|
96
|
+
return;
|
|
97
|
+
}
|
|
98
|
+
const next = resolveTarget();
|
|
99
|
+
if (next === boundTarget) {
|
|
100
|
+
return;
|
|
101
|
+
}
|
|
102
|
+
detach();
|
|
103
|
+
next == null ? void 0 : next.addEventListener(type, listener, { passive, capture });
|
|
104
|
+
boundTarget = next;
|
|
105
|
+
};
|
|
106
|
+
const cleanup = () => {
|
|
107
|
+
if (cleaned) {
|
|
108
|
+
return;
|
|
109
|
+
}
|
|
110
|
+
cleaned = true;
|
|
111
|
+
active = false;
|
|
112
|
+
stop == null ? void 0 : stop();
|
|
113
|
+
detach();
|
|
114
|
+
};
|
|
115
|
+
if (isRef(target) || isFunction(target)) {
|
|
116
|
+
stop = watch(resolveTarget, bind);
|
|
117
|
+
}
|
|
118
|
+
onSmartMounted(() => {
|
|
119
|
+
active = true;
|
|
120
|
+
bind();
|
|
121
|
+
});
|
|
122
|
+
onDeactivated(() => {
|
|
123
|
+
active = false;
|
|
124
|
+
detach();
|
|
125
|
+
});
|
|
126
|
+
onBeforeUnmount(cleanup);
|
|
127
|
+
return cleanup;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
// src/useClickOutside.ts
|
|
131
|
+
import { unref as unref2 } from "vue";
|
|
132
|
+
import { inBrowser as inBrowser2, isFunction as isFunction2 } from "@maple-design/shared";
|
|
133
|
+
function useClickOutside(target, type, listener) {
|
|
134
|
+
if (!inBrowser2()) {
|
|
135
|
+
return;
|
|
136
|
+
}
|
|
137
|
+
const handler = (event) => {
|
|
138
|
+
const element = isFunction2(target) ? target() : unref2(target);
|
|
139
|
+
if (element && !event.composedPath().includes(element) && !element.contains(event.target)) {
|
|
140
|
+
listener(event);
|
|
141
|
+
}
|
|
142
|
+
};
|
|
143
|
+
useEventListener(document, type, handler);
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
// src/onSmartUnmounted.ts
|
|
147
|
+
import { onDeactivated as onDeactivated2, onUnmounted } from "vue";
|
|
148
|
+
function onSmartUnmounted(hook) {
|
|
149
|
+
let keepalive = false;
|
|
150
|
+
onDeactivated2(() => {
|
|
151
|
+
keepalive = true;
|
|
152
|
+
hook();
|
|
153
|
+
});
|
|
154
|
+
onUnmounted(() => {
|
|
155
|
+
if (keepalive) {
|
|
156
|
+
return;
|
|
157
|
+
}
|
|
158
|
+
hook();
|
|
159
|
+
});
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
// src/useParent.ts
|
|
163
|
+
import {
|
|
164
|
+
computed,
|
|
165
|
+
getCurrentInstance,
|
|
166
|
+
inject,
|
|
167
|
+
nextTick as nextTick2,
|
|
168
|
+
onActivated as onActivated2,
|
|
169
|
+
onBeforeUnmount as onBeforeUnmount2,
|
|
170
|
+
onDeactivated as onDeactivated3,
|
|
171
|
+
onMounted as onMounted2
|
|
172
|
+
} from "vue";
|
|
173
|
+
function keyInProvides(key) {
|
|
174
|
+
const instance = getCurrentInstance();
|
|
175
|
+
return key in instance.provides;
|
|
176
|
+
}
|
|
177
|
+
function useParent(key) {
|
|
178
|
+
if (!keyInProvides(key)) {
|
|
179
|
+
return {
|
|
180
|
+
index: null,
|
|
181
|
+
parentProvider: null,
|
|
182
|
+
bindParent: null
|
|
183
|
+
};
|
|
184
|
+
}
|
|
185
|
+
const provider = inject(key);
|
|
186
|
+
const _a = provider, { childInstances, collect, clear } = _a, parentProvider = __objRest(_a, ["childInstances", "collect", "clear"]);
|
|
187
|
+
const childInstance = getCurrentInstance();
|
|
188
|
+
const index = computed(() => childInstances.indexOf(childInstance));
|
|
189
|
+
const bindParent = (childProvider) => {
|
|
190
|
+
let active = false;
|
|
191
|
+
let registered = false;
|
|
192
|
+
const add = () => {
|
|
193
|
+
active = true;
|
|
194
|
+
nextTick2(() => {
|
|
195
|
+
if (active && !registered) {
|
|
196
|
+
collect(childInstance, childProvider);
|
|
197
|
+
registered = true;
|
|
198
|
+
}
|
|
199
|
+
});
|
|
200
|
+
};
|
|
201
|
+
const remove = () => {
|
|
202
|
+
active = false;
|
|
203
|
+
if (registered) {
|
|
204
|
+
clear(childInstance, childProvider);
|
|
205
|
+
registered = false;
|
|
206
|
+
}
|
|
207
|
+
};
|
|
208
|
+
onMounted2(add);
|
|
209
|
+
onActivated2(add);
|
|
210
|
+
onDeactivated3(remove);
|
|
211
|
+
onBeforeUnmount2(remove);
|
|
212
|
+
};
|
|
213
|
+
return {
|
|
214
|
+
index,
|
|
215
|
+
parentProvider,
|
|
216
|
+
bindParent
|
|
217
|
+
};
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
// src/useChildren.ts
|
|
221
|
+
import {
|
|
222
|
+
computed as computed2,
|
|
223
|
+
getCurrentInstance as getCurrentInstance2,
|
|
224
|
+
isVNode,
|
|
225
|
+
onUpdated,
|
|
226
|
+
provide,
|
|
227
|
+
reactive
|
|
228
|
+
} from "vue";
|
|
229
|
+
import { isArray, removeItem } from "@maple-design/shared";
|
|
230
|
+
function flatVNodes(subTree) {
|
|
231
|
+
const vNodes = [];
|
|
232
|
+
const flat = (subTree2) => {
|
|
233
|
+
if (subTree2 == null ? void 0 : subTree2.component) {
|
|
234
|
+
flat(subTree2 == null ? void 0 : subTree2.component.subTree);
|
|
235
|
+
return;
|
|
236
|
+
}
|
|
237
|
+
if (isArray(subTree2 == null ? void 0 : subTree2.children)) {
|
|
238
|
+
subTree2.children.forEach((child) => {
|
|
239
|
+
if (isVNode(child)) {
|
|
240
|
+
vNodes.push(child);
|
|
241
|
+
flat(child);
|
|
242
|
+
}
|
|
243
|
+
});
|
|
244
|
+
}
|
|
245
|
+
};
|
|
246
|
+
flat(subTree);
|
|
247
|
+
return vNodes;
|
|
248
|
+
}
|
|
249
|
+
function useChildren(key) {
|
|
250
|
+
const parentInstance = getCurrentInstance2();
|
|
251
|
+
const childInstances = reactive([]);
|
|
252
|
+
const childProviders = [];
|
|
253
|
+
const length = computed2(() => childInstances.length);
|
|
254
|
+
const sortInstances = () => {
|
|
255
|
+
const vNodes = flatVNodes(parentInstance.subTree);
|
|
256
|
+
const sorted = [...childInstances].sort((a, b) => vNodes.indexOf(a.vnode) - vNodes.indexOf(b.vnode));
|
|
257
|
+
if (sorted.every((instance, index) => instance === childInstances[index])) {
|
|
258
|
+
return;
|
|
259
|
+
}
|
|
260
|
+
const providers = sorted.map((instance) => childProviders[childInstances.indexOf(instance)]);
|
|
261
|
+
childInstances.splice(0, childInstances.length, ...sorted);
|
|
262
|
+
childProviders.splice(0, childProviders.length, ...providers);
|
|
263
|
+
};
|
|
264
|
+
onUpdated(sortInstances);
|
|
265
|
+
const collect = (childInstance, childProvider) => {
|
|
266
|
+
if (childInstances.includes(childInstance)) {
|
|
267
|
+
return;
|
|
268
|
+
}
|
|
269
|
+
childInstances.push(childInstance);
|
|
270
|
+
childProviders.push(childProvider);
|
|
271
|
+
sortInstances();
|
|
272
|
+
};
|
|
273
|
+
const clear = (childInstance, childProvider) => {
|
|
274
|
+
removeItem(childInstances, childInstance);
|
|
275
|
+
removeItem(childProviders, childProvider);
|
|
276
|
+
};
|
|
277
|
+
const bindChildren = (parentProvider) => {
|
|
278
|
+
provide(key, __spreadValues({
|
|
279
|
+
childInstances,
|
|
280
|
+
collect,
|
|
281
|
+
clear
|
|
282
|
+
}, parentProvider));
|
|
283
|
+
};
|
|
284
|
+
return {
|
|
285
|
+
length,
|
|
286
|
+
childProviders,
|
|
287
|
+
bindChildren
|
|
288
|
+
};
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
// src/onWindowResize.ts
|
|
292
|
+
function onWindowResize(listener) {
|
|
293
|
+
useEventListener(() => window, "resize", listener, { passive: true });
|
|
294
|
+
useEventListener(() => window, "orientationchange", listener, { passive: true });
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
// src/useInitialized.ts
|
|
298
|
+
import { ref, watch as watch2 } from "vue";
|
|
299
|
+
function useInitialized(source, value) {
|
|
300
|
+
const initialized = ref(false);
|
|
301
|
+
watch2(
|
|
302
|
+
source,
|
|
303
|
+
(newValue) => {
|
|
304
|
+
if (value === newValue) {
|
|
305
|
+
initialized.value = true;
|
|
306
|
+
}
|
|
307
|
+
},
|
|
308
|
+
{ immediate: true }
|
|
309
|
+
);
|
|
310
|
+
return initialized;
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
// src/useTouch.ts
|
|
314
|
+
import { onBeforeUnmount as onBeforeUnmount3, onDeactivated as onDeactivated4, ref as ref2 } from "vue";
|
|
315
|
+
import { getScrollTop } from "@maple-design/shared";
|
|
316
|
+
function getDirection(x, y) {
|
|
317
|
+
if (x > y) {
|
|
318
|
+
return "horizontal";
|
|
319
|
+
}
|
|
320
|
+
if (y > x) {
|
|
321
|
+
return "vertical";
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
function useTouch() {
|
|
325
|
+
const startX = ref2(0);
|
|
326
|
+
const startY = ref2(0);
|
|
327
|
+
const deltaX = ref2(0);
|
|
328
|
+
const deltaY = ref2(0);
|
|
329
|
+
const offsetX = ref2(0);
|
|
330
|
+
const offsetY = ref2(0);
|
|
331
|
+
const prevX = ref2(0);
|
|
332
|
+
const prevY = ref2(0);
|
|
333
|
+
const moveX = ref2(0);
|
|
334
|
+
const moveY = ref2(0);
|
|
335
|
+
const direction = ref2();
|
|
336
|
+
const touching = ref2(false);
|
|
337
|
+
const dragging = ref2(false);
|
|
338
|
+
const startTime = ref2(0);
|
|
339
|
+
const distance = ref2(0);
|
|
340
|
+
let draggingAnimationFrame = null;
|
|
341
|
+
const cancelFrame = () => {
|
|
342
|
+
if (draggingAnimationFrame !== null && typeof window !== "undefined") {
|
|
343
|
+
window.cancelAnimationFrame(draggingAnimationFrame);
|
|
344
|
+
}
|
|
345
|
+
draggingAnimationFrame = null;
|
|
346
|
+
};
|
|
347
|
+
const resetTouch = () => {
|
|
348
|
+
cancelFrame();
|
|
349
|
+
startX.value = 0;
|
|
350
|
+
startY.value = 0;
|
|
351
|
+
deltaX.value = 0;
|
|
352
|
+
deltaY.value = 0;
|
|
353
|
+
offsetX.value = 0;
|
|
354
|
+
offsetY.value = 0;
|
|
355
|
+
prevX.value = 0;
|
|
356
|
+
prevY.value = 0;
|
|
357
|
+
moveX.value = 0;
|
|
358
|
+
moveY.value = 0;
|
|
359
|
+
direction.value = void 0;
|
|
360
|
+
touching.value = false;
|
|
361
|
+
dragging.value = false;
|
|
362
|
+
startTime.value = 0;
|
|
363
|
+
distance.value = 0;
|
|
364
|
+
};
|
|
365
|
+
const startTouch = (event) => {
|
|
366
|
+
resetTouch();
|
|
367
|
+
if (!event.touches.length) {
|
|
368
|
+
return;
|
|
369
|
+
}
|
|
370
|
+
const { clientX: x, clientY: y } = event.touches[0];
|
|
371
|
+
startX.value = x;
|
|
372
|
+
startY.value = y;
|
|
373
|
+
prevX.value = x;
|
|
374
|
+
prevY.value = y;
|
|
375
|
+
touching.value = true;
|
|
376
|
+
startTime.value = performance.now();
|
|
377
|
+
dragging.value = false;
|
|
378
|
+
if (draggingAnimationFrame) {
|
|
379
|
+
window.cancelAnimationFrame(draggingAnimationFrame);
|
|
380
|
+
}
|
|
381
|
+
};
|
|
382
|
+
const moveTouch = (event) => {
|
|
383
|
+
if (!event.touches.length) {
|
|
384
|
+
return;
|
|
385
|
+
}
|
|
386
|
+
const { clientX: x, clientY: y } = event.touches[0];
|
|
387
|
+
dragging.value = true;
|
|
388
|
+
deltaX.value = x - startX.value;
|
|
389
|
+
deltaY.value = y - startY.value;
|
|
390
|
+
offsetX.value = Math.abs(deltaX.value);
|
|
391
|
+
offsetY.value = Math.abs(deltaY.value);
|
|
392
|
+
distance.value = Math.sqrt(offsetX.value ** 2 + offsetY.value ** 2);
|
|
393
|
+
moveX.value = x - prevX.value;
|
|
394
|
+
moveY.value = y - prevY.value;
|
|
395
|
+
if (!direction.value) {
|
|
396
|
+
direction.value = getDirection(offsetX.value, offsetY.value);
|
|
397
|
+
}
|
|
398
|
+
prevX.value = x;
|
|
399
|
+
prevY.value = y;
|
|
400
|
+
};
|
|
401
|
+
const endTouch = () => {
|
|
402
|
+
touching.value = false;
|
|
403
|
+
cancelFrame();
|
|
404
|
+
if (typeof window === "undefined") {
|
|
405
|
+
dragging.value = false;
|
|
406
|
+
return;
|
|
407
|
+
}
|
|
408
|
+
draggingAnimationFrame = window.requestAnimationFrame(() => {
|
|
409
|
+
draggingAnimationFrame = null;
|
|
410
|
+
dragging.value = false;
|
|
411
|
+
});
|
|
412
|
+
};
|
|
413
|
+
const isReachTop = (element) => {
|
|
414
|
+
const scrollTop = getScrollTop(element);
|
|
415
|
+
return scrollTop === 0 && deltaY.value > 0;
|
|
416
|
+
};
|
|
417
|
+
const isReachBottom = (element, offset = 1) => {
|
|
418
|
+
const { scrollHeight, clientHeight, scrollTop } = element;
|
|
419
|
+
const offsetBottom = Math.abs(scrollHeight - scrollTop - clientHeight);
|
|
420
|
+
return deltaY.value < 0 && offsetBottom <= offset;
|
|
421
|
+
};
|
|
422
|
+
onBeforeUnmount3(resetTouch);
|
|
423
|
+
onDeactivated4(resetTouch);
|
|
424
|
+
return {
|
|
425
|
+
startX,
|
|
426
|
+
startY,
|
|
427
|
+
deltaX,
|
|
428
|
+
deltaY,
|
|
429
|
+
offsetX,
|
|
430
|
+
offsetY,
|
|
431
|
+
prevX,
|
|
432
|
+
prevY,
|
|
433
|
+
moveX,
|
|
434
|
+
moveY,
|
|
435
|
+
direction,
|
|
436
|
+
touching,
|
|
437
|
+
dragging,
|
|
438
|
+
startTime,
|
|
439
|
+
distance,
|
|
440
|
+
resetTouch,
|
|
441
|
+
startTouch,
|
|
442
|
+
moveTouch,
|
|
443
|
+
endTouch,
|
|
444
|
+
isReachTop,
|
|
445
|
+
isReachBottom
|
|
446
|
+
};
|
|
447
|
+
}
|
|
448
|
+
|
|
449
|
+
// src/useId.ts
|
|
450
|
+
import * as Vue from "vue";
|
|
451
|
+
import { kebabCase } from "@maple-design/shared";
|
|
452
|
+
function useId2() {
|
|
453
|
+
var _a, _b;
|
|
454
|
+
const instance = Vue.getCurrentInstance();
|
|
455
|
+
const name = kebabCase((_a = instance.type.name) != null ? _a : "component");
|
|
456
|
+
const nativeId = (_b = Vue.useId) == null ? void 0 : _b();
|
|
457
|
+
const id = Vue.ref();
|
|
458
|
+
id.value = process.env.NODE_ENV === "test" ? `${name}-mock-id` : nativeId ? `${name}-${nativeId}` : void 0;
|
|
459
|
+
Vue.onMounted(() => {
|
|
460
|
+
if (id.value === void 0) {
|
|
461
|
+
id.value = `${name}-${instance.uid}`;
|
|
462
|
+
}
|
|
463
|
+
});
|
|
464
|
+
return id;
|
|
465
|
+
}
|
|
466
|
+
|
|
467
|
+
// src/useClientId.ts
|
|
468
|
+
import { getCurrentInstance as getCurrentInstance4, onMounted as onMounted4, ref as ref4 } from "vue";
|
|
469
|
+
import { kebabCase as kebabCase2 } from "@maple-design/shared";
|
|
470
|
+
function useClientId() {
|
|
471
|
+
const instance = getCurrentInstance4();
|
|
472
|
+
const name = kebabCase2(instance.type.name);
|
|
473
|
+
const id = ref4(process.env.NODE_ENV === "test" ? `${name}-mock-id` : void 0);
|
|
474
|
+
onMounted4(() => {
|
|
475
|
+
if (process.env.NODE_ENV !== "test") {
|
|
476
|
+
id.value = `${name}-${instance.uid}`;
|
|
477
|
+
}
|
|
478
|
+
});
|
|
479
|
+
return id;
|
|
480
|
+
}
|
|
481
|
+
|
|
482
|
+
// src/useWindowSize.ts
|
|
483
|
+
import { ref as ref5 } from "vue";
|
|
484
|
+
import { inBrowser as inBrowser3 } from "@maple-design/shared";
|
|
485
|
+
function useWindowSize(options = {}) {
|
|
486
|
+
const { initialWidth = 0, initialHeight = 0 } = options;
|
|
487
|
+
const width = ref5(initialWidth);
|
|
488
|
+
const height = ref5(initialHeight);
|
|
489
|
+
const update = () => {
|
|
490
|
+
if (!inBrowser3()) {
|
|
491
|
+
return;
|
|
492
|
+
}
|
|
493
|
+
width.value = window.innerWidth;
|
|
494
|
+
height.value = window.innerHeight;
|
|
495
|
+
};
|
|
496
|
+
onSmartMounted(update);
|
|
497
|
+
onWindowResize(update);
|
|
498
|
+
return {
|
|
499
|
+
width,
|
|
500
|
+
height
|
|
501
|
+
};
|
|
502
|
+
}
|
|
503
|
+
|
|
504
|
+
// src/useVModel.ts
|
|
505
|
+
import { computed as computed3, nextTick as nextTick3, ref as ref6, watch as watch3 } from "vue";
|
|
506
|
+
import { call } from "@maple-design/shared";
|
|
507
|
+
function useVModel(props, key, options = {}) {
|
|
508
|
+
const { passive = true, eventName, defaultValue, emit } = options;
|
|
509
|
+
const event = eventName != null ? eventName : `${emit ? "update" : "onUpdate"}:${key.toString()}`;
|
|
510
|
+
const getValue = () => {
|
|
511
|
+
var _a;
|
|
512
|
+
return (_a = props[key]) != null ? _a : defaultValue;
|
|
513
|
+
};
|
|
514
|
+
if (!passive) {
|
|
515
|
+
return computed3({
|
|
516
|
+
get() {
|
|
517
|
+
return getValue();
|
|
518
|
+
},
|
|
519
|
+
set(value) {
|
|
520
|
+
emit ? emit(event, value) : call(props[event], value);
|
|
521
|
+
}
|
|
522
|
+
});
|
|
523
|
+
}
|
|
524
|
+
const proxy = ref6(getValue());
|
|
525
|
+
let shouldEmit = true;
|
|
526
|
+
watch3(
|
|
527
|
+
() => props[key],
|
|
528
|
+
() => {
|
|
529
|
+
shouldEmit = false;
|
|
530
|
+
proxy.value = getValue();
|
|
531
|
+
nextTick3(() => {
|
|
532
|
+
shouldEmit = true;
|
|
533
|
+
});
|
|
534
|
+
}
|
|
535
|
+
);
|
|
536
|
+
watch3(
|
|
537
|
+
() => proxy.value,
|
|
538
|
+
(newValue) => {
|
|
539
|
+
if (!shouldEmit) {
|
|
540
|
+
return;
|
|
541
|
+
}
|
|
542
|
+
emit ? emit(event, newValue) : call(props[event], newValue);
|
|
543
|
+
}
|
|
544
|
+
);
|
|
545
|
+
return proxy;
|
|
546
|
+
}
|
|
547
|
+
|
|
548
|
+
// src/useMotion.ts
|
|
549
|
+
import { ref as ref7 } from "vue";
|
|
550
|
+
import { isFunction as isFunction3, motion } from "@maple-design/shared";
|
|
551
|
+
function useMotion(options) {
|
|
552
|
+
const value = ref7(getter(options.from));
|
|
553
|
+
const state = ref7("pending");
|
|
554
|
+
let ctx = createMotionContext();
|
|
555
|
+
function getter(value2) {
|
|
556
|
+
return isFunction3(value2) ? value2() : value2;
|
|
557
|
+
}
|
|
558
|
+
function reset() {
|
|
559
|
+
ctx.reset();
|
|
560
|
+
value.value = getter(options.from);
|
|
561
|
+
state.value = "pending";
|
|
562
|
+
ctx = createMotionContext();
|
|
563
|
+
}
|
|
564
|
+
function start() {
|
|
565
|
+
ctx.start();
|
|
566
|
+
}
|
|
567
|
+
function pause() {
|
|
568
|
+
ctx.pause();
|
|
569
|
+
}
|
|
570
|
+
function createMotionContext() {
|
|
571
|
+
return motion({
|
|
572
|
+
from: getter(options.from),
|
|
573
|
+
to: getter(options.to),
|
|
574
|
+
duration: options.duration === void 0 ? 300 : getter(options.duration),
|
|
575
|
+
timingFunction: options.timingFunction,
|
|
576
|
+
onStateChange(newState) {
|
|
577
|
+
state.value = newState;
|
|
578
|
+
},
|
|
579
|
+
frame({ value: newValue, done }) {
|
|
580
|
+
var _a;
|
|
581
|
+
value.value = newValue;
|
|
582
|
+
if (done) {
|
|
583
|
+
(_a = options.onFinished) == null ? void 0 : _a.call(options, value.value);
|
|
584
|
+
}
|
|
585
|
+
}
|
|
586
|
+
});
|
|
587
|
+
}
|
|
588
|
+
return {
|
|
589
|
+
value,
|
|
590
|
+
state,
|
|
591
|
+
start,
|
|
592
|
+
pause,
|
|
593
|
+
reset
|
|
594
|
+
};
|
|
595
|
+
}
|
|
596
|
+
|
|
597
|
+
// src/useAsyncData.ts
|
|
598
|
+
import { getCurrentInstance as getCurrentInstance5, getCurrentScope, onMounted as onMounted5, onScopeDispose, readonly, ref as ref8, shallowRef } from "vue";
|
|
599
|
+
function useAsyncData(loader, options) {
|
|
600
|
+
const data = shallowRef(options.initialData);
|
|
601
|
+
const loading = ref8(false);
|
|
602
|
+
const error = shallowRef();
|
|
603
|
+
const initialKey = Symbol("initial");
|
|
604
|
+
const cache = /* @__PURE__ */ new Map();
|
|
605
|
+
let active;
|
|
606
|
+
let last = {};
|
|
607
|
+
let disposed = false;
|
|
608
|
+
let started = false;
|
|
609
|
+
function cancel() {
|
|
610
|
+
const task = active;
|
|
611
|
+
active = void 0;
|
|
612
|
+
if (task) {
|
|
613
|
+
clearTimeout(task.timer);
|
|
614
|
+
task.controller.abort();
|
|
615
|
+
task.settle({ status: "cancelled" });
|
|
616
|
+
}
|
|
617
|
+
loading.value = false;
|
|
618
|
+
}
|
|
619
|
+
function run(request, delay) {
|
|
620
|
+
var _a, _b, _c;
|
|
621
|
+
cancel();
|
|
622
|
+
if (disposed) {
|
|
623
|
+
return Promise.resolve({ status: "cancelled" });
|
|
624
|
+
}
|
|
625
|
+
started = true;
|
|
626
|
+
last = request;
|
|
627
|
+
error.value = void 0;
|
|
628
|
+
const currentLoader = (_a = request.loader) != null ? _a : loader;
|
|
629
|
+
const key = (_b = request.cacheKey) != null ? _b : request.loader ? void 0 : initialKey;
|
|
630
|
+
const ttl = (_c = options.cacheTime) != null ? _c : 0;
|
|
631
|
+
const cached = key === void 0 ? void 0 : cache.get(key);
|
|
632
|
+
if (!request.force && ttl > 0 && cached && cached.expires > Date.now()) {
|
|
633
|
+
data.value = cached.data;
|
|
634
|
+
return Promise.resolve({ status: "success", data: cached.data });
|
|
635
|
+
}
|
|
636
|
+
if (key !== void 0) {
|
|
637
|
+
cache.delete(key);
|
|
638
|
+
}
|
|
639
|
+
loading.value = true;
|
|
640
|
+
return new Promise((settle) => {
|
|
641
|
+
const task = {
|
|
642
|
+
controller: new AbortController(),
|
|
643
|
+
settle,
|
|
644
|
+
timer: void 0
|
|
645
|
+
};
|
|
646
|
+
active = task;
|
|
647
|
+
const start = () => __async(this, null, function* () {
|
|
648
|
+
try {
|
|
649
|
+
const value = yield currentLoader({ signal: task.controller.signal });
|
|
650
|
+
if (active !== task) {
|
|
651
|
+
return;
|
|
652
|
+
}
|
|
653
|
+
data.value = value;
|
|
654
|
+
if (ttl > 0 && key !== void 0) {
|
|
655
|
+
if (cache.size >= 50) {
|
|
656
|
+
cache.delete(cache.keys().next().value);
|
|
657
|
+
}
|
|
658
|
+
cache.set(key, { data: value, expires: Date.now() + ttl });
|
|
659
|
+
}
|
|
660
|
+
settle({ status: "success", data: value });
|
|
661
|
+
} catch (cause) {
|
|
662
|
+
if (active !== task) {
|
|
663
|
+
return;
|
|
664
|
+
}
|
|
665
|
+
error.value = cause;
|
|
666
|
+
settle({ status: "error", error: cause });
|
|
667
|
+
} finally {
|
|
668
|
+
if (active === task) {
|
|
669
|
+
active = void 0;
|
|
670
|
+
loading.value = false;
|
|
671
|
+
}
|
|
672
|
+
}
|
|
673
|
+
});
|
|
674
|
+
if (delay > 0) {
|
|
675
|
+
task.timer = setTimeout(start, delay);
|
|
676
|
+
} else {
|
|
677
|
+
void start();
|
|
678
|
+
}
|
|
679
|
+
});
|
|
680
|
+
}
|
|
681
|
+
const execute = (request = {}) => {
|
|
682
|
+
var _a;
|
|
683
|
+
return run(request, (_a = options.debounce) != null ? _a : 0);
|
|
684
|
+
};
|
|
685
|
+
const refresh = () => run(__spreadProps(__spreadValues({}, last), { force: true }), 0);
|
|
686
|
+
const initialize = () => {
|
|
687
|
+
if (!started && !disposed && options.immediate !== false) {
|
|
688
|
+
void run({}, 0);
|
|
689
|
+
}
|
|
690
|
+
};
|
|
691
|
+
if (getCurrentInstance5()) {
|
|
692
|
+
onMounted5(initialize);
|
|
693
|
+
} else {
|
|
694
|
+
initialize();
|
|
695
|
+
}
|
|
696
|
+
if (getCurrentScope()) {
|
|
697
|
+
onScopeDispose(() => {
|
|
698
|
+
disposed = true;
|
|
699
|
+
cancel();
|
|
700
|
+
cache.clear();
|
|
701
|
+
});
|
|
702
|
+
}
|
|
703
|
+
return {
|
|
704
|
+
data,
|
|
705
|
+
loading: readonly(loading),
|
|
706
|
+
error: readonly(error),
|
|
707
|
+
execute,
|
|
708
|
+
refresh,
|
|
709
|
+
cancel,
|
|
710
|
+
clearCache: () => cache.clear()
|
|
711
|
+
};
|
|
712
|
+
}
|
|
713
|
+
|
|
714
|
+
// src/useDirection.ts
|
|
715
|
+
import {
|
|
716
|
+
computed as computed4,
|
|
717
|
+
getCurrentInstance as getCurrentInstance6,
|
|
718
|
+
inject as inject2,
|
|
719
|
+
onActivated as onActivated3,
|
|
720
|
+
onDeactivated as onDeactivated5,
|
|
721
|
+
onMounted as onMounted6,
|
|
722
|
+
onScopeDispose as onScopeDispose2,
|
|
723
|
+
provide as provide2,
|
|
724
|
+
ref as ref9,
|
|
725
|
+
shallowRef as shallowRef2,
|
|
726
|
+
watchPostEffect
|
|
727
|
+
} from "vue";
|
|
728
|
+
var readingDirectionKey = Symbol("reading-direction");
|
|
729
|
+
function provideDirection(direction) {
|
|
730
|
+
provide2(readingDirectionKey, direction);
|
|
731
|
+
}
|
|
732
|
+
function resolveDirection(element, fallback = "ltr") {
|
|
733
|
+
var _a, _b;
|
|
734
|
+
const scope = element == null ? void 0 : element.closest("[dir]");
|
|
735
|
+
const value = (_a = scope == null ? void 0 : scope.getAttribute("dir")) == null ? void 0 : _a.toLowerCase();
|
|
736
|
+
if (value === "ltr" || value === "rtl") {
|
|
737
|
+
return value;
|
|
738
|
+
}
|
|
739
|
+
if (value === "auto" && scope) {
|
|
740
|
+
return ((_b = scope.ownerDocument.defaultView) == null ? void 0 : _b.getComputedStyle(scope).direction) === "rtl" ? "rtl" : "ltr";
|
|
741
|
+
}
|
|
742
|
+
return fallback;
|
|
743
|
+
}
|
|
744
|
+
var documents = /* @__PURE__ */ new WeakMap();
|
|
745
|
+
function useDirection(target, explicit) {
|
|
746
|
+
const inherited = inject2(readingDirectionKey, void 0);
|
|
747
|
+
const instance = getCurrentInstance6();
|
|
748
|
+
const element = shallowRef2();
|
|
749
|
+
const version = shallowRef2();
|
|
750
|
+
let release;
|
|
751
|
+
let active = false;
|
|
752
|
+
function observe(node) {
|
|
753
|
+
var _a;
|
|
754
|
+
if (node === element.value && release) {
|
|
755
|
+
return;
|
|
756
|
+
}
|
|
757
|
+
release == null ? void 0 : release();
|
|
758
|
+
release = void 0;
|
|
759
|
+
element.value = node != null ? node : void 0;
|
|
760
|
+
version.value = void 0;
|
|
761
|
+
const doc = node == null ? void 0 : node.ownerDocument;
|
|
762
|
+
const Observer = (_a = doc == null ? void 0 : doc.defaultView) == null ? void 0 : _a.MutationObserver;
|
|
763
|
+
if (!doc || !Observer) {
|
|
764
|
+
return;
|
|
765
|
+
}
|
|
766
|
+
let state = documents.get(doc);
|
|
767
|
+
if (!state) {
|
|
768
|
+
const counter = ref9(0);
|
|
769
|
+
const observer = new Observer(() => {
|
|
770
|
+
counter.value++;
|
|
771
|
+
});
|
|
772
|
+
observer.observe(doc.documentElement, { subtree: true, attributes: true, attributeFilter: ["dir"] });
|
|
773
|
+
state = { version: counter, observer, users: 0 };
|
|
774
|
+
documents.set(doc, state);
|
|
775
|
+
}
|
|
776
|
+
state.users++;
|
|
777
|
+
version.value = state.version;
|
|
778
|
+
const current = state;
|
|
779
|
+
release = () => {
|
|
780
|
+
if (--current.users === 0) {
|
|
781
|
+
current.observer.disconnect();
|
|
782
|
+
documents.delete(doc);
|
|
783
|
+
}
|
|
784
|
+
};
|
|
785
|
+
}
|
|
786
|
+
function refresh() {
|
|
787
|
+
const node = target ? target() : instance == null ? void 0 : instance.vnode.el;
|
|
788
|
+
observe((node == null ? void 0 : node.nodeType) === 1 ? node : node == null ? void 0 : node.parentElement);
|
|
789
|
+
}
|
|
790
|
+
function start() {
|
|
791
|
+
active = true;
|
|
792
|
+
refresh();
|
|
793
|
+
}
|
|
794
|
+
function stop() {
|
|
795
|
+
active = false;
|
|
796
|
+
release == null ? void 0 : release();
|
|
797
|
+
release = void 0;
|
|
798
|
+
version.value = void 0;
|
|
799
|
+
}
|
|
800
|
+
onMounted6(start);
|
|
801
|
+
onActivated3(start);
|
|
802
|
+
onDeactivated5(stop);
|
|
803
|
+
onScopeDispose2(stop);
|
|
804
|
+
watchPostEffect(() => {
|
|
805
|
+
const node = target == null ? void 0 : target();
|
|
806
|
+
if (active) {
|
|
807
|
+
if (target) {
|
|
808
|
+
observe(node);
|
|
809
|
+
} else {
|
|
810
|
+
refresh();
|
|
811
|
+
}
|
|
812
|
+
}
|
|
813
|
+
});
|
|
814
|
+
const dir = computed4(() => {
|
|
815
|
+
var _a, _b;
|
|
816
|
+
void ((_a = version.value) == null ? void 0 : _a.value);
|
|
817
|
+
const value = explicit == null ? void 0 : explicit();
|
|
818
|
+
if (value === "ltr" || value === "rtl") {
|
|
819
|
+
return value;
|
|
820
|
+
}
|
|
821
|
+
return resolveDirection(element.value, (_b = inherited == null ? void 0 : inherited.value) != null ? _b : "ltr");
|
|
822
|
+
});
|
|
823
|
+
return { dir, isRtl: computed4(() => dir.value === "rtl") };
|
|
824
|
+
}
|
|
825
|
+
export {
|
|
826
|
+
keyInProvides,
|
|
827
|
+
onSmartMounted,
|
|
828
|
+
onSmartUnmounted,
|
|
829
|
+
onWindowResize,
|
|
830
|
+
provideDirection,
|
|
831
|
+
readingDirectionKey,
|
|
832
|
+
resolveDirection,
|
|
833
|
+
useAsyncData,
|
|
834
|
+
useChildren,
|
|
835
|
+
useClickOutside,
|
|
836
|
+
useClientId,
|
|
837
|
+
useDirection,
|
|
838
|
+
useEventListener,
|
|
839
|
+
useId2 as useId,
|
|
840
|
+
useInitialized,
|
|
841
|
+
useMotion,
|
|
842
|
+
useParent,
|
|
843
|
+
useTouch,
|
|
844
|
+
useVModel,
|
|
845
|
+
useWindowSize
|
|
846
|
+
};
|