Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add SimplenoteImporter #975

Merged
merged 3 commits into from
Nov 2, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion .babelrc
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,10 @@
"@babel/plugin-proposal-class-properties",
"@babel/plugin-proposal-object-rest-spread",
"@babel/plugin-syntax-dynamic-import"
]
],
"env": {
"test": {
"plugins": ["dynamic-import-node"]
}
}
}
6 changes: 3 additions & 3 deletions lib/utils/import/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ class CoreImporter extends EventEmitter {
this.noteBucket.add(importedNote);
};

importNotes = (notes = {}) => {
importNotes = (notes = {}, options) => {
if (isEmpty(notes)) {
this.emit('status', 'error', 'No notes to import.');
return;
Expand All @@ -80,9 +80,9 @@ class CoreImporter extends EventEmitter {
return;
}

get(notes, 'activeNotes', []).map(note => this.importNote(note));
get(notes, 'activeNotes', []).map(note => this.importNote(note, options));
get(notes, 'trashedNotes', []).map(note =>
this.importNote(note, { isTrashed: true })
this.importNote(note, { ...options, isTrashed: true })
);
};
}
Expand Down
59 changes: 59 additions & 0 deletions lib/utils/import/simplenote/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import { EventEmitter } from 'events';
import CoreImporter from '../';
import { endsWith, isEmpty } from 'lodash';

class SimplenoteImporter extends EventEmitter {
constructor({ noteBucket, tagBucket, options }) {
super();
this.noteBucket = noteBucket;
this.tagBucket = tagBucket;
this.options = options;
}

importNotes = filesArray => {
const coreImporter = new CoreImporter({
noteBucket: this.noteBucket,
tagBucket: this.tagBucket,
});

if (isEmpty(filesArray)) {
this.emit('status', 'error', 'No file to import.');
return;
}

const file = filesArray[0];

if (!endsWith(file.name.toLowerCase(), '.json')) {
this.emit('status', 'error', 'File name does not end in ".json".');
return;
}

// Limit file size we will read to 1mb
if (file.size > 1000000) {
this.emit('status', 'error', 'File should be less than 1 MB.');
return;
}

const fileReader = new FileReader();

fileReader.onload = event => {
const fileContent = event.target.result;

if (!fileContent) {
this.emit('status', 'error', 'File was empty.');
return;
}

const dataObj = JSON.parse(fileContent);
const noteCount =
dataObj.activeNotes.length + dataObj.trashedNotes.length;

coreImporter.importNotes(dataObj, this.options);
this.emit('status', 'complete', noteCount);
};

fileReader.readAsText(file);
};
}

export default SimplenoteImporter;
12 changes: 10 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
"babel-core": "7.0.0-bridge.0",
"babel-eslint": "7.2.3",
"babel-loader": "8.0.0",
"babel-plugin-dynamic-import-node": "2.2.0",
"classnames": "2.2.5",
"concurrently": "^4.0.1",
"css-loader": "0.28.9",
Expand Down