Skip to content
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
52 changes: 52 additions & 0 deletions browser/main/NoteList/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ 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'
import markdown from 'browser/lib/markdown'
import { findNoteTitle } from 'browser/lib/findNoteTitle'

const { remote } = require('electron')
const { Menu, MenuItem, dialog } = remote
Expand Down Expand Up @@ -42,6 +46,7 @@ class NoteList extends React.Component {
this.alertIfSnippetHandler = () => {
this.alertIfSnippet()
}
this.importFromFileHandler = this.importFromFile.bind(this)

this.jumpToTopHandler = () => {
this.jumpToTop()
Expand All @@ -59,6 +64,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) {
Expand All @@ -80,6 +86,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) {
Expand Down Expand Up @@ -365,6 +372,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', 'multiSelections']
}

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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Question] Do you test this situation? Doesn't it crush?

Copy link
Contributor Author

@asmsuechan asmsuechan May 4, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Umm, I tried by hand, but the probability is quite low.

const content = data.toString()
const newNote = {
content: content,
folder: folderKey,
title: markdown.strip(findNoteTitle(content)),
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'
Expand Down
14 changes: 14 additions & 0 deletions lib/main-menu.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down