Skip to content

Commit b429435

Browse files
author
Kévin Commaille
committed
Add feature to export a note
1 parent f3fbe13 commit b429435

File tree

9 files changed

+70
-22
lines changed

9 files changed

+70
-22
lines changed

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
"expo-updates": "~0.3.2",
3030
"file-saver": "^2.0.5",
3131
"immutable": "^4.0.0-rc.12",
32+
"js-yaml": "^4.0.0",
3233
"localforage": "^1.9.0",
3334
"moment": "^2.29.0",
3435
"react": "16.13.1",
@@ -62,6 +63,7 @@
6263
"@babel/core": "~7.9.0",
6364
"@expo/webpack-config": "^0.12.53",
6465
"@types/file-saver": "^2.0.1",
66+
"@types/js-yaml": "^4.0.0",
6567
"@types/markdown-it": "^12.0.0",
6668
"@types/react": "~16.9.35",
6769
"@types/react-dom": "~16.9.8",

src/import-export/export.android.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,13 @@ export function canExport() {
88
}
99

1010
export async function exportItem(item: Etebase.Item) {
11-
const { name, content } = await getItemData(item);
11+
const { name, content } = await getItemData(item, "export");
1212
const result = await IntentLauncher.startActivityAsync("android.intent.action.CREATE_DOCUMENT", {
1313
type: "text/markdown",
1414
category: "android.intent.category.OPENABLE",
1515
extra: { "android.intent.extra.TITLE": `${name}.md` },
1616
});
17-
console.log("Export Item Result", result);
18-
if (result.resultCode === -1 && result?.data) {
19-
console.log("Content to write", content);
17+
if (result.resultCode === IntentLauncher.ResultCode.Success && result?.data) {
2018
await RNFS.writeFile(result.data, content);
2119
}
2220
}

src/import-export/export.ios.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
11
import * as Etebase from "etebase";
2+
import { Share } from "react-native";
3+
import { getItemData } from "./utils";
24

35
export function canExport() {
4-
return false;
6+
return true;
57
}
68

79
export async function exportItem(item: Etebase.Item) {
8-
const { name } = item.getMeta();
9-
throw Error(`Cannot export item "${name}". Exporting is not implemented on this Platform`);
10+
const { name, content } = await getItemData(item, "export");
11+
12+
await Share.share({
13+
message: content,
14+
title: name,
15+
});
1016
}

src/import-export/export.web.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ export function canExport() {
1212
}
1313

1414
export async function exportItem(item: Etebase.Item) {
15-
const { name, content } = await getItemData(item);
15+
const { name, content } = await getItemData(item, "export");
1616
const blob = new Blob([content], { type: "text/markdown;charset=utf-8" });
1717
FileSaver.saveAs(blob, `${name}.md`);
1818
}

src/import-export/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1+
export * from "./export";
12
export * from "./share";

src/import-export/share.native.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,9 @@ export function canShare() {
99

1010
export async function shareItem(item: Etebase.Item) {
1111
const { name, content } = await getItemData(item);
12-
13-
// Adding the name as a title at the beginning of the content because it is not shared otherwise
14-
const message = `# ${name}\n\n${content}`;
12+
1513
await Share.share({
16-
message,
14+
message: content,
1715
title: name,
1816
});
1917
}

src/import-export/utils.ts

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,22 @@
11
import * as Etebase from "etebase";
2+
import YAML from "js-yaml";
23

3-
export async function getItemData(item: Etebase.Item) {
4-
const { name } = item.getMeta();
5-
const content = await item.getContent(Etebase.OutputFormat.String);
6-
return {
7-
name,
8-
content,
4+
export async function getItemData(item: Etebase.Item, format = "") {
5+
const data = {
6+
name: item.getMeta().name,
7+
content: "",
98
};
9+
const content = await item.getContent(Etebase.OutputFormat.String);
10+
11+
switch (format) {
12+
case "export": {
13+
const frontmatter = YAML.dump({ title: data.name });
14+
data.content = `---\n${frontmatter}---\n\n${content}`;
15+
break;
16+
}
17+
default: {
18+
data.content = `# ${data.name}\n\n${content}`;
19+
}
20+
}
21+
return data;
1022
}

src/screens/NoteEditScreen.tsx

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import ConfirmationDialog from "../widgets/ConfirmationDialog";
2424
import NotFound from "../widgets/NotFound";
2525
import { fontFamilies } from "../helpers";
2626
import { RootStackParamList } from "../RootStackParamList";
27-
import { canShare, shareItem } from "../import-export";
27+
import { canExport, exportItem, canShare, shareItem } from "../import-export";
2828

2929
type NavigationProp = StackNavigationProp<RootStackParamList, "NoteEdit">;
3030

@@ -88,6 +88,7 @@ export default function NoteEditScreen(props: PropsType) {
8888
onEdit={() => navigation.navigate("NoteProps", { colUid, itemUid })}
8989
onDelete={() => setNoteDeleteDialogShow(true)}
9090
onShare={onShare}
91+
onExport={() => onShare(false)}
9192
changed={changed}
9293
/>
9394
),
@@ -171,13 +172,17 @@ export default function NoteEditScreen(props: PropsType) {
171172
}));
172173
}
173174

174-
function onShare() {
175+
function onShare(share = true) {
175176
(async () => {
176177
const colMgr = etebase.getCollectionManager();
177178
const col = colMgr.cacheLoad(cacheCollections.get(colUid)!.cache);
178179
const itemMgr = colMgr.getItemManager(col);
179180
const item = itemMgr.cacheLoad(cacheItem!.cache);
180-
await shareItem(item);
181+
if (share) {
182+
await shareItem(item);
183+
} else {
184+
await exportItem(item);
185+
}
181186
})();
182187
}
183188

@@ -246,9 +251,10 @@ interface RightActionViewProps {
246251
onSave: () => void;
247252
onDelete: () => void;
248253
onShare: () => void;
254+
onExport: () => void;
249255
}
250256

251-
function RightAction({ viewMode, setViewMode, onSave, onEdit, onDelete, onShare, changed }: RightActionViewProps) {
257+
function RightAction({ viewMode, setViewMode, onSave, onEdit, onDelete, onShare, onExport, changed }: RightActionViewProps) {
252258
const [showMenu, setShowMenu] = React.useState(false);
253259

254260
return (
@@ -290,6 +296,14 @@ function RightAction({ viewMode, setViewMode, onSave, onEdit, onDelete, onShare,
290296
}}
291297
/>
292298
) : null}
299+
{(canExport()) ? (
300+
<Menu.Item icon="export" title="Export"
301+
onPress={() => {
302+
setShowMenu(false);
303+
onExport();
304+
}}
305+
/>
306+
) : null}
293307
</Menu>
294308
</View>
295309
);

yarn.lock

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2892,6 +2892,11 @@
28922892
"@types/istanbul-lib-coverage" "*"
28932893
"@types/istanbul-lib-report" "*"
28942894

2895+
"@types/js-yaml@^4.0.0":
2896+
version "4.0.0"
2897+
resolved "https://registry.yarnpkg.com/@types/js-yaml/-/js-yaml-4.0.0.tgz#d1a11688112091f2c711674df3a65ea2f47b5dfb"
2898+
integrity sha512-4vlpCM5KPCL5CfGmTbpjwVKbISRYhduEJvvUWsH5EB7QInhEj94XPZ3ts/9FPiLZFqYO0xoW4ZL8z2AabTGgJA==
2899+
28952900
"@types/json-schema@^7.0.3", "@types/json-schema@^7.0.5", "@types/json-schema@^7.0.6":
28962901
version "7.0.6"
28972902
resolved "https://registry.yarnpkg.com/@types/json-schema/-/json-schema-7.0.6.tgz#f4c7ec43e81b319a9815115031709f26987891f0"
@@ -3573,6 +3578,11 @@ argparse@^1.0.7:
35733578
dependencies:
35743579
sprintf-js "~1.0.2"
35753580

3581+
argparse@^2.0.1:
3582+
version "2.0.1"
3583+
resolved "https://registry.yarnpkg.com/argparse/-/argparse-2.0.1.tgz#246f50f3ca78a3240f6c997e8a9bd1eac49e4b38"
3584+
integrity sha512-8+9WqebbFzpX9OR+Wa6O29asIogeRMzcGtAINdpMHHyAg10f05aSFVBbcEqGf/PXw1EjAZ+q2/bEBg3DvurK3Q==
3585+
35763586
arr-diff@^1.0.1:
35773587
version "1.1.0"
35783588
resolved "https://registry.yarnpkg.com/arr-diff/-/arr-diff-1.1.0.tgz#687c32758163588fef7de7b36fabe495eb1a399a"
@@ -8218,6 +8228,13 @@ js-yaml@^3.13.1:
82188228
argparse "^1.0.7"
82198229
esprima "^4.0.0"
82208230

8231+
js-yaml@^4.0.0:
8232+
version "4.0.0"
8233+
resolved "https://registry.yarnpkg.com/js-yaml/-/js-yaml-4.0.0.tgz#f426bc0ff4b4051926cd588c71113183409a121f"
8234+
integrity sha512-pqon0s+4ScYUvX30wxQi3PogGFAlUyH0awepWvwkj4jD4v+ova3RiYw8bmA6x2rDrEaj8i/oWKoRxpVNW+Re8Q==
8235+
dependencies:
8236+
argparse "^2.0.1"
8237+
82218238
jsbn@~0.1.0:
82228239
version "0.1.1"
82238240
resolved "https://registry.yarnpkg.com/jsbn/-/jsbn-0.1.1.tgz#a5e654c2e5a2deb5f201d96cefbca80c0ef2f513"

0 commit comments

Comments
 (0)