mh-two-thousand-and-two
2024-04-12 3d2ec2fd0578d3ba0a414b0cc4e4a2ae60878596
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
import { parseTitleNView } from './title-nview-parser'
 
import { parsePullToRefresh } from './pull-to-refresh-parser'
 
import { parseStyleUnit } from './style-unit-parser'
 
import { parseTheme } from '../../theme'
 
const WEBVIEW_STYLE_BLACKLIST = [
  'navigationBarBackgroundColor',
  'navigationBarTextStyle',
  'navigationBarTitleText',
  'navigationBarShadow',
  'navigationStyle',
  'disableScroll',
  'backgroundColor',
  'backgroundTextStyle',
  'enablePullDownRefresh',
  'onReachBottomDistance',
  'usingComponents',
  // 需要解析的
  'titleNView',
  'pullToRefresh'
]
 
export function parseWebviewStyle (id, path, _routeOptions = {}) {
  const webviewStyle = {
    bounce: 'vertical'
  }
 
  // 合并
  _routeOptions.window = parseStyleUnit(
    Object.assign(
      JSON.parse(JSON.stringify(__uniConfig.window || {})),
      _routeOptions.window || {}
    )
  )
 
  const routeOptions = parseTheme(_routeOptions)
 
  Object.keys(routeOptions.window).forEach(name => {
    if (WEBVIEW_STYLE_BLACKLIST.indexOf(name) === -1) {
      webviewStyle[name] = routeOptions.window[name]
    }
  })
 
  let backgroundColor = routeOptions.window.backgroundColor
  if (
    /^#[a-z0-9]{6}$/i.test(backgroundColor) ||
    backgroundColor === 'transparent'
  ) {
    if (!webviewStyle.background) {
      webviewStyle.background = backgroundColor
    } else {
      backgroundColor = webviewStyle.background
    }
    if (!webviewStyle.backgroundColorTop) {
      webviewStyle.backgroundColorTop = backgroundColor
    }
    if (!webviewStyle.backgroundColorBottom) {
      webviewStyle.backgroundColorBottom = backgroundColor
    }
    if (!webviewStyle.animationAlphaBGColor) {
      webviewStyle.animationAlphaBGColor = backgroundColor
    }
    if (typeof webviewStyle.webviewBGTransparent === 'undefined') {
      webviewStyle.webviewBGTransparent = true
    }
  }
 
  const titleNView = parseTitleNView(id, routeOptions)
  if (titleNView) {
    if (
      id === 1 &&
      __uniConfig.realEntryPagePath &&
      !routeOptions.meta.isQuit // 可能是tabBar
    ) {
      titleNView.autoBackButton = true
    }
    webviewStyle.titleNView = titleNView
  }
 
  const pullToRefresh = parsePullToRefresh(routeOptions)
  if (pullToRefresh) {
    if (pullToRefresh.style === 'circle') {
      webviewStyle.bounce = 'none'
    }
    webviewStyle.pullToRefresh = pullToRefresh
  }
 
  // 不支持 hide
  if (webviewStyle.popGesture === 'hide') {
    delete webviewStyle.popGesture
  }
 
  if (routeOptions.meta.isQuit) {
    // 退出
    webviewStyle.popGesture = plus.os.name === 'iOS' ? 'appback' : 'none'
  }
 
  // TODO 下拉刷新
 
  if (path && routeOptions.meta.isNVue) {
    webviewStyle.uniNView = {
      path,
      defaultFontSize: __uniConfig.defaultFontSize,
      viewport: __uniConfig.viewport
    }
  }
 
  _routeOptions.meta = routeOptions.meta
  return webviewStyle
}