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

374 lines
11 KiB
JavaScript
Raw Normal View History

/*
This file is part of the Notesnook project (https://notesnook.com/)
2023-01-16 13:44:52 +05:00
Copyright (C) 2023 Streetwriters (Private) Limited
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
2022-08-30 16:13:11 +05:00
2022-09-16 16:02:40 +05:00
import React, { Fragment, useState } from "react";
2022-10-14 17:31:36 +05:00
import { Platform, StyleSheet, View, ActivityIndicator } from "react-native";
import FileViewer from "react-native-file-viewer";
import Share from "react-native-share";
import Icon from "react-native-vector-icons/MaterialCommunityIcons";
import { notesnook } from "../../../../e2e/test.ids";
2022-10-14 17:31:36 +05:00
import { db } from "../../../common/database";
2022-09-16 16:02:40 +05:00
import { presentSheet, ToastEvent } from "../../../services/event-manager";
import Exporter from "../../../services/exporter";
2022-10-14 17:31:36 +05:00
import PremiumService from "../../../services/premium";
2022-08-29 16:19:17 +05:00
import { useThemeStore } from "../../../stores/use-theme-store";
2022-10-18 16:45:03 +05:00
import { useUserStore } from "../../../stores/use-user-store";
import { getElevation } from "../../../utils";
import { ph, pv, SIZE } from "../../../utils/size";
import { sleep } from "../../../utils/time";
import DialogHeader from "../../dialog/dialog-header";
2022-10-18 16:45:03 +05:00
import { ProTag } from "../../premium/pro-tag";
2022-08-29 16:19:17 +05:00
import { Button } from "../../ui/button";
2022-10-14 17:31:36 +05:00
import { IconButton } from "../../ui/icon-button";
import { PressableButton } from "../../ui/pressable";
import Seperator from "../../ui/seperator";
import Heading from "../../ui/typography/heading";
import Paragraph from "../../ui/typography/paragraph";
2022-11-23 15:37:56 +05:00
import { eSendEvent } from "../../../services/event-manager";
2023-01-03 10:23:48 +05:00
import { eCloseSheet } from "../../../utils/events";
2021-09-29 13:22:32 +05:00
const ExportNotesSheet = ({ notes, update }) => {
2022-09-16 16:02:40 +05:00
const colors = useThemeStore((state) => state.colors);
2020-09-07 12:33:35 +05:00
const [exporting, setExporting] = useState(false);
const [complete, setComplete] = useState(false);
2020-09-20 15:05:16 +05:00
const [result, setResult] = useState({});
2022-10-14 17:31:36 +05:00
const [status, setStatus] = useState(null);
2022-10-18 16:45:03 +05:00
const premium = useUserStore((state) => state.premium);
2020-09-20 14:37:46 +05:00
2022-10-14 17:31:36 +05:00
const save = async (type) => {
2020-11-25 12:54:20 +05:00
if (exporting) return;
2022-10-14 17:31:36 +05:00
if (!PremiumService.get() && type !== "txt") return;
2020-09-20 15:05:16 +05:00
setExporting(true);
update({ disableClosing: true });
setComplete(false);
2022-10-14 17:31:36 +05:00
let result;
if (notes.length > 1) {
result = await Exporter.bulkExport(notes, type, setStatus);
} else {
result = await Exporter.exportNote(notes[0], type);
await sleep(1000);
2020-09-20 15:05:16 +05:00
}
2022-10-18 16:45:03 +05:00
if (!result) {
update({ disableClosing: false });
2022-10-18 16:45:03 +05:00
return setExporting(false);
}
2022-10-14 17:31:36 +05:00
setResult(result);
update({ disableClosing: false });
2020-09-20 15:05:16 +05:00
setComplete(true);
2022-09-16 16:02:40 +05:00
setExporting(false);
2020-09-07 12:33:35 +05:00
};
const actions = [
{
title: "PDF",
2020-09-20 14:37:46 +05:00
func: async () => {
2022-10-14 17:31:36 +05:00
await save("pdf");
2020-09-20 14:37:46 +05:00
},
icon: "file-pdf-box",
2022-10-18 16:45:03 +05:00
desc: "View in any pdf reader app",
id: notesnook.ids.dialogs.export.pdf,
pro: premium
2020-09-07 12:33:35 +05:00
},
{
title: "Markdown",
2020-09-20 15:05:16 +05:00
func: async () => {
2022-10-14 17:31:36 +05:00
await save("md");
2020-09-20 15:05:16 +05:00
},
icon: "language-markdown",
2022-10-18 16:45:03 +05:00
desc: "View in any text or markdown editor",
id: notesnook.ids.dialogs.export.md,
pro: premium
2020-09-07 12:33:35 +05:00
},
{
title: "Plain Text",
2020-09-20 15:05:16 +05:00
func: async () => {
2022-10-14 17:31:36 +05:00
await save("txt");
2020-09-20 15:05:16 +05:00
},
icon: "card-text",
2022-10-18 16:45:03 +05:00
desc: "View in any text editor",
id: notesnook.ids.dialogs.export.text,
pro: true
2020-09-07 12:33:35 +05:00
},
{
title: "HTML",
2020-09-20 15:05:16 +05:00
func: async () => {
2022-10-14 17:31:36 +05:00
await save("html");
2020-09-20 15:05:16 +05:00
},
icon: "language-html5",
2022-10-18 16:45:03 +05:00
desc: "View in any web browser & html reader",
id: notesnook.ids.dialogs.export.html,
pro: premium
2021-09-29 13:22:32 +05:00
}
2020-09-07 12:33:35 +05:00
];
2022-09-16 16:02:40 +05:00
return (
<View>
2022-10-14 17:31:36 +05:00
{!complete && !exporting ? (
<>
<View
style={{
paddingHorizontal: 12
}}
>
<DialogHeader
icon="export"
2022-10-14 17:44:35 +05:00
title={
notes.length > 1
? `Export ${notes.length} Notes`
: "Export Note"
}
2022-10-14 17:31:36 +05:00
paragraph={`All exports are saved in ${
Platform.OS === "android"
? "the selected"
: "Notesnook/exported"
} folder in phone storage`}
/>
</View>
<Seperator half />
</>
) : null}
2020-09-20 14:37:46 +05:00
2022-09-16 16:02:40 +05:00
<View style={styles.buttonContainer}>
2022-10-14 17:31:36 +05:00
{!exporting && !complete ? (
actions.map((item) => (
<Fragment key={item.title}>
<Seperator half />
<PressableButton
onPress={item.func}
customStyle={{
width: "100%",
alignItems: "center",
flexDirection: "row",
paddingRight: 12,
paddingVertical: 10,
justifyContent: "flex-start",
borderRadius: 0,
2022-10-18 16:45:03 +05:00
paddingHorizontal: 12,
opacity: item.pro ? 1 : 0.5
2022-01-22 12:57:05 +05:00
}}
>
2022-10-14 17:31:36 +05:00
<View
style={{
backgroundColor: colors.shade,
borderRadius: 5,
height: 60,
width: 60,
justifyContent: "center",
alignItems: "center"
}}
>
<Icon
name={item.icon}
2022-10-18 16:45:03 +05:00
color={item.pro ? colors.accent : colors.icon}
2022-10-14 17:31:36 +05:00
size={SIZE.xxxl + 10}
/>
</View>
<View
style={{
flexShrink: 1
}}
>
2022-10-18 16:45:03 +05:00
{!item.pro ? <ProTag size={12} /> : null}
2022-10-14 17:31:36 +05:00
<Heading style={{ marginLeft: 10 }} size={SIZE.md}>
{item.title}
</Heading>
<Paragraph
style={{ marginLeft: 10 }}
size={SIZE.sm}
color={colors.icon}
>
{item.desc}
</Paragraph>
</View>
</PressableButton>
</Fragment>
))
) : (
<View
style={{
minHeight: 150,
justifyContent: "center",
alignItems: "center",
width: "100%",
paddingHorizontal: 12,
paddingVertical: 20
}}
>
{!complete ? (
<>
<ActivityIndicator />
<Paragraph>
{notes.length === 1
? "Exporting note... Please wait"
: `Exporting notes${
status ? ` (${status})` : ``
}... Please wait`}
</Paragraph>
</>
) : (
<>
<IconButton
name="export"
color={colors.icon}
size={50}
customStyle={{
width: 70,
height: 70
}}
2022-09-16 16:02:40 +05:00
/>
2022-10-14 17:31:36 +05:00
<Heading
style={{
textAlign: "center",
marginTop: 10
}}
color={colors.icon}
>
2022-10-14 17:44:35 +05:00
{notes.length > 1
? `${notes.length} Notes exported`
: "Note exported"}
2022-09-16 16:02:40 +05:00
</Heading>
<Paragraph
2022-10-14 17:31:36 +05:00
style={{
textAlign: "center"
}}
2022-01-22 12:57:05 +05:00
>
2022-10-14 17:31:36 +05:00
Your {notes.length > 1 ? "notes are" : "note is"} exported
successfully as {result.fileName}
2022-09-16 16:02:40 +05:00
</Paragraph>
2022-10-14 17:31:36 +05:00
<Button
title="Open"
type="accent"
width={250}
fontSize={SIZE.md}
style={{
marginTop: 10,
borderRadius: 100
}}
onPress={async () => {
2023-01-03 10:23:48 +05:00
eSendEvent(eCloseSheet);
2022-10-14 17:31:36 +05:00
await sleep(500);
2022-09-16 16:02:40 +05:00
FileViewer.open(result.filePath, {
showOpenWithDialog: true,
2022-10-14 17:31:36 +05:00
showAppsSuggestions: true
2022-11-23 15:37:56 +05:00
}).catch((e) => {
console.log(e);
2022-10-14 17:31:36 +05:00
ToastEvent.show({
heading: "Cannot open",
message: `No application found to open ${result.name} file.`,
type: "success",
context: "local"
});
});
}}
/>
<Button
title="Share"
type="grayAccent"
width={250}
fontSize={SIZE.md}
style={{
marginTop: 10,
borderRadius: 100
}}
onPress={async () => {
if (Platform.OS === "ios") {
Share.open({
url: result.filePath
}).catch(console.log);
} else {
FileViewer.open(result.filePath, {
showOpenWithDialog: true,
showAppsSuggestions: true,
shareFile: true
}).catch(console.log);
}
}}
/>
<Button
title="Export in another format"
type="grayAccent"
width={250}
fontSize={SIZE.md}
style={{
marginTop: 10,
borderRadius: 100
}}
onPress={async () => {
setComplete(false);
setResult(null);
setExporting(false);
}}
/>
</>
)}
</View>
)}
2020-09-07 12:33:35 +05:00
</View>
2022-09-16 16:02:40 +05:00
</View>
2020-09-07 12:33:35 +05:00
);
};
2022-10-14 17:44:35 +05:00
ExportNotesSheet.present = (notes, allNotes) => {
2022-09-16 16:02:40 +05:00
presentSheet({
component: (ref, close, update) => (
<ExportNotesSheet
notes={allNotes ? db.notes.all : notes}
update={update}
/>
)
2022-09-16 16:02:40 +05:00
});
};
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",
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",
2020-09-07 12:33:35 +05:00
borderWidth: 1,
flexDirection: "row"
2020-09-07 12:33:35 +05:00
},
buttonText: {
2021-01-03 10:38:07 +05:00
//fontFamily: "sans-serif",
color: "white",
2020-09-07 12:33:35 +05:00
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%",
position: "absolute"
2021-09-29 13:22:32 +05:00
}
2020-09-07 12:33:35 +05:00
});
2022-02-28 15:32:55 +05:00
export default ExportNotesSheet;