-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
194 additions
and
44 deletions.
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
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 |
---|---|---|
@@ -0,0 +1,2 @@ | ||
// pretending we are sending request from same origin using a Chrome browser. So image site won't block our request. | ||
export const FAKE_USER_AGENT = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36'; |
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
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
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
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
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 |
---|---|---|
@@ -0,0 +1,79 @@ | ||
/* eslint-disable @typescript-eslint/strict-boolean-expressions */ | ||
import { useState } from 'react'; | ||
import { useTranslation } from 'react-i18next'; | ||
import { MD3Colors, Text } from 'react-native-paper'; | ||
import { WebView } from 'react-native-webview'; | ||
import { styled } from 'styled-components/native'; | ||
import { FAKE_USER_AGENT } from '../../constants/webview'; | ||
import { IPageWorkspace } from '../../store/workspace'; | ||
|
||
const WebViewContainer = styled.View` | ||
flex: 2; | ||
height: 100%; | ||
display: flex; | ||
flex-direction: column; | ||
justify-content: center; | ||
align-items: center; | ||
`; | ||
const ErrorText = styled(Text)` | ||
color: ${MD3Colors.error50}; | ||
`; | ||
|
||
export interface WikiViewerProps { | ||
webPageWorkspace: IPageWorkspace; | ||
} | ||
|
||
export const WebPageViewer = ({ webPageWorkspace }: WikiViewerProps) => { | ||
const { t } = useTranslation(); | ||
const [loadHtmlError, setLoadHtmlError] = useState(''); | ||
const [loaded, setLoaded] = useState(false); | ||
const [webViewKeyToReloadAfterRecycleByOS, setWebViewKeyToReloadAfterRecycleByOS] = useState(0); | ||
const triggerFullReload = () => { | ||
setWebViewKeyToReloadAfterRecycleByOS(webViewKeyToReloadAfterRecycleByOS + 1); | ||
}; | ||
if (loadHtmlError) { | ||
return ( | ||
<WebViewContainer> | ||
<ErrorText variant='titleLarge'>{loadHtmlError}</ErrorText> | ||
</WebViewContainer> | ||
); | ||
} | ||
return ( | ||
<WebView | ||
key={webViewKeyToReloadAfterRecycleByOS} | ||
originWhitelist={['*']} | ||
mediaPlaybackRequiresUserAction={false} | ||
allowsInlineMediaPlayback | ||
javaScriptCanOpenWindowsAutomatically | ||
allowsBackForwardNavigationGestures | ||
allowsProtectedMedia | ||
allowFileAccess | ||
allowFileAccessFromFileURLs | ||
allowUniversalAccessFromFileURLs | ||
focusable | ||
geolocationEnabled | ||
importantForAccessibility='yes' | ||
keyboardDisplayRequiresUserAction={false} | ||
mediaCapturePermissionGrantType='grant' | ||
mixedContentMode='always' | ||
allowsAirPlayForMediaPlayback | ||
allowsFullscreenVideo | ||
userAgent={FAKE_USER_AGENT} | ||
source={{ uri: webPageWorkspace.uri }} | ||
renderError={(errorName) => <Text>{errorName}</Text>} | ||
renderLoading={() => <Text>{t('Loading')}</Text>} | ||
onRenderProcessGone={() => { | ||
// fix webview recycled by system https://github.com/react-native-webview/react-native-webview/issues/3062#issuecomment-1711645611 | ||
triggerFullReload(); | ||
}} | ||
onError={(syntheticEvent) => { | ||
setLoadHtmlError(syntheticEvent.nativeEvent.description); | ||
}} | ||
onLoadEnd={() => { | ||
// this is called every time a tiddler is opened. And will be call 3 times before wiki loaded, seems including when setting innerHTML. | ||
setLoaded(true); | ||
}} | ||
webviewDebuggingEnabled={true /* Open chrome://inspect/#devices to debug the WebView */} | ||
/> | ||
); | ||
}; |
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
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
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.