Files
notesnook/apps/mobile/src/components/NoteHistory/preview.js

155 lines
4.3 KiB
JavaScript
Raw Normal View History

2022-01-22 12:57:05 +05:00
import React, { useRef } from 'react';
import { Alert, Platform, View } from 'react-native';
2021-12-14 12:33:39 +05:00
import WebView from 'react-native-webview';
2022-01-22 12:57:05 +05:00
import { useTracked } from '../../provider';
import { useEditorStore } from '../../provider/stores';
import { eSendEvent, ToastEvent } from '../../services/EventManager';
2021-12-14 12:33:39 +05:00
import Navigation from '../../services/Navigation';
2022-01-22 12:57:05 +05:00
import { db } from '../../utils/database';
2022-02-28 13:48:59 +05:00
import { eCloseProgressDialog, eOnLoadNote } from '../../utils/events';
2022-01-22 12:57:05 +05:00
import { openLinkInBrowser } from '../../utils/functions';
2022-02-28 13:48:59 +05:00
import { normalize } from '../../utils/size';
2022-01-22 12:57:05 +05:00
import { getNote, sourceUri } from '../../views/Editor/Functions';
2022-01-05 19:32:58 +05:00
import tiny from '../../views/Editor/tiny/tiny';
2022-02-28 13:48:59 +05:00
import { IconButton } from '../ui/icon-button';
import { Button } from '../ui/button';
import DialogHeader from '../dialog/dialog-header';
import Paragraph from '../ui/typography/paragraph';
2021-12-14 12:33:39 +05:00
2022-01-22 12:57:05 +05:00
export default function NotePreview({ session, content }) {
2021-12-14 12:33:39 +05:00
const [state] = useTracked();
2022-01-22 12:57:05 +05:00
const { colors } = state;
2021-12-14 12:33:39 +05:00
const webviewRef = useRef();
const onLoad = async () => {
2022-01-22 12:57:05 +05:00
let preview = await db.content.insertPlaceholders(content, 'placeholder.svg');
2022-01-22 12:57:05 +05:00
let theme = { ...colors };
2021-12-14 12:33:39 +05:00
theme.factor = normalize(1);
2022-01-05 19:32:58 +05:00
webviewRef.current?.injectJavaScript(`
(function() {
let v = ${JSON.stringify(theme)}
if (pageTheme) {
pageTheme.colors = v;
}
setTheme()
})();
`);
postMessage('htmldiff', preview?.data);
2021-12-14 12:33:39 +05:00
};
function postMessage(type, value = null) {
let message = {
type: type,
value
};
webviewRef.current?.postMessage(JSON.stringify(message));
}
const _onShouldStartLoadWithRequest = request => {
2022-01-05 20:16:52 +05:00
if (request.url.includes('https')) {
if (Platform.OS === 'ios' && !request.isTopFrame) return;
2021-12-14 12:33:39 +05:00
openLinkInBrowser(request.url, colors)
.catch(e =>
ToastEvent.show({
title: 'Failed to open link',
message: e.message,
type: 'success',
context: 'local'
})
)
.then(r => {
console.log('closed');
});
return false;
} else {
return true;
}
};
async function restore() {
await db.noteHistory.restore(session.id);
if (useEditorStore.getState()?.currentEditingNote === session?.noteId) {
if (getNote()) {
2022-01-22 12:57:05 +05:00
eSendEvent(eOnLoadNote, { ...getNote(), forced: true });
}
}
2021-12-14 12:33:39 +05:00
eSendEvent(eCloseProgressDialog, 'note_history');
eSendEvent(eCloseProgressDialog);
Navigation.setRoutesToUpdate([
Navigation.routeNames.NotesPage,
Navigation.routeNames.Favorites,
Navigation.routeNames.Notes
]);
2021-12-14 12:33:39 +05:00
ToastEvent.show({
heading: 'Note restored successfully',
type: 'success'
});
}
return (
<View
style={{
2022-01-05 20:16:52 +05:00
height: session.locked ? null : 600,
2021-12-14 12:33:39 +05:00
width: '100%'
2022-01-22 12:57:05 +05:00
}}
>
2021-12-22 13:59:58 +05:00
<DialogHeader padding={12} title={session.session} />
2021-12-29 13:13:37 +05:00
{!session.locked ? (
<WebView
ref={webviewRef}
onShouldStartLoadWithRequest={_onShouldStartLoadWithRequest}
onLoad={onLoad}
style={{
width: '100%',
height: '100%',
backgroundColor: 'transparent'
}}
2022-01-05 19:32:58 +05:00
onError={e => {
console.log(e);
}}
2022-01-03 11:09:06 +05:00
nestedScrollEnabled
2021-12-29 13:13:37 +05:00
domStorageEnabled={true}
scrollEnabled={true}
bounces={false}
allowFileAccess={true}
scalesPageToFit={true}
allowingReadAccessToURL={Platform.OS === 'android' ? true : null}
allowFileAccessFromFileURLs={true}
allowUniversalAccessFromFileURLs={true}
originWhitelist={['*']}
javaScriptEnabled={true}
2022-01-05 19:32:58 +05:00
cacheMode="LOAD_DEFAULT"
2021-12-29 13:13:37 +05:00
cacheEnabled={true}
source={{
2022-01-05 20:40:36 +05:00
uri: sourceUri + 'plaineditor.html'
2021-12-29 13:13:37 +05:00
}}
/>
) : (
<View
style={{
width: '100%',
height: 100,
justifyContent: 'center',
alignItems: 'center'
2022-01-22 12:57:05 +05:00
}}
>
<Paragraph color={colors.icon}>Preview not available, content is encrypted.</Paragraph>
2021-12-29 13:13:37 +05:00
</View>
)}
2021-12-14 12:33:39 +05:00
<View
style={{
paddingHorizontal: 12
2022-01-22 12:57:05 +05:00
}}
>
<Button onPress={restore} title="Restore this version" type="accent" width="100%" />
2021-12-14 12:33:39 +05:00
</View>
</View>
);
}