lyb-js 1.6.10 → 1.6.13

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.
@@ -9,7 +9,7 @@ export declare class LibJsResizeWatcher {
9
9
  private _listeners;
10
10
  /** 当前适配模式 */
11
11
  private _mode;
12
- constructor(mode: "h" | "v" | "hv");
12
+ constructor(mode?: "h" | "v" | "hv");
13
13
  /**
14
14
  * @description 注册一个窗口尺寸变化的监听器
15
15
  * @param cb 回调函数,参数为当前窗口宽度和高度
@@ -1,10 +1,8 @@
1
1
  /** @description 监听窗口变化,内部只注册一次resize事件,调用监听自身可取消监听 */
2
2
  export class LibJsResizeWatcher {
3
- constructor(mode) {
3
+ constructor(mode = "hv") {
4
4
  /** 存储所有监听器及其是否需要立即执行的标志 */
5
5
  this._listeners = [];
6
- /** 当前适配模式 */
7
- this._mode = "hv";
8
6
  /** @description 内部 resize 回调函数,调用所有注册的监听器 */
9
7
  this._handleResize = () => {
10
8
  const w = window.innerWidth;
@@ -0,0 +1,7 @@
1
+ /**
2
+ * @description 从候选数组中随机取一个未使用的元素
3
+ * @param pool 全部候选数组
4
+ * @param used 已使用数组
5
+ * @returns 未使用的随机元素,若无可用则返回 undefined
6
+ */
7
+ export declare const libJsPickUnique: <T>(pool: T[], used?: T[]) => T | undefined;
@@ -0,0 +1,14 @@
1
+ import { libJsRandom } from "../Random/LibJsRandom";
2
+ /**
3
+ * @description 从候选数组中随机取一个未使用的元素
4
+ * @param pool 全部候选数组
5
+ * @param used 已使用数组
6
+ * @returns 未使用的随机元素,若无可用则返回 undefined
7
+ */
8
+ export const libJsPickUnique = (pool, used = []) => {
9
+ const usedSet = new Set(used);
10
+ const available = pool.filter((item) => !usedSet.has(item));
11
+ if (available.length === 0)
12
+ return undefined;
13
+ return available[libJsRandom(0, available.length - 1)];
14
+ };
@@ -0,0 +1,21 @@
1
+ import { LibJsClassObservable } from "./LibJsClassObservable";
2
+ interface PullUpLoadObservable {
3
+ /** 当前加载状态文案 */
4
+ statusText: string;
5
+ /** 加载状态 */
6
+ loadStatus: "idle" | "loading" | "noMore" | "empty";
7
+ }
8
+ interface PullUpLoadParams {
9
+ /** 滚动容器 */
10
+ scrollEl: HTMLElement;
11
+ /** 加载状态元素 */
12
+ loadStatusEl: HTMLElement;
13
+ /** 触发加载回调 */
14
+ onLoad: () => void;
15
+ }
16
+ /** @description 上拉加载 */
17
+ export declare class LibJsPullUpLoad extends LibJsClassObservable<PullUpLoadObservable> {
18
+ constructor(params: PullUpLoadParams);
19
+ private checkAutoLoad;
20
+ }
21
+ export {};
@@ -0,0 +1,47 @@
1
+ import { LibJsClassObservable } from "./LibJsClassObservable";
2
+ /** @description 上拉加载 */
3
+ export class LibJsPullUpLoad extends LibJsClassObservable {
4
+ constructor(params) {
5
+ super({
6
+ statusText: "加载中...",
7
+ loadStatus: "idle",
8
+ });
9
+ const { scrollEl, loadStatusEl, onLoad } = params;
10
+ this.checkAutoLoad(onLoad, scrollEl);
11
+ this.onValue("loadStatus", (v) => {
12
+ if (v === "idle") {
13
+ this.checkAutoLoad(onLoad, scrollEl);
14
+ }
15
+ });
16
+ /** @description 滚动触发 */
17
+ scrollEl.addEventListener("scroll", () => {
18
+ //如果所有数据已加载完毕,则不再触发加载
19
+ if (["noMore", "empty", "loading"].includes(this.getValue("loadStatus")))
20
+ return;
21
+ const y = scrollEl.scrollTop;
22
+ //获取距离加载触发像素值
23
+ const loadDistance = scrollEl.scrollHeight -
24
+ scrollEl.clientHeight -
25
+ y -
26
+ loadStatusEl.offsetHeight;
27
+ if (loadDistance <= 0) {
28
+ this.setValue("loadStatus", "loading");
29
+ this.setValue("statusText", "加载中...");
30
+ setTimeout(() => {
31
+ onLoad();
32
+ }, 500);
33
+ }
34
+ });
35
+ }
36
+ // 1. 新增方法
37
+ checkAutoLoad(onLoad, scrollEl) {
38
+ if (!["noMore", "empty", "loading"].includes(this.getValue("loadStatus")) &&
39
+ scrollEl.scrollHeight <= scrollEl.clientHeight) {
40
+ this.setValue("loadStatus", "loading");
41
+ this.setValue("statusText", "加载中...");
42
+ setTimeout(() => {
43
+ onLoad();
44
+ }, 500);
45
+ }
46
+ }
47
+ }
package/README.md CHANGED
@@ -363,6 +363,10 @@ const newArr = libReverseArrayFromIndex([1, 2, 3, 4, 5], 1);
363
363
  console.log(newArr); // [1, 2, 5, 4, 3]
364
364
  ```
365
365
 
366
+ ### libJsPickUnique-随机选择未使用元素
367
+
368
+ > 从候选数组中随机取一个未使用的元素
369
+
366
370
  ## File-文件
367
371
 
368
372
  ### LibJsDownloadImageLink-图片下载
package/libJs.d.ts CHANGED
@@ -1,6 +1,7 @@
1
1
  import { LibJsNumberStepper } from "./Misc/LibJsNumberStepper";
2
2
  import { LibJsClassObservable } from "./Misc/LibJsClassObservable";
3
3
  import { LibJsResizeWatcher } from "./Base/LibJsResizeWatcher";
4
+ import { LibJsPullUpLoad } from "./Misc/LibJsPullUpLoad";
4
5
  /** @description 基础方法 */
5
6
  export declare const Base: {
6
7
  /**
@@ -112,6 +113,8 @@ export declare const Data: {
112
113
  * @link 使用方法:https://www.npmjs.com/package/lyb-js#LibReverseArrayFromIndex-数组定位翻转
113
114
  */
114
115
  libReverseArrayFromIndex: <T>(arr: T[], index: number) => T[];
116
+ /** @description 随机选择未使用元素 */
117
+ libJsPickUnique: <T>(pool: T[], used?: T[]) => T | undefined;
115
118
  };
116
119
  /** @description 文件相关方法 */
117
120
  export declare const File: {
@@ -278,6 +281,8 @@ export declare const Misc: {
278
281
  * @link 使用方法:https://www.npmjs.com/package/lyb-js#LibJsClassObservable-类属性监听器
279
282
  */
280
283
  LibJsClassObservable: typeof LibJsClassObservable;
284
+ /** @description 上拉加载 */
285
+ LibJsPullUpLoad: typeof LibJsPullUpLoad;
281
286
  };
282
287
  /** @description 随机相关方法 */
283
288
  export declare const Random: {
package/libJs.js CHANGED
@@ -43,6 +43,8 @@ import { LibJsNormalizeInRange } from "./Math/LibJsNormalizeInRange";
43
43
  import { LibJsClassObservable } from "./Misc/LibJsClassObservable";
44
44
  import { libJsCopy } from "./Browser/LibJsCopy";
45
45
  import { LibJsResizeWatcher } from "./Base/LibJsResizeWatcher";
46
+ import { LibJsPullUpLoad } from "./Misc/LibJsPullUpLoad";
47
+ import { libJsPickUnique } from "./Data/libJsPickUnique";
46
48
  /** @description 基础方法 */
47
49
  export const Base = {
48
50
  /**
@@ -151,6 +153,8 @@ export const Data = {
151
153
  * @link 使用方法:https://www.npmjs.com/package/lyb-js#LibReverseArrayFromIndex-数组定位翻转
152
154
  */
153
155
  libReverseArrayFromIndex,
156
+ /** @description 随机选择未使用元素 */
157
+ libJsPickUnique,
154
158
  };
155
159
  /** @description 文件相关方法 */
156
160
  export const File = {
@@ -290,6 +294,8 @@ export const Misc = {
290
294
  * @link 使用方法:https://www.npmjs.com/package/lyb-js#LibJsClassObservable-类属性监听器
291
295
  */
292
296
  LibJsClassObservable,
297
+ /** @description 上拉加载 */
298
+ LibJsPullUpLoad,
293
299
  };
294
300
  /** @description 随机相关方法 */
295
301
  export const Random = {
package/lyb.js CHANGED
@@ -3501,9 +3501,8 @@ ${log3.label}:`, log3.value];
3501
3501
  }
3502
3502
  };
3503
3503
  class LibJsResizeWatcher {
3504
- constructor(mode) {
3504
+ constructor(mode = "hv") {
3505
3505
  this._listeners = [];
3506
- this._mode = "hv";
3507
3506
  this._handleResize = () => {
3508
3507
  const w = window.innerWidth;
3509
3508
  const h = window.innerHeight;
@@ -3555,6 +3554,51 @@ ${log3.label}:`, log3.value];
3555
3554
  };
3556
3555
  }
3557
3556
  }
3557
+ class LibJsPullUpLoad extends LibJsClassObservable {
3558
+ constructor(params) {
3559
+ super({
3560
+ statusText: "加载中...",
3561
+ loadStatus: "idle"
3562
+ });
3563
+ const { scrollEl, loadStatusEl, onLoad } = params;
3564
+ this.checkAutoLoad(onLoad, scrollEl);
3565
+ this.onValue("loadStatus", (v) => {
3566
+ if (v === "idle") {
3567
+ this.checkAutoLoad(onLoad, scrollEl);
3568
+ }
3569
+ });
3570
+ scrollEl.addEventListener("scroll", () => {
3571
+ if (["noMore", "empty", "loading"].includes(this.getValue("loadStatus")))
3572
+ return;
3573
+ const y = scrollEl.scrollTop;
3574
+ const loadDistance = scrollEl.scrollHeight - scrollEl.clientHeight - y - loadStatusEl.offsetHeight;
3575
+ if (loadDistance <= 0) {
3576
+ this.setValue("loadStatus", "loading");
3577
+ this.setValue("statusText", "加载中...");
3578
+ setTimeout(() => {
3579
+ onLoad();
3580
+ }, 500);
3581
+ }
3582
+ });
3583
+ }
3584
+ // 1. 新增方法
3585
+ checkAutoLoad(onLoad, scrollEl) {
3586
+ if (!["noMore", "empty", "loading"].includes(this.getValue("loadStatus")) && scrollEl.scrollHeight <= scrollEl.clientHeight) {
3587
+ this.setValue("loadStatus", "loading");
3588
+ this.setValue("statusText", "加载中...");
3589
+ setTimeout(() => {
3590
+ onLoad();
3591
+ }, 500);
3592
+ }
3593
+ }
3594
+ }
3595
+ const libJsPickUnique = (pool, used = []) => {
3596
+ const usedSet = new Set(used);
3597
+ const available = pool.filter((item) => !usedSet.has(item));
3598
+ if (available.length === 0)
3599
+ return void 0;
3600
+ return available[libJsRandom(0, available.length - 1)];
3601
+ };
3558
3602
  const Base = {
3559
3603
  /**
3560
3604
  * @description 返回数据类型
@@ -3659,7 +3703,9 @@ ${log3.label}:`, log3.value];
3659
3703
  * @param index 开始索引
3660
3704
  * @link 使用方法:https://www.npmjs.com/package/lyb-js#LibReverseArrayFromIndex-数组定位翻转
3661
3705
  */
3662
- libReverseArrayFromIndex
3706
+ libReverseArrayFromIndex,
3707
+ /** @description 随机选择未使用元素 */
3708
+ libJsPickUnique
3663
3709
  };
3664
3710
  const File$1 = {
3665
3711
  /** @description 下载图片链接
@@ -3794,7 +3840,9 @@ ${log3.label}:`, log3.value];
3794
3840
  /** @description 类属性监听器
3795
3841
  * @link 使用方法:https://www.npmjs.com/package/lyb-js#LibJsClassObservable-类属性监听器
3796
3842
  */
3797
- LibJsClassObservable
3843
+ LibJsClassObservable,
3844
+ /** @description 上拉加载 */
3845
+ LibJsPullUpLoad
3798
3846
  };
3799
3847
  const Random = {
3800
3848
  /** @description 百分比概率结果
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "lyb-js",
3
- "version": "1.6.10",
3
+ "version": "1.6.13",
4
4
  "description": "自用JS方法库",
5
5
  "license": "ISC",
6
6
  "type": "module",