'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
import {
  supportsPassive
} from 'uni-shared'
 
import {
  setLocale
} from 'uni-core/helpers/i18n'
 
import {
  disableScroll as disableScrollListener,
  createScrollListener
} from 'uni-core/view/bridge/subscribe/scroll'
 
import {
  ON_PAGE_CREATE
} from '../../constants'
 
import {
  WEBVIEW_READY,
  SET_LOCALE
} from '../../../constants'
 
const passiveOptions = supportsPassive ? {
  passive: false
} : false
 
function onCssVar ({
  statusbarHeight,
  windowTop,
  windowBottom
}) {
  global.__WINDOW_TOP = windowTop
  global.__WINDOW_BOTTOM = windowBottom
  if (uni.canIUse('css.var')) {
    const style = document.documentElement.style
    // TODO
    style.setProperty('--window-left', '0px')
    style.setProperty('--window-right', '0px')
 
    style.setProperty('--window-top', windowTop + 'px')
    style.setProperty('--window-bottom', windowBottom + 'px')
    style.setProperty('--status-bar-height', statusbarHeight + 'px')
    if (process.env.NODE_ENV !== 'production') {
      console.log(`--status-bar-height=${statusbarHeight}`)
      console.log(`--window-top=${windowTop}`)
      console.log(`--window-bottom=${windowBottom}`)
    }
  }
}
 
function onPageCreate ({
  locale,
  statusbarHeight,
  windowTop,
  windowBottom,
  disableScroll,
  onPageScroll,
  onPageReachBottom,
  onReachBottomDistance
}, pageId) {
  setLocale(locale)
 
  onCssVar({
    statusbarHeight,
    windowTop,
    windowBottom
  })
 
  if (disableScroll) {
    document.addEventListener('touchmove', disableScrollListener, passiveOptions)
  } else if (onPageScroll || onPageReachBottom) {
    requestAnimationFrame(function () { // 避免监听太早,直接触发了 scroll
      document.addEventListener('scroll', createScrollListener(pageId, {
        enablePageScroll: onPageScroll,
        enablePageReachBottom: onPageReachBottom,
        onReachBottomDistance
      }))
    })
  }
}
 
function onWebviewReady () { // service 主动发起检测
  UniViewJSBridge.publishHandler('webviewReady')
}
 
export default function initSubscribe (subscribe) {
  subscribe(WEBVIEW_READY, onWebviewReady)
  subscribe(ON_PAGE_CREATE, onPageCreate)
  subscribe(SET_LOCALE, setLocale)
}