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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
import {
  stringifyQuery
} from 'uni-shared'
 
import {
  parseWebviewStyle
} from './parser/webview-style-parser'
 
import {
  initSubNVues
} from './parser/sub-nvue-parser'
 
import {
  VIEW_WEBVIEW_PATH
} from '../../constants'
 
import {
  onWebviewClose
} from './on-webview-close'
 
import {
  onWebviewResize
} from './on-webview-resize'
 
import {
  onWebviewRecovery
} from './on-webview-recovery'
 
import {
  onWebviewPopGesture
} from './on-webview-pop-gesture'
 
import { useWebviewThemeChange } from '../theme'
 
export let preloadWebview
 
let id = 2
 
const WEBVIEW_LISTENERS = {
  pullToRefresh: 'onPullDownRefresh',
  titleNViewSearchInputChanged: 'onNavigationBarSearchInputChanged',
  titleNViewSearchInputConfirmed: 'onNavigationBarSearchInputConfirmed',
  titleNViewSearchInputClicked: 'onNavigationBarSearchInputClicked',
  titleNViewSearchInputFocusChanged: 'onNavigationBarSearchInputFocusChanged'
}
 
export function setPreloadWebview (webview) {
  preloadWebview = webview
}
 
function noop (str) {
  return str
}
 
function getUniPageUrl (path, query) {
  const queryString = query ? stringifyQuery(query, noop) : ''
  return {
    path: path.substr(1),
    query: queryString ? queryString.substr(1) : queryString
  }
}
 
function getDebugRefresh (path, query, routeOptions) {
  const queryString = query ? stringifyQuery(query, noop) : ''
  return {
    isTab: routeOptions.meta.isTabBar,
    arguments: JSON.stringify({
      path: path.substr(1),
      query: queryString ? queryString.substr(1) : queryString
    })
  }
}
 
export function createWebview (path, routeOptions, query, extras = {}) {
  if (routeOptions.meta.isNVue) {
    const getWebviewStyle = () => parseWebviewStyle(
      webviewId,
      path,
      routeOptions
    )
    const webviewId = id++
    const webviewStyle = getWebviewStyle()
    webviewStyle.uniPageUrl = getUniPageUrl(path, query)
    if (process.env.NODE_ENV !== 'production') {
      console.log('[uni-app] createWebview', webviewId, path, webviewStyle)
    }
    // android 需要使用
    webviewStyle.isTab = !!routeOptions.meta.isTabBar
    const webview = plus.webview.create('', String(webviewId), webviewStyle, Object.assign({
      nvue: true
    }, extras))
 
    useWebviewThemeChange(webview, getWebviewStyle)
 
    return webview
  }
  if (id === 2) { // 如果首页非 nvue,则直接返回 Launch Webview
    return plus.webview.getLaunchWebview()
  }
  const webview = preloadWebview
  return webview
}
 
export function initWebview (webview, routeOptions, path, query) {
  // 首页或非 nvue 页面
  if (webview.id === '1' || !routeOptions.meta.isNVue) {
    const getWebviewStyle = () => parseWebviewStyle(
      parseInt(webview.id),
      '',
      routeOptions
    )
    const webviewStyle = getWebviewStyle()
 
    webviewStyle.uniPageUrl = getUniPageUrl(path, query)
 
    if (!routeOptions.meta.isNVue) {
      webviewStyle.debugRefresh = getDebugRefresh(path, query, routeOptions)
    } else {
      // android 需要使用
      webviewStyle.isTab = !!routeOptions.meta.isTabBar
    }
    if (process.env.NODE_ENV !== 'production') {
      console.log('[uni-app] updateWebview', webviewStyle)
    }
 
    useWebviewThemeChange(webview, getWebviewStyle)
 
    webview.setStyle(webviewStyle)
  }
 
  const {
    on,
    emit
  } = UniServiceJSBridge
 
  initSubNVues(routeOptions, webview)
 
  Object.keys(WEBVIEW_LISTENERS).forEach(name => {
    webview.addEventListener(name, (e) => {
      emit(WEBVIEW_LISTENERS[name], e, parseInt(webview.id))
    })
  })
 
  onWebviewClose(webview)
  onWebviewResize(webview)
 
  if (plus.os.name === 'iOS') {
    !webview.nvue && onWebviewRecovery(webview, routeOptions)
    onWebviewPopGesture(webview)
  }
 
  on(webview.id + '.startPullDownRefresh', () => {
    webview.beginPullToRefresh()
  })
 
  on(webview.id + '.stopPullDownRefresh', () => {
    webview.endPullToRefresh()
  })
 
  return webview
}
 
export function createPreloadWebview () {
  if (!preloadWebview || preloadWebview.__uniapp_route) { // 不存在,或已被使用
    preloadWebview = plus.webview.create(VIEW_WEBVIEW_PATH, String(id++), { contentAdjust: false })
    if (process.env.NODE_ENV !== 'production') {
      console.log(`[uni-app] preloadWebview[${preloadWebview.id}]`)
    }
  }
  return preloadWebview
}
 
const webviewReadyCallbacks = {}
 
export function registerWebviewReady (pageId, callback) {
  (webviewReadyCallbacks[pageId] || (webviewReadyCallbacks[pageId] = [])).push(callback)
}
 
export function consumeWebviewReady (pageId) {
  const callbacks = webviewReadyCallbacks[pageId]
  Array.isArray(callbacks) && callbacks.forEach(callback => callback())
  delete webviewReadyCallbacks[pageId]
}