Files
notesnook/apps/mobile/app/components/sheets/export-notes/index.js

296 lines
8.6 KiB
JavaScript
Raw Normal View History

2021-12-25 10:59:02 +05:00
import React, { Fragment, useEffect, useRef, useState } from 'react';
import { Platform, StyleSheet, View } from 'react-native';
2020-09-24 09:51:15 +05:00
import FileViewer from 'react-native-file-viewer';
2021-09-29 13:22:32 +05:00
import Share from 'react-native-share';
2020-09-07 12:33:35 +05:00
import Icon from 'react-native-vector-icons/MaterialCommunityIcons';
2022-02-28 15:32:55 +05:00
import { notesnook } from '../../../../e2e/test.ids';
2022-04-24 05:59:14 +05:00
import { useThemeStore } from '../../../stores/use-theme-store';
2022-02-28 23:25:18 +05:00
import { ToastEvent } from '../../../services/event-manager';
import Exporter from '../../../services/exporter';
2022-02-28 15:32:55 +05:00
import { getElevation } from '../../../utils';
import { ph, pv, SIZE } from '../../../utils/size';
import { sleep } from '../../../utils/time';
import SheetWrapper from '../../ui/sheet';
import { Button } from '../../ui/button';
import DialogHeader from '../../dialog/dialog-header';
import { PressableButton } from '../../ui/pressable';
import Seperator from '../../ui/seperator';
import Heading from '../../ui/typography/heading';
import Paragraph from '../../ui/typography/paragraph';
2022-03-09 13:50:50 +05:00
import { eSubscribeEvent, eUnSubscribeEvent } from '../../../services/event-manager';
import { eOpenExportDialog, eCloseExportDialog } from '../../../utils/events';
2020-09-07 12:33:35 +05:00
2022-02-28 15:32:55 +05:00
const ExportNotesSheet = () => {
2022-02-28 23:25:18 +05:00
const colors = useThemeStore(state => state.colors);
2021-09-29 13:22:32 +05:00
2020-09-10 10:19:36 +05:00
const [visible, setVisible] = useState(false);
2021-09-29 13:22:32 +05:00
const actionSheetRef = useRef();
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);
};
}, []);
2021-09-29 13:22:32 +05:00
const open = data => {
2020-09-07 12:33:35 +05:00
setVisible(true);
setNotes(data);
};
2021-09-29 13:22:32 +05:00
const close = data => {
2020-09-07 12:33:35 +05:00
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 ${
2022-01-22 12:57:05 +05:00
Platform.OS === 'ios' ? 'Files Manager/Notesnook' : `Storage/Notesnook/exported/${name}`
2021-09-29 13:22:32 +05:00
}.`
2020-09-20 15:05:16 +05:00
);
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
};
2021-09-29 13:22:32 +05:00
useEffect(() => {
if (visible) {
actionSheetRef.current.show();
}
}, [visible]);
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',
2021-09-29 13:22:32 +05:00
desc: 'Can be opened in a pdf reader like Adobe 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',
2021-09-29 13:22:32 +05:00
desc: 'Can be opened in any 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',
2021-09-29 13:22:32 +05:00
desc: 'Can be opened in any 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',
2021-09-29 13:22:32 +05:00
desc: 'Can be opened in any web browser',
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 : (
2021-12-25 11:16:33 +05:00
<SheetWrapper fwdRef={actionSheetRef} onClose={close} visible={true}>
2021-09-29 13:22:32 +05:00
<View>
<View
style={{
paddingHorizontal: 12
2022-01-22 12:57:05 +05:00
}}
>
2021-09-29 13:22:32 +05:00
<DialogHeader
icon="export"
title="Export Note"
2022-01-22 12:57:05 +05:00
paragraph={'All exports are saved in Notesnook/exported folder in phone storage'}
2021-09-29 13:22:32 +05:00
/>
</View>
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}>
2021-09-29 13:22:32 +05:00
{actions.map(item => (
2020-11-25 12:54:20 +05:00
<Fragment key={item.title}>
<Seperator half />
2021-09-29 13:22:32 +05:00
<PressableButton
2020-11-25 12:54:20 +05:00
onPress={item.func}
2021-09-29 13:22:32 +05:00
customStyle={{
2020-11-14 10:10:09 +05:00
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,
2021-09-29 13:22:32 +05:00
justifyContent: 'flex-start',
borderRadius: 0,
paddingHorizontal: 12
2022-01-22 12:57:05 +05:00
}}
>
2020-11-25 12:54:20 +05:00
<View
style={{
backgroundColor: colors.shade,
borderRadius: 5,
2021-09-29 13:22:32 +05:00
height: 60,
width: 60,
2020-11-25 12:54:20 +05:00
justifyContent: 'center',
2021-09-29 13:22:32 +05:00
alignItems: 'center'
2022-01-22 12:57:05 +05:00
}}
>
<Icon name={item.icon} color={colors.accent} size={SIZE.xxxl + 10} />
2020-11-25 12:54:20 +05:00
</View>
2021-09-29 13:22:32 +05:00
<View
style={{
flexShrink: 1
2022-01-22 12:57:05 +05:00
}}
>
<Heading style={{ marginLeft: 10 }} size={SIZE.md}>
{item.title}
</Heading>
2022-01-22 12:57:05 +05:00
<Paragraph style={{ marginLeft: 10 }} size={SIZE.sm} color={colors.icon}>
2020-11-25 12:54:20 +05:00
{item.desc}
</Paragraph>
</View>
2021-09-29 13:22:32 +05:00
</PressableButton>
2020-11-25 12:54:20 +05:00
</Fragment>
))}
<View
style={{
width: '100%',
2021-09-29 13:22:32 +05:00
paddingHorizontal: 12,
marginTop: 10
2022-01-22 12:57:05 +05:00
}}
>
2021-09-29 13:22:32 +05:00
{complete && (
<>
<Button
title="Open"
type="accent"
width="100%"
fontSize={SIZE.md}
onPress={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
});
2021-09-29 13:22:32 +05:00
});
}}
height={50}
/>
<Button
title="Share"
type="shade"
width="100%"
fontSize={SIZE.md}
style={{
marginTop: 10
}}
onPress={async () => {
2021-10-28 11:32:05 +05:00
if (Platform.OS === 'ios') {
Share.open({
url: 'file:/' + result.filePath
}).catch(console.log);
} else {
FileViewer.open(result.filePath, {
showOpenWithDialog: true,
showAppsSuggestions: true,
shareFile: true
}).catch(console.log);
}
2021-09-29 13:22:32 +05:00
}}
height={50}
/>
</>
)}
{complete && (
<Paragraph
style={{
textAlign: 'center',
marginTop: 5
}}
color={colors.icon}
2022-01-22 12:57:05 +05:00
size={SIZE.xs}
>
2021-10-20 14:50:09 +05:00
{'Note exported as ' + result.fileName}
2021-09-29 13:22:32 +05:00
</Paragraph>
)}
2022-01-22 12:57:05 +05:00
{exporting && !complete && <Button loading={true} height={50} width="100%" />}
2020-11-25 12:54:20 +05:00
</View>
</View>
2020-09-07 12:33:35 +05:00
</View>
2021-12-25 11:16:33 +05:00
</SheetWrapper>
2020-09-07 12:33:35 +05:00
);
};
const styles = StyleSheet.create({
container: {
...getElevation(5),
borderRadius: 5,
2021-09-29 13:22:32 +05:00
paddingVertical: pv
2020-09-07 12:33:35 +05:00
},
buttonContainer: {
justifyContent: 'space-between',
2021-09-29 13:22:32 +05:00
alignItems: 'center'
2020-09-07 12:33:35 +05:00
},
button: {
paddingVertical: pv,
paddingHorizontal: ph,
marginTop: 10,
borderRadius: 5,
width: '100%',
justifyContent: 'center',
alignItems: 'center',
borderWidth: 1,
2021-09-29 13:22:32 +05:00
flexDirection: 'row'
2020-09-07 12:33:35 +05:00
},
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,
2021-09-29 13:22:32 +05:00
marginLeft: 5
2020-09-07 12:33:35 +05:00
},
overlay: {
width: '100%',
height: '100%',
2021-09-29 13:22:32 +05:00
position: 'absolute'
}
2020-09-07 12:33:35 +05:00
});
2022-02-28 15:32:55 +05:00
export default ExportNotesSheet;