@vrojs/use 0.0.1 → 0.0.3
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/dist/index.d.ts +212 -1
- package/dist/index.js +586 -2
- package/package.json +2 -10
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,15 @@
|
|
|
1
1
|
import * as vue from 'vue';
|
|
2
2
|
import { Ref } from 'vue';
|
|
3
|
+
import { RouteLocationRaw } from 'vue-router';
|
|
4
|
+
|
|
5
|
+
declare function debounceRef<T = any>(value: T, ms?: number): vue.Ref<T, T>;
|
|
6
|
+
|
|
7
|
+
declare module 'vue-router' {
|
|
8
|
+
interface RouteMeta {
|
|
9
|
+
enterClass?: string;
|
|
10
|
+
leaveClass?: string;
|
|
11
|
+
}
|
|
12
|
+
}
|
|
3
13
|
|
|
4
14
|
type AnyPromiseFn = (...args: any[]) => Promise<any>;
|
|
5
15
|
interface UseAsyncTaskOptions<T extends AnyPromiseFn> {
|
|
@@ -40,13 +50,214 @@ interface UseAsyncTaskResult<T, D> {
|
|
|
40
50
|
declare function useAsyncTask<T extends AnyPromiseFn>(task: T, options: UseAsyncTaskOptions<T> & Required<Pick<UseAsyncTaskOptions<T>, 'initialValue'>>): UseAsyncTaskResult<T, Awaited<ReturnType<T>>>;
|
|
41
51
|
declare function useAsyncTask<T extends AnyPromiseFn>(task: T, options?: UseAsyncTaskOptions<T>): UseAsyncTaskResult<T, Awaited<ReturnType<T>> | undefined>;
|
|
42
52
|
|
|
53
|
+
type MaybeRef<T> = T | Ref<T>;
|
|
54
|
+
interface UseBodyScrollLockReturn {
|
|
55
|
+
locked: Ref<boolean>;
|
|
56
|
+
lock: () => void;
|
|
57
|
+
unlock: () => void;
|
|
58
|
+
toggle: (value?: boolean) => void;
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* 锁定/解锁 body 滚动
|
|
62
|
+
* @param initialLocked - 初始锁定状态,或外部响应式布尔值
|
|
63
|
+
*/
|
|
64
|
+
declare function useBodyScrollLock(initialLocked?: MaybeRef<boolean>): UseBodyScrollLockReturn;
|
|
65
|
+
|
|
43
66
|
declare function useComposition(): {
|
|
44
67
|
handleCompositionStart: (event: CompositionEvent) => void;
|
|
45
68
|
handleCompositionEnd: (event: CompositionEvent) => void;
|
|
46
69
|
};
|
|
47
70
|
|
|
71
|
+
declare function useCountDown(start?: number): readonly [vue.Ref<number, number>, (st?: number) => void];
|
|
72
|
+
|
|
73
|
+
type Loose<T> = T & {
|
|
74
|
+
[key: string]: any;
|
|
75
|
+
};
|
|
76
|
+
type TargetRef = HTMLElement | EventTarget | Loose<{
|
|
77
|
+
$el: HTMLElement;
|
|
78
|
+
}> | Ref<EventTarget | Loose<{
|
|
79
|
+
$el: HTMLElement;
|
|
80
|
+
}> | undefined>;
|
|
81
|
+
type UseEventListenerOptions = {
|
|
82
|
+
target?: TargetRef;
|
|
83
|
+
capture?: boolean;
|
|
84
|
+
passive?: boolean;
|
|
85
|
+
};
|
|
86
|
+
declare function useEventListener<K extends keyof DocumentEventMap>(type: K, listener: (event: DocumentEventMap[K]) => void, options?: UseEventListenerOptions): void;
|
|
87
|
+
declare function useEventListener(type: string, listener: EventListener, options?: UseEventListenerOptions): void;
|
|
88
|
+
|
|
48
89
|
declare function useId(): string;
|
|
49
90
|
|
|
91
|
+
interface UseKeepAliveState$1 {
|
|
92
|
+
/**
|
|
93
|
+
* Route name 和 Component name 要保证一致,不然缓存会失败
|
|
94
|
+
*/
|
|
95
|
+
name: string;
|
|
96
|
+
/**
|
|
97
|
+
* vue-router history.state.position
|
|
98
|
+
* https://router.vuejs.org/zh/api/interfaces/RouterHistory.html#Properties-state
|
|
99
|
+
*/
|
|
100
|
+
position: number;
|
|
101
|
+
/**
|
|
102
|
+
* 模式,如果是 auto 则自动会管理,如果设置的是 custom 则需要手动管理
|
|
103
|
+
*/
|
|
104
|
+
mode: 'auto' | 'custom';
|
|
105
|
+
/**
|
|
106
|
+
* 关联的路由 如果是一个路由有子路由 则需要关联下子路由name
|
|
107
|
+
*/
|
|
108
|
+
relations?: string[];
|
|
109
|
+
}
|
|
110
|
+
type UseKeepAliveOptions = Partial<Omit<UseKeepAliveState$1, 'position'>> | number;
|
|
111
|
+
/**
|
|
112
|
+
* 结合 KeepAlive 组件,维护其 includes
|
|
113
|
+
*/
|
|
114
|
+
declare function useKeepAlive(options?: UseKeepAliveOptions): {
|
|
115
|
+
keepAliveList: vue.Ref<UseKeepAliveState$1[] | undefined, UseKeepAliveState$1[] | undefined>;
|
|
116
|
+
includes: vue.Ref<string[], string[]>;
|
|
117
|
+
};
|
|
118
|
+
|
|
119
|
+
interface UseKeepAliveState {
|
|
120
|
+
name: string;
|
|
121
|
+
position: number;
|
|
122
|
+
mode: 'auto' | 'custom';
|
|
123
|
+
}
|
|
124
|
+
type UseKeepAliveByPositionOptions = Partial<Omit<UseKeepAliveState, 'position'>> | boolean;
|
|
125
|
+
/**
|
|
126
|
+
* 结合 KeepAlive 组件,维护其 includes
|
|
127
|
+
* @deprecated 目前因为有路由会产生BUG,已经过期,后期版本会删除掉,请使用 useKeepAlive 方法
|
|
128
|
+
*/
|
|
129
|
+
declare function useKeepAliveByPosition(options?: UseKeepAliveByPositionOptions): {
|
|
130
|
+
keepAliveList: vue.Ref<UseKeepAliveState[] | undefined, UseKeepAliveState[] | undefined>;
|
|
131
|
+
includes: vue.ComputedRef<string[]>;
|
|
132
|
+
keep: (data: UseKeepAliveState) => void;
|
|
133
|
+
};
|
|
134
|
+
|
|
135
|
+
interface UseKeepPositionOptions {
|
|
136
|
+
/**
|
|
137
|
+
* 获取需要恢复位置的元素
|
|
138
|
+
*/
|
|
139
|
+
selectors?: string;
|
|
140
|
+
/**
|
|
141
|
+
* 获取需要恢复位置的元素
|
|
142
|
+
*/
|
|
143
|
+
getTarget?: () => HTMLElement;
|
|
144
|
+
}
|
|
145
|
+
interface UseKeepPositionState {
|
|
146
|
+
scrollLeft: number;
|
|
147
|
+
scrollTop: number;
|
|
148
|
+
}
|
|
149
|
+
declare function useKeepPosition(options?: UseKeepPositionOptions): {
|
|
150
|
+
keep: () => void;
|
|
151
|
+
restore: () => void;
|
|
152
|
+
};
|
|
153
|
+
|
|
154
|
+
interface UsePagingStatus {
|
|
155
|
+
pagingIndex: number;
|
|
156
|
+
pagingSize: number;
|
|
157
|
+
pagingTotal: number;
|
|
158
|
+
pagingError: string;
|
|
159
|
+
pagingLoading: boolean;
|
|
160
|
+
}
|
|
161
|
+
type UsePagingTaskResult<T = any> = {
|
|
162
|
+
pagingList: T[];
|
|
163
|
+
pagingTotal: number;
|
|
164
|
+
} | [T[], number];
|
|
165
|
+
interface UsePagingTaskOptions {
|
|
166
|
+
loading?: boolean;
|
|
167
|
+
toast?: boolean;
|
|
168
|
+
}
|
|
169
|
+
interface UsePagingTask<T = any> {
|
|
170
|
+
(state: [number, number], options: UsePagingTaskOptions): Promise<UsePagingTaskResult<T>>;
|
|
171
|
+
}
|
|
172
|
+
interface UsePagingOptions {
|
|
173
|
+
/**
|
|
174
|
+
* 初始化的状态
|
|
175
|
+
*/
|
|
176
|
+
initialStatus?: Partial<UsePagingStatus>;
|
|
177
|
+
/**
|
|
178
|
+
* 是否立即执行 onBeforeMount 执行,默认 false
|
|
179
|
+
*/
|
|
180
|
+
immediate?: boolean;
|
|
181
|
+
/**
|
|
182
|
+
* 需要滚动的元素 默认 .hor-scroll
|
|
183
|
+
*/
|
|
184
|
+
scrollSelector?: string;
|
|
185
|
+
}
|
|
186
|
+
type UsePagingTriggerOptions = ((...args: any[]) => any) | boolean | UsePagingTaskOptions;
|
|
187
|
+
declare function usePaging<T = any>(task: UsePagingTask<T>, options?: UsePagingOptions): {
|
|
188
|
+
pagingFinished: vue.ComputedRef<boolean>;
|
|
189
|
+
pagingData: Ref<T[], T[]>;
|
|
190
|
+
pagingStatus: {
|
|
191
|
+
pagingIndex: number;
|
|
192
|
+
pagingSize: number;
|
|
193
|
+
pagingTotal: number;
|
|
194
|
+
pagingError: string;
|
|
195
|
+
pagingLoading: boolean;
|
|
196
|
+
};
|
|
197
|
+
pagingTrigger: (pagingIndex: number, options?: UsePagingTriggerOptions) => void;
|
|
198
|
+
pagingRefresh: (options?: UsePagingTriggerOptions) => void;
|
|
199
|
+
pagingLoad: (options?: UsePagingTriggerOptions) => void;
|
|
200
|
+
pagingReset: () => void;
|
|
201
|
+
};
|
|
202
|
+
|
|
203
|
+
declare function useParams<T extends Record<string, string>>(): T;
|
|
204
|
+
|
|
205
|
+
declare function useQuery<T extends Record<string, string>>(): T;
|
|
206
|
+
|
|
207
|
+
type UseSetStateAction<S> = S | ((prevState: S) => S);
|
|
208
|
+
type UseSetState<T> = (initialState: T) => void;
|
|
209
|
+
declare function useState<S = undefined>(): [
|
|
210
|
+
Ref<S | undefined>,
|
|
211
|
+
UseSetState<UseSetStateAction<S | undefined>>
|
|
212
|
+
];
|
|
213
|
+
declare function useState<S>(initialState: S): [Ref<S>, UseSetState<UseSetStateAction<S>>];
|
|
214
|
+
|
|
215
|
+
declare function useToggle(initialValue?: boolean): readonly [vue.Ref<boolean, boolean>, (value?: boolean) => void];
|
|
216
|
+
|
|
217
|
+
interface TransferOptions {
|
|
218
|
+
/**
|
|
219
|
+
* 是否中转
|
|
220
|
+
*/
|
|
221
|
+
disabled?: boolean;
|
|
222
|
+
/**
|
|
223
|
+
* 路由配置 推荐使用 [to]
|
|
224
|
+
*/
|
|
225
|
+
replace?: boolean;
|
|
226
|
+
query?: Record<string, any>;
|
|
227
|
+
params?: Record<string, any>;
|
|
228
|
+
path?: string;
|
|
229
|
+
/**
|
|
230
|
+
* 路由配置
|
|
231
|
+
*/
|
|
232
|
+
to?: RouteLocationRaw;
|
|
233
|
+
/**
|
|
234
|
+
* 函数执行
|
|
235
|
+
*/
|
|
236
|
+
fn?(options: TransferOptions, ...args: any[]): void | Promise<void>;
|
|
237
|
+
[prop: string]: any;
|
|
238
|
+
}
|
|
239
|
+
declare function useTransfer(): (options?: TransferOptions, ...args: any[]) => Promise<void>;
|
|
240
|
+
|
|
241
|
+
interface UseTransitionNameOptions {
|
|
242
|
+
/**
|
|
243
|
+
* 进入动画样式
|
|
244
|
+
*/
|
|
245
|
+
enterClass?: string;
|
|
246
|
+
/**
|
|
247
|
+
* 离开动画样式
|
|
248
|
+
*/
|
|
249
|
+
leaveClass?: string;
|
|
250
|
+
deep?: boolean;
|
|
251
|
+
}
|
|
252
|
+
interface UseTransitionNameState {
|
|
253
|
+
position: number;
|
|
254
|
+
fullPath: string;
|
|
255
|
+
}
|
|
256
|
+
/**
|
|
257
|
+
* 配合 Transition 动画
|
|
258
|
+
*/
|
|
259
|
+
declare function useTransitionName(options: UseTransitionNameOptions): vue.Ref<string, string>;
|
|
260
|
+
|
|
50
261
|
interface UseVisibleOptions<T extends Record<string, any>, P = any> {
|
|
51
262
|
showCallback?: (options?: T) => void | Promise<void>;
|
|
52
263
|
hideCallback?: (reason: any) => void | Promise<void>;
|
|
@@ -62,4 +273,4 @@ declare function useVisible<T extends Record<string, any>, R = any>(options?: Us
|
|
|
62
273
|
hide: (reason?: any) => Promise<void>;
|
|
63
274
|
};
|
|
64
275
|
|
|
65
|
-
export { type UseAsyncTaskOptions, type UseAsyncTaskResult, type UseVisibleOptions, type UseVisibleShowOptions, useAsyncTask, useComposition, useId, useVisible };
|
|
276
|
+
export { type TransferOptions, type UseAsyncTaskOptions, type UseAsyncTaskResult, type UseBodyScrollLockReturn, type UseEventListenerOptions, type UseKeepAliveOptions, type UseKeepAliveState$1 as UseKeepAliveState, type UseKeepPositionOptions, type UseKeepPositionState, type UsePagingOptions, type UsePagingStatus, type UsePagingTask, type UsePagingTaskOptions, type UsePagingTaskResult, type UsePagingTriggerOptions, type UseSetState, type UseSetStateAction, type UseTransitionNameOptions, type UseTransitionNameState, type UseVisibleOptions, type UseVisibleShowOptions, debounceRef, useAsyncTask, useBodyScrollLock, useComposition, useCountDown, useEventListener, useId, useKeepAlive, useKeepAliveByPosition, useKeepPosition, usePaging, useParams, useQuery, useState, useToggle, useTransfer, useTransitionName, useVisible };
|
package/dist/index.js
CHANGED
|
@@ -1,3 +1,31 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
|
3
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
4
|
+
var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
|
5
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
6
|
+
var __spreadValues = (a, b) => {
|
|
7
|
+
for (var prop in b || (b = {}))
|
|
8
|
+
if (__hasOwnProp.call(b, prop))
|
|
9
|
+
__defNormalProp(a, prop, b[prop]);
|
|
10
|
+
if (__getOwnPropSymbols)
|
|
11
|
+
for (var prop of __getOwnPropSymbols(b)) {
|
|
12
|
+
if (__propIsEnum.call(b, prop))
|
|
13
|
+
__defNormalProp(a, prop, b[prop]);
|
|
14
|
+
}
|
|
15
|
+
return a;
|
|
16
|
+
};
|
|
17
|
+
var __objRest = (source, exclude) => {
|
|
18
|
+
var target = {};
|
|
19
|
+
for (var prop in source)
|
|
20
|
+
if (__hasOwnProp.call(source, prop) && exclude.indexOf(prop) < 0)
|
|
21
|
+
target[prop] = source[prop];
|
|
22
|
+
if (source != null && __getOwnPropSymbols)
|
|
23
|
+
for (var prop of __getOwnPropSymbols(source)) {
|
|
24
|
+
if (exclude.indexOf(prop) < 0 && __propIsEnum.call(source, prop))
|
|
25
|
+
target[prop] = source[prop];
|
|
26
|
+
}
|
|
27
|
+
return target;
|
|
28
|
+
};
|
|
1
29
|
var __async = (__this, __arguments, generator) => {
|
|
2
30
|
return new Promise((resolve, reject) => {
|
|
3
31
|
var fulfilled = (value) => {
|
|
@@ -19,6 +47,27 @@ var __async = (__this, __arguments, generator) => {
|
|
|
19
47
|
});
|
|
20
48
|
};
|
|
21
49
|
|
|
50
|
+
// src/debounceRef/index.ts
|
|
51
|
+
import { customRef } from "vue";
|
|
52
|
+
function debounceRef(value, ms = 0) {
|
|
53
|
+
let timer;
|
|
54
|
+
return customRef((track, trigger) => {
|
|
55
|
+
return {
|
|
56
|
+
get() {
|
|
57
|
+
track();
|
|
58
|
+
return value;
|
|
59
|
+
},
|
|
60
|
+
set(val) {
|
|
61
|
+
clearTimeout(timer);
|
|
62
|
+
timer = setTimeout(() => {
|
|
63
|
+
trigger();
|
|
64
|
+
value = val;
|
|
65
|
+
}, ms);
|
|
66
|
+
}
|
|
67
|
+
};
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
|
|
22
71
|
// src/useAsyncTask/index.ts
|
|
23
72
|
import { isFunction } from "@daysnap/utils";
|
|
24
73
|
import { onActivated, onBeforeMount, ref } from "vue";
|
|
@@ -68,6 +117,66 @@ function useAsyncTask(task, options) {
|
|
|
68
117
|
};
|
|
69
118
|
}
|
|
70
119
|
|
|
120
|
+
// src/useBodyScrollLock/index.ts
|
|
121
|
+
import { getCurrentScope, isRef, onScopeDispose, ref as ref2, unref, watch } from "vue";
|
|
122
|
+
var lockCount = 0;
|
|
123
|
+
var originalOverflow = "";
|
|
124
|
+
function lockBody() {
|
|
125
|
+
if (typeof document === "undefined") return;
|
|
126
|
+
if (lockCount === 0) {
|
|
127
|
+
originalOverflow = document.body.style.overflow;
|
|
128
|
+
document.body.style.overflow = "hidden";
|
|
129
|
+
}
|
|
130
|
+
lockCount++;
|
|
131
|
+
}
|
|
132
|
+
function unlockBody() {
|
|
133
|
+
if (typeof document === "undefined" || lockCount <= 0) return;
|
|
134
|
+
lockCount--;
|
|
135
|
+
if (lockCount === 0) {
|
|
136
|
+
document.body.style.overflow = originalOverflow;
|
|
137
|
+
originalOverflow = "";
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
function useBodyScrollLock(initialLocked = false) {
|
|
141
|
+
const locked = isRef(initialLocked) ? initialLocked : ref2(unref(initialLocked));
|
|
142
|
+
let currentLocked = false;
|
|
143
|
+
const sync = (value) => {
|
|
144
|
+
if (value === currentLocked) return;
|
|
145
|
+
if (value) {
|
|
146
|
+
lockBody();
|
|
147
|
+
} else {
|
|
148
|
+
unlockBody();
|
|
149
|
+
}
|
|
150
|
+
currentLocked = value;
|
|
151
|
+
};
|
|
152
|
+
const stop = watch(locked, sync, { immediate: true });
|
|
153
|
+
const cleanup = () => {
|
|
154
|
+
stop();
|
|
155
|
+
if (currentLocked) {
|
|
156
|
+
unlockBody();
|
|
157
|
+
currentLocked = false;
|
|
158
|
+
}
|
|
159
|
+
};
|
|
160
|
+
if (getCurrentScope()) {
|
|
161
|
+
onScopeDispose(cleanup);
|
|
162
|
+
}
|
|
163
|
+
const lock = () => {
|
|
164
|
+
locked.value = true;
|
|
165
|
+
};
|
|
166
|
+
const unlock = () => {
|
|
167
|
+
locked.value = false;
|
|
168
|
+
};
|
|
169
|
+
const toggle = (value = !locked.value) => {
|
|
170
|
+
locked.value = value;
|
|
171
|
+
};
|
|
172
|
+
return {
|
|
173
|
+
locked,
|
|
174
|
+
lock,
|
|
175
|
+
unlock,
|
|
176
|
+
toggle
|
|
177
|
+
};
|
|
178
|
+
}
|
|
179
|
+
|
|
71
180
|
// src/useComposition/index.ts
|
|
72
181
|
function useComposition() {
|
|
73
182
|
const handleCompositionStart = (event) => {
|
|
@@ -86,6 +195,75 @@ function useComposition() {
|
|
|
86
195
|
};
|
|
87
196
|
}
|
|
88
197
|
|
|
198
|
+
// src/useCountDown/index.ts
|
|
199
|
+
import { ref as ref3 } from "vue";
|
|
200
|
+
function useCountDown(start = 60) {
|
|
201
|
+
const countRef = ref3(0);
|
|
202
|
+
let timer;
|
|
203
|
+
const trigger = (st = start) => {
|
|
204
|
+
clearTimeout(timer);
|
|
205
|
+
let loop;
|
|
206
|
+
(loop = (count) => {
|
|
207
|
+
countRef.value = count;
|
|
208
|
+
if (count <= 0) {
|
|
209
|
+
countRef.value = 0;
|
|
210
|
+
return;
|
|
211
|
+
}
|
|
212
|
+
count--;
|
|
213
|
+
timer = setTimeout(loop, 1e3, count);
|
|
214
|
+
})(st);
|
|
215
|
+
};
|
|
216
|
+
return [countRef, trigger];
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
// src/useEventListener/index.ts
|
|
220
|
+
import {
|
|
221
|
+
isRef as isRef2,
|
|
222
|
+
onActivated as onActivated2,
|
|
223
|
+
onDeactivated,
|
|
224
|
+
onMounted,
|
|
225
|
+
onUnmounted,
|
|
226
|
+
unref as unref2,
|
|
227
|
+
watch as watch2
|
|
228
|
+
} from "vue";
|
|
229
|
+
function useEventListener(type, listener, options = {}) {
|
|
230
|
+
const { target = window, passive = false, capture = false } = options;
|
|
231
|
+
let attached;
|
|
232
|
+
const add = (target2) => {
|
|
233
|
+
let element = unref2(target2);
|
|
234
|
+
if (element == null ? void 0 : element.$el) {
|
|
235
|
+
element = element.$el;
|
|
236
|
+
}
|
|
237
|
+
if (element && !attached) {
|
|
238
|
+
element.addEventListener(type, listener, {
|
|
239
|
+
capture,
|
|
240
|
+
passive
|
|
241
|
+
});
|
|
242
|
+
attached = true;
|
|
243
|
+
}
|
|
244
|
+
};
|
|
245
|
+
const remove = (target2) => {
|
|
246
|
+
let element = unref2(target2);
|
|
247
|
+
if (element == null ? void 0 : element.$el) {
|
|
248
|
+
element = element.$el;
|
|
249
|
+
}
|
|
250
|
+
if (element && attached) {
|
|
251
|
+
element.removeEventListener(type, listener, capture);
|
|
252
|
+
attached = false;
|
|
253
|
+
}
|
|
254
|
+
};
|
|
255
|
+
onUnmounted(() => remove(target));
|
|
256
|
+
onDeactivated(() => remove(target));
|
|
257
|
+
onActivated2(() => add(target));
|
|
258
|
+
onMounted(() => add(target));
|
|
259
|
+
if (isRef2(target)) {
|
|
260
|
+
watch2(target, (val, oldVal) => {
|
|
261
|
+
remove(oldVal);
|
|
262
|
+
add(val);
|
|
263
|
+
});
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
|
|
89
267
|
// src/useId/index.ts
|
|
90
268
|
import { getCurrentInstance } from "vue";
|
|
91
269
|
var current = 0;
|
|
@@ -95,11 +273,403 @@ function useId() {
|
|
|
95
273
|
return `${name}-${++current}`;
|
|
96
274
|
}
|
|
97
275
|
|
|
276
|
+
// src/useKeepAlive/index.ts
|
|
277
|
+
import { isNumber, isUndefined } from "@daysnap/utils";
|
|
278
|
+
import { ref as ref4, watch as watch3 } from "vue";
|
|
279
|
+
import { useRoute } from "vue-router";
|
|
280
|
+
var keepAliveList = ref4();
|
|
281
|
+
var includes = ref4([]);
|
|
282
|
+
var getHistoryState = () => {
|
|
283
|
+
var _a;
|
|
284
|
+
return (_a = history.state) != null ? _a : {};
|
|
285
|
+
};
|
|
286
|
+
function useKeepAlive(options) {
|
|
287
|
+
var _b;
|
|
288
|
+
if (isUndefined(keepAliveList.value)) {
|
|
289
|
+
keepAliveList.value = [];
|
|
290
|
+
const route = useRoute();
|
|
291
|
+
watch3(
|
|
292
|
+
() => __spreadValues({}, route),
|
|
293
|
+
(to, from) => {
|
|
294
|
+
var _a2;
|
|
295
|
+
const { position, replaced, scroll } = getHistoryState();
|
|
296
|
+
keepAliveList.value = ((_a2 = keepAliveList.value) != null ? _a2 : []).filter((item) => {
|
|
297
|
+
const { mode, relations } = item;
|
|
298
|
+
if (mode === "custom") {
|
|
299
|
+
return true;
|
|
300
|
+
}
|
|
301
|
+
if (replaced && scroll === false) {
|
|
302
|
+
if (relations == null ? void 0 : relations.length) {
|
|
303
|
+
const formName = from.name;
|
|
304
|
+
if (relations.includes(formName)) {
|
|
305
|
+
const toName = to.name;
|
|
306
|
+
return relations.includes(toName);
|
|
307
|
+
}
|
|
308
|
+
}
|
|
309
|
+
return position > item.position;
|
|
310
|
+
}
|
|
311
|
+
return position >= item.position;
|
|
312
|
+
});
|
|
313
|
+
}
|
|
314
|
+
);
|
|
315
|
+
const ms = isNumber(options) ? options : 0;
|
|
316
|
+
watch3(
|
|
317
|
+
() => keepAliveList.value,
|
|
318
|
+
(nv, ov) => {
|
|
319
|
+
if (JSON.stringify(nv) !== JSON.stringify(ov)) {
|
|
320
|
+
setTimeout(() => {
|
|
321
|
+
var _a2, _b2;
|
|
322
|
+
includes.value = (_b2 = (_a2 = keepAliveList.value) == null ? void 0 : _a2.map((item) => item.name)) != null ? _b2 : [];
|
|
323
|
+
}, ms);
|
|
324
|
+
}
|
|
325
|
+
},
|
|
326
|
+
{
|
|
327
|
+
immediate: true
|
|
328
|
+
}
|
|
329
|
+
);
|
|
330
|
+
}
|
|
331
|
+
if (!isNumber(options)) {
|
|
332
|
+
const _a = useRoute(), { meta } = _a, rest = __objRest(_a, ["meta"]);
|
|
333
|
+
const { name, mode, relations } = Object.assign({ mode: "auto" }, rest, meta, options);
|
|
334
|
+
const { position = 0 } = getHistoryState();
|
|
335
|
+
if (name) {
|
|
336
|
+
keepAliveList.value = [
|
|
337
|
+
...((_b = keepAliveList.value) != null ? _b : []).filter((item) => item.name !== name),
|
|
338
|
+
{ position, name, mode, relations }
|
|
339
|
+
];
|
|
340
|
+
}
|
|
341
|
+
}
|
|
342
|
+
return {
|
|
343
|
+
keepAliveList,
|
|
344
|
+
includes
|
|
345
|
+
};
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
// src/useKeepAliveByPosition/index.ts
|
|
349
|
+
import { isBoolean, isUndefined as isUndefined2 } from "@daysnap/utils";
|
|
350
|
+
import { computed, ref as ref5, watch as watch4 } from "vue";
|
|
351
|
+
import { useRoute as useRoute2 } from "vue-router";
|
|
352
|
+
var keepAliveList2 = ref5();
|
|
353
|
+
var setState = (state, ms = 0) => {
|
|
354
|
+
if (JSON.stringify(state) !== JSON.stringify(keepAliveList2.value)) {
|
|
355
|
+
setTimeout(() => keepAliveList2.value = state, ms);
|
|
356
|
+
}
|
|
357
|
+
};
|
|
358
|
+
var keep = (data) => {
|
|
359
|
+
var _a;
|
|
360
|
+
const { name } = data;
|
|
361
|
+
const list = [...((_a = keepAliveList2.value) != null ? _a : []).filter((item) => item.name !== name), data];
|
|
362
|
+
setState(list);
|
|
363
|
+
};
|
|
364
|
+
var includes2 = computed(() => keepAliveList2.value.map((item) => item.name));
|
|
365
|
+
var getState = () => {
|
|
366
|
+
var _a;
|
|
367
|
+
return (_a = history.state) != null ? _a : {};
|
|
368
|
+
};
|
|
369
|
+
function useKeepAliveByPosition(options) {
|
|
370
|
+
if (isUndefined2(keepAliveList2.value)) {
|
|
371
|
+
keepAliveList2.value = [];
|
|
372
|
+
const route = useRoute2();
|
|
373
|
+
watch4(
|
|
374
|
+
() => __spreadValues({}, route),
|
|
375
|
+
(_, from) => {
|
|
376
|
+
var _a2;
|
|
377
|
+
const { position, replaced } = getState();
|
|
378
|
+
const list = ((_a2 = keepAliveList2.value) != null ? _a2 : []).filter((item) => {
|
|
379
|
+
const { mode, name } = item;
|
|
380
|
+
if (mode === "custom") {
|
|
381
|
+
return true;
|
|
382
|
+
}
|
|
383
|
+
if (replaced && from.name === name) {
|
|
384
|
+
return false;
|
|
385
|
+
}
|
|
386
|
+
return position >= item.position;
|
|
387
|
+
});
|
|
388
|
+
setState(list, 300);
|
|
389
|
+
}
|
|
390
|
+
);
|
|
391
|
+
}
|
|
392
|
+
if (!isBoolean(options)) {
|
|
393
|
+
const _a = useRoute2(), { meta } = _a, rest = __objRest(_a, ["meta"]);
|
|
394
|
+
const { name, mode } = Object.assign({ mode: "auto" }, rest, meta, options);
|
|
395
|
+
const { position = 0 } = getState();
|
|
396
|
+
if (name) {
|
|
397
|
+
keep({ position, name, mode });
|
|
398
|
+
}
|
|
399
|
+
}
|
|
400
|
+
return {
|
|
401
|
+
keepAliveList: keepAliveList2,
|
|
402
|
+
includes: includes2,
|
|
403
|
+
keep
|
|
404
|
+
};
|
|
405
|
+
}
|
|
406
|
+
|
|
407
|
+
// src/useKeepPosition/index.ts
|
|
408
|
+
import { pick } from "@daysnap/utils";
|
|
409
|
+
import { nextTick, onActivated as onActivated3, ref as ref6 } from "vue";
|
|
410
|
+
import { onBeforeRouteLeave } from "vue-router";
|
|
411
|
+
function useKeepPosition(options = {}) {
|
|
412
|
+
const { selectors } = options;
|
|
413
|
+
const state = ref6();
|
|
414
|
+
const getTarget = () => {
|
|
415
|
+
var _a, _b;
|
|
416
|
+
return (_b = (_a = options.getTarget) == null ? void 0 : _a.call(options)) != null ? _b : selectors ? document.querySelector(selectors) : document.documentElement;
|
|
417
|
+
};
|
|
418
|
+
const keep2 = () => {
|
|
419
|
+
const target = getTarget();
|
|
420
|
+
if (target) {
|
|
421
|
+
state.value = pick(target, ["scrollTop", "scrollLeft"]);
|
|
422
|
+
}
|
|
423
|
+
};
|
|
424
|
+
const restore = () => {
|
|
425
|
+
nextTick(() => {
|
|
426
|
+
var _a;
|
|
427
|
+
const target = getTarget();
|
|
428
|
+
if (target) {
|
|
429
|
+
const { scrollLeft = 0, scrollTop = 0 } = (_a = state.value) != null ? _a : {};
|
|
430
|
+
state.value = void 0;
|
|
431
|
+
const setScroll = () => {
|
|
432
|
+
target.scrollLeft = scrollLeft;
|
|
433
|
+
target.scrollTop = scrollTop;
|
|
434
|
+
};
|
|
435
|
+
setScroll();
|
|
436
|
+
}
|
|
437
|
+
});
|
|
438
|
+
};
|
|
439
|
+
onBeforeRouteLeave(() => keep2());
|
|
440
|
+
onActivated3(() => restore());
|
|
441
|
+
return { keep: keep2, restore };
|
|
442
|
+
}
|
|
443
|
+
|
|
444
|
+
// src/usePaging/index.ts
|
|
445
|
+
import { isArray, isBoolean as isBoolean2, isFunction as isFunction2, isObject, isString } from "@daysnap/utils";
|
|
446
|
+
import { computed as computed2, onBeforeMount as onBeforeMount2, reactive, ref as ref7 } from "vue";
|
|
447
|
+
function usePaging(task, options = {}) {
|
|
448
|
+
const { initialStatus, immediate, scrollSelector = ".hor-scroll" } = options;
|
|
449
|
+
const pagingStatus = reactive(
|
|
450
|
+
Object.assign(
|
|
451
|
+
{
|
|
452
|
+
pagingIndex: 0,
|
|
453
|
+
pagingSize: 10,
|
|
454
|
+
pagingTotal: -1,
|
|
455
|
+
pagingError: "",
|
|
456
|
+
pagingLoading: ""
|
|
457
|
+
},
|
|
458
|
+
initialStatus
|
|
459
|
+
)
|
|
460
|
+
);
|
|
461
|
+
const scrollTop = () => {
|
|
462
|
+
if (scrollSelector) {
|
|
463
|
+
const els = document.querySelectorAll(scrollSelector);
|
|
464
|
+
const el = els[els.length - 1];
|
|
465
|
+
if (el) {
|
|
466
|
+
el.scrollTop = 0;
|
|
467
|
+
}
|
|
468
|
+
}
|
|
469
|
+
};
|
|
470
|
+
const pagingData = ref7([]);
|
|
471
|
+
const pagingReset = () => {
|
|
472
|
+
pagingStatus.pagingIndex = 0;
|
|
473
|
+
pagingStatus.pagingTotal = -1;
|
|
474
|
+
pagingStatus.pagingError = "";
|
|
475
|
+
pagingStatus.pagingLoading = false;
|
|
476
|
+
pagingData.value = [];
|
|
477
|
+
};
|
|
478
|
+
const pagingTrigger = (pagingIndex, options2) => {
|
|
479
|
+
const opt = { loading: false, toast: false };
|
|
480
|
+
if (isBoolean2(options2)) {
|
|
481
|
+
opt.loading = options2;
|
|
482
|
+
opt.toast = options2;
|
|
483
|
+
} else if (isObject(options2)) {
|
|
484
|
+
Object.assign(opt, options2);
|
|
485
|
+
}
|
|
486
|
+
if (pagingIndex === 1) {
|
|
487
|
+
setTimeout(scrollTop);
|
|
488
|
+
}
|
|
489
|
+
pagingStatus.pagingLoading = true;
|
|
490
|
+
pagingStatus.pagingError = "";
|
|
491
|
+
const promise = task([pagingIndex, pagingStatus.pagingSize], opt).then((res) => {
|
|
492
|
+
let pagingList = [];
|
|
493
|
+
let pagingTotal = -1;
|
|
494
|
+
if (isArray(res)) {
|
|
495
|
+
;
|
|
496
|
+
[pagingList, pagingTotal] = res;
|
|
497
|
+
} else {
|
|
498
|
+
;
|
|
499
|
+
({ pagingList, pagingTotal } = res);
|
|
500
|
+
}
|
|
501
|
+
if (!pagingList) {
|
|
502
|
+
pagingList = [];
|
|
503
|
+
}
|
|
504
|
+
pagingStatus.pagingIndex = pagingIndex;
|
|
505
|
+
pagingStatus.pagingTotal = pagingTotal;
|
|
506
|
+
pagingData.value = pagingIndex === 1 ? pagingList : [...pagingData.value, ...pagingList];
|
|
507
|
+
if (!pagingList.length) {
|
|
508
|
+
pagingStatus.pagingTotal = pagingData.value.length;
|
|
509
|
+
}
|
|
510
|
+
});
|
|
511
|
+
(promise.toast ? promise.toast((_, msg) => {
|
|
512
|
+
pagingStatus.pagingError = msg;
|
|
513
|
+
return opt.toast;
|
|
514
|
+
}) : promise.catch((err) => {
|
|
515
|
+
pagingStatus.pagingError = isString(err) ? err : JSON.stringify(err);
|
|
516
|
+
})).finally(() => {
|
|
517
|
+
pagingStatus.pagingLoading = false;
|
|
518
|
+
if (isFunction2(options2)) {
|
|
519
|
+
options2(pagingStatus.pagingError);
|
|
520
|
+
}
|
|
521
|
+
if (pagingStatus.pagingError && pagingIndex === 1) {
|
|
522
|
+
pagingStatus.pagingIndex = pagingIndex;
|
|
523
|
+
pagingStatus.pagingTotal = -1;
|
|
524
|
+
pagingData.value = [];
|
|
525
|
+
}
|
|
526
|
+
});
|
|
527
|
+
};
|
|
528
|
+
const pagingRefresh = (options2) => {
|
|
529
|
+
pagingTrigger(1, options2);
|
|
530
|
+
};
|
|
531
|
+
const pagingLoad = (options2) => {
|
|
532
|
+
if (pagingFinished.value) {
|
|
533
|
+
if (isFunction2(options2)) {
|
|
534
|
+
options2();
|
|
535
|
+
}
|
|
536
|
+
return console.log("\u6CA1\u6709\u66F4\u591A\u4E86...");
|
|
537
|
+
}
|
|
538
|
+
if (pagingStatus.pagingLoading) {
|
|
539
|
+
return console.log("\u6B63\u5728\u52A0\u8F7D\u4E2D...");
|
|
540
|
+
}
|
|
541
|
+
pagingTrigger(pagingStatus.pagingIndex + 1, options2);
|
|
542
|
+
};
|
|
543
|
+
const pagingFinished = computed2(() => {
|
|
544
|
+
return pagingStatus.pagingTotal > 0 && pagingData.value.length >= pagingStatus.pagingTotal;
|
|
545
|
+
});
|
|
546
|
+
onBeforeMount2(() => {
|
|
547
|
+
if (immediate) {
|
|
548
|
+
pagingRefresh();
|
|
549
|
+
}
|
|
550
|
+
});
|
|
551
|
+
return {
|
|
552
|
+
pagingFinished,
|
|
553
|
+
pagingData,
|
|
554
|
+
pagingStatus,
|
|
555
|
+
pagingTrigger,
|
|
556
|
+
pagingRefresh,
|
|
557
|
+
pagingLoad,
|
|
558
|
+
pagingReset
|
|
559
|
+
};
|
|
560
|
+
}
|
|
561
|
+
|
|
562
|
+
// src/useParams/index.ts
|
|
563
|
+
import { useRoute as useRoute3 } from "vue-router";
|
|
564
|
+
function useParams() {
|
|
565
|
+
const { params } = useRoute3();
|
|
566
|
+
return params;
|
|
567
|
+
}
|
|
568
|
+
|
|
569
|
+
// src/useQuery/index.ts
|
|
570
|
+
import { useRoute as useRoute4 } from "vue-router";
|
|
571
|
+
function useQuery() {
|
|
572
|
+
const { query } = useRoute4();
|
|
573
|
+
return query;
|
|
574
|
+
}
|
|
575
|
+
|
|
576
|
+
// src/useState/index.ts
|
|
577
|
+
import { isFunction as isFunction3 } from "@daysnap/utils";
|
|
578
|
+
import { ref as ref8 } from "vue";
|
|
579
|
+
function useState(initialState) {
|
|
580
|
+
const state = ref8(initialState);
|
|
581
|
+
const setState2 = (value) => {
|
|
582
|
+
state.value = isFunction3(value) ? value(state.value) : value;
|
|
583
|
+
};
|
|
584
|
+
return [state, setState2];
|
|
585
|
+
}
|
|
586
|
+
|
|
587
|
+
// src/useToggle/index.ts
|
|
588
|
+
import { ref as ref9 } from "vue";
|
|
589
|
+
function useToggle(initialValue = false) {
|
|
590
|
+
const visible = ref9(initialValue);
|
|
591
|
+
const toggle = (value = !visible.value) => {
|
|
592
|
+
visible.value = value;
|
|
593
|
+
};
|
|
594
|
+
return [visible, toggle];
|
|
595
|
+
}
|
|
596
|
+
|
|
597
|
+
// src/useTransfer/index.ts
|
|
598
|
+
import { useRouter } from "vue-router";
|
|
599
|
+
function useTransfer() {
|
|
600
|
+
const router = useRouter();
|
|
601
|
+
return (..._0) => __async(null, [..._0], function* (options = {}, ...args) {
|
|
602
|
+
const _a = options, { disabled, path, replace = false, fn, query, to } = _a, rest = __objRest(_a, ["disabled", "path", "replace", "fn", "query", "to"]);
|
|
603
|
+
if (disabled) {
|
|
604
|
+
return;
|
|
605
|
+
}
|
|
606
|
+
if (to) {
|
|
607
|
+
router.push(to);
|
|
608
|
+
return;
|
|
609
|
+
}
|
|
610
|
+
if (path) {
|
|
611
|
+
if (!query) {
|
|
612
|
+
rest.query = __spreadValues({}, options);
|
|
613
|
+
}
|
|
614
|
+
router.push(__spreadValues({ path, replace }, rest));
|
|
615
|
+
return;
|
|
616
|
+
}
|
|
617
|
+
if (fn) {
|
|
618
|
+
yield fn(options, ...args);
|
|
619
|
+
return;
|
|
620
|
+
}
|
|
621
|
+
});
|
|
622
|
+
}
|
|
623
|
+
|
|
624
|
+
// src/useTransitionName/index.ts
|
|
625
|
+
import { ref as ref10, watch as watch5 } from "vue";
|
|
626
|
+
import { useRoute as useRoute5 } from "vue-router";
|
|
627
|
+
var KEY = "$$useTransitionName__state";
|
|
628
|
+
var get = () => {
|
|
629
|
+
let res;
|
|
630
|
+
try {
|
|
631
|
+
res = JSON.parse(window.sessionStorage.getItem(KEY));
|
|
632
|
+
} catch (e) {
|
|
633
|
+
}
|
|
634
|
+
return res;
|
|
635
|
+
};
|
|
636
|
+
var set = (state) => {
|
|
637
|
+
window.sessionStorage.setItem(KEY, JSON.stringify(state));
|
|
638
|
+
};
|
|
639
|
+
function useTransitionName(options) {
|
|
640
|
+
const { deep } = options;
|
|
641
|
+
const name = ref10("");
|
|
642
|
+
const route = useRoute5();
|
|
643
|
+
watch5(
|
|
644
|
+
() => __spreadValues({}, route),
|
|
645
|
+
(to, from) => {
|
|
646
|
+
const { position } = history.state || {};
|
|
647
|
+
const prevState = get();
|
|
648
|
+
if (prevState && prevState.fullPath !== to.fullPath) {
|
|
649
|
+
if (position >= prevState.position) {
|
|
650
|
+
const { enterClass } = Object.assign({}, options, to.meta, to.query);
|
|
651
|
+
name.value = enterClass != null ? enterClass : "";
|
|
652
|
+
} else {
|
|
653
|
+
const { leaveClass } = Object.assign({}, options, from.meta, from.query);
|
|
654
|
+
name.value = leaveClass != null ? leaveClass : "";
|
|
655
|
+
}
|
|
656
|
+
} else {
|
|
657
|
+
name.value = "";
|
|
658
|
+
}
|
|
659
|
+
set({ fullPath: to.fullPath, position });
|
|
660
|
+
},
|
|
661
|
+
{
|
|
662
|
+
deep
|
|
663
|
+
}
|
|
664
|
+
);
|
|
665
|
+
return name;
|
|
666
|
+
}
|
|
667
|
+
|
|
98
668
|
// src/useVisible/index.ts
|
|
99
|
-
import { ref as
|
|
669
|
+
import { ref as ref11 } from "vue";
|
|
100
670
|
function useVisible(options = {}) {
|
|
101
671
|
const { showCallback, hideCallback, confirmCallback } = options;
|
|
102
|
-
const visible =
|
|
672
|
+
const visible = ref11(false);
|
|
103
673
|
let resolve;
|
|
104
674
|
let reject;
|
|
105
675
|
let beforeClose;
|
|
@@ -141,8 +711,22 @@ function useVisible(options = {}) {
|
|
|
141
711
|
};
|
|
142
712
|
}
|
|
143
713
|
export {
|
|
714
|
+
debounceRef,
|
|
144
715
|
useAsyncTask,
|
|
716
|
+
useBodyScrollLock,
|
|
145
717
|
useComposition,
|
|
718
|
+
useCountDown,
|
|
719
|
+
useEventListener,
|
|
146
720
|
useId,
|
|
721
|
+
useKeepAlive,
|
|
722
|
+
useKeepAliveByPosition,
|
|
723
|
+
useKeepPosition,
|
|
724
|
+
usePaging,
|
|
725
|
+
useParams,
|
|
726
|
+
useQuery,
|
|
727
|
+
useState,
|
|
728
|
+
useToggle,
|
|
729
|
+
useTransfer,
|
|
730
|
+
useTransitionName,
|
|
147
731
|
useVisible
|
|
148
732
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@vrojs/use",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.3",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "vrojs composables",
|
|
6
6
|
"main": "dist/index.js",
|
|
@@ -28,7 +28,7 @@
|
|
|
28
28
|
"vue-router": ">=4.2.4"
|
|
29
29
|
},
|
|
30
30
|
"devDependencies": {
|
|
31
|
-
"@daysnap/utils": "^0.1.
|
|
31
|
+
"@daysnap/utils": "^0.1.36",
|
|
32
32
|
"rimraf": "^6.0.1",
|
|
33
33
|
"tsup": "^8.5.1",
|
|
34
34
|
"typescript": "^5.9.3",
|
|
@@ -39,13 +39,5 @@
|
|
|
39
39
|
"clean": "rimraf ./dist",
|
|
40
40
|
"release": "npm run build && dsc publish --pnpm",
|
|
41
41
|
"sync": "dsc sync"
|
|
42
|
-
},
|
|
43
|
-
"exports": {
|
|
44
|
-
".": {
|
|
45
|
-
"types": "./dist/index.d.ts",
|
|
46
|
-
"import": "./dist/index.js",
|
|
47
|
-
"default": "./dist/index.js"
|
|
48
|
-
},
|
|
49
|
-
"./package.json": "./package.json"
|
|
50
42
|
}
|
|
51
43
|
}
|