@tarojs/taro-h5 4.2.2-beta.5 → 4.2.3-beta.1

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
@@ -5756,6 +5756,9 @@ class TaroH5IntersectionObserver {
5756
5756
  this._observerInst.unobserve(listener.element);
5757
5757
  }
5758
5758
  this._observerInst.disconnect();
5759
+ // 置空,防止 disconnect 后 pending 的 observe 回调复活观察(原生 IO 允许 disconnect 后再次 observe)
5760
+ this._observerInst = undefined;
5761
+ this._isInited = false;
5759
5762
  }
5760
5763
  }
5761
5764
  observe(targetSelector, callback) {
@@ -5767,14 +5770,36 @@ class TaroH5IntersectionObserver {
5767
5770
  console.warn('Intersection observer will be ignored because no relative nodes are found.');
5768
5771
  return;
5769
5772
  }
5770
- const nodeList = this._options.observeAll
5771
- ? this.container.querySelectorAll(targetSelector)
5772
- : [this.container.querySelector(targetSelector)];
5773
5773
  Taro.nextTick(() => {
5774
- nodeList.forEach(element => {
5775
- if (!element)
5776
- return;
5777
- this._observerInst.observe(element);
5774
+ var _a;
5775
+ // 已 disconnect(或 relativeTo 重建)时不再观察
5776
+ if (!this._observerInst || this._listeners.length)
5777
+ return;
5778
+ const container = this.container;
5779
+ const nodeList = this._options.observeAll
5780
+ ? Array.from(container.querySelectorAll(targetSelector))
5781
+ : [container.querySelector(targetSelector)];
5782
+ // 特殊处理 - 选自己(同 SelectorQuery,querySelector 只查后代、不含容器自身)
5783
+ if (container !== document) {
5784
+ const $nodeList = (_a = container.parentNode) === null || _a === void 0 ? void 0 : _a.querySelectorAll(targetSelector);
5785
+ if ($nodeList) {
5786
+ for (let i = 0, len = $nodeList.length; i < len; ++i) {
5787
+ if (container === $nodeList[i]) {
5788
+ nodeList.push(container);
5789
+ break;
5790
+ }
5791
+ }
5792
+ }
5793
+ }
5794
+ // 查询时机已推迟到下一 tick,仍可能查不到(懒渲染等),或节点已脱离文档(页面被切走)
5795
+ const elements = nodeList.filter((element) => !!element && document.contains(element));
5796
+ if (!elements.length) {
5797
+ console.warn(`IntersectionObserver.observe:fail cannot find the node for selector "${targetSelector}"`);
5798
+ return;
5799
+ }
5800
+ elements.forEach(element => {
5801
+ var _a;
5802
+ (_a = this._observerInst) === null || _a === void 0 ? void 0 : _a.observe(element);
5778
5803
  this._listeners.push({ element, callback });
5779
5804
  });
5780
5805
  });
@@ -5785,7 +5810,11 @@ class TaroH5IntersectionObserver {
5785
5810
  console.error('Relative nodes cannot be added after "observe" call in IntersectionObserver');
5786
5811
  return this;
5787
5812
  }
5788
- this._root = this.container.querySelector(selector) || null;
5813
+ const root = this.container.querySelector(selector);
5814
+ if (!root) {
5815
+ console.warn(`IntersectionObserver.relativeTo:fail cannot find the node for selector "${selector}"`);
5816
+ }
5817
+ this._root = root || null;
5789
5818
  if (margins) {
5790
5819
  this._rootMargin = margins;
5791
5820
  }
@@ -5793,7 +5822,42 @@ class TaroH5IntersectionObserver {
5793
5822
  return this;
5794
5823
  }
5795
5824
  relativeToViewport(margins) {
5796
- return this.relativeTo('.taro_page', margins);
5825
+ var _a, _b, _c;
5826
+ // 已设置observe监听后,重新关联节点
5827
+ if (this._listeners.length) {
5828
+ console.error('Relative nodes cannot be added after "observe" call in IntersectionObserver');
5829
+ return this;
5830
+ }
5831
+ // 微信小程序语义:参照「页面显示区域」。
5832
+ // 默认(容器滚动)模式参照当前页 .taro_page;usingWindowScroll 下 .taro_page 不是滚动容器,
5833
+ // 参照节点与目标随文档同滚、相交比永不变化,必须参照浏览器视口(root 为 null)。
5834
+ // 注意直接用 document 级查询:旧实现 relativeTo('.taro_page') 在 container 作用域内查,
5835
+ // 多页栈时会命中首个已隐藏的 .taro_page(旧页不卸载、display:none),导致恒不可见。
5836
+ const id = (_b = (_a = Current$1.page) === null || _a === void 0 ? void 0 : _a.path) === null || _b === void 0 ? void 0 : _b.replace(/([^a-z0-9\u00a0-\uffff_-])/ig, '\\$1');
5837
+ let pageEl = id ? document.querySelector(`.taro_page#${id}`) : null;
5838
+ if (!pageEl) {
5839
+ // 当前页 = 最后一个非 shade 的已 show 页面(旧页保留 taro_page_show,被切走时追加 taro_page_shade)
5840
+ const $pages = document.querySelectorAll('.taro_router > .taro_page.taro_page_show:not(.taro_page_shade)');
5841
+ pageEl = $pages.length ? $pages[$pages.length - 1] : null;
5842
+ }
5843
+ // __taroAppConfig.usingWindowScroll 是页面级配置、mount 时按页写入,切换瞬间可能滞后,用当前页样式二次确认
5844
+ const usingWindowScroll = ((_c = window.__taroAppConfig) === null || _c === void 0 ? void 0 : _c.usingWindowScroll) === true ||
5845
+ (!!pageEl && !/scroll|auto/.test(getComputedStyle(pageEl).overflowY));
5846
+ if (usingWindowScroll) {
5847
+ this._root = null;
5848
+ }
5849
+ else if (pageEl) {
5850
+ this._root = pageEl;
5851
+ }
5852
+ else {
5853
+ console.warn('IntersectionObserver.relativeToViewport:fail cannot find current page element');
5854
+ this._root = null;
5855
+ }
5856
+ if (margins) {
5857
+ this._rootMargin = margins;
5858
+ }
5859
+ this._observerInst = this.createInst();
5860
+ return this;
5797
5861
  }
5798
5862
  _getCallbackByElement(element) {
5799
5863
  const listener = this._listeners.find(listener => listener.element === element);