@whitesev/utils 2.2.9 → 2.3.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/dist/index.esm.js CHANGED
@@ -3645,6 +3645,245 @@ class WindowApi {
3645
3645
  }
3646
3646
  }
3647
3647
 
3648
+ const VueUtils = {
3649
+ /** 标签 */
3650
+ ReactiveFlags: {
3651
+ IS_REACTIVE: Symbol("isReactive"),
3652
+ },
3653
+ /**
3654
+ * 判断是否是对象
3655
+ * @param value
3656
+ */
3657
+ isObject(value) {
3658
+ return typeof value === "object" && value !== null;
3659
+ },
3660
+ /**
3661
+ * 判断是否是函数
3662
+ * @param val
3663
+ */
3664
+ isFunction(val) {
3665
+ return typeof val === "function";
3666
+ },
3667
+ /**
3668
+ * 处理对象再次代理,可以直接返回
3669
+ * @param value
3670
+ */
3671
+ isReactive(value) {
3672
+ return !!(value && value[VueUtils.ReactiveFlags.IS_REACTIVE]);
3673
+ },
3674
+ /**
3675
+ * 判断是否是数组
3676
+ * @param value
3677
+ */
3678
+ isArray(value) {
3679
+ return Array.isArray(value);
3680
+ },
3681
+ };
3682
+ class ReactiveEffect {
3683
+ deps = [];
3684
+ active = true;
3685
+ fn;
3686
+ // @ts-ignore
3687
+ scheduler;
3688
+ constructor(fn, scheduler) {
3689
+ this.fn = fn;
3690
+ this.scheduler = scheduler;
3691
+ }
3692
+ run(cb) {
3693
+ if (!this.active) {
3694
+ this.fn();
3695
+ }
3696
+ try {
3697
+ if (typeof cb === "function") {
3698
+ cb(this);
3699
+ }
3700
+ return this.fn();
3701
+ }
3702
+ finally {
3703
+ if (typeof cb === "function") {
3704
+ cb(undefined);
3705
+ }
3706
+ }
3707
+ }
3708
+ }
3709
+ class RefImpl {
3710
+ _value;
3711
+ _isRef = true;
3712
+ _rawValue;
3713
+ _vue;
3714
+ constructor(vueIns, rawValue) {
3715
+ this._vue = vueIns;
3716
+ this._rawValue = rawValue;
3717
+ this._value = this._vue.toReactive(rawValue);
3718
+ }
3719
+ get value() {
3720
+ return this._value;
3721
+ }
3722
+ set value(newValue) {
3723
+ if (newValue !== this._rawValue) {
3724
+ this._value = this._vue.toReactive(newValue);
3725
+ this._rawValue = newValue;
3726
+ }
3727
+ }
3728
+ }
3729
+ class ObjectRefImpl {
3730
+ object;
3731
+ key;
3732
+ constructor(object, key) {
3733
+ this.object = object;
3734
+ this.key = key;
3735
+ }
3736
+ get value() {
3737
+ return this.object[this.key];
3738
+ }
3739
+ set value(newValue) {
3740
+ this.object[this.key] = newValue;
3741
+ }
3742
+ }
3743
+ class Vue {
3744
+ reactMap = new WeakMap();
3745
+ targetMap = new WeakMap();
3746
+ activeEffect = undefined;
3747
+ constructor() {
3748
+ // 将数据转化成响应式的数据,只能做对象的代理
3749
+ }
3750
+ /**
3751
+ * 生成一个被代理的对象
3752
+ * @param target 需要代理的对象
3753
+ */
3754
+ reactive(target) {
3755
+ const that = this;
3756
+ if (!(typeof target === "object" && target !== null)) {
3757
+ // @ts-ignore
3758
+ return;
3759
+ }
3760
+ if (VueUtils.isReactive(target)) {
3761
+ return target;
3762
+ }
3763
+ let exisProxy = this.reactMap.get(target);
3764
+ if (exisProxy) {
3765
+ return exisProxy;
3766
+ }
3767
+ const proxy = new Proxy(target, {
3768
+ get(target, key, receiver) {
3769
+ if (key === VueUtils.ReactiveFlags.IS_REACTIVE) {
3770
+ return true;
3771
+ }
3772
+ that.track(target, "get", key);
3773
+ return Reflect.get(target, key, receiver);
3774
+ },
3775
+ set(target, key, value, receiver) {
3776
+ let oldValue = target[key];
3777
+ let result = Reflect.set(target, key, value, receiver);
3778
+ if (oldValue !== value) {
3779
+ that.trigger(target, "set", key, oldValue, value);
3780
+ }
3781
+ return result;
3782
+ },
3783
+ });
3784
+ that.reactMap.set(target, proxy);
3785
+ return proxy;
3786
+ }
3787
+ /**
3788
+ * 观察被reactive的对象值改变
3789
+ * @param source 被观察的对象,这里采用函数返回对象
3790
+ * @param changeCallBack 值改变的回调
3791
+ */
3792
+ watch(source, changeCallBack) {
3793
+ let getter;
3794
+ if (VueUtils.isReactive(source)) {
3795
+ getter = () => this.traversal(source);
3796
+ }
3797
+ else if (VueUtils.isFunction(source)) {
3798
+ getter = source;
3799
+ }
3800
+ else {
3801
+ return;
3802
+ }
3803
+ let oldValue;
3804
+ const job = () => {
3805
+ const newValue = effect.run((activeEffect) => {
3806
+ this.activeEffect = activeEffect;
3807
+ });
3808
+ changeCallBack(newValue, oldValue);
3809
+ oldValue = newValue;
3810
+ };
3811
+ const effect = new ReactiveEffect(getter, job);
3812
+ oldValue = effect.run((activeEffect) => {
3813
+ this.activeEffect = activeEffect;
3814
+ });
3815
+ }
3816
+ toReactive(value) {
3817
+ return VueUtils.isObject(value) ? this.reactive(value) : value;
3818
+ }
3819
+ ref(value) {
3820
+ return new RefImpl(this, value);
3821
+ }
3822
+ toRef(object, key) {
3823
+ return new ObjectRefImpl(object, key);
3824
+ }
3825
+ toRefs(object) {
3826
+ const result = VueUtils.isArray(object) ? new Array(object.length) : {};
3827
+ for (let key in object) {
3828
+ // @ts-ignore
3829
+ result[key] = this.toRef(object, key);
3830
+ }
3831
+ return result;
3832
+ }
3833
+ trigger(target, type, key, oldValue, value) {
3834
+ const depsMap = this.targetMap.get(target);
3835
+ if (!depsMap)
3836
+ return;
3837
+ const effects = depsMap.get(key);
3838
+ this.triggerEffect(effects, "effects");
3839
+ }
3840
+ triggerEffect(effects, name) {
3841
+ effects &&
3842
+ effects.forEach((effect) => {
3843
+ if (effect.scheduler) {
3844
+ effect.scheduler();
3845
+ }
3846
+ else {
3847
+ effect.run();
3848
+ }
3849
+ });
3850
+ }
3851
+ track(target, type, key) {
3852
+ if (!this.activeEffect)
3853
+ return;
3854
+ let depsMap = this.targetMap.get(target);
3855
+ if (!depsMap) {
3856
+ this.targetMap.set(target, (depsMap = new Map()));
3857
+ }
3858
+ let dep = depsMap.get(key);
3859
+ if (!dep) {
3860
+ depsMap.set(key, (dep = new Set()));
3861
+ }
3862
+ this.trackEffect(dep);
3863
+ }
3864
+ trackEffect(dep) {
3865
+ if (this.activeEffect) {
3866
+ let shouldTrack = !dep.has(this.activeEffect);
3867
+ if (shouldTrack) {
3868
+ dep.add(this.activeEffect);
3869
+ this.activeEffect.deps.push(dep);
3870
+ }
3871
+ }
3872
+ }
3873
+ traversal(value, set = new Set()) {
3874
+ if (!VueUtils.isObject(value))
3875
+ return value;
3876
+ if (set.has(value)) {
3877
+ return value;
3878
+ }
3879
+ set.add(value);
3880
+ for (let key in value) {
3881
+ this.traversal(value[key], set);
3882
+ }
3883
+ return value;
3884
+ }
3885
+ }
3886
+
3648
3887
  class Utils {
3649
3888
  windowApi;
3650
3889
  constructor(option) {
@@ -5764,7 +6003,6 @@ class Utils {
5764
6003
  }
5765
6004
  /**
5766
6005
  * 申请剪贴板权限
5767
- * @returns {Promise<boolean>}
5768
6006
  */
5769
6007
  requestClipboardPermission() {
5770
6008
  return new Promise((resolve, reject) => {
@@ -5777,9 +6015,7 @@ class Utils {
5777
6015
  .then((permissionStatus) => {
5778
6016
  resolve(true);
5779
6017
  })
5780
- .catch(
5781
- /** @param {TypeError} error */
5782
- (error) => {
6018
+ .catch((error) => {
5783
6019
  console.error([
5784
6020
  "申请剪贴板权限失败,尝试直接写入👉",
5785
6021
  error.message ?? error.name ?? error.stack,
@@ -6712,6 +6948,10 @@ class Utils {
6712
6948
  * Utils.generateUUID()
6713
6949
  */
6714
6950
  generateUUID = GenerateUUID;
6951
+ /**
6952
+ * 自定义的动态响应对象
6953
+ */
6954
+ Vue = Vue;
6715
6955
  }
6716
6956
  let utils = new Utils();
6717
6957