a2bei4-utils 1.0.8 → 1.0.10

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.
@@ -461,7 +461,37 @@ const fullscreenHelper = {
461
461
  document.removeEventListener("msfullscreenchange", handler);
462
462
  };
463
463
  }
464
- };
464
+ };
465
+
466
+ /**
467
+ * 复制文本到剪贴板
468
+ * @param {String} text
469
+ * @returns {Promise<void>}
470
+ */
471
+ async function copyTextToClipboard(text) {
472
+ if (window.navigator.clipboard) {
473
+ await window.navigator.clipboard.writeText(text);
474
+ return;
475
+ }
476
+
477
+ return new Promise((resolve, reject) => {
478
+ const ta = document.createElement("textarea");
479
+ ta.value = text;
480
+ ta.style.position = "fixed"; // 防止滚动
481
+ ta.style.opacity = "0";
482
+ document.body.appendChild(ta);
483
+ ta.focus();
484
+ ta.select();
485
+
486
+ try {
487
+ document.execCommand("copy") ? resolve() : reject(new Error("execCommand failed"));
488
+ } catch (err) {
489
+ reject(err);
490
+ } finally {
491
+ document.body.removeChild(ta);
492
+ }
493
+ });
494
+ }
465
495
 
466
496
  //#region 数据类型判断
467
497
 
@@ -1793,6 +1823,10 @@ class IntervalTimer {
1793
1823
  this._fn = fn;
1794
1824
  this._ms = ms;
1795
1825
  this._timerId = null;
1826
+ /**
1827
+ * @type {symbol | null}
1828
+ */
1829
+ this._loopId = null;
1796
1830
  }
1797
1831
 
1798
1832
  /**
@@ -1801,8 +1835,17 @@ class IntervalTimer {
1801
1835
  */
1802
1836
  start() {
1803
1837
  this.stop();
1838
+
1839
+ const curLoopId = Symbol();
1840
+ this._loopId = curLoopId;
1841
+
1804
1842
  const loop = () => {
1843
+ if (this._loopId !== curLoopId) return;
1844
+
1805
1845
  this._fn(); // 执行业务
1846
+
1847
+ if (this._loopId !== curLoopId) return;
1848
+
1806
1849
  this._timerId = setTimeout(loop, this._ms);
1807
1850
  };
1808
1851
  loop(); // 立即执行第一次
@@ -1812,6 +1855,7 @@ class IntervalTimer {
1812
1855
  * 停止定时器。
1813
1856
  */
1814
1857
  stop() {
1858
+ this._loopId = null;
1815
1859
  if (this._timerId !== null) {
1816
1860
  clearTimeout(this._timerId);
1817
1861
  this._timerId = null;
@@ -2478,6 +2522,7 @@ exports.MyEvent_CrossPagePlugin = MyEvent_CrossPagePlugin;
2478
2522
  exports.MyId = MyId;
2479
2523
  exports.WebSocketManager = WebSocketManager;
2480
2524
  exports.assignExisting = assignExisting;
2525
+ exports.copyTextToClipboard = copyTextToClipboard;
2481
2526
  exports.debounce = debounce;
2482
2527
  exports.deepCloneByJSON = deepCloneByJSON;
2483
2528
  exports.downloadByBlob = downloadByBlob;