1
0
mirror of https://github.com/BoostIo/Boostnote synced 2025-12-13 17:56:25 +00:00

Add importer which imports files from .md/.txt

This commit is contained in:
asmsuechan
2017-06-13 11:06:00 +09:00
parent d1942868e4
commit c38a76d587
2 changed files with 64 additions and 0 deletions

View File

@@ -9,6 +9,8 @@ import ConfigManager from 'browser/main/lib/ConfigManager'
import NoteItem from 'browser/components/NoteItem' import NoteItem from 'browser/components/NoteItem'
import NoteItemSimple from 'browser/components/NoteItemSimple' import NoteItemSimple from 'browser/components/NoteItemSimple'
import searchFromNotes from 'browser/lib/search' import searchFromNotes from 'browser/lib/search'
import fs from 'fs'
import { hashHistory } from 'react-router'
const { remote } = require('electron') const { remote } = require('electron')
const { Menu, MenuItem, dialog } = remote const { Menu, MenuItem, dialog } = remote
@@ -42,6 +44,7 @@ class NoteList extends React.Component {
this.alertIfSnippetHandler = () => { this.alertIfSnippetHandler = () => {
this.alertIfSnippet() this.alertIfSnippet()
} }
this.importFromFileHandler = this.importFromFile.bind(this)
this.jumpToTopHandler = () => { this.jumpToTopHandler = () => {
this.jumpToTop() this.jumpToTop()
@@ -59,6 +62,7 @@ class NoteList extends React.Component {
ee.on('list:isMarkdownNote', this.alertIfSnippetHandler) ee.on('list:isMarkdownNote', this.alertIfSnippetHandler)
ee.on('list:top', this.jumpToTopHandler) ee.on('list:top', this.jumpToTopHandler)
ee.on('list:jumpToTop', this.jumpToTopHandler) ee.on('list:jumpToTop', this.jumpToTopHandler)
ee.on('import:file', this.importFromFileHandler)
} }
componentWillReceiveProps (nextProps) { componentWillReceiveProps (nextProps) {
@@ -80,6 +84,7 @@ class NoteList extends React.Component {
ee.off('list:isMarkdownNote', this.alertIfSnippetHandler) ee.off('list:isMarkdownNote', this.alertIfSnippetHandler)
ee.off('list:top', this.jumpToTopHandler) ee.off('list:top', this.jumpToTopHandler)
ee.off('list:jumpToTop', this.jumpToTopHandler) ee.off('list:jumpToTop', this.jumpToTopHandler)
ee.off('import:file', this.importFromFileHandler)
} }
componentDidUpdate (prevProps) { componentDidUpdate (prevProps) {
@@ -365,6 +370,51 @@ class NoteList extends React.Component {
e.dataTransfer.setData('note', noteData) 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 () { render () {
let { location, notes, config, dispatch } = this.props let { location, notes, config, dispatch } = this.props
let sortFunc = config.sortBy === 'CREATED_AT' let sortFunc = config.sortBy === 'CREATED_AT'

View File

@@ -92,6 +92,20 @@ const file = {
{ {
type: 'separator' type: 'separator'
}, },
{
label: 'Import from',
submenu: [
{
label: 'Plain Text, MarkDown (.txt, .md)',
click () {
mainWindow.webContents.send('import:file')
}
}
]
},
{
type: 'separator'
},
{ {
label: 'Delete Note', label: 'Delete Note',
accelerator: macOS ? 'Control+Backspace' : 'Control+Delete', accelerator: macOS ? 'Control+Backspace' : 'Control+Delete',