-
Notifications
You must be signed in to change notification settings - Fork 382
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Webview settitle #1733
Merged
Merged
Webview settitle #1733
Changes from 6 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
01a1bcc
补充title逻辑&干掉无用的seturl
bcee9b6
改造webview消息同步
386c2b8
修改navigation取值位置
266a0fb
合并master并修改noop逻辑
9a23379
修改标题逻辑
af3d7c2
拉齐写法
241a85e
先改一版提交
00ca1df
修改web和rn的webview提交
b50f43c
fix comment
bb7d9cf
干掉传递的data
07c65e6
支持invoke
401b4a9
修改接收参数逻辑
2fc23bc
补充web部分,解决冲突
0099299
修改判断条件
9592597
去掉无用的添加
5ffd1e7
补充错误回调
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,5 @@ | ||
import { forwardRef, JSX, useEffect, useRef, useContext, useMemo } from 'react' | ||
import { noop, warn } from '@mpxjs/utils' | ||
import { View } from 'react-native' | ||
import { warn, getFocusedNavigation } from '@mpxjs/utils' | ||
import { Portal } from '@ant-design/react-native' | ||
import { getCustomEvent } from './getInnerListeners' | ||
import { promisify, redirectTo, navigateTo, navigateBack, reLaunch, switchTab } from '@mpxjs/api-proxy' | ||
|
@@ -40,10 +39,10 @@ type MessageData = { | |
callbackId?: number | ||
} | ||
|
||
const _WebView = forwardRef<HandlerRef<WebView, WebViewProps>, WebViewProps>((props, ref): JSX.Element => { | ||
const { src, bindmessage = noop, bindload = noop, binderror = noop } = props | ||
const _WebView = forwardRef<HandlerRef<WebView, WebViewProps>, WebViewProps>((props, ref): JSX.Element | null => { | ||
const { src, bindmessage, bindload, binderror } = props | ||
if (!src) { | ||
return (<View></View>) | ||
return null | ||
} | ||
if (props.style) { | ||
warn('The web-view component does not support the style prop.') | ||
|
@@ -64,29 +63,6 @@ const _WebView = forwardRef<HandlerRef<WebView, WebViewProps>, WebViewProps>((pr | |
style: defaultWebViewStyle | ||
}) | ||
|
||
const _messageList = useRef<any[]>([]) | ||
const handleUnload = () => { | ||
// 这里是 WebView 销毁前执行的逻辑 | ||
bindmessage(getCustomEvent('messsage', {}, { | ||
detail: { | ||
data: _messageList.current | ||
}, | ||
layoutRef: webViewRef | ||
})) | ||
} | ||
|
||
useEffect(() => { | ||
if (currentPage) { | ||
currentPage.__webViewUrl = src | ||
} | ||
}, [src, currentPage]) | ||
|
||
useEffect(() => { | ||
// 组件卸载时执行 | ||
return () => { | ||
handleUnload() | ||
} | ||
}, []) | ||
const _load = function (res: WebViewNavigationEvent) { | ||
const result = { | ||
type: 'load', | ||
|
@@ -107,8 +83,37 @@ const _WebView = forwardRef<HandlerRef<WebView, WebViewProps>, WebViewProps>((pr | |
} | ||
binderror(result) | ||
} | ||
const injectedJavaScript = ` | ||
if (window.ReactNativeWebView && window.ReactNativeWebView.postMessage) { | ||
var _documentTitle = document.title; | ||
window.ReactNativeWebView.postMessage(JSON.stringify({ | ||
type: 'setTitle', | ||
payload: { | ||
data: { | ||
_documentTitle: _documentTitle | ||
} | ||
} | ||
})) | ||
Object.defineProperty(document, 'title', { | ||
set (val) { | ||
_documentTitle = val | ||
window.ReactNativeWebView.postMessage(JSON.stringify({ | ||
type: 'setTitle', | ||
payload: { | ||
data: { | ||
_documentTitle: _documentTitle | ||
} | ||
} | ||
})) | ||
}, | ||
get () { | ||
return _documentTitle | ||
} | ||
}); | ||
} | ||
` | ||
const _changeUrl = function (navState: WebViewNavigation) { | ||
if (currentPage) { | ||
if (navState.navigationType) { // navigationType这个事件在页面开始加载时和页面加载完成时都会被触发所以判断这个避免其他无效触发执行该逻辑 | ||
currentPage.__webViewUrl = navState.url | ||
} | ||
} | ||
|
@@ -126,8 +131,22 @@ const _WebView = forwardRef<HandlerRef<WebView, WebViewProps>, WebViewProps>((pr | |
} | ||
const postData: PayloadData = data.payload || {} | ||
switch (data.type) { | ||
case 'setTitle': | ||
{ // case下不允许直接声明,包个块解决该问题 | ||
const title = postData.data?._documentTitle | ||
if (title) { | ||
const navigation = getFocusedNavigation() | ||
navigation && navigation.setOptions({ title }) | ||
} | ||
} | ||
break | ||
case 'postMessage': | ||
_messageList.current.push(postData.data) | ||
bindmessage && bindmessage(getCustomEvent('messsage', {}, { // RN组件销毁顺序与小程序不一致,所以改成和支付宝消息一致 | ||
detail: { | ||
data: postData.data | ||
}, | ||
layoutRef: webViewRef | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 这咋能这么写呢 |
||
})) | ||
asyncCallback = Promise.resolve({ | ||
errMsg: 'invokeWebappApi:ok' | ||
}) | ||
|
@@ -172,18 +191,17 @@ const _WebView = forwardRef<HandlerRef<WebView, WebViewProps>, WebViewProps>((pr | |
onError: _error | ||
}) | ||
} | ||
if (bindmessage) { | ||
extendObject(events, { | ||
onMessage: _message | ||
}) | ||
} | ||
extendObject(events, { | ||
onMessage: _message | ||
}) | ||
return (<Portal> | ||
<WebView | ||
style={defaultWebViewStyle} | ||
source={{ uri: src }} | ||
ref={webViewRef} | ||
{...events} | ||
onNavigationStateChange={_changeUrl} | ||
injectedJavaScript={injectedJavaScript} | ||
javaScriptEnabled={true} | ||
></WebView> | ||
</Portal>) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这个不用再包一层data啦,key值改为title