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

249 lines
6.1 KiB
JavaScript
Raw Normal View History

2020-09-24 09:51:15 +05:00
import React, {Fragment, useEffect, useState} from 'react';
2020-09-07 12:33:35 +05:00
import {
Modal,
2020-09-24 09:51:15 +05:00
Platform,
2020-09-07 12:33:35 +05:00
StyleSheet,
Text,
TouchableOpacity,
View,
} from 'react-native';
2020-09-24 09:51:15 +05:00
import FileViewer from 'react-native-file-viewer';
2020-09-07 12:33:35 +05:00
import Icon from 'react-native-vector-icons/MaterialCommunityIcons';
import {opacity, ph, pv, SIZE, WEIGHT} from '../../common/common';
import {useTracked} from '../../provider';
2020-09-24 09:51:15 +05:00
import storage from '../../utils/storage';
import {DDS, getElevation, ToastEvent} from '../../utils/utils';
import {Button} from '../Button/index';
2020-09-07 12:33:35 +05:00
import {Loading} from '../Loading';
2020-09-24 09:51:15 +05:00
import Seperator from '../Seperator';
2020-09-20 14:37:46 +05:00
2020-09-07 12:33:35 +05:00
const {
eSubscribeEvent,
eUnSubscribeEvent,
} = require('../../services/eventManager');
const {
eOpenExportDialog,
eCloseExportDialog,
} = require('../../services/events');
const ExportDialog = () => {
const [state, dispatch] = useTracked();
const {colors, tags, premiumUser} = state;
2020-09-10 10:19:36 +05:00
const [visible, setVisible] = useState(false);
2020-09-07 12:33:35 +05:00
const [notes, setNotes] = useState([]);
const [exporting, setExporting] = useState(false);
const [complete, setComplete] = useState(false);
2020-09-20 14:37:46 +05:00
const [doneText, setDoneText] = useState(null);
2020-09-20 15:05:16 +05:00
const [result, setResult] = useState({});
2020-09-07 12:33:35 +05:00
useEffect(() => {
eSubscribeEvent(eOpenExportDialog, open);
eSubscribeEvent(eCloseExportDialog, close);
return () => {
eUnSubscribeEvent(eOpenExportDialog, open);
eUnSubscribeEvent(eCloseExportDialog, close);
};
}, []);
const open = (data) => {
setVisible(true);
setNotes(data);
};
const close = (data) => {
setComplete(false);
setExporting(false);
setVisible(false);
2020-09-20 14:37:46 +05:00
setNotes([]);
};
2020-09-20 15:40:19 +05:00
const save = async (func, name) => {
2020-09-20 15:05:16 +05:00
setExporting(true);
let res;
for (var i = 0; i < notes.length; i++) {
let note = notes[i];
res = await func(note);
if (!res) {
setExporting(false);
return;
}
}
setDoneText(
`Note exported successfully! You can find the exported note in ${
Platform.OS === 'ios'
? 'Files Manager/Notesnook'
: `Storage/Notesnook/exported/${name}`
}.`,
);
2020-09-20 15:40:19 +05:00
setResult(res);
2020-09-20 15:05:16 +05:00
setComplete(true);
2020-09-07 12:33:35 +05:00
};
const actions = [
{
title: 'PDF',
2020-09-20 14:37:46 +05:00
func: async () => {
2020-09-20 15:40:19 +05:00
await save(storage.saveToPDF, 'PDF');
2020-09-20 14:37:46 +05:00
},
2020-09-07 12:33:35 +05:00
icon: 'file-pdf-box',
},
{
title: 'Markdown',
2020-09-20 15:05:16 +05:00
func: async () => {
2020-09-20 15:40:19 +05:00
await save(storage.saveToMarkdown, 'Markdown');
2020-09-20 15:05:16 +05:00
},
2020-09-07 12:33:35 +05:00
icon: 'language-markdown',
},
{
title: 'Plain Text',
2020-09-20 15:05:16 +05:00
func: async () => {
2020-09-20 15:40:19 +05:00
await save(storage.saveToText, 'Text');
2020-09-20 15:05:16 +05:00
},
2020-09-07 12:33:35 +05:00
icon: 'card-text',
},
{
title: 'HTML',
2020-09-20 15:05:16 +05:00
func: async () => {
2020-09-20 15:40:19 +05:00
await save(storage.saveToHTML, 'Html');
2020-09-20 15:05:16 +05:00
},
2020-09-07 12:33:35 +05:00
icon: 'language-html5',
},
];
return (
<Modal
visible={visible}
transparent={true}
animated
animationType="fade"
onRequestClose={close}>
<View style={styles.wrapper}>
<TouchableOpacity onPress={close} style={styles.overlay} />
<View
style={[
{
width: DDS.isTab ? '40%' : '80%',
backgroundColor: colors.bg,
},
styles.container,
]}>
<View style={styles.headingContainer}>
<Icon name="export" color={colors.accent} size={SIZE.lg} />
<Text style={[{color: colors.accent}, styles.heading]}>
Export
{notes.length === 0 || notes.length === 1
? ''
: ' ' + notes.length}{' '}
Note
</Text>
</View>
2020-09-20 14:37:46 +05:00
<Seperator half />
2020-09-07 12:33:35 +05:00
{exporting ? (
<Loading
done={complete}
2020-09-20 14:37:46 +05:00
doneText={doneText}
onDone={() => {
2020-09-20 15:40:19 +05:00
FileViewer.open(result.filePath, {
showOpenWithDialog: true,
showAppsSuggestions: true,
}).catch((e) => {
console.log(e);
ToastEvent.show(
`No application found to open ${result.name} file`,
);
});
2020-09-20 14:37:46 +05:00
close();
}}
2020-09-20 15:40:19 +05:00
tagline="Exporting notes"
2020-09-07 12:33:35 +05:00
/>
) : (
2020-09-20 14:37:46 +05:00
<>
<Text
style={[
{
color: colors.pri,
fontSize: SIZE.xs + 1,
alignSelf: 'center',
},
]}>
Export your note in any of the following formats.
</Text>
<View style={styles.buttonContainer}>
{actions.map((item) => (
2020-09-24 09:51:15 +05:00
<Fragment key={item.title}>
2020-09-20 14:37:46 +05:00
<Seperator half />
<Button
width="100%"
title={item.title}
icon={item.icon}
activeOpacity={opacity}
onPress={item.func}
/>
2020-09-24 09:51:15 +05:00
</Fragment>
2020-09-20 14:37:46 +05:00
))}
</View>
</>
2020-09-07 12:33:35 +05:00
)}
</View>
</View>
</Modal>
);
};
const styles = StyleSheet.create({
wrapper: {
width: '100%',
height: '100%',
backgroundColor: 'rgba(0,0,0,0.3)',
justifyContent: 'center',
alignItems: 'center',
},
container: {
...getElevation(5),
maxHeight: 350,
borderRadius: 5,
paddingHorizontal: ph,
paddingVertical: pv,
},
headingContainer: {
flexDirection: 'row',
justifyContent: 'center',
alignItems: 'center',
},
buttonContainer: {
justifyContent: 'space-between',
alignItems: 'center',
},
heading: {
fontFamily: WEIGHT.bold,
marginLeft: 5,
fontSize: SIZE.md,
},
button: {
paddingVertical: pv,
paddingHorizontal: ph,
marginTop: 10,
borderRadius: 5,
width: '100%',
justifyContent: 'center',
alignItems: 'center',
borderWidth: 1,
flexDirection: 'row',
},
buttonText: {
fontFamily: WEIGHT.medium,
color: 'white',
fontSize: SIZE.sm,
marginLeft: 5,
},
overlay: {
width: '100%',
height: '100%',
position: 'absolute',
},
});
export default ExportDialog;