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