Skip to content

Commit

Permalink
Merge pull request #793 from asmsuechan/add-drop-on-notelist
Browse files Browse the repository at this point in the history
Add file drop on NoteList
  • Loading branch information
kazup01 authored Sep 23, 2017
2 parents 8e89fb8 + f8e6a93 commit 3ef33c0
Showing 1 changed file with 37 additions and 22 deletions.
59 changes: 37 additions & 22 deletions browser/main/NoteList/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -348,35 +348,49 @@ class NoteList extends React.Component {
properties: ['openFile', 'multiSelections']
}

dialog.showOpenDialog(remote.getCurrentWindow(), options, (filepaths) => {
this.addNotesFromFiles(filepaths)
})
}

handleDrop (e) {
e.preventDefault()
const { location } = this.props
const filepaths = Array.from(e.dataTransfer.files).map(file => { return file.path })
if (!location.pathname.match(/\/trashed/)) this.addNotes(filepaths)
}

// Add notes to the current folder
addNotesFromFiles (filepaths) {
const { dispatch, location } = this.props

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)
const content = data.toString()
const newNote = {
content: content,
folder: folderKey,
title: markdown.strip(striptags(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}`}
})
if (filepaths === undefined) return
filepaths.forEach((filepath) => {
fs.readFile(filepath, (err, data) => {
if (err) throw Error('File reading error: ', err)
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}`}
})
})
})
Expand Down Expand Up @@ -439,6 +453,7 @@ class NoteList extends React.Component {
<div className='NoteList'
styleName='root'
style={this.props.style}
onDrop={(e) => this.handleDrop(e)}
>
<div styleName='control'>
<div styleName='control-sortBy'>
Expand Down

0 comments on commit 3ef33c0

Please sign in to comment.