feat(windows): JS-WebView messaging bridge & multiple WebViews fixes (#1617)

* Manage to build webview using Visual Studio

* WebView in content of UserControl

* Destructor not needed

* Example app tested

* Add messagingEnabled prop

* WebViewBridge

* Message posting

* Store bridge as instance variable

* Use bridge if messagingEnabled

* Free event delegate

* PostMessage api uses string message

* script.notify() usage removed

* Debug log removed

* Bridge reference added

* Base for web allowed object implemented c++/winrt

* Bribge works

* "Microsoft.Windows.CppWinRT" version="2.0.200729.8"

* Update bridge implementation

* version changes

* Reference fix

* WebView prj builds

* yarn lock update

* Platfrom version update

* NuGet package reference updated

* Messaging test added into example app

* Typo fix

* try_as() to as() calls

* WebView.PostMessage() not supported anymore, use injectJavaScript

* Fix WebView.postMessage to work using injectJavascript

* Eval postMessage fix

* postMessage via injectJavascript

* Example fixed to handle postMessage call
This commit is contained in:
Tero Paananen 2020-10-26 15:01:40 +02:00 committed by GitHub
parent 63984157fa
commit b930e25a8f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
27 changed files with 1942 additions and 1438 deletions

View File

@ -17,8 +17,17 @@ import Downloads from './examples/Downloads';
import Uploads from './examples/Uploads';
import Injection from './examples/Injection';
import LocalPageLoad from './examples/LocalPageLoad';
import Messaging from './examples/Messaging';
const TESTS = {
Messaging: {
title: 'Messaging',
testId: 'messaging',
description: 'js-webview postMessage messaging test',
render() {
return <Messaging />;
},
},
Alerts: {
title: 'Alerts',
testId: 'alerts',
@ -78,7 +87,7 @@ const TESTS = {
};
type Props = {};
type State = {restarting: boolean, currentTest: Object};
type State = {restarting: boolean; currentTest: Object};
export default class App extends Component<Props, State> {
state = {
@ -90,7 +99,7 @@ export default class App extends Component<Props, State> {
this.setState({restarting: true}, () => this.setState({restarting: false}));
};
_changeTest = testName => {
_changeTest = (testName) => {
this.setState({currentTest: TESTS[testName]});
};
@ -138,16 +147,25 @@ export default class App extends Component<Props, State> {
title="LocalPageLoad"
onPress={() => this._changeTest('PageLoad')}
/>
{Platform.OS == "ios" && <Button
{Platform.OS == 'ios' && (
<Button
testID="testType_downloads"
title="Downloads"
onPress={() => this._changeTest('Downloads')}
/>}
{Platform.OS === 'android' && <Button
/>
)}
{Platform.OS === 'android' && (
<Button
testID="testType_uploads"
title="Uploads"
onPress={() => this._changeTest('Uploads')}
/>}
/>
)}
<Button
testID="testType_messaging"
title="Messaging"
onPress={() => this._changeTest('Messaging')}
/>
</View>
{restarting ? null : (

View File

@ -0,0 +1,63 @@
import React, {Component} from 'react';
import {View, Alert} from 'react-native';
import WebView from 'react-native-webview';
const HTML = `<!DOCTYPE html>\n
<html>
<head>
<title>Messaging</title>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<meta name="viewport" content="width=320, user-scalable=no">
<style type="text/css">
body {
margin: 0;
padding: 0;
font: 62.5% arial, sans-serif;
background: #ccc;
}
</style>
</head>
<body>
<button onclick="sendPostMessage()">Send post message from JS to WebView</button>
<p id="demo"></p>
<script>
function sendPostMessage() {
window.ReactNativeWebView.postMessage('Message from JS');
}
window.addEventListener('message',function(event){
console.log("Message received from RN: ",event.data)
},false);
</script>
</body>
</html>`;
type Props = {};
type State = {};
export default class Messaging extends Component<Props, State> {
state = {};
constructor(props) {
super(props);
this.webView = React.createRef();
}
render() {
return (
<View style={{height: 120}}>
<WebView
ref={this.webView}
source={{html: HTML}}
onLoadEnd={()=>{this.webView.current.postMessage('Hello from RN');}}
automaticallyAdjustContentInsets={false}
onMessage={(e: {nativeEvent: {data?: string}}) => {
Alert.alert('Message received from JS: ', e.nativeEvent.data);
}}
/>
</View>
);
}
}

View File

@ -42,9 +42,12 @@ Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "Mso", "..\..\node_modules\r
EndProject
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "ReactNativeWebView", "..\..\windows\ReactNativeWebView\ReactNativeWebView.vcxproj", "{729D9AF8-CD9E-4427-9F6C-FB757E287729}"
EndProject
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "WebViewBridgeComponent", "..\..\windows\WebViewBridgeComponent\WebViewBridgeComponent.vcxproj", "{FCB612AD-FACC-410C-828C-24B5940B2762}"
EndProject
Global
GlobalSection(SharedMSBuildProjectFiles) = preSolution
..\..\node_modules\react-native-windows\JSI\Shared\JSI.Shared.vcxitems*{0cc28589-39e4-4288-b162-97b959f8b843}*SharedItemsImports = 9
..\..\node_modules\react-native-windows\ReactWindowsCore\ReactWindowsCore.vcxitems*{11c084a3-a57c-4296-a679-cac17b603144}*SharedItemsImports = 4
..\..\node_modules\react-native-windows\Shared\Shared.vcxitems*{2049dbe9-8d13-42c9-ae4b-413ae38fffd0}*SharedItemsImports = 9
..\..\node_modules\react-native-windows\Microsoft.ReactNative.SharedManaged\Microsoft.ReactNative.SharedManaged.projitems*{67a1076f-7790-4203-86ea-4402ccb5e782}*SharedItemsImports = 13
..\..\node_modules\react-native-windows\Microsoft.ReactNative.Cxx\Microsoft.ReactNative.Cxx.vcxitems*{729d9af8-cd9e-4427-9f6c-fb757e287729}*SharedItemsImports = 4
@ -60,16 +63,19 @@ Global
..\..\node_modules\react-native-windows\Shared\Shared.vcxitems*{f7d32bd0-2749-483e-9a0d-1635ef7e3136}*SharedItemsImports = 4
EndGlobalSection
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Debug|ARM = Debug|ARM
Debug|ARM64 = Debug|ARM64
Debug|x64 = Debug|x64
Debug|x86 = Debug|x86
Release|Any CPU = Release|Any CPU
Release|ARM = Release|ARM
Release|ARM64 = Release|ARM64
Release|x64 = Release|x64
Release|x86 = Release|x86
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{AC6C354F-8C80-47EA-9E40-996ABE271843}.Debug|Any CPU.ActiveCfg = Debug|Win32
{AC6C354F-8C80-47EA-9E40-996ABE271843}.Debug|ARM.ActiveCfg = Debug|ARM
{AC6C354F-8C80-47EA-9E40-996ABE271843}.Debug|ARM.Build.0 = Debug|ARM
{AC6C354F-8C80-47EA-9E40-996ABE271843}.Debug|ARM.Deploy.0 = Debug|ARM
@ -82,6 +88,7 @@ Global
{AC6C354F-8C80-47EA-9E40-996ABE271843}.Debug|x86.ActiveCfg = Debug|Win32
{AC6C354F-8C80-47EA-9E40-996ABE271843}.Debug|x86.Build.0 = Debug|Win32
{AC6C354F-8C80-47EA-9E40-996ABE271843}.Debug|x86.Deploy.0 = Debug|Win32
{AC6C354F-8C80-47EA-9E40-996ABE271843}.Release|Any CPU.ActiveCfg = Release|Win32
{AC6C354F-8C80-47EA-9E40-996ABE271843}.Release|ARM.ActiveCfg = Release|ARM
{AC6C354F-8C80-47EA-9E40-996ABE271843}.Release|ARM.Build.0 = Release|ARM
{AC6C354F-8C80-47EA-9E40-996ABE271843}.Release|ARM.Deploy.0 = Release|ARM
@ -94,6 +101,7 @@ Global
{AC6C354F-8C80-47EA-9E40-996ABE271843}.Release|x86.ActiveCfg = Release|Win32
{AC6C354F-8C80-47EA-9E40-996ABE271843}.Release|x86.Build.0 = Release|Win32
{AC6C354F-8C80-47EA-9E40-996ABE271843}.Release|x86.Deploy.0 = Release|Win32
{A990658C-CE31-4BCC-976F-0FC6B1AF693D}.Debug|Any CPU.ActiveCfg = Debug|Win32
{A990658C-CE31-4BCC-976F-0FC6B1AF693D}.Debug|ARM.ActiveCfg = Debug|ARM
{A990658C-CE31-4BCC-976F-0FC6B1AF693D}.Debug|ARM.Build.0 = Debug|ARM
{A990658C-CE31-4BCC-976F-0FC6B1AF693D}.Debug|ARM64.ActiveCfg = Debug|ARM64
@ -102,6 +110,7 @@ Global
{A990658C-CE31-4BCC-976F-0FC6B1AF693D}.Debug|x64.Build.0 = Debug|x64
{A990658C-CE31-4BCC-976F-0FC6B1AF693D}.Debug|x86.ActiveCfg = Debug|Win32
{A990658C-CE31-4BCC-976F-0FC6B1AF693D}.Debug|x86.Build.0 = Debug|Win32
{A990658C-CE31-4BCC-976F-0FC6B1AF693D}.Release|Any CPU.ActiveCfg = Release|Win32
{A990658C-CE31-4BCC-976F-0FC6B1AF693D}.Release|ARM.ActiveCfg = Release|ARM
{A990658C-CE31-4BCC-976F-0FC6B1AF693D}.Release|ARM.Build.0 = Release|ARM
{A990658C-CE31-4BCC-976F-0FC6B1AF693D}.Release|ARM64.ActiveCfg = Release|ARM64
@ -110,6 +119,7 @@ Global
{A990658C-CE31-4BCC-976F-0FC6B1AF693D}.Release|x64.Build.0 = Release|x64
{A990658C-CE31-4BCC-976F-0FC6B1AF693D}.Release|x86.ActiveCfg = Release|Win32
{A990658C-CE31-4BCC-976F-0FC6B1AF693D}.Release|x86.Build.0 = Release|Win32
{A9D95A91-4DB7-4F72-BEB6-FE8A5C89BFBD}.Debug|Any CPU.ActiveCfg = Debug|Win32
{A9D95A91-4DB7-4F72-BEB6-FE8A5C89BFBD}.Debug|ARM.ActiveCfg = Debug|ARM
{A9D95A91-4DB7-4F72-BEB6-FE8A5C89BFBD}.Debug|ARM.Build.0 = Debug|ARM
{A9D95A91-4DB7-4F72-BEB6-FE8A5C89BFBD}.Debug|ARM64.ActiveCfg = Debug|ARM64
@ -118,6 +128,7 @@ Global
{A9D95A91-4DB7-4F72-BEB6-FE8A5C89BFBD}.Debug|x64.Build.0 = Debug|x64
{A9D95A91-4DB7-4F72-BEB6-FE8A5C89BFBD}.Debug|x86.ActiveCfg = Debug|Win32
{A9D95A91-4DB7-4F72-BEB6-FE8A5C89BFBD}.Debug|x86.Build.0 = Debug|Win32
{A9D95A91-4DB7-4F72-BEB6-FE8A5C89BFBD}.Release|Any CPU.ActiveCfg = Release|Win32
{A9D95A91-4DB7-4F72-BEB6-FE8A5C89BFBD}.Release|ARM.ActiveCfg = Release|ARM
{A9D95A91-4DB7-4F72-BEB6-FE8A5C89BFBD}.Release|ARM.Build.0 = Release|ARM
{A9D95A91-4DB7-4F72-BEB6-FE8A5C89BFBD}.Release|ARM64.ActiveCfg = Release|ARM64
@ -126,6 +137,7 @@ Global
{A9D95A91-4DB7-4F72-BEB6-FE8A5C89BFBD}.Release|x64.Build.0 = Release|x64
{A9D95A91-4DB7-4F72-BEB6-FE8A5C89BFBD}.Release|x86.ActiveCfg = Release|Win32
{A9D95A91-4DB7-4F72-BEB6-FE8A5C89BFBD}.Release|x86.Build.0 = Release|Win32
{11C084A3-A57C-4296-A679-CAC17B603144}.Debug|Any CPU.ActiveCfg = Debug|Win32
{11C084A3-A57C-4296-A679-CAC17B603144}.Debug|ARM.ActiveCfg = Debug|ARM
{11C084A3-A57C-4296-A679-CAC17B603144}.Debug|ARM.Build.0 = Debug|ARM
{11C084A3-A57C-4296-A679-CAC17B603144}.Debug|ARM64.ActiveCfg = Debug|ARM64
@ -134,6 +146,7 @@ Global
{11C084A3-A57C-4296-A679-CAC17B603144}.Debug|x64.Build.0 = Debug|x64
{11C084A3-A57C-4296-A679-CAC17B603144}.Debug|x86.ActiveCfg = Debug|Win32
{11C084A3-A57C-4296-A679-CAC17B603144}.Debug|x86.Build.0 = Debug|Win32
{11C084A3-A57C-4296-A679-CAC17B603144}.Release|Any CPU.ActiveCfg = Release|Win32
{11C084A3-A57C-4296-A679-CAC17B603144}.Release|ARM.ActiveCfg = Release|ARM
{11C084A3-A57C-4296-A679-CAC17B603144}.Release|ARM.Build.0 = Release|ARM
{11C084A3-A57C-4296-A679-CAC17B603144}.Release|ARM64.ActiveCfg = Release|ARM64
@ -142,6 +155,7 @@ Global
{11C084A3-A57C-4296-A679-CAC17B603144}.Release|x64.Build.0 = Release|x64
{11C084A3-A57C-4296-A679-CAC17B603144}.Release|x86.ActiveCfg = Release|Win32
{11C084A3-A57C-4296-A679-CAC17B603144}.Release|x86.Build.0 = Release|Win32
{F7D32BD0-2749-483E-9A0D-1635EF7E3136}.Debug|Any CPU.ActiveCfg = Debug|Win32
{F7D32BD0-2749-483E-9A0D-1635EF7E3136}.Debug|ARM.ActiveCfg = Debug|ARM
{F7D32BD0-2749-483E-9A0D-1635EF7E3136}.Debug|ARM.Build.0 = Debug|ARM
{F7D32BD0-2749-483E-9A0D-1635EF7E3136}.Debug|ARM64.ActiveCfg = Debug|ARM64
@ -150,6 +164,7 @@ Global
{F7D32BD0-2749-483E-9A0D-1635EF7E3136}.Debug|x64.Build.0 = Debug|x64
{F7D32BD0-2749-483E-9A0D-1635EF7E3136}.Debug|x86.ActiveCfg = Debug|Win32
{F7D32BD0-2749-483E-9A0D-1635EF7E3136}.Debug|x86.Build.0 = Debug|Win32
{F7D32BD0-2749-483E-9A0D-1635EF7E3136}.Release|Any CPU.ActiveCfg = Release|Win32
{F7D32BD0-2749-483E-9A0D-1635EF7E3136}.Release|ARM.ActiveCfg = Release|ARM
{F7D32BD0-2749-483E-9A0D-1635EF7E3136}.Release|ARM.Build.0 = Release|ARM
{F7D32BD0-2749-483E-9A0D-1635EF7E3136}.Release|ARM64.ActiveCfg = Release|ARM64
@ -158,6 +173,7 @@ Global
{F7D32BD0-2749-483E-9A0D-1635EF7E3136}.Release|x64.Build.0 = Release|x64
{F7D32BD0-2749-483E-9A0D-1635EF7E3136}.Release|x86.ActiveCfg = Release|Win32
{F7D32BD0-2749-483E-9A0D-1635EF7E3136}.Release|x86.Build.0 = Release|Win32
{A62D504A-16B8-41D2-9F19-E2E86019E5E4}.Debug|Any CPU.ActiveCfg = Debug|Win32
{A62D504A-16B8-41D2-9F19-E2E86019E5E4}.Debug|ARM.ActiveCfg = Debug|ARM
{A62D504A-16B8-41D2-9F19-E2E86019E5E4}.Debug|ARM.Build.0 = Debug|ARM
{A62D504A-16B8-41D2-9F19-E2E86019E5E4}.Debug|ARM64.ActiveCfg = Debug|ARM64
@ -166,6 +182,7 @@ Global
{A62D504A-16B8-41D2-9F19-E2E86019E5E4}.Debug|x64.Build.0 = Debug|x64
{A62D504A-16B8-41D2-9F19-E2E86019E5E4}.Debug|x86.ActiveCfg = Debug|Win32
{A62D504A-16B8-41D2-9F19-E2E86019E5E4}.Debug|x86.Build.0 = Debug|Win32
{A62D504A-16B8-41D2-9F19-E2E86019E5E4}.Release|Any CPU.ActiveCfg = Release|Win32
{A62D504A-16B8-41D2-9F19-E2E86019E5E4}.Release|ARM.ActiveCfg = Release|ARM
{A62D504A-16B8-41D2-9F19-E2E86019E5E4}.Release|ARM.Build.0 = Release|ARM
{A62D504A-16B8-41D2-9F19-E2E86019E5E4}.Release|ARM64.ActiveCfg = Release|ARM64
@ -174,6 +191,7 @@ Global
{A62D504A-16B8-41D2-9F19-E2E86019E5E4}.Release|x64.Build.0 = Release|x64
{A62D504A-16B8-41D2-9F19-E2E86019E5E4}.Release|x86.ActiveCfg = Release|Win32
{A62D504A-16B8-41D2-9F19-E2E86019E5E4}.Release|x86.Build.0 = Release|Win32
{FCA38F3C-7C73-4C47-BE4E-32F77FA8538D}.Debug|Any CPU.ActiveCfg = Debug|Win32
{FCA38F3C-7C73-4C47-BE4E-32F77FA8538D}.Debug|ARM.ActiveCfg = Debug|ARM
{FCA38F3C-7C73-4C47-BE4E-32F77FA8538D}.Debug|ARM.Build.0 = Debug|ARM
{FCA38F3C-7C73-4C47-BE4E-32F77FA8538D}.Debug|ARM64.ActiveCfg = Debug|ARM64
@ -182,6 +200,7 @@ Global
{FCA38F3C-7C73-4C47-BE4E-32F77FA8538D}.Debug|x64.Build.0 = Debug|x64
{FCA38F3C-7C73-4C47-BE4E-32F77FA8538D}.Debug|x86.ActiveCfg = Debug|Win32
{FCA38F3C-7C73-4C47-BE4E-32F77FA8538D}.Debug|x86.Build.0 = Debug|Win32
{FCA38F3C-7C73-4C47-BE4E-32F77FA8538D}.Release|Any CPU.ActiveCfg = Release|Win32
{FCA38F3C-7C73-4C47-BE4E-32F77FA8538D}.Release|ARM.ActiveCfg = Release|ARM
{FCA38F3C-7C73-4C47-BE4E-32F77FA8538D}.Release|ARM.Build.0 = Release|ARM
{FCA38F3C-7C73-4C47-BE4E-32F77FA8538D}.Release|ARM64.ActiveCfg = Release|ARM64
@ -190,6 +209,7 @@ Global
{FCA38F3C-7C73-4C47-BE4E-32F77FA8538D}.Release|x64.Build.0 = Release|x64
{FCA38F3C-7C73-4C47-BE4E-32F77FA8538D}.Release|x86.ActiveCfg = Release|Win32
{FCA38F3C-7C73-4C47-BE4E-32F77FA8538D}.Release|x86.Build.0 = Release|Win32
{729D9AF8-CD9E-4427-9F6C-FB757E287729}.Debug|Any CPU.ActiveCfg = Debug|Win32
{729D9AF8-CD9E-4427-9F6C-FB757E287729}.Debug|ARM.ActiveCfg = Debug|ARM
{729D9AF8-CD9E-4427-9F6C-FB757E287729}.Debug|ARM.Build.0 = Debug|ARM
{729D9AF8-CD9E-4427-9F6C-FB757E287729}.Debug|ARM64.ActiveCfg = Debug|Win32
@ -197,6 +217,7 @@ Global
{729D9AF8-CD9E-4427-9F6C-FB757E287729}.Debug|x64.Build.0 = Debug|x64
{729D9AF8-CD9E-4427-9F6C-FB757E287729}.Debug|x86.ActiveCfg = Debug|Win32
{729D9AF8-CD9E-4427-9F6C-FB757E287729}.Debug|x86.Build.0 = Debug|Win32
{729D9AF8-CD9E-4427-9F6C-FB757E287729}.Release|Any CPU.ActiveCfg = Release|Win32
{729D9AF8-CD9E-4427-9F6C-FB757E287729}.Release|ARM.ActiveCfg = Release|ARM
{729D9AF8-CD9E-4427-9F6C-FB757E287729}.Release|ARM.Build.0 = Release|ARM
{729D9AF8-CD9E-4427-9F6C-FB757E287729}.Release|ARM64.ActiveCfg = Release|Win32
@ -204,6 +225,24 @@ Global
{729D9AF8-CD9E-4427-9F6C-FB757E287729}.Release|x64.Build.0 = Release|x64
{729D9AF8-CD9E-4427-9F6C-FB757E287729}.Release|x86.ActiveCfg = Release|Win32
{729D9AF8-CD9E-4427-9F6C-FB757E287729}.Release|x86.Build.0 = Release|Win32
{FCB612AD-FACC-410C-828C-24B5940B2762}.Debug|Any CPU.ActiveCfg = Debug|Win32
{FCB612AD-FACC-410C-828C-24B5940B2762}.Debug|ARM.ActiveCfg = Debug|ARM
{FCB612AD-FACC-410C-828C-24B5940B2762}.Debug|ARM.Build.0 = Debug|ARM
{FCB612AD-FACC-410C-828C-24B5940B2762}.Debug|ARM64.ActiveCfg = Debug|ARM64
{FCB612AD-FACC-410C-828C-24B5940B2762}.Debug|ARM64.Build.0 = Debug|ARM64
{FCB612AD-FACC-410C-828C-24B5940B2762}.Debug|x64.ActiveCfg = Debug|x64
{FCB612AD-FACC-410C-828C-24B5940B2762}.Debug|x64.Build.0 = Debug|x64
{FCB612AD-FACC-410C-828C-24B5940B2762}.Debug|x86.ActiveCfg = Debug|Win32
{FCB612AD-FACC-410C-828C-24B5940B2762}.Debug|x86.Build.0 = Debug|Win32
{FCB612AD-FACC-410C-828C-24B5940B2762}.Release|Any CPU.ActiveCfg = Release|Win32
{FCB612AD-FACC-410C-828C-24B5940B2762}.Release|ARM.ActiveCfg = Release|ARM
{FCB612AD-FACC-410C-828C-24B5940B2762}.Release|ARM.Build.0 = Release|ARM
{FCB612AD-FACC-410C-828C-24B5940B2762}.Release|ARM64.ActiveCfg = Release|ARM64
{FCB612AD-FACC-410C-828C-24B5940B2762}.Release|ARM64.Build.0 = Release|ARM64
{FCB612AD-FACC-410C-828C-24B5940B2762}.Release|x64.ActiveCfg = Release|x64
{FCB612AD-FACC-410C-828C-24B5940B2762}.Release|x64.Build.0 = Release|x64
{FCB612AD-FACC-410C-828C-24B5940B2762}.Release|x86.ActiveCfg = Release|Win32
{FCB612AD-FACC-410C-828C-24B5940B2762}.Release|x86.Build.0 = Release|Win32
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE

View File

@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(SolutionDir)packages\Microsoft.Windows.CppWinRT.2.0.190730.2\build\native\Microsoft.Windows.CppWinRT.props" Condition="Exists('$(SolutionDir)packages\Microsoft.Windows.CppWinRT.2.0.190730.2\build\native\Microsoft.Windows.CppWinRT.props')" />
<Import Project="$(SolutionDir)packages\Microsoft.Windows.CppWinRT.2.0.200729.8\build\native\Microsoft.Windows.CppWinRT.props" Condition="Exists('$(SolutionDir)packages\Microsoft.Windows.CppWinRT.2.0.200729.8\build\native\Microsoft.Windows.CppWinRT.props')" />
<PropertyGroup Label="Globals">
<CppWinRTOptimized>true</CppWinRTOptimized>
<CppWinRTRootNamespaceAutoMerge>true</CppWinRTRootNamespaceAutoMerge>
@ -167,15 +167,15 @@
<Import Project="..\..\..\node_modules\react-native-windows\PropertySheets\Bundle.Cpp.targets" />
<Import Project="..\..\..\node_modules\react-native-windows\PropertySheets\Bundle.props" />
<ImportGroup Label="ExtensionTargets">
<Import Project="$(SolutionDir)packages\Microsoft.Windows.CppWinRT.2.0.190730.2\build\native\Microsoft.Windows.CppWinRT.targets" Condition="Exists('$(SolutionDir)packages\Microsoft.Windows.CppWinRT.2.0.190730.2\build\native\Microsoft.Windows.CppWinRT.targets')" />
<Import Project="$(SolutionDir)packages\Microsoft.Windows.CppWinRT.2.0.200729.8\build\native\Microsoft.Windows.CppWinRT.targets" Condition="Exists('$(SolutionDir)packages\Microsoft.Windows.CppWinRT.2.0.200729.8\build\native\Microsoft.Windows.CppWinRT.targets')" />
<Import Project="$(SolutionDir)packages\Microsoft.UI.Xaml.2.3.191129002\build\native\Microsoft.UI.Xaml.targets" Condition="Exists('$(SolutionDir)packages\Microsoft.UI.Xaml.2.3.191129002\build\native\Microsoft.UI.Xaml.targets')" />
</ImportGroup>
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
<PropertyGroup>
<ErrorText>This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}.</ErrorText>
</PropertyGroup>
<Error Condition="!Exists('$(SolutionDir)packages\Microsoft.Windows.CppWinRT.2.0.190730.2\build\native\Microsoft.Windows.CppWinRT.props')" Text="$([System.String]::Format('$(ErrorText)', '$(SolutionDir)packages\Microsoft.Windows.CppWinRT.2.0.190730.2\build\native\Microsoft.Windows.CppWinRT.props'))" />
<Error Condition="!Exists('$(SolutionDir)packages\Microsoft.Windows.CppWinRT.2.0.190730.2\build\native\Microsoft.Windows.CppWinRT.targets')" Text="$([System.String]::Format('$(ErrorText)', '$(SolutionDir)packages\Microsoft.Windows.CppWinRT.2.0.190730.2\build\native\Microsoft.Windows.CppWinRT.targets'))" />
<Error Condition="!Exists('$(SolutionDir)packages\Microsoft.Windows.CppWinRT.2.0.200729.8\build\native\Microsoft.Windows.CppWinRT.props')" Text="$([System.String]::Format('$(ErrorText)', '$(SolutionDir)packages\Microsoft.Windows.CppWinRT.2.0.200729.8\build\native\Microsoft.Windows.CppWinRT.props'))" />
<Error Condition="!Exists('$(SolutionDir)packages\Microsoft.Windows.CppWinRT.2.0.200729.8\build\native\Microsoft.Windows.CppWinRT.targets')" Text="$([System.String]::Format('$(ErrorText)', '$(SolutionDir)packages\Microsoft.Windows.CppWinRT.2.0.200729.8\build\native\Microsoft.Windows.CppWinRT.targets'))" />
<Error Condition="!Exists('$(SolutionDir)packages\Microsoft.UI.Xaml.2.3.191129002\build\native\Microsoft.UI.Xaml.targets')" Text="$([System.String]::Format('$(ErrorText)', '$(SolutionDir)packages\Microsoft.UI.Xaml.2.3.191129002\build\native\Microsoft.UI.Xaml.targets'))" />
</Target>
</Project>

View File

@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Microsoft.Windows.CppWinRT" version="2.0.190730.2" targetFramework="native" />
<package id="Microsoft.Windows.CppWinRT" version="2.0.200729.8" targetFramework="native" />
<package id="Microsoft.UI.Xaml" version="2.3.191129002" targetFramework="native" />
</packages>

View File

@ -65,7 +65,7 @@
"react": "16.11.0",
"react-native": "0.62.2",
"react-native-macos": "0.60.0-microsoft.73",
"react-native-windows": "^0.62.0-0",
"react-native-windows": "^0.62.5",
"semantic-release": "15.13.24",
"typescript": "3.8.3",
"appium": "1.17.0",

View File

@ -105,13 +105,22 @@ export default class WebView extends React.Component<WebViewSharedProps, State>
}
postMessage = (data: string) => {
const message = this.getInjectableJSMessage(data);
UIManager.dispatchViewManagerCommand(
this.getWebViewHandle(),
UIManager.getViewManagerConfig('RCTWebView').Commands.postMessage,
[String(data)],
UIManager.getViewManagerConfig('RCTWebView').Commands.injectJavaScript,
[message],
);
};
getInjectableJSMessage = (message: string ) => {
return `(function() {window.dispatchEvent(new MessageEvent('message', {data: ${JSON.stringify(
message
)}}));})();`;
}
/**
* We return an event with a bunch of fields including:
* url, title, loading, canGoBack, canGoForward

View File

@ -29,10 +29,14 @@ Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "ReactUWP", "..\node_modules
EndProject
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "ReactWindowsCore", "..\node_modules\react-native-windows\ReactWindowsCore\ReactWindowsCore.vcxproj", "{11C084A3-A57C-4296-A679-CAC17B603144}"
EndProject
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "WebViewBridgeComponent", "WebViewBridgeComponent\WebViewBridgeComponent.vcxproj", "{FCB612AD-FACC-410C-828C-24B5940B2762}"
EndProject
Global
GlobalSection(SharedMSBuildProjectFiles) = preSolution
..\node_modules\react-native-windows\JSI\Shared\JSI.Shared.vcxitems*{0cc28589-39e4-4288-b162-97b959f8b843}*SharedItemsImports = 9
..\node_modules\react-native-windows\ReactWindowsCore\ReactWindowsCore.vcxitems*{11c084a3-a57c-4296-a679-cac17b603144}*SharedItemsImports = 4
..\node_modules\react-native-windows\Chakra\Chakra.vcxitems*{2d5d43d9-cffc-4c40-b4cd-02efb4e2742b}*SharedItemsImports = 4
..\node_modules\react-native-windows\Mso\Mso.vcxitems*{2d5d43d9-cffc-4c40-b4cd-02efb4e2742b}*SharedItemsImports = 4
..\node_modules\react-native-windows\Shared\Shared.vcxitems*{2d5d43d9-cffc-4c40-b4cd-02efb4e2742b}*SharedItemsImports = 4
..\node_modules\react-native-windows\Microsoft.ReactNative.SharedManaged\Microsoft.ReactNative.SharedManaged.projitems*{67a1076f-7790-4203-86ea-4402ccb5e782}*SharedItemsImports = 13
..\node_modules\react-native-windows\Microsoft.ReactNative.Cxx\Microsoft.ReactNative.Cxx.vcxitems*{729d9af8-cd9e-4427-9f6c-fb757e287729}*SharedItemsImports = 4
@ -41,6 +45,7 @@ Global
..\node_modules\react-native-windows\Microsoft.ReactNative.Cxx\Microsoft.ReactNative.Cxx.vcxitems*{da8b35b3-da00-4b02-bde6-6a397b3fd46b}*SharedItemsImports = 9
..\node_modules\react-native-windows\Chakra\Chakra.vcxitems*{f7d32bd0-2749-483e-9a0d-1635ef7e3136}*SharedItemsImports = 4
..\node_modules\react-native-windows\JSI\Shared\JSI.Shared.vcxitems*{f7d32bd0-2749-483e-9a0d-1635ef7e3136}*SharedItemsImports = 4
..\node_modules\react-native-windows\Microsoft.ReactNative.Cxx\Microsoft.ReactNative.Cxx.vcxitems*{f7d32bd0-2749-483e-9a0d-1635ef7e3136}*SharedItemsImports = 4
..\node_modules\react-native-windows\Mso\Mso.vcxitems*{f7d32bd0-2749-483e-9a0d-1635ef7e3136}*SharedItemsImports = 4
..\node_modules\react-native-windows\Shared\Shared.vcxitems*{f7d32bd0-2749-483e-9a0d-1635ef7e3136}*SharedItemsImports = 4
EndGlobalSection
@ -181,6 +186,22 @@ Global
{11C084A3-A57C-4296-A679-CAC17B603144}.Release|x64.Build.0 = Release|x64
{11C084A3-A57C-4296-A679-CAC17B603144}.Release|x86.ActiveCfg = Release|Win32
{11C084A3-A57C-4296-A679-CAC17B603144}.Release|x86.Build.0 = Release|Win32
{FCB612AD-FACC-410C-828C-24B5940B2762}.Debug|ARM.ActiveCfg = Debug|ARM
{FCB612AD-FACC-410C-828C-24B5940B2762}.Debug|ARM.Build.0 = Debug|ARM
{FCB612AD-FACC-410C-828C-24B5940B2762}.Debug|ARM64.ActiveCfg = Debug|ARM64
{FCB612AD-FACC-410C-828C-24B5940B2762}.Debug|ARM64.Build.0 = Debug|ARM64
{FCB612AD-FACC-410C-828C-24B5940B2762}.Debug|x64.ActiveCfg = Debug|x64
{FCB612AD-FACC-410C-828C-24B5940B2762}.Debug|x64.Build.0 = Debug|x64
{FCB612AD-FACC-410C-828C-24B5940B2762}.Debug|x86.ActiveCfg = Debug|Win32
{FCB612AD-FACC-410C-828C-24B5940B2762}.Debug|x86.Build.0 = Debug|Win32
{FCB612AD-FACC-410C-828C-24B5940B2762}.Release|ARM.ActiveCfg = Release|ARM
{FCB612AD-FACC-410C-828C-24B5940B2762}.Release|ARM.Build.0 = Release|ARM
{FCB612AD-FACC-410C-828C-24B5940B2762}.Release|ARM64.ActiveCfg = Release|ARM64
{FCB612AD-FACC-410C-828C-24B5940B2762}.Release|ARM64.Build.0 = Release|ARM64
{FCB612AD-FACC-410C-828C-24B5940B2762}.Release|x64.ActiveCfg = Release|x64
{FCB612AD-FACC-410C-828C-24B5940B2762}.Release|x64.Build.0 = Release|x64
{FCB612AD-FACC-410C-828C-24B5940B2762}.Release|x86.ActiveCfg = Release|Win32
{FCB612AD-FACC-410C-828C-24B5940B2762}.Release|x86.Build.0 = Release|Win32
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE

View File

@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(SolutionDir)packages\Microsoft.Windows.CppWinRT.2.0.190730.2\build\native\Microsoft.Windows.CppWinRT.props" Condition="Exists('$(SolutionDir)packages\Microsoft.Windows.CppWinRT.2.0.190730.2\build\native\Microsoft.Windows.CppWinRT.props')" />
<Import Project="$(SolutionDir)packages\Microsoft.Windows.CppWinRT.2.0.200316.3\build\native\Microsoft.Windows.CppWinRT.props" Condition="Exists('$(SolutionDir)packages\Microsoft.Windows.CppWinRT.2.0.200316.3\build\native\Microsoft.Windows.CppWinRT.props')" />
<PropertyGroup Label="Globals">
<CppWinRTOptimized>true</CppWinRTOptimized>
<CppWinRTRootNamespaceAutoMerge>true</CppWinRTRootNamespaceAutoMerge>
@ -145,16 +145,19 @@
<Project>{f7d32bd0-2749-483e-9a0d-1635ef7e3136}</Project>
<Private>false</Private>
</ProjectReference>
<ProjectReference Include="..\WebViewBridgeComponent\WebViewBridgeComponent.vcxproj">
<Project>{176323cd-97ce-4b0f-9d11-a2b77bda829b}</Project>
</ProjectReference>
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
<Import Project="$(SolutionDir)packages\Microsoft.Windows.CppWinRT.2.0.190730.2\build\native\Microsoft.Windows.CppWinRT.targets" Condition="Exists('$(SolutionDir)packages\Microsoft.Windows.CppWinRT.2.0.190730.2\build\native\Microsoft.Windows.CppWinRT.targets')" />
<Import Project="$(SolutionDir)packages\Microsoft.Windows.CppWinRT.2.0.200316.3\build\native\Microsoft.Windows.CppWinRT.targets" Condition="Exists('$(SolutionDir)packages\Microsoft.Windows.CppWinRT.2.0.200316.3\build\native\Microsoft.Windows.CppWinRT.targets')" />
</ImportGroup>
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
<PropertyGroup>
<ErrorText>This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}.</ErrorText>
</PropertyGroup>
<Error Condition="!Exists('$(SolutionDir)packages\Microsoft.Windows.CppWinRT.2.0.190730.2\build\native\Microsoft.Windows.CppWinRT.props')" Text="$([System.String]::Format('$(ErrorText)', '$(SolutionDir)packages\Microsoft.Windows.CppWinRT.2.0.190730.2\build\native\Microsoft.Windows.CppWinRT.props'))" />
<Error Condition="!Exists('$(SolutionDir)packages\Microsoft.Windows.CppWinRT.2.0.190730.2\build\native\Microsoft.Windows.CppWinRT.targets')" Text="$([System.String]::Format('$(ErrorText)', '$(SolutionDir)packages\Microsoft.Windows.CppWinRT.2.0.190730.2\build\native\Microsoft.Windows.CppWinRT.targets'))" />
<Error Condition="!Exists('$(SolutionDir)packages\Microsoft.Windows.CppWinRT.2.0.200316.3\build\native\Microsoft.Windows.CppWinRT.props')" Text="$([System.String]::Format('$(ErrorText)', '$(SolutionDir)packages\Microsoft.Windows.CppWinRT.2.0.200316.3\build\native\Microsoft.Windows.CppWinRT.props'))" />
<Error Condition="!Exists('$(SolutionDir)packages\Microsoft.Windows.CppWinRT.2.0.200316.3\build\native\Microsoft.Windows.CppWinRT.targets')" Text="$([System.String]::Format('$(ErrorText)', '$(SolutionDir)packages\Microsoft.Windows.CppWinRT.2.0.200316.3\build\native\Microsoft.Windows.CppWinRT.targets'))" />
</Target>
</Project>

View File

@ -6,6 +6,8 @@
#include "ReactWebView.h"
#include "ReactWebView.g.cpp"
namespace winrt {
using namespace Microsoft::ReactNative;
using namespace Windows::Data::Json;
@ -26,11 +28,15 @@ namespace winrt::ReactNativeWebView::implementation {
#else
m_webView = winrt::WebView();
#endif
this->Content(m_webView);
RegisterEvents();
}
winrt::WebView ReactWebView::GetView() {
return m_webView;
ReactWebView::~ReactWebView()
{
if (m_messagingEnabled) {
m_webBridge.MessagePostEvent(m_messageToken);
}
}
void ReactWebView::RegisterEvents() {
@ -55,71 +61,80 @@ namespace winrt::ReactNativeWebView::implementation {
self->OnNavigationFailed(sender, args);
}
});
m_scriptNotifyRevoker = m_webView.ScriptNotify(
winrt::auto_revoke, [ref = get_weak()](auto const& sender, auto const& args) {
if (auto self = ref.get()) {
self->OnScriptNotify(sender, args);
}
});
}
void ReactWebView::WriteWebViewNavigationEventArg(winrt::IJSValueWriter const& eventDataWriter) {
auto tag = m_webView.GetValue(winrt::FrameworkElement::TagProperty()).as<winrt::IPropertyValue>().GetInt64();
WriteProperty(eventDataWriter, L"canGoBack", m_webView.CanGoBack());
WriteProperty(eventDataWriter, L"canGoForward", m_webView.CanGoForward());
WriteProperty(eventDataWriter, L"loading", !m_webView.IsLoaded());
void ReactWebView::WriteWebViewNavigationEventArg(winrt::WebView const& sender, winrt::IJSValueWriter const& eventDataWriter) {
auto tag = this->GetValue(winrt::FrameworkElement::TagProperty()).as<winrt::IPropertyValue>().GetInt64();
WriteProperty(eventDataWriter, L"canGoBack", sender.CanGoBack());
WriteProperty(eventDataWriter, L"canGoForward", sender.CanGoForward());
WriteProperty(eventDataWriter, L"loading", !sender.IsLoaded());
WriteProperty(eventDataWriter, L"target", tag);
WriteProperty(eventDataWriter, L"title", m_webView.DocumentTitle());
if (auto uri = m_webView.Source()) {
WriteProperty(eventDataWriter, L"title", sender.DocumentTitle());
if (auto uri = sender.Source()) {
WriteProperty(eventDataWriter, L"url", uri.AbsoluteCanonicalUri());
}
}
void ReactWebView::OnNavigationStarting(winrt::WebView const& webView, winrt::WebViewNavigationStartingEventArgs const& /*args*/) {
m_reactContext.DispatchEvent(
webView,
*this,
L"topLoadingStart",
[&](winrt::IJSValueWriter const& eventDataWriter) noexcept {
eventDataWriter.WriteObjectBegin();
WriteWebViewNavigationEventArg(eventDataWriter);
WriteWebViewNavigationEventArg(webView, eventDataWriter);
eventDataWriter.WriteObjectEnd();
});
if (m_messagingEnabled) {
m_webBridge = WebBridge();
m_messageToken = m_webBridge.MessagePostEvent([this](const auto&, hstring const& message)
{
this->OnMessagePosted(message);
});
webView.AddWebAllowedObject(L"__REACT_WEB_VIEW_BRIDGE", m_webBridge);
}
}
void ReactWebView::OnMessagePosted(hstring const& message)
{
HandleMessageFromJS(message);
}
void ReactWebView::OnNavigationCompleted(winrt::WebView const& webView, winrt::WebViewNavigationCompletedEventArgs const& /*args*/) {
m_reactContext.DispatchEvent(
webView,
*this,
L"topLoadingFinish",
[&](winrt::IJSValueWriter const& eventDataWriter) noexcept {
eventDataWriter.WriteObjectBegin();
WriteWebViewNavigationEventArg(eventDataWriter);
WriteWebViewNavigationEventArg(webView, eventDataWriter);
eventDataWriter.WriteObjectEnd();
});
winrt::hstring windowAlert = L"window.alert = function (msg) {window.external.notify(`{\"type\":\"__alert\",\"message\":\"${msg}\"}`)};";
winrt::hstring postMessage = L"window.ReactNativeWebView = {postMessage: function (data) {window.external.notify(String(data))}};";
m_webView.InvokeScriptAsync(L"eval", { windowAlert + postMessage });
if (m_messagingEnabled) {
winrt::hstring windowAlert = L"window.alert = function (msg) {__REACT_WEB_VIEW_BRIDGE.postMessage(`{\"type\":\"__alert\",\"message\":\"${msg}\"}`)};";
winrt::hstring postMessage = L"window.ReactNativeWebView = {postMessage: function (data) {__REACT_WEB_VIEW_BRIDGE.postMessage(String(data))}};";
webView.InvokeScriptAsync(L"eval", { windowAlert + postMessage });
}
}
void ReactWebView::OnNavigationFailed(winrt::IInspectable const& /*sender*/, winrt::WebViewNavigationFailedEventArgs const& args) {
m_reactContext.DispatchEvent(
m_webView,
*this,
L"topLoadingError",
[&](winrt::IJSValueWriter const& eventDataWriter) noexcept {
auto httpCode = static_cast<int32_t>(args.WebErrorStatus());
eventDataWriter.WriteObjectBegin();
{
WriteProperty(eventDataWriter, L"code", httpCode);
WriteWebViewNavigationEventArg(eventDataWriter);
WriteWebViewNavigationEventArg(m_webView, eventDataWriter);
}
eventDataWriter.WriteObjectEnd();
});
}
void ReactWebView::OnScriptNotify(winrt::IInspectable const& /*sender*/, winrt::Windows::UI::Xaml::Controls::NotifyEventArgs const& args) {
void ReactWebView::HandleMessageFromJS(winrt::hstring const& message) {
winrt::JsonObject jsonObject;
if (winrt::JsonObject::TryParse(args.Value(), jsonObject) && jsonObject.HasKey(L"type")) {
if (winrt::JsonObject::TryParse(message, jsonObject) && jsonObject.HasKey(L"type")) {
auto type = jsonObject.GetNamedString(L"type");
if (type == L"__alert") {
auto dialog = winrt::MessageDialog(jsonObject.GetNamedString(L"message"));
@ -129,12 +144,8 @@ namespace winrt::ReactNativeWebView::implementation {
}
}
PostMessage(winrt::hstring(args.Value()));
}
void ReactWebView::PostMessage(winrt::hstring const& message) {
m_reactContext.DispatchEvent(
m_webView,
*this,
L"topMessage",
[&](winrt::Microsoft::ReactNative::IJSValueWriter const& eventDataWriter) noexcept {
eventDataWriter.WriteObjectBegin();
@ -145,4 +156,8 @@ namespace winrt::ReactNativeWebView::implementation {
});
}
void ReactWebView::SetMessagingEnabled(bool enabled) {
m_messagingEnabled = enabled;
}
} // namespace winrt::ReactNativeWebView::implementation

View File

@ -7,28 +7,35 @@
#include "NativeModules.h"
#include "ReactWebView.g.h"
#include "winrt/WebViewBridgeComponent.h"
using namespace winrt::WebViewBridgeComponent;
namespace winrt::ReactNativeWebView::implementation {
class ReactWebView : public ReactWebViewT<ReactWebView> {
public:
ReactWebView(Microsoft::ReactNative::IReactContext const& reactContext);
winrt::Windows::UI::Xaml::Controls::WebView GetView();
void PostMessage(winrt::hstring const& message);
void SetMessagingEnabled(bool enabled);
~ReactWebView();
private:
bool m_messagingEnabled{ true };
winrt::Windows::UI::Xaml::Controls::WebView m_webView{ nullptr };
Microsoft::ReactNative::IReactContext m_reactContext{ nullptr };
WebBridge m_webBridge{ nullptr };
winrt::event_token m_messageToken;
winrt::Windows::UI::Xaml::Controls::WebView::NavigationStarting_revoker m_navigationStartingRevoker{};
winrt::Windows::UI::Xaml::Controls::WebView::NavigationCompleted_revoker m_navigationCompletedRevoker{};
winrt::Windows::UI::Xaml::Controls::WebView::NavigationFailed_revoker m_navigationFailedRevoker{};
winrt::Windows::UI::Xaml::Controls::WebView::ScriptNotify_revoker m_scriptNotifyRevoker{};
void HandleMessageFromJS(winrt::hstring const& message);
void RegisterEvents();
void WriteWebViewNavigationEventArg(winrt::Microsoft::ReactNative::IJSValueWriter const& eventDataWriter);
void WriteWebViewNavigationEventArg(winrt::Windows::UI::Xaml::Controls::WebView const& sender, winrt::Microsoft::ReactNative::IJSValueWriter const& eventDataWriter);
void OnNavigationStarting(winrt::Windows::UI::Xaml::Controls::WebView const& sender, winrt::Windows::UI::Xaml::Controls::WebViewNavigationStartingEventArgs const& args);
void OnNavigationCompleted(winrt::Windows::UI::Xaml::Controls::WebView const& sender, winrt::Windows::UI::Xaml::Controls::WebViewNavigationCompletedEventArgs const& args);
void OnNavigationFailed(winrt::Windows::Foundation::IInspectable const& sender, winrt::Windows::UI::Xaml::Controls::WebViewNavigationFailedEventArgs const& args);
void OnScriptNotify(winrt::Windows::Foundation::IInspectable const& sender, winrt::Windows::UI::Xaml::Controls::NotifyEventArgs const& args);
void OnMessagePosted(hstring const& message);
};
} // namespace winrt::ReactNativeWebView::implementation

View File

@ -2,7 +2,6 @@ namespace ReactNativeWebView{
[default_interface]
runtimeclass ReactWebView : Windows.UI.Xaml.Controls.UserControl{
ReactWebView(Microsoft.ReactNative.IReactContext context);
Windows.UI.Xaml.Controls.WebView GetView();
void PostMessage(String message);
void SetMessagingEnabled(Boolean enabled);
};
} // namespace ReactNativeWebView

View File

@ -23,8 +23,8 @@ namespace winrt::ReactNativeWebView::implementation {
}
winrt::FrameworkElement ReactWebViewManager::CreateView() noexcept {
m_reactWebView = *winrt::make_self<ReactWebView>(m_reactContext);
return m_reactWebView.GetView();
auto view = winrt::ReactNativeWebView::ReactWebView(m_reactContext);
return view;
}
// IViewManagerWithReactContext
@ -40,13 +40,17 @@ namespace winrt::ReactNativeWebView::implementation {
IMapView<hstring, ViewManagerPropertyType> ReactWebViewManager::NativeProps() noexcept {
auto nativeProps = winrt::single_threaded_map<hstring, ViewManagerPropertyType>();
nativeProps.Insert(L"source", ViewManagerPropertyType::Map);
nativeProps.Insert(L"backgroundColor", ViewManagerPropertyType::Color);
nativeProps.Insert(L"messagingEnabled", ViewManagerPropertyType::Boolean);
return nativeProps.GetView();
}
void ReactWebViewManager::UpdateProperties(
FrameworkElement const& view,
IJSValueReader const& propertyMapReader) noexcept {
if (auto webView = view.try_as<winrt::WebView>()) {
auto control = view.as<winrt::UserControl>();
auto content = control.Content();
auto webView = content.as<winrt::WebView>();
const JSValueObject& propertyMap = JSValueObject::ReadFrom(propertyMapReader);
for (auto const& pair : propertyMap) {
@ -82,6 +86,10 @@ namespace winrt::ReactNativeWebView::implementation {
auto color = propertyValue.To<winrt::Color>();
webView.DefaultBackgroundColor(color.A==0 ? winrt::Colors::Transparent() : color);
}
else if (propertyName == "messagingEnabled") {
auto messagingEnabled = propertyValue.To<bool>();
auto reactWebView = view.as<ReactNativeWebView::ReactWebView>();
reactWebView.SetMessagingEnabled(messagingEnabled);
}
}
}
@ -108,7 +116,6 @@ namespace winrt::ReactNativeWebView::implementation {
commands.Append(L"reload");
commands.Append(L"stopLoading");
commands.Append(L"injectJavaScript");
commands.Append(L"postMessage");
return commands.GetView();
}
@ -116,8 +123,11 @@ namespace winrt::ReactNativeWebView::implementation {
FrameworkElement const& view,
winrt::hstring const& commandId,
winrt::IJSValueReader const& commandArgsReader) noexcept {
auto control = view.as<winrt::UserControl>();
auto content = control.Content();
auto webView = content.as<winrt::WebView>();
auto commandArgs = JSValue::ReadArrayFrom(commandArgsReader);
if (auto webView = view.try_as<winrt::WebView>()) {
if (commandId == L"goForward") {
if (webView.CanGoForward()) {
webView.GoForward();
@ -136,9 +146,6 @@ namespace winrt::ReactNativeWebView::implementation {
}
else if (commandId == L"injectJavaScript") {
webView.InvokeScriptAsync(L"eval", { winrt::to_hstring(commandArgs[0].AsString()) });
} else if(commandId == L"postMessage") {
m_reactWebView.PostMessage(winrt::to_hstring(commandArgs[0].AsString()));
}
}
}

View File

@ -47,7 +47,6 @@ namespace winrt::ReactNativeWebView::implementation {
winrt::Microsoft::ReactNative::IJSValueReader const& commandArgsReader) noexcept;
private:
winrt::ReactNativeWebView::ReactWebView m_reactWebView{ nullptr };
winrt::Microsoft::ReactNative::IReactContext m_reactContext{ nullptr };
};

View File

@ -1,4 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Microsoft.Windows.CppWinRT" version="2.0.190730.2" targetFramework="native" />
<package id="Microsoft.Windows.CppWinRT" version="2.0.200316.3" targetFramework="native" />
</packages>

View File

@ -0,0 +1,16 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ImportGroup Label="PropertySheets" />
<PropertyGroup Label="UserMacros" />
<!--
To customize common C++/WinRT project properties:
* right-click the project node
* expand the Common Properties item
* select the C++/WinRT property page
For more advanced scenarios, and complete documentation, please see:
https://github.com/Microsoft/cppwinrt/tree/master/nuget
-->
<PropertyGroup />
<ItemDefinitionGroup />
</Project>

View File

@ -0,0 +1,19 @@
#include "pch.h"
#include "WebBridge.h"
#include "WebBridge.g.cpp"
namespace winrt::WebViewBridgeComponent::implementation
{
void WebBridge::PostMessage(hstring const& message)
{
m_messageEvent(*this, message);
}
winrt::event_token WebBridge::MessagePostEvent(Windows::Foundation::EventHandler<hstring> const& handler)
{
return m_messageEvent.add(handler);
}
void WebBridge::MessagePostEvent(winrt::event_token const& token) noexcept
{
m_messageEvent.remove(token);
}
}

View File

@ -0,0 +1,24 @@
#pragma once
#include "WebBridge.g.h"
namespace winrt::WebViewBridgeComponent::implementation
{
struct WebBridge : WebBridgeT<WebBridge>
{
WebBridge() = default;
void PostMessage(hstring const& message);
winrt::event_token MessagePostEvent(Windows::Foundation::EventHandler<hstring> const& handler);
void MessagePostEvent(winrt::event_token const& token) noexcept;
private:
winrt::event<Windows::Foundation::EventHandler<winrt::hstring>> m_messageEvent;
};
}
namespace winrt::WebViewBridgeComponent::factory_implementation
{
struct WebBridge : WebBridgeT<WebBridge, implementation::WebBridge>
{
};
}

View File

@ -0,0 +1,11 @@
namespace WebViewBridgeComponent
{
[default_interface]
[allowforweb]
runtimeclass WebBridge
{
WebBridge();
void PostMessage(String message);
event Windows.Foundation.EventHandler<String> MessagePostEvent;
}
}

View File

@ -0,0 +1,3 @@
EXPORTS
DllCanUnloadNow = WINRT_CanUnloadNow PRIVATE
DllGetActivationFactory = WINRT_GetActivationFactory PRIVATE

View File

@ -0,0 +1,158 @@
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(SolutionDir)packages\Microsoft.Windows.CppWinRT.2.0.200729.8\build\native\Microsoft.Windows.CppWinRT.props" Condition="Exists('$(SolutionDir)packages\Microsoft.Windows.CppWinRT.2.0.200729.8\build\native\Microsoft.Windows.CppWinRT.props')" />
<PropertyGroup Label="Globals">
<CppWinRTOptimized>true</CppWinRTOptimized>
<CppWinRTRootNamespaceAutoMerge>true</CppWinRTRootNamespaceAutoMerge>
<CppWinRTGenerateWindowsMetadata>true</CppWinRTGenerateWindowsMetadata>
<MinimalCoreWin>true</MinimalCoreWin>
<ProjectGuid>{fcb612ad-facc-410c-828c-24b5940b2762}</ProjectGuid>
<ProjectName>WebViewBridgeComponent</ProjectName>
<RootNamespace>WebViewBridgeComponent</RootNamespace>
<DefaultLanguage>en-US</DefaultLanguage>
<MinimumVisualStudioVersion>14.0</MinimumVisualStudioVersion>
<AppContainerApplication>true</AppContainerApplication>
<ApplicationType>Windows Store</ApplicationType>
<ApplicationTypeRevision>10.0</ApplicationTypeRevision>
<WindowsTargetPlatformVersion Condition=" '$(WindowsTargetPlatformVersion)' == '' ">10.0.18362.0</WindowsTargetPlatformVersion>
<WindowsTargetPlatformMinVersion>10.0.17134.0</WindowsTargetPlatformMinVersion>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
<ItemGroup Label="ProjectConfigurations">
<ProjectConfiguration Include="Debug|ARM">
<Configuration>Debug</Configuration>
<Platform>ARM</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Debug|ARM64">
<Configuration>Debug</Configuration>
<Platform>ARM64</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Debug|Win32">
<Configuration>Debug</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Debug|x64">
<Configuration>Debug</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|ARM">
<Configuration>Release</Configuration>
<Platform>ARM</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|ARM64">
<Configuration>Release</Configuration>
<Platform>ARM64</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|Win32">
<Configuration>Release</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|x64">
<Configuration>Release</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
</ItemGroup>
<PropertyGroup Label="Configuration">
<ConfigurationType>DynamicLibrary</ConfigurationType>
<PlatformToolset>v140</PlatformToolset>
<PlatformToolset Condition="'$(VisualStudioVersion)' == '15.0'">v141</PlatformToolset>
<PlatformToolset Condition="'$(VisualStudioVersion)' == '16.0'">v142</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
<GenerateManifest>false</GenerateManifest>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)'=='Debug'" Label="Configuration">
<UseDebugLibraries>true</UseDebugLibraries>
<LinkIncremental>true</LinkIncremental>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)'=='Release'" Label="Configuration">
<UseDebugLibraries>false</UseDebugLibraries>
<WholeProgramOptimization>true</WholeProgramOptimization>
<LinkIncremental>false</LinkIncremental>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
<ImportGroup Label="ExtensionSettings">
</ImportGroup>
<ImportGroup Label="Shared">
</ImportGroup>
<ImportGroup Label="PropertySheets">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets">
<Import Project="PropertySheet.props" />
</ImportGroup>
<PropertyGroup Label="UserMacros" />
<PropertyGroup />
<ItemDefinitionGroup>
<ClCompile>
<PrecompiledHeader>Use</PrecompiledHeader>
<PrecompiledHeaderFile>pch.h</PrecompiledHeaderFile>
<PrecompiledHeaderOutputFile>$(IntDir)pch.pch</PrecompiledHeaderOutputFile>
<WarningLevel>Level4</WarningLevel>
<AdditionalOptions>%(AdditionalOptions) /bigobj</AdditionalOptions>
<!--Temporarily disable cppwinrt heap enforcement to work around xaml compiler generated std::shared_ptr use -->
<AdditionalOptions Condition="'$(CppWinRTHeapEnforcement)'==''">/DWINRT_NO_MAKE_DETECTION %(AdditionalOptions)</AdditionalOptions>
<DisableSpecificWarnings>
</DisableSpecificWarnings>
<PreprocessorDefinitions>_WINRT_DLL;WIN32_LEAN_AND_MEAN;WINRT_LEAN_AND_MEAN;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<AdditionalUsingDirectories>$(WindowsSDK_WindowsMetadata);$(AdditionalUsingDirectories)</AdditionalUsingDirectories>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<GenerateWindowsMetadata>false</GenerateWindowsMetadata>
<ModuleDefinitionFile>WebViewBridgeComponent.def</ModuleDefinitionFile>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)'=='Debug'">
<ClCompile>
<PreprocessorDefinitions>_DEBUG;%(PreprocessorDefinitions)</PreprocessorDefinitions>
</ClCompile>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)'=='Release'">
<ClCompile>
<PreprocessorDefinitions>NDEBUG;%(PreprocessorDefinitions)</PreprocessorDefinitions>
</ClCompile>
<Link>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
</Link>
</ItemDefinitionGroup>
<ItemGroup>
<ClInclude Include="pch.h" />
<ClInclude Include="WebBridge.h">
<DependentUpon>WebBridge.idl</DependentUpon>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="pch.cpp">
<PrecompiledHeader>Create</PrecompiledHeader>
</ClCompile>
<ClCompile Include="WebBridge.cpp">
<DependentUpon>WebBridge.idl</DependentUpon>
</ClCompile>
<ClCompile Include="$(GeneratedFilesDir)module.g.cpp" />
</ItemGroup>
<ItemGroup>
<Midl Include="WebBridge.idl" />
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
<None Include="WebViewBridgeComponent.def" />
</ItemGroup>
<ItemGroup>
<None Include="PropertySheet.props" />
<Text Include="readme.txt">
<DeploymentContent>false</DeploymentContent>
</Text>
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
<Import Project="$(SolutionDir)packages\Microsoft.Windows.CppWinRT.2.0.200729.8\build\native\Microsoft.Windows.CppWinRT.targets" Condition="Exists('$(SolutionDir)packages\Microsoft.Windows.CppWinRT.2.0.200729.8\build\native\Microsoft.Windows.CppWinRT.targets')" />
</ImportGroup>
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
<PropertyGroup>
<ErrorText>This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}.</ErrorText>
</PropertyGroup>
<Error Condition="!Exists('$(SolutionDir)packages\Microsoft.Windows.CppWinRT.2.0.200729.8\build\native\Microsoft.Windows.CppWinRT.props')" Text="$([System.String]::Format('$(ErrorText)', '$(SolutionDir)packages\Microsoft.Windows.CppWinRT.2.0.200729.8\build\native\Microsoft.Windows.CppWinRT.props'))" />
<Error Condition="!Exists('$(SolutionDir)packages\Microsoft.Windows.CppWinRT.2.0.200729.8\build\native\Microsoft.Windows.CppWinRT.targets')" Text="$([System.String]::Format('$(ErrorText)', '$(SolutionDir)packages\Microsoft.Windows.CppWinRT.2.0.200729.8\build\native\Microsoft.Windows.CppWinRT.targets'))" />
</Target>
</Project>

View File

@ -0,0 +1,33 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<Filter Include="Resources">
<UniqueIdentifier>accd3aa8-1ba0-4223-9bbe-0c431709210b</UniqueIdentifier>
<Extensions>rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tga;tiff;tif;png;wav;mfcribbon-ms</Extensions>
</Filter>
<Filter Include="Generated Files">
<UniqueIdentifier>{926ab91d-31b4-48c3-b9a4-e681349f27f0}</UniqueIdentifier>
</Filter>
</ItemGroup>
<ItemGroup>
<ClCompile Include="pch.cpp" />
<ClCompile Include="WebBridge.cpp" />
<ClCompile Include="$(GeneratedFilesDir)module.g.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="pch.h" />
</ItemGroup>
<ItemGroup>
<None Include="WebViewBridgeComponent.def" />
<None Include="packages.config" />
</ItemGroup>
<ItemGroup>
<None Include="PropertySheet.props" />
</ItemGroup>
<ItemGroup>
<Text Include="readme.txt" />
</ItemGroup>
<ItemGroup>
<Midl Include="WebBridge.idl" />
</ItemGroup>
</Project>

View File

@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Microsoft.Windows.CppWinRT" version="2.0.200729.8" targetFramework="native" />
</packages>

View File

@ -0,0 +1 @@
#include "pch.h"

View File

@ -0,0 +1,4 @@
#pragma once
#include <unknwn.h>
#include <winrt/Windows.Foundation.h>
#include <winrt/Windows.Foundation.Collections.h>

View File

@ -0,0 +1,23 @@
========================================================================
C++/WinRT WebViewBridgeComponent Project Overview
========================================================================
This project demonstrates how to get started authoring Windows Runtime
classes directly with standard C++, using the C++/WinRT SDK component
to generate implementation headers from interface (IDL) files. The
generated Windows Runtime component binary and WinMD files should then
be bundled with the Universal Windows Platform (UWP) app consuming them.
Steps:
1. Create an interface (IDL) file to define your Windows Runtime class,
its default interface, and any other interfaces it implements.
2. Build the project once to generate module.g.cpp, module.h.cpp, and
implementation templates under the "Generated Files" folder, as
well as skeleton class definitions under "Generated Files\sources".
3. Use the skeleton class definitions for reference to implement your
Windows Runtime classes.
========================================================================
Learn more about C++/WinRT here:
http://aka.ms/cppwinrt/
========================================================================

2634
yarn.lock

File diff suppressed because it is too large Load Diff