mirror of
https://github.com/streetwriters/notesnook.git
synced 2025-12-23 15:09:33 +01:00
add saveToPDF method
This commit is contained in:
@@ -6,8 +6,9 @@ import MMKVStorage from 'react-native-mmkv-storage';
|
||||
import {generateSecureRandom} from 'react-native-securerandom';
|
||||
import Sodium from 'react-native-sodium';
|
||||
import RNFetchBlob from 'rn-fetch-blob';
|
||||
import {db, requestStoragePermission, ToastEvent} from './utils';
|
||||
export const MMKV = new MMKVStorage.Loader().initialize();
|
||||
|
||||
import he from 'he';
|
||||
async function read(key, isArray = false) {
|
||||
let data;
|
||||
|
||||
@@ -43,16 +44,14 @@ async function readMulti(keys) {
|
||||
let obj;
|
||||
try {
|
||||
obj = JSON.parse(value);
|
||||
|
||||
} catch (e) {
|
||||
obj = value;
|
||||
}
|
||||
|
||||
return [key, obj];
|
||||
})
|
||||
});
|
||||
|
||||
return map;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -80,19 +79,6 @@ async function deriveKey(password, salt) {
|
||||
} catch (e) {}
|
||||
}
|
||||
|
||||
async function saveToPDF(html, filename) {
|
||||
let options = {
|
||||
html: html,
|
||||
fileName: filename,
|
||||
directory:
|
||||
Platform.OS === 'ios'
|
||||
? 'Documents'
|
||||
: RNFetchBlob.fs.dirs.SDCardDir + '/Call Of Writing/exported/PDF/',
|
||||
};
|
||||
|
||||
return await RNHTMLtoPDF.convert(options);
|
||||
}
|
||||
|
||||
async function getAllKeys() {
|
||||
return await MMKV.indexer.getKeys();
|
||||
}
|
||||
@@ -101,6 +87,35 @@ async function getRandomBytes(length) {
|
||||
return await generateSecureRandom(length);
|
||||
}
|
||||
|
||||
async function saveToPDF(note) {
|
||||
let androidSavePath = '/Notesnook/exported/PDF';
|
||||
if (Platform.OS === 'android') {
|
||||
let hasPermission = await requestStoragePermission();
|
||||
if (!hasPermission) {
|
||||
ToastEvent.show('Failed to get storage permission');
|
||||
return null;
|
||||
}
|
||||
}
|
||||
let html = await db.notes.note(note).export('html');
|
||||
html = he.decode(html);
|
||||
let options = {
|
||||
html: html,
|
||||
fileName: note.title,
|
||||
directory: Platform.OS === 'ios' ? 'Documents' : androidSavePath,
|
||||
};
|
||||
return await RNHTMLtoPDF.convert(options);
|
||||
}
|
||||
|
||||
async function checkAndCreateDir(dir) {
|
||||
let exists = RNFetchBlob.fs.exists(dir);
|
||||
let isDir = RNFetchBlob.fs.isDir(dir);
|
||||
|
||||
if (!exists || !isDir) {
|
||||
await RNFetchBlob.fs.mkdir(dir);
|
||||
}
|
||||
return dir;
|
||||
}
|
||||
|
||||
export default {
|
||||
read,
|
||||
write,
|
||||
@@ -113,4 +128,5 @@ export default {
|
||||
saveToPDF,
|
||||
getAllKeys,
|
||||
getRandomBytes,
|
||||
checkAndCreateDir,
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user