Files
notesnook/apps/mobile/src/components/MergeEditor/index.js

669 lines
20 KiB
JavaScript
Raw Normal View History

2020-04-06 13:14:27 +05:00
import React, { createRef, useEffect, useState } from 'react';
import { Modal, Text, TouchableOpacity, View } from 'react-native';
import Animated, { Easing } from 'react-native-reanimated';
2020-03-26 15:42:37 +05:00
import Icon from 'react-native-vector-icons/MaterialCommunityIcons';
2020-03-26 13:39:04 +05:00
import WebView from 'react-native-webview';
2020-04-06 13:14:27 +05:00
import { normalize, SIZE, WEIGHT } from '../../common/common';
import { useTracked } from '../../provider';
2020-03-26 15:50:10 +05:00
import {
eSubscribeEvent,
eUnSubscribeEvent,
eSendEvent,
} from '../../services/eventManager';
import {
eApplyChanges,
eShowMergeDialog,
refreshNotesPage,
} from '../../services/events';
2020-04-06 13:14:27 +05:00
import { getElevation, h, db } from '../../utils/utils';
import { simpleDialogEvent, updateEvent } from '../DialogManager/recievers';
import { TEMPLATE_APPLY_CHANGES } from '../DialogManager/templates';
import { ACTIONS } from '../../provider/actions';
2020-03-26 13:39:04 +05:00
2020-04-06 13:14:27 +05:00
const { Value, timing } = Animated;
2020-04-11 11:11:42 +05:00
2020-03-26 13:39:04 +05:00
const firstWebViewHeight = new Value(h * 0.5 - 50);
const secondWebViewHeight = new Value(h * 0.5 - 50);
const primaryWebView = createRef();
const secondaryWebView = createRef();
2020-03-26 15:42:37 +05:00
let note = null;
2020-03-26 13:39:04 +05:00
2020-03-26 15:42:37 +05:00
function openEditorAnimation(
2020-03-26 13:39:04 +05:00
heightToAnimate,
extendedHeight = null,
siblingStatus,
) {
let openConfig = {
duration: 300,
toValue: !siblingStatus ? h - 100 : h * 0.5 - 50,
easing: Easing.inOut(Easing.ease),
};
let extendConfig = {
duration: 300,
toValue: h * 0.5 - 50,
easing: Easing.inOut(Easing.ease),
};
if (extendedHeight) {
timing(extendedHeight, extendConfig).start();
}
timing(heightToAnimate, openConfig).start();
}
2020-03-26 15:42:37 +05:00
function closeEditorAnimation(heightToAnimate, heightToExtend = null) {
2020-03-26 13:39:04 +05:00
let closeConfig = {
duration: 300,
toValue: 0,
easing: Easing.inOut(Easing.ease),
};
let extendConfig = {
duration: 300,
toValue: h - 100,
easing: Easing.inOut(Easing.ease),
};
if (heightToExtend) {
timing(heightToExtend, extendConfig).start();
}
timing(heightToAnimate, closeConfig).start();
}
let primaryDelta = null;
2020-03-26 15:42:37 +05:00
let primaryText = '';
2020-03-26 13:39:04 +05:00
let secondaryDelta = null;
2020-03-26 15:42:37 +05:00
let secondaryText = '';
2020-03-26 13:39:04 +05:00
const MergeEditor = () => {
const [state, dispatch] = useTracked();
2020-04-06 13:14:27 +05:00
const { colors } = state;
2020-03-26 15:42:37 +05:00
const [visible, setVisible] = useState(false);
2020-03-26 13:39:04 +05:00
const [primary, setPrimary] = useState(true);
const [secondary, setSecondary] = useState(true);
const [keepContentFrom, setKeepContentFrom] = useState(null);
const [copyToSave, setCopyToSave] = useState(null);
const [disardedContent, setDiscardedContent] = useState(null);
const postMessageToPrimaryWebView = message =>
primaryWebView.current?.postMessage(JSON.stringify(message));
const postMessageToSecondaryWebView = message =>
secondaryWebView.current?.postMessage(JSON.stringify(message));
const onPrimaryWebViewLoad = () => {
postMessageToPrimaryWebView({
2020-03-26 15:42:37 +05:00
type: 'delta',
value: primaryDelta,
2020-03-26 13:39:04 +05:00
});
2020-04-06 13:14:27 +05:00
let c = { ...colors };
2020-03-26 13:39:04 +05:00
c.factor = normalize(1);
postMessageToPrimaryWebView({
type: 'theme',
value: c,
});
};
const onSecondaryWebViewLoad = () => {
postMessageToSecondaryWebView({
2020-03-26 15:42:37 +05:00
type: 'delta',
value: secondaryDelta,
2020-03-26 13:39:04 +05:00
});
2020-04-06 13:14:27 +05:00
let c = { ...colors };
2020-03-26 13:39:04 +05:00
c.factor = normalize(1);
postMessageToSecondaryWebView({
type: 'theme',
value: c,
});
};
const _onShouldStartLoadWithRequest = request => {
if (request.url.includes('https')) {
Linking.openURL(request.url);
return false;
} else {
return true;
}
};
const onMessageFromPrimaryWebView = evt => {
2020-04-11 12:16:05 +05:00
2020-03-26 15:42:37 +05:00
if (evt.nativeEvent.data !== '') {
2020-03-26 13:39:04 +05:00
let data = JSON.parse(evt.nativeEvent.data);
primaryDelta = data.delta;
primaryText = data.text;
}
};
const onMessageFromSecondaryWebView = evt => {
2020-04-11 11:11:42 +05:00
2020-03-26 15:42:37 +05:00
if (evt.nativeEvent.data !== '') {
2020-03-26 13:39:04 +05:00
let data = JSON.parse(evt.nativeEvent.data);
secondaryDelta = data.delta;
secondaryText = data.text;
}
};
2020-03-26 15:42:37 +05:00
const applyChanges = async () => {
2020-04-11 11:11:42 +05:00
2020-03-26 15:42:37 +05:00
if (keepContentFrom === 'primary') {
await db.notes.add({
content: {
text: primaryText,
2020-04-07 10:22:17 +05:00
delta: {
data: primaryDelta,
resolved: true
},
2020-03-26 15:42:37 +05:00
},
id: note.id,
2020-03-31 10:55:19 +05:00
conflicted: false,
2020-03-26 15:42:37 +05:00
});
2020-03-26 15:50:10 +05:00
} else if (keepContentFrom === 'secondary') {
2020-03-26 15:42:37 +05:00
await db.notes.add({
content: {
text: secondaryText,
2020-04-07 10:22:17 +05:00
delta: {
data: primaryDelta,
resolved: true
},
2020-03-26 15:42:37 +05:00
},
id: note.id,
2020-03-31 10:55:19 +05:00
conflicted: false,
2020-03-26 15:42:37 +05:00
});
}
if (copyToSave === 'primary') {
await db.notes.add({
content: {
text: primaryText,
delta: primaryDelta,
},
id: null,
});
2020-03-26 15:50:10 +05:00
} else if (copyToSave === 'secondary') {
2020-03-26 15:42:37 +05:00
await db.notes.add({
content: {
text: secondaryText,
delta: secondaryDelta,
},
id: null,
});
}
2020-03-26 15:50:10 +05:00
eSendEvent(refreshNotesPage);
2020-04-06 13:14:27 +05:00
updateEvent({ type: ACTIONS.NOTES });
updateEvent({ type: ACTIONS.FAVORITES });
2020-03-26 15:42:37 +05:00
close();
};
const show = async item => {
note = item;
2020-04-11 11:11:42 +05:00
let rawDelta = await db.delta.raw(note.content.delta);
primaryDelta = rawDelta.data;
secondaryDelta = rawDelta.conflicted.data;
setVisible(true);
2020-03-26 15:42:37 +05:00
openEditorAnimation(firstWebViewHeight, secondWebViewHeight, true);
2020-03-26 13:39:04 +05:00
};
useEffect(() => {
eSubscribeEvent(eApplyChanges, applyChanges);
2020-03-26 15:42:37 +05:00
eSubscribeEvent(eShowMergeDialog, show);
2020-03-26 13:39:04 +05:00
return () => {
eUnSubscribeEvent(eApplyChanges, applyChanges);
2020-03-26 15:42:37 +05:00
eUnSubscribeEvent(eShowMergeDialog, show);
2020-03-26 13:39:04 +05:00
};
2020-05-08 23:43:38 +05:00
},[]);
2020-03-26 13:39:04 +05:00
const onPressKeepFromPrimaryWebView = () => {
if (keepContentFrom == 'primary') {
setKeepContentFrom(null);
openEditorAnimation(firstWebViewHeight, secondWebViewHeight);
} else {
setKeepContentFrom('primary');
closeEditorAnimation(firstWebViewHeight, secondWebViewHeight);
}
};
const onPressSaveCopyFromPrimaryWebView = () => {
setCopyToSave('primary');
simpleDialogEvent(TEMPLATE_APPLY_CHANGES);
};
const onPressKeepFromSecondaryWebView = () => {
if (keepContentFrom == 'secondary') {
setKeepContentFrom(null);
openEditorAnimation(secondWebViewHeight, firstWebViewHeight);
} else {
setKeepContentFrom('secondary');
closeEditorAnimation(secondWebViewHeight, firstWebViewHeight);
}
};
const onPressSaveCopyFromSecondaryWebView = () => {
setCopyToSave('secondary');
simpleDialogEvent(TEMPLATE_APPLY_CHANGES);
};
const onPressDiscardFromPrimaryWebView = () => {
setDiscardedContent('primary');
simpleDialogEvent(TEMPLATE_APPLY_CHANGES);
};
const onPressDiscardFromSecondaryWebView = () => {
setDiscardedContent('secondary');
simpleDialogEvent(TEMPLATE_APPLY_CHANGES);
};
2020-03-26 15:42:37 +05:00
const close = () => {
setVisible(false);
setPrimary(true);
setSecondary(true);
setCopyToSave(null);
setDiscardedContent(null);
setKeepContentFrom(null);
primaryDelta = null;
secondaryDelta = null;
primaryText = null;
secondaryText = null;
note = null;
openEditorAnimation(firstWebViewHeight, secondWebViewHeight, true);
};
2020-03-26 13:39:04 +05:00
const params = 'platform=' + Platform.OS;
const sourceUri =
(Platform.OS === 'android' ? 'file:///android_asset/' : '') +
'Web.bundle/loader.html';
const injectedJS = `if (!window.location.search) {
var link = document.getElementById('progress-bar');
link.href = './site2/plaineditor.html?${params}';
link.click();
}`;
return (
2020-04-11 11:11:42 +05:00
<Modal transparent={false} animated animationType="fade" visible={visible}>
2020-03-26 13:39:04 +05:00
<View
style={{
height: '100%',
width: '100%',
backgroundColor: 'rgba(0,0,0,0.3)',
}}>
<View
style={{
backgroundColor: '#f0f0f0',
width: '100%',
height: 50,
flexDirection: 'row',
justifyContent: 'space-between',
alignItems: 'center',
paddingHorizontal: 12,
}}>
<View
style={{
flexDirection: 'row',
alignItems: 'center',
justifyContent: 'space-between',
}}>
<Icon
style={{
width: 50,
height: 50,
textAlign: 'center',
textAlignVertical: 'center',
marginLeft: -8,
}}
2020-03-26 15:42:37 +05:00
onPress={close}
2020-03-26 13:39:04 +05:00
size={SIZE.xxl}
name="chevron-left"
/>
<TouchableOpacity
onPress={() => {
if (keepContentFrom === 'primary') return;
if (!primary) {
openEditorAnimation(
firstWebViewHeight,
secondary && keepContentFrom !== 'secondary'
? secondWebViewHeight
: null,
secondary && keepContentFrom !== 'secondary',
);
setPrimary(true);
} else {
closeEditorAnimation(
firstWebViewHeight,
secondary && keepContentFrom !== 'secondary'
? secondWebViewHeight
: null,
);
setPrimary(false);
}
}}
style={{
flexDirection: 'row',
alignItems: 'center',
justifyContent: 'space-between',
}}>
<Text
style={{
color: colors.icon,
fontSize: SIZE.xxs,
}}>
Saved on 10/10/20 {'\n'}
12:30pm on Tablet
</Text>
<Icon
size={SIZE.lg}
name={primary ? 'chevron-up' : 'chevron-down'}
/>
</TouchableOpacity>
</View>
<View
style={{
flexDirection: 'row',
alignItems: 'center',
justifyContent: 'space-between',
}}>
{keepContentFrom === 'secondary' ? (
<TouchableOpacity
onPress={onPressSaveCopyFromPrimaryWebView}
style={{
...getElevation(5),
height: 35,
backgroundColor: colors.accent,
borderRadius: 5,
paddingHorizontal: 12,
alignItems: 'center',
justifyContent: 'center',
marginRight: 10,
}}>
<Text
style={{
color: 'white',
textAlign: 'center',
fontFamily: WEIGHT.regular,
fontSize: SIZE.sm - 2,
}}>
Save as a copy
</Text>
</TouchableOpacity>
) : null}
{keepContentFrom === 'secondary' ? (
<TouchableOpacity
onPress={onPressDiscardFromPrimaryWebView}
style={{
...getElevation(5),
height: 35,
backgroundColor: colors.errorText,
borderRadius: 5,
paddingHorizontal: 12,
alignItems: 'center',
justifyContent: 'center',
}}>
<Text
style={{
color: 'white',
textAlign: 'center',
fontFamily: WEIGHT.regular,
fontSize: SIZE.sm - 2,
}}>
Discard
</Text>
</TouchableOpacity>
) : null}
{keepContentFrom === 'secondary' ? null : (
<TouchableOpacity
onPress={onPressKeepFromPrimaryWebView}
style={{
...getElevation(5),
height: 35,
backgroundColor:
keepContentFrom === 'primary'
? colors.errorText
: colors.accent,
borderRadius: 5,
paddingHorizontal: 12,
alignItems: 'center',
justifyContent: 'center',
}}>
<Text
style={{
color: 'white',
textAlign: 'center',
fontFamily: WEIGHT.regular,
fontSize: SIZE.sm - 2,
}}>
{keepContentFrom === 'primary' ? 'Undo' : 'Keep'}
</Text>
</TouchableOpacity>
)}
</View>
</View>
<Animated.View
style={{
height: firstWebViewHeight,
}}>
<WebView
onLoad={onPrimaryWebViewLoad}
ref={primaryWebView}
style={{
width: '100%',
height: '100%',
}}
injectedJavaScript={Platform.OS === 'ios' ? injectedJS : null}
onShouldStartLoadWithRequest={_onShouldStartLoadWithRequest}
cacheMode="LOAD_DEFAULT"
domStorageEnabled={true}
scrollEnabled={false}
bounces={false}
allowFileAccess={true}
scalesPageToFit={true}
allowingReadAccessToURL={Platform.OS === 'android' ? true : null}
allowFileAccessFromFileURLs={true}
allowUniversalAccessFromFileURLs={true}
originWhitelist={['*']}
javaScriptEnabled={true}
cacheEnabled={true}
onMessage={onMessageFromPrimaryWebView}
source={
Platform.OS === 'ios'
2020-04-06 13:14:27 +05:00
? { uri: sourceUri }
2020-03-26 13:39:04 +05:00
: {
2020-04-06 13:14:27 +05:00
uri: 'file:///android_asset/plaineditor.html',
baseUrl: 'file:///android_asset/',
}
2020-03-26 13:39:04 +05:00
}
/>
</Animated.View>
<View
style={{
backgroundColor: '#f0f0f0',
width: '100%',
height: 50,
flexDirection: 'row',
justifyContent: 'space-between',
alignItems: 'center',
paddingHorizontal: 12,
}}>
<View
style={{
flexDirection: 'row',
alignItems: 'center',
justifyContent: 'space-between',
}}>
<TouchableOpacity
onPress={() => {
if (keepContentFrom === 'secondary') return;
if (!secondary) {
openEditorAnimation(
secondWebViewHeight,
primary && keepContentFrom !== 'primary'
? firstWebViewHeight
: null,
primary && keepContentFrom !== 'primary',
);
setSecondary(true);
} else {
closeEditorAnimation(
secondWebViewHeight,
primary && keepContentFrom !== 'primary'
? firstWebViewHeight
: null,
);
setSecondary(false);
}
}}
style={{
flexDirection: 'row',
alignItems: 'center',
justifyContent: 'space-between',
}}>
<Text
style={{
color: colors.icon,
fontSize: SIZE.xxs,
}}>
Saved on 10/10/20 {'\n'}
12:30pm on Tablet
</Text>
<Icon
size={SIZE.lg}
name={secondary ? 'chevron-up' : 'chevron-down'}
/>
</TouchableOpacity>
</View>
<View
style={{
flexDirection: 'row',
alignItems: 'center',
justifyContent: 'space-between',
}}>
{keepContentFrom === 'primary' ? (
<TouchableOpacity
onPress={onPressSaveCopyFromSecondaryWebView}
style={{
...getElevation(5),
height: 35,
backgroundColor: colors.accent,
borderRadius: 5,
paddingHorizontal: 12,
alignItems: 'center',
justifyContent: 'center',
marginRight: 10,
}}>
<Text
style={{
color: 'white',
textAlign: 'center',
fontFamily: WEIGHT.regular,
fontSize: SIZE.sm - 2,
}}>
Save as a copy
</Text>
</TouchableOpacity>
) : null}
{keepContentFrom === 'primary' ? (
<TouchableOpacity
onPress={onPressDiscardFromSecondaryWebView}
style={{
...getElevation(5),
height: 35,
backgroundColor: colors.errorText,
borderRadius: 5,
paddingHorizontal: 12,
alignItems: 'center',
justifyContent: 'center',
}}>
<Text
style={{
color: 'white',
textAlign: 'center',
fontFamily: WEIGHT.regular,
fontSize: SIZE.sm - 2,
}}>
Discard
</Text>
</TouchableOpacity>
) : null}
{keepContentFrom === 'primary' ? null : (
<TouchableOpacity
onPress={onPressKeepFromSecondaryWebView}
style={{
...getElevation(5),
height: 35,
backgroundColor:
keepContentFrom === 'secondary'
? colors.errorText
: colors.accent,
borderRadius: 5,
paddingHorizontal: 12,
alignItems: 'center',
justifyContent: 'center',
}}>
<Text
style={{
color: 'white',
textAlign: 'center',
fontFamily: WEIGHT.regular,
fontSize: SIZE.sm - 2,
}}>
{keepContentFrom === 'secondary' ? 'Undo' : 'Keep'}
</Text>
</TouchableOpacity>
)}
</View>
</View>
<Animated.View
style={{
height: secondWebViewHeight,
}}>
<WebView
onLoad={onSecondaryWebViewLoad}
ref={secondaryWebView}
style={{
width: '100%',
height: '100%',
}}
injectedJavaScript={Platform.OS === 'ios' ? injectedJS : null}
onShouldStartLoadWithRequest={_onShouldStartLoadWithRequest}
cacheMode="LOAD_DEFAULT"
domStorageEnabled={true}
scrollEnabled={false}
bounces={false}
allowFileAccess={true}
scalesPageToFit={true}
allowingReadAccessToURL={Platform.OS === 'android' ? true : null}
allowFileAccessFromFileURLs={true}
allowUniversalAccessFromFileURLs={true}
originWhitelist={['*']}
javaScriptEnabled={true}
cacheEnabled={true}
onMessage={onMessageFromSecondaryWebView}
source={
Platform.OS === 'ios'
2020-04-06 13:14:27 +05:00
? { uri: sourceUri }
2020-03-26 13:39:04 +05:00
: {
2020-04-06 13:14:27 +05:00
uri: 'file:///android_asset/plaineditor.html',
baseUrl: 'file:///android_asset/',
}
2020-03-26 13:39:04 +05:00
}
/>
</Animated.View>
</View>
</Modal>
);
};
export default MergeEditor;