'a'
mh-two-thousand-and-two
2024-04-12 44d2c92345cd156a59fc327b3060292a282d2893
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import {
  isPlainObject,
  supportsPassive
} from 'uni-shared'
 
import {
  hasLifecycleHook
} from 'uni-helpers/index'
 
import {
  NAVBAR_HEIGHT,
  TABBAR_HEIGHT
} from 'uni-helpers/constants'
 
import {
  disableScroll,
  createScrollListener
} from 'uni-core/view/bridge/subscribe/scroll'
 
const passiveOptions = supportsPassive ? {
  passive: false
} : false
 
function updateCssVar (vm) {
  if (uni.canIUse('css.var')) {
    const uniConfigTabbarHeight = parseFloat(__uniConfig.tabBar.height)
    const pageVm = vm.$parent.$parent
    const navigationBarType = pageVm.navigationBar.type
    const windowTopValue = navigationBarType === 'default' || navigationBarType === 'float' ? NAVBAR_HEIGHT : 0
    const windowBottomValue = getApp().$children[0].showTabBar ? isNaN(uniConfigTabbarHeight) ? TABBAR_HEIGHT : uniConfigTabbarHeight : 0
    const envMethod = uni.canIUse('css.env') ? 'env' : (uni.canIUse('css.constant') ? 'constant' : '')
    const windowTop = windowTopValue && envMethod ? `calc(${windowTopValue}px + ${envMethod}(safe-area-inset-top))`
      : `${windowTopValue}px`
    const windowBottom = windowBottomValue && envMethod
      ? `calc(${windowBottomValue}px + ${envMethod}(safe-area-inset-bottom))` : `${windowBottomValue}px`
    const style = document.documentElement.style
    style.setProperty('--window-top', `calc(var(--top-window-height) + ${windowTop})`)
    style.setProperty('--window-bottom', windowBottom)
    console.debug(`${vm.$page.route}[${vm.$page.id}]:--window-top=${windowTop}`)
    console.debug(`${vm.$page.route}[${vm.$page.id}]:--window-bottom=${windowBottom}`)
  }
}
 
export default function initSubscribe (subscribe) {
  let scrollListener = false
  let disableScrollListener = false
 
  subscribe('onPageLoad', vm => { // 用户 onLoad 之前 update
    updateCssVar(vm)
  })
 
  subscribe('onPageShow', vm => {
    const pageVm = vm.$parent.$parent
 
    if (vm._isMounted) { // 非首次 show 才 update(首次 show 的时候在 onPageLoad 中触发了)
      updateCssVar(vm)
    }
 
    if (disableScrollListener) {
      document.removeEventListener('touchmove', disableScrollListener, passiveOptions)
    }
 
    if (pageVm.disableScroll) {
      disableScrollListener = disableScroll
      document.addEventListener('touchmove', disableScrollListener, passiveOptions)
    }
 
    const enablePageScroll = hasLifecycleHook(vm.$options, 'onPageScroll')
    const enablePageReachBottom = hasLifecycleHook(vm.$options, 'onReachBottom')
    const onReachBottomDistance = pageVm.onReachBottomDistance
 
    const enableTransparentTitleNView = (isPlainObject(pageVm.titleNView) && pageVm.titleNView.type ===
      'transparent') || (isPlainObject(pageVm.navigationBar) && pageVm.navigationBar.type === 'transparent')
 
    if (scrollListener) {
      document.removeEventListener('scroll', scrollListener)
    }
 
    if (enableTransparentTitleNView || enablePageScroll || enablePageReachBottom) { // 初始化 scroll 监听
      scrollListener = createScrollListener(vm.$page.id, {
        enablePageScroll,
        enablePageReachBottom,
        onReachBottomDistance,
        enableTransparentTitleNView
      })
      requestAnimationFrame(function () { // 避免监听太早,直接触发了 scroll
        document.addEventListener('scroll', scrollListener)
      })
    }
  })
}