Skip to content

Commit d9b2981

Browse files
authored
Merge pull request #325 from asmsuechan/feature-import-from-md-file
Add importer which imports files from .md/.txt
2 parents 68b91bf + 68e36d2 commit d9b2981

2 files changed

Lines changed: 66 additions & 0 deletions

File tree

browser/main/NoteList/index.js

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ import ConfigManager from 'browser/main/lib/ConfigManager'
99
import NoteItem from 'browser/components/NoteItem'
1010
import NoteItemSimple from 'browser/components/NoteItemSimple'
1111
import 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

1317
const { remote } = require('electron')
1418
const { 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'

lib/main-menu.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,20 @@ const file = {
9292
{
9393
type: 'separator'
9494
},
95+
{
96+
label: 'Import from',
97+
submenu: [
98+
{
99+
label: 'Plain Text, MarkDown (.txt, .md)',
100+
click () {
101+
mainWindow.webContents.send('import:file')
102+
}
103+
}
104+
]
105+
},
106+
{
107+
type: 'separator'
108+
},
95109
{
96110
label: 'Delete Note',
97111
accelerator: macOS ? 'Control+Backspace' : 'Control+Delete',

0 commit comments

Comments
 (0)