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