From c38a76d587bb2cf0e2d02933bd99b5b2b9c3bb70 Mon Sep 17 00:00:00 2001 From: asmsuechan Date: Tue, 13 Jun 2017 11:06:00 +0900 Subject: [PATCH 1/5] Add importer which imports files from .md/.txt --- browser/main/NoteList/index.js | 50 ++++++++++++++++++++++++++++++++++ lib/main-menu.js | 14 ++++++++++ 2 files changed, 64 insertions(+) diff --git a/browser/main/NoteList/index.js b/browser/main/NoteList/index.js index feccd3af..2c6abce2 100644 --- a/browser/main/NoteList/index.js +++ b/browser/main/NoteList/index.js @@ -9,6 +9,8 @@ import ConfigManager from 'browser/main/lib/ConfigManager' import NoteItem from 'browser/components/NoteItem' import NoteItemSimple from 'browser/components/NoteItemSimple' import searchFromNotes from 'browser/lib/search' +import fs from 'fs' +import { hashHistory } from 'react-router' const { remote } = require('electron') const { Menu, MenuItem, dialog } = remote @@ -42,6 +44,7 @@ class NoteList extends React.Component { this.alertIfSnippetHandler = () => { this.alertIfSnippet() } + this.importFromFileHandler = this.importFromFile.bind(this) this.jumpToTopHandler = () => { this.jumpToTop() @@ -59,6 +62,7 @@ class NoteList extends React.Component { ee.on('list:isMarkdownNote', this.alertIfSnippetHandler) ee.on('list:top', this.jumpToTopHandler) ee.on('list:jumpToTop', this.jumpToTopHandler) + ee.on('import:file', this.importFromFileHandler) } componentWillReceiveProps (nextProps) { @@ -80,6 +84,7 @@ class NoteList extends React.Component { ee.off('list:isMarkdownNote', this.alertIfSnippetHandler) ee.off('list:top', this.jumpToTopHandler) ee.off('list:jumpToTop', this.jumpToTopHandler) + ee.off('import:file', this.importFromFileHandler) } componentDidUpdate (prevProps) { @@ -365,6 +370,51 @@ class NoteList extends React.Component { e.dataTransfer.setData('note', noteData) } + importFromFile () { + const { dispatch, location } = this.props + + const options = { + filters: [ + { name: 'Documents', extensions: ['md', 'txt']} + ], + properties: ['openFile'] + } + + const targetIndex = _.findIndex(this.notes, (note) => { + return note != null && note.storage + '-' + note.key === location.query.key + }) + + const storageKey = this.notes[targetIndex].storage + const folderKey = this.notes[targetIndex].folder + + dialog.showOpenDialog(remote.getCurrentWindow(), options, (filepaths) => { + if (filepaths === undefined) return + filepaths.forEach((filepath) => { + fs.readFile(filepath, (err, data) => { + if (err) throw Error('File reading error: ', err) + // TODO: fill the title + const newNote = { + content: data.toString(), + folder: folderKey, + title: '', + type: 'MARKDOWN_NOTE' + } + dataApi.createNote(storageKey, newNote) + .then((note) => { + dispatch({ + type: 'UPDATE_NOTE', + note: note + }) + hashHistory.push({ + pathname: location.pathname, + query: {key: note.storage + '-' + note.key} + }) + }) + }) + }) + }) + } + render () { let { location, notes, config, dispatch } = this.props let sortFunc = config.sortBy === 'CREATED_AT' diff --git a/lib/main-menu.js b/lib/main-menu.js index 6c239771..8e26d356 100644 --- a/lib/main-menu.js +++ b/lib/main-menu.js @@ -92,6 +92,20 @@ const file = { { type: 'separator' }, + { + label: 'Import from', + submenu: [ + { + label: 'Plain Text, MarkDown (.txt, .md)', + click () { + mainWindow.webContents.send('import:file') + } + } + ] + }, + { + type: 'separator' + }, { label: 'Delete Note', accelerator: macOS ? 'Control+Backspace' : 'Control+Delete', From 9f383ba4911c24a772858b20d97df68004e9c916 Mon Sep 17 00:00:00 2001 From: asmsuechan Date: Thu, 16 Mar 2017 13:22:42 -0700 Subject: [PATCH 2/5] Add an option to enable selectiong multiple files --- browser/main/NoteList/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/browser/main/NoteList/index.js b/browser/main/NoteList/index.js index 2c6abce2..b90de31e 100644 --- a/browser/main/NoteList/index.js +++ b/browser/main/NoteList/index.js @@ -377,7 +377,7 @@ class NoteList extends React.Component { filters: [ { name: 'Documents', extensions: ['md', 'txt']} ], - properties: ['openFile'] + properties: ['openFile', 'multiSelections'] } const targetIndex = _.findIndex(this.notes, (note) => { From d24bcb7f8646acd56878c9b22ceec705793c882c Mon Sep 17 00:00:00 2001 From: asmsuechan Date: Thu, 4 May 2017 10:15:22 -0700 Subject: [PATCH 3/5] Change to template literal --- browser/main/NoteList/index.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/browser/main/NoteList/index.js b/browser/main/NoteList/index.js index b90de31e..6bd76c1f 100644 --- a/browser/main/NoteList/index.js +++ b/browser/main/NoteList/index.js @@ -381,7 +381,7 @@ class NoteList extends React.Component { } const targetIndex = _.findIndex(this.notes, (note) => { - return note != null && note.storage + '-' + note.key === location.query.key + return note !== null && `${note.storage}-${note.key}` === location.query.key }) const storageKey = this.notes[targetIndex].storage @@ -407,7 +407,7 @@ class NoteList extends React.Component { }) hashHistory.push({ pathname: location.pathname, - query: {key: note.storage + '-' + note.key} + query: {key: `${note.storage}-${note.key}`} }) }) }) From 07df26d5c4b1a78805804849c179de79c62af94e Mon Sep 17 00:00:00 2001 From: asmsuechan Date: Tue, 13 Jun 2017 11:13:07 +0900 Subject: [PATCH 4/5] Change to use findNoteTitle() --- browser/main/NoteList/index.js | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/browser/main/NoteList/index.js b/browser/main/NoteList/index.js index 6bd76c1f..d703e1f3 100644 --- a/browser/main/NoteList/index.js +++ b/browser/main/NoteList/index.js @@ -11,6 +11,8 @@ import NoteItemSimple from 'browser/components/NoteItemSimple' import searchFromNotes from 'browser/lib/search' import fs from 'fs' import { hashHistory } from 'react-router' +import markdown from 'browser/lib/markdown' +import { findNoteTitle } from 'browser/lib/findNoteTitle' const { remote } = require('electron') const { Menu, MenuItem, dialog } = remote @@ -392,11 +394,11 @@ class NoteList extends React.Component { filepaths.forEach((filepath) => { fs.readFile(filepath, (err, data) => { if (err) throw Error('File reading error: ', err) - // TODO: fill the title + const content = data.toString() const newNote = { - content: data.toString(), + content: content, folder: folderKey, - title: '', + title: markdown.strip(findNoteTitle(content)), type: 'MARKDOWN_NOTE' } dataApi.createNote(storageKey, newNote) From 68e36d2a6dde5dab8e4f933d03ad4d45b3af7d6e Mon Sep 17 00:00:00 2001 From: asmsuechan Date: Fri, 16 Jun 2017 21:17:39 +0900 Subject: [PATCH 5/5] Fix some pointed by lint --- browser/main/NoteList/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/browser/main/NoteList/index.js b/browser/main/NoteList/index.js index d703e1f3..55ce4980 100644 --- a/browser/main/NoteList/index.js +++ b/browser/main/NoteList/index.js @@ -377,7 +377,7 @@ class NoteList extends React.Component { const options = { filters: [ - { name: 'Documents', extensions: ['md', 'txt']} + { name: 'Documents', extensions: ['md', 'txt'] } ], properties: ['openFile', 'multiSelections'] }