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

273 lines
7.6 KiB
JavaScript
Raw Normal View History

import React, {Fragment, useEffect, useState} from 'react';
import {Platform, StyleSheet, 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 {notesnook} from '../../../e2e/test.ids';
import {useTracked} from '../../provider';
import {DDS} from '../../services/DeviceDetection';
import {ToastEvent} from '../../services/EventManager';
2020-11-14 14:04:16 +05:00
import Exporter from '../../services/Exporter';
import {getElevation} from '../../utils';
import {ph, pv, SIZE} from '../../utils/SizeUtils';
import {sleep} from '../../utils/TimeUtils';
import {GetPremium} from '../ActionSheetComponent/GetPremium';
2020-09-27 13:05:26 +05:00
import BaseDialog from '../Dialog/base-dialog';
2020-11-25 12:54:20 +05:00
import DialogButtons from '../Dialog/dialog-buttons';
2020-09-27 13:05:26 +05:00
import DialogHeader from '../Dialog/dialog-header';
2020-09-24 09:51:15 +05:00
import Seperator from '../Seperator';
2020-11-14 10:10:09 +05:00
import Heading from '../Typography/Heading';
import Paragraph from '../Typography/Paragraph';
2020-09-20 14:37:46 +05:00
2020-09-07 12:33:35 +05:00
const {
eSubscribeEvent,
eUnSubscribeEvent,
2020-10-13 17:02:14 +05:00
} = require('../../services/EventManager');
2020-11-14 14:04:16 +05:00
const {
eOpenExportDialog,
eCloseExportDialog,
eShowGetPremium,
} = require('../../utils/Events');
2020-09-07 12:33:35 +05:00
const ExportDialog = () => {
2020-11-14 10:10:09 +05:00
const [state] = useTracked();
2021-06-05 21:10:20 +05:00
const {colors} = 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-11-25 12:54:20 +05:00
if (exporting) return;
2020-09-20 15:05:16 +05:00
setExporting(true);
setComplete(false);
2020-09-20 15:05:16 +05:00
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);
setExporting(false);
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 () => {
await save(Exporter.saveToPDF, 'PDF');
2020-09-20 14:37:46 +05:00
},
2020-09-07 12:33:35 +05:00
icon: 'file-pdf-box',
desc:
'Can be opened in any pdf reader like Adobe Acrobat or Foxit Reader',
id: notesnook.ids.dialogs.export.pdf,
2020-09-07 12:33:35 +05:00
},
{
title: 'Markdown',
2020-09-20 15:05:16 +05:00
func: async () => {
await save(Exporter.saveToMarkdown, 'Markdown');
2020-09-20 15:05:16 +05:00
},
2020-09-07 12:33:35 +05:00
icon: 'language-markdown',
desc: 'Can be opened in any plain-text or markdown editor',
id: notesnook.ids.dialogs.export.md,
2020-09-07 12:33:35 +05:00
},
{
title: 'Plain Text',
2020-09-20 15:05:16 +05:00
func: async () => {
2020-11-14 14:04:16 +05:00
await save(Exporter.saveToText, 'Text');
2020-09-20 15:05:16 +05:00
},
2020-09-07 12:33:35 +05:00
icon: 'card-text',
desc: 'Can be opened in any plain text editor',
id: notesnook.ids.dialogs.export.text,
2020-09-07 12:33:35 +05:00
},
{
title: 'HTML',
2020-09-20 15:05:16 +05:00
func: async () => {
await save(Exporter.saveToHTML, 'Html');
2020-09-20 15:05:16 +05:00
},
2020-09-07 12:33:35 +05:00
icon: 'language-html5',
desc: 'Can be opened in any web browser like Google Chrome.',
id: notesnook.ids.dialogs.export.html,
2020-09-07 12:33:35 +05:00
},
];
2020-11-23 12:32:33 +05:00
return !visible ? null : (
2020-11-15 12:17:46 +05:00
<BaseDialog
premium={<GetPremium context="export" offset={50} close={close} />}
onRequestClose={close}
2020-11-23 12:32:33 +05:00
visible={true}>
2020-09-27 13:05:26 +05:00
<View
style={[
{
2020-10-04 09:46:50 +05:00
width: DDS.isTab ? 350 : '80%',
2020-09-27 13:05:26 +05:00
backgroundColor: colors.bg,
},
styles.container,
]}>
2020-09-27 13:14:24 +05:00
<DialogHeader
icon="export"
title="Export Note"
paragraph={
exporting
? null
: 'All exports are saved in Notesnook/exported folder in phone storage'
2020-09-27 13:14:24 +05:00
}
/>
2020-09-20 14:37:46 +05:00
2020-09-27 13:05:26 +05:00
<Seperator half />
2020-11-25 12:54:20 +05:00
<View style={styles.buttonContainer}>
{actions.map((item) => (
<Fragment key={item.title}>
<Seperator half />
<TouchableOpacity
2020-12-01 22:52:01 +05:00
testID={item.id}
2020-11-25 12:54:20 +05:00
onPress={item.func}
activeOpacity={1}
2020-11-14 10:10:09 +05:00
style={{
width: '100%',
2020-11-25 12:54:20 +05:00
alignItems: 'center',
2020-11-14 10:10:09 +05:00
flexDirection: 'row',
2020-11-25 12:54:20 +05:00
paddingRight: 12,
paddingVertical: 10,
2020-11-14 10:10:09 +05:00
}}>
2020-11-25 12:54:20 +05:00
<View
style={{
backgroundColor: colors.shade,
borderRadius: 5,
height: 40,
width: 40,
justifyContent: 'center',
alignItems: 'center',
}}>
<Icon
name={item.icon}
color={colors.accent}
size={SIZE.xxxl}
/>
</View>
<View>
<Heading
style={{marginLeft: 5, maxWidth: '90%'}}
size={SIZE.md}>
{item.title}
</Heading>
<Paragraph
style={{marginLeft: 5, maxWidth: '90%'}}
size={SIZE.sm}
color={colors.icon}>
2020-11-25 12:54:20 +05:00
{item.desc}
</Paragraph>
</View>
2020-11-25 12:54:20 +05:00
</TouchableOpacity>
</Fragment>
))}
<View
style={{
width: '100%',
}}>
<DialogButtons
onPressNegative={close}
negativeTitle="Cancel"
2021-02-16 16:11:10 +05:00
positiveTitle={complete && 'Open file'}
2020-11-25 12:54:20 +05:00
onPressPositive={
complete
? async () => {
close();
await sleep(500);
FileViewer.open(result.filePath, {
showOpenWithDialog: true,
showAppsSuggestions: true,
}).catch((e) => {
ToastEvent.show({
heading: 'Cannot open',
message: `No application found to open ${result.name} file.`,
type: 'success',
context: 'local',
});
2020-11-25 12:54:20 +05:00
});
}
: null
}
loading={exporting && !complete}
doneText={complete && 'Exported as ' + result.name}
2020-11-25 12:54:20 +05:00
/>
</View>
</View>
2020-09-07 12:33:35 +05:00
</View>
2020-09-27 13:05:26 +05:00
</BaseDialog>
2020-09-07 12:33:35 +05:00
);
};
const styles = StyleSheet.create({
container: {
...getElevation(5),
borderRadius: 5,
paddingHorizontal: ph,
paddingVertical: pv,
},
buttonContainer: {
justifyContent: 'space-between',
alignItems: 'center',
},
button: {
paddingVertical: pv,
paddingHorizontal: ph,
marginTop: 10,
borderRadius: 5,
width: '100%',
justifyContent: 'center',
alignItems: 'center',
borderWidth: 1,
flexDirection: 'row',
},
buttonText: {
2021-01-03 10:38:07 +05:00
//fontFamily: "sans-serif",
2020-09-07 12:33:35 +05:00
color: 'white',
fontSize: SIZE.sm,
marginLeft: 5,
},
overlay: {
width: '100%',
height: '100%',
position: 'absolute',
},
});
export default ExportDialog;