@@ -9,6 +9,10 @@ import ConfigManager from 'browser/main/lib/ConfigManager'
99import NoteItem from 'browser/components/NoteItem'
1010import NoteItemSimple from 'browser/components/NoteItemSimple'
1111import searchFromNotes from 'browser/lib/search'
12+ import fs from 'fs'
13+ import { hashHistory } from 'react-router'
14+ import markdown from 'browser/lib/markdown'
15+ import { findNoteTitle } from 'browser/lib/findNoteTitle'
1216
1317const { remote } = require ( 'electron' )
1418const { Menu, MenuItem, dialog } = remote
@@ -42,6 +46,7 @@ class NoteList extends React.Component {
4246 this . alertIfSnippetHandler = ( ) => {
4347 this . alertIfSnippet ( )
4448 }
49+ this . importFromFileHandler = this . importFromFile . bind ( this )
4550
4651 this . jumpToTopHandler = ( ) => {
4752 this . jumpToTop ( )
@@ -59,6 +64,7 @@ class NoteList extends React.Component {
5964 ee . on ( 'list:isMarkdownNote' , this . alertIfSnippetHandler )
6065 ee . on ( 'list:top' , this . jumpToTopHandler )
6166 ee . on ( 'list:jumpToTop' , this . jumpToTopHandler )
67+ ee . on ( 'import:file' , this . importFromFileHandler )
6268 }
6369
6470 componentWillReceiveProps ( nextProps ) {
@@ -80,6 +86,7 @@ class NoteList extends React.Component {
8086 ee . off ( 'list:isMarkdownNote' , this . alertIfSnippetHandler )
8187 ee . off ( 'list:top' , this . jumpToTopHandler )
8288 ee . off ( 'list:jumpToTop' , this . jumpToTopHandler )
89+ ee . off ( 'import:file' , this . importFromFileHandler )
8390 }
8491
8592 componentDidUpdate ( prevProps ) {
@@ -365,6 +372,51 @@ class NoteList extends React.Component {
365372 e . dataTransfer . setData ( 'note' , noteData )
366373 }
367374
375+ importFromFile ( ) {
376+ const { dispatch, location } = this . props
377+
378+ const options = {
379+ filters : [
380+ { name : 'Documents' , extensions : [ 'md' , 'txt' ] }
381+ ] ,
382+ properties : [ 'openFile' , 'multiSelections' ]
383+ }
384+
385+ const targetIndex = _ . findIndex ( this . notes , ( note ) => {
386+ return note !== null && `${ note . storage } -${ note . key } ` === location . query . key
387+ } )
388+
389+ const storageKey = this . notes [ targetIndex ] . storage
390+ const folderKey = this . notes [ targetIndex ] . folder
391+
392+ dialog . showOpenDialog ( remote . getCurrentWindow ( ) , options , ( filepaths ) => {
393+ if ( filepaths === undefined ) return
394+ filepaths . forEach ( ( filepath ) => {
395+ fs . readFile ( filepath , ( err , data ) => {
396+ if ( err ) throw Error ( 'File reading error: ' , err )
397+ const content = data . toString ( )
398+ const newNote = {
399+ content : content ,
400+ folder : folderKey ,
401+ title : markdown . strip ( findNoteTitle ( content ) ) ,
402+ type : 'MARKDOWN_NOTE'
403+ }
404+ dataApi . createNote ( storageKey , newNote )
405+ . then ( ( note ) => {
406+ dispatch ( {
407+ type : 'UPDATE_NOTE' ,
408+ note : note
409+ } )
410+ hashHistory . push ( {
411+ pathname : location . pathname ,
412+ query : { key : `${ note . storage } -${ note . key } ` }
413+ } )
414+ } )
415+ } )
416+ } )
417+ } )
418+ }
419+
368420 render ( ) {
369421 let { location, notes, config, dispatch } = this . props
370422 let sortFunc = config . sortBy === 'CREATED_AT'
0 commit comments