@whitesev/utils 2.2.9 → 2.3.1

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/src/Utils.ts CHANGED
@@ -16,6 +16,7 @@ import type { Vue2Object } from "./VueObject";
16
16
  import type { UtilsAjaxHookResult } from "./AjaxHookerType";
17
17
  import { GenerateUUID } from "./UtilsCommon";
18
18
  import { WindowApi, type UtilsWindowApiOption } from "./WindowApi";
19
+ import { Vue } from "./Vue";
19
20
 
20
21
  export declare var unsafeWindow: Window & typeof globalThis;
21
22
 
@@ -1515,12 +1516,12 @@ class Utils {
1515
1516
  * Utils.getReactObj(document.querySelector("input"))?.reactProps?.onChange({target:{value:"123"}});
1516
1517
  */
1517
1518
  getReactObj(element: HTMLElement | Element): {
1518
- reactFiber?: AnyObject;
1519
- reactProps?: AnyObject;
1520
- reactEvents?: AnyObject;
1521
- reactEventHandlers?: AnyObject;
1522
- reactInternalInstance?: AnyObject;
1523
- reactContainer?: AnyObject;
1519
+ reactFiber?: any;
1520
+ reactProps?: any;
1521
+ reactEvents?: any;
1522
+ reactEventHandlers?: any;
1523
+ reactInternalInstance?: any;
1524
+ reactContainer?: any;
1524
1525
  } {
1525
1526
  let result = {};
1526
1527
  Object.keys(element).forEach((domPropsName) => {
@@ -2343,8 +2344,8 @@ class Utils {
2343
2344
  * Utils.parseObjectToArray({"工具类":"jsonToArray","return","Array"});
2344
2345
  * > ['jsonToArray', 'Array']
2345
2346
  **/
2346
- parseObjectToArray(target: AnyObject): any;
2347
- parseObjectToArray(target: AnyObject): any {
2347
+ parseObjectToArray(target: any): any;
2348
+ parseObjectToArray(target: any): any {
2348
2349
  if (typeof target !== "object") {
2349
2350
  throw new Error(
2350
2351
  "Utils.parseObjectToArray 参数 target 必须为 object 类型"
@@ -3322,7 +3323,6 @@ class Utils {
3322
3323
  }
3323
3324
  /**
3324
3325
  * 申请剪贴板权限
3325
- * @returns {Promise<boolean>}
3326
3326
  */
3327
3327
  requestClipboardPermission() {
3328
3328
  return new Promise((resolve, reject) => {
@@ -3335,16 +3335,13 @@ class Utils {
3335
3335
  .then((permissionStatus) => {
3336
3336
  resolve(true);
3337
3337
  })
3338
- .catch(
3339
- /** @param {TypeError} error */
3340
- (error) => {
3341
- console.error([
3342
- "申请剪贴板权限失败,尝试直接写入👉",
3343
- error.message ?? error.name ?? error.stack,
3344
- ]);
3345
- resolve(false);
3346
- }
3347
- );
3338
+ .catch((error: TypeError) => {
3339
+ console.error([
3340
+ "申请剪贴板权限失败,尝试直接写入👉",
3341
+ error.message ?? error.name ?? error.stack,
3342
+ ]);
3343
+ resolve(false);
3344
+ });
3348
3345
  } else {
3349
3346
  resolve(false);
3350
3347
  }
@@ -4665,11 +4662,11 @@ class Utils {
4665
4662
  *
4666
4663
  */
4667
4664
  waitProperty<T extends any>(
4668
- checkObj: AnyObject | (() => AnyObject),
4665
+ checkObj: any | (() => any),
4669
4666
  checkPropertyName: string
4670
4667
  ): Promise<T>;
4671
4668
  waitProperty<T extends any>(
4672
- checkObj: AnyObject | (() => AnyObject),
4669
+ checkObj: any | (() => any),
4673
4670
  checkPropertyName: string
4674
4671
  ): Promise<T> {
4675
4672
  return new Promise((resolve) => {
@@ -4703,13 +4700,13 @@ class Utils {
4703
4700
  * console.log("test success set");
4704
4701
  */
4705
4702
  waitPropertyByInterval<T extends any>(
4706
- checkObj: AnyObject | (() => AnyObject),
4703
+ checkObj: any | (() => any),
4707
4704
  checkPropertyName: string | ((obj: any) => boolean),
4708
4705
  intervalTimer?: number,
4709
4706
  maxTime?: number
4710
4707
  ): Promise<T>;
4711
4708
  waitPropertyByInterval<T extends any>(
4712
- checkObj: AnyObject | (() => AnyObject),
4709
+ checkObj: any | (() => any),
4713
4710
  checkPropertyName: string | ((obj: any) => boolean),
4714
4711
  intervalTimer: number = 250,
4715
4712
  maxTime: number = -1
@@ -4839,13 +4836,13 @@ class Utils {
4839
4836
  * > 111;
4840
4837
  */
4841
4838
  watchObject(
4842
- target: AnyObject,
4839
+ target: any,
4843
4840
  propertyName: string,
4844
4841
  getCallBack: (value: any) => void,
4845
4842
  setCallBack: (value: any) => void
4846
4843
  ): void;
4847
4844
  watchObject(
4848
- target: AnyObject,
4845
+ target: any,
4849
4846
  propertyName: string,
4850
4847
  getCallBack: (value: any) => void,
4851
4848
  setCallBack: (value: any) => void
@@ -4970,6 +4967,10 @@ class Utils {
4970
4967
  * Utils.generateUUID()
4971
4968
  */
4972
4969
  generateUUID = GenerateUUID;
4970
+ /**
4971
+ * 自定义的动态响应对象
4972
+ */
4973
+ Vue = Vue;
4973
4974
  }
4974
4975
 
4975
4976
  let utils = new Utils();
package/src/Vue.ts ADDED
@@ -0,0 +1,241 @@
1
+ const VueUtils = {
2
+ /** 标签 */
3
+ ReactiveFlags: {
4
+ IS_REACTIVE: Symbol("isReactive"),
5
+ },
6
+ /**
7
+ * 判断是否是对象
8
+ * @param value
9
+ */
10
+ isObject(value: any) {
11
+ return typeof value === "object" && value !== null;
12
+ },
13
+ /**
14
+ * 判断是否是函数
15
+ * @param val
16
+ */
17
+ isFunction(val: any) {
18
+ return typeof val === "function";
19
+ },
20
+ /**
21
+ * 处理对象再次代理,可以直接返回
22
+ * @param value
23
+ */
24
+ isReactive(value: any) {
25
+ return !!(value && value[VueUtils.ReactiveFlags.IS_REACTIVE]);
26
+ },
27
+ /**
28
+ * 判断是否是数组
29
+ * @param value
30
+ */
31
+ isArray(value: any): boolean {
32
+ return Array.isArray(value);
33
+ },
34
+ };
35
+
36
+ class ReactiveEffect {
37
+ deps: any[] = [];
38
+ private active = true;
39
+ private fn;
40
+ // @ts-ignore
41
+ private scheduler;
42
+ constructor(fn: Function, scheduler: any) {
43
+ this.fn = fn;
44
+ this.scheduler = scheduler;
45
+ }
46
+ run(cb: (activeEffect: any) => void) {
47
+ if (!this.active) {
48
+ this.fn();
49
+ }
50
+ try {
51
+ if (typeof cb === "function") {
52
+ cb(this);
53
+ }
54
+ return this.fn();
55
+ } finally {
56
+ if (typeof cb === "function") {
57
+ cb(undefined);
58
+ }
59
+ }
60
+ }
61
+ }
62
+ class RefImpl {
63
+ _value;
64
+ _isRef = true;
65
+ _rawValue;
66
+ _vue: Vue;
67
+ constructor(vueIns: Vue, rawValue: any) {
68
+ this._vue = vueIns;
69
+ this._rawValue = rawValue;
70
+ this._value = this._vue.toReactive(rawValue);
71
+ }
72
+ get value() {
73
+ return this._value;
74
+ }
75
+ set value(newValue) {
76
+ if (newValue !== this._rawValue) {
77
+ this._value = this._vue.toReactive(newValue);
78
+ this._rawValue = newValue;
79
+ }
80
+ }
81
+ }
82
+ class ObjectRefImpl {
83
+ object;
84
+ key;
85
+ constructor(object: any, key: any) {
86
+ this.object = object;
87
+ this.key = key;
88
+ }
89
+ get value() {
90
+ return this.object[this.key];
91
+ }
92
+ set value(newValue) {
93
+ this.object[this.key] = newValue;
94
+ }
95
+ }
96
+ export class Vue {
97
+ private reactMap = new WeakMap();
98
+ private targetMap = new WeakMap();
99
+ private activeEffect = undefined as any as ReactiveEffect;
100
+ constructor() {
101
+ // 将数据转化成响应式的数据,只能做对象的代理
102
+ }
103
+ /**
104
+ * 生成一个被代理的对象
105
+ * @param target 需要代理的对象
106
+ */
107
+ reactive<T extends object>(target: T): T {
108
+ const that = this;
109
+ if (!(typeof target === "object" && target !== null)) {
110
+ // @ts-ignore
111
+ return;
112
+ }
113
+ if (VueUtils.isReactive(target)) {
114
+ return target;
115
+ }
116
+ let exisProxy = this.reactMap.get(target);
117
+ if (exisProxy) {
118
+ return exisProxy;
119
+ }
120
+ const proxy = new Proxy(target, {
121
+ get(target, key, receiver) {
122
+ if (key === VueUtils.ReactiveFlags.IS_REACTIVE) {
123
+ return true;
124
+ }
125
+ that.track(target, "get", key);
126
+ return Reflect.get(target, key, receiver);
127
+ },
128
+ set(target, key, value, receiver) {
129
+ let oldValue = target[key as keyof T];
130
+ let result = Reflect.set(target, key, value, receiver);
131
+ if (oldValue !== value) {
132
+ that.trigger(target, "set", key, oldValue, value);
133
+ }
134
+ return result;
135
+ },
136
+ });
137
+ that.reactMap.set(target, proxy);
138
+ return proxy;
139
+ }
140
+ /**
141
+ * 观察被reactive的对象值改变
142
+ * @param source 被观察的对象,这里采用函数返回对象
143
+ * @param changeCallBack 值改变的回调
144
+ */
145
+ watch<T>(
146
+ source: () => T,
147
+ changeCallBack: (newValue: T | undefined, oldValue: T | undefined) => void
148
+ ) {
149
+ let getter;
150
+ if (VueUtils.isReactive(source)) {
151
+ getter = () => this.traversal(source);
152
+ } else if (VueUtils.isFunction(source)) {
153
+ getter = source;
154
+ } else {
155
+ return;
156
+ }
157
+ let oldValue: any;
158
+ const job = () => {
159
+ const newValue = effect.run((activeEffect) => {
160
+ this.activeEffect = activeEffect;
161
+ });
162
+ changeCallBack(newValue, oldValue);
163
+ oldValue = newValue;
164
+ };
165
+ const effect = new ReactiveEffect(getter, job);
166
+ oldValue = effect.run((activeEffect) => {
167
+ this.activeEffect = activeEffect;
168
+ });
169
+ }
170
+ toReactive(value: any) {
171
+ return VueUtils.isObject(value) ? this.reactive(value) : value;
172
+ }
173
+ ref(value: any) {
174
+ return new RefImpl(this, value);
175
+ }
176
+ toRef(object: any, key: any) {
177
+ return new ObjectRefImpl(object, key);
178
+ }
179
+ toRefs(object: any) {
180
+ const result = VueUtils.isArray(object) ? new Array(object.length) : {};
181
+ for (let key in object) {
182
+ // @ts-ignore
183
+ result[key] = this.toRef(object, key);
184
+ }
185
+ return result;
186
+ }
187
+ private trigger(
188
+ target: any,
189
+ type: string,
190
+ key: string | symbol,
191
+ oldValue: any,
192
+ value: any
193
+ ) {
194
+ const depsMap = this.targetMap.get(target);
195
+ if (!depsMap) return;
196
+ const effects = depsMap.get(key);
197
+ this.triggerEffect(effects, "effects");
198
+ }
199
+ private triggerEffect(effects: any[], name: string) {
200
+ effects &&
201
+ effects.forEach((effect) => {
202
+ if (effect.scheduler) {
203
+ effect.scheduler();
204
+ } else {
205
+ effect.run();
206
+ }
207
+ });
208
+ }
209
+ private track(target: WeakKey, type: string, key: string | symbol) {
210
+ if (!this.activeEffect) return;
211
+ let depsMap = this.targetMap.get(target);
212
+ if (!depsMap) {
213
+ this.targetMap.set(target, (depsMap = new Map()));
214
+ }
215
+ let dep = depsMap.get(key);
216
+ if (!dep) {
217
+ depsMap.set(key, (dep = new Set()));
218
+ }
219
+ this.trackEffect(dep);
220
+ }
221
+ private trackEffect(dep: any) {
222
+ if (this.activeEffect) {
223
+ let shouldTrack = !dep.has(this.activeEffect);
224
+ if (shouldTrack) {
225
+ dep.add(this.activeEffect);
226
+ this.activeEffect.deps.push(dep);
227
+ }
228
+ }
229
+ }
230
+ private traversal(value: any, set = new Set()) {
231
+ if (!VueUtils.isObject(value)) return value;
232
+ if (set.has(value)) {
233
+ return value;
234
+ }
235
+ set.add(value);
236
+ for (let key in value) {
237
+ this.traversal(value[key], set);
238
+ }
239
+ return value;
240
+ }
241
+ }
package/src/VueObject.ts CHANGED
@@ -1,52 +1,50 @@
1
- import type { AnyObject } from "./Utils";
2
-
3
- export declare interface Vue2Object extends AnyObject {
4
- $attrs: AnyObject;
1
+ export declare interface Vue2Object {
2
+ $attrs: any;
5
3
  $children: Vue2Object[];
6
4
  $createElement: (...args: any[]) => any;
7
5
  $el: HTMLElement;
8
- $listeners: AnyObject;
9
- $options: AnyObject;
6
+ $listeners: any;
7
+ $options: any;
10
8
  $parent: Vue2Object;
11
- $refs: AnyObject;
9
+ $refs: any;
12
10
  $root: Vue2Object;
13
- $scopedSlots: AnyObject;
14
- $slots: AnyObject;
15
- $store: AnyObject;
16
- $vnode: AnyObject;
11
+ $scopedSlots: any;
12
+ $slots: any;
13
+ $store: any;
14
+ $vnode: any;
17
15
 
18
- _data: AnyObject;
16
+ _data: any;
19
17
  _directInactive: boolean;
20
- _events: AnyObject;
18
+ _events: any;
21
19
  _hasHookEvent: boolean;
22
20
  _isBeingDestroyed: boolean;
23
21
  _isDestroyed: boolean;
24
22
  _isMounted: boolean;
25
23
  _isVue: boolean;
26
24
 
27
- $data: AnyObject;
25
+ $data: any;
28
26
  $isServer: boolean;
29
- $props: AnyObject;
30
- $route: AnyObject & {
27
+ $props: any;
28
+ $route: any & {
31
29
  fullPath: string;
32
30
  hash: string;
33
- matched: AnyObject[];
34
- meta: AnyObject;
31
+ matched: any[];
32
+ meta: any;
35
33
  name: string;
36
- params: AnyObject;
34
+ params: any;
37
35
  path: string;
38
- query: AnyObject;
36
+ query: any;
39
37
  };
40
- $router: AnyObject & {
38
+ $router: any & {
41
39
  afterHooks: Function[];
42
40
  app: Vue2Object;
43
41
  apps: Vue2Object[];
44
42
  beforeHooks: Function[];
45
43
  fallback: boolean;
46
- history: AnyObject & {
44
+ history: any & {
47
45
  base: string;
48
- current: AnyObject;
49
- listeners: AnyObject[];
46
+ current: any;
47
+ listeners: any[];
50
48
  router: Vue2Object["$router"];
51
49
  /**
52
50
  *
@@ -61,16 +59,16 @@ export declare interface Vue2Object extends AnyObject {
61
59
  * @param data 可选的 HistoryState 以关联该导航记录
62
60
  * @returns
63
61
  */
64
- push: (to: string, data?: AnyObject) => void;
62
+ push: (to: string, data?: any) => void;
65
63
  /**
66
64
  *
67
65
  * @param to 要设置的地址
68
66
  * @param data 可选的 HistoryState 以关联该导航记录
69
67
  * @returns
70
68
  */
71
- replace: (to: string, data?: AnyObject) => void;
69
+ replace: (to: string, data?: any) => void;
72
70
  };
73
- matcher: AnyObject & {
71
+ matcher: any & {
74
72
  addRoute: (...args: any[]) => any;
75
73
  addRoutes: (...args: any[]) => any;
76
74
  getRoutes: () => any;
@@ -78,7 +76,7 @@ export declare interface Vue2Object extends AnyObject {
78
76
  };
79
77
  mode: string;
80
78
  resolveHooks: ((...args: any[]) => any)[];
81
- currentRoute: AnyObject;
79
+ currentRoute: any;
82
80
  beforeEach: (
83
81
  callback:
84
82
  | ((
@@ -111,7 +109,7 @@ export declare interface Vue2Object extends AnyObject {
111
109
  | (() => void)
112
110
  ) => void;
113
111
  };
114
- $ssrContext: AnyObject;
112
+ $ssrContext: any;
115
113
 
116
114
  $watch: (
117
115
  key: string | string[] | (() => any),
@@ -121,6 +119,8 @@ export declare interface Vue2Object extends AnyObject {
121
119
  deep?: boolean;
122
120
  }
123
121
  ) => void;
122
+
123
+ [key: string]: any;
124
124
  }
125
125
 
126
126
  export declare interface HTMLVue2DivElement extends HTMLDivElement {