1
0
mirror of https://github.com/BoostIo/Boostnote synced 2025-12-14 02:06:29 +00:00

change folder

This commit is contained in:
Dick Choi
2016-07-21 23:44:00 +09:00
parent 3882df41f1
commit a6e3dbd825
6 changed files with 118 additions and 20 deletions

View File

@@ -166,20 +166,20 @@ class FolderSelect extends React.Component {
} }
} }
handleOptionClick (folderKey) { handleOptionClick (storageKey, folderKey) {
return (e) => { return (e) => {
e.stopPropagation() e.stopPropagation()
this.setState({ this.setState({
status: 'FOCUS' status: 'FOCUS'
}, () => { }, () => {
this.setValue(folderKey) this.setValue(storageKey + '-' + folderKey)
this.refs.root.focus() this.refs.root.focus()
}) })
} }
} }
setValue (folderKey) { setValue (value) {
this.value = folderKey this.value = value
this.props.onChange() this.props.onChange()
} }
@@ -208,7 +208,7 @@ class FolderSelect extends React.Component {
: 'search-optionList-item' : 'search-optionList-item'
} }
key={option.storage.key + '-' + option.folder.key} key={option.storage.key + '-' + option.folder.key}
onClick={(e) => this.handleOptionClick(option.folder.key)(e)} onClick={(e) => this.handleOptionClick(option.storage.key, option.folder.key)(e)}
> >
<span styleName='search-optionList-item-name' <span styleName='search-optionList-item-name'
style={{borderColor: option.folder.color}} style={{borderColor: option.folder.color}}

View File

@@ -7,6 +7,7 @@ import TagSelect from './TagSelect'
import FolderSelect from './FolderSelect' import FolderSelect from './FolderSelect'
import Commander from 'browser/main/lib/Commander' import Commander from 'browser/main/lib/Commander'
import dataApi from 'browser/main/lib/dataApi' import dataApi from 'browser/main/lib/dataApi'
import { hashHistory } from 'react-router'
const electron = require('electron') const electron = require('electron')
const { remote } = electron const { remote } = electron
@@ -20,9 +21,9 @@ class MarkdownNoteDetail extends React.Component {
this.state = { this.state = {
note: Object.assign({ note: Object.assign({
title: '', title: '',
content: '' content: '',
}, props.note), isMovingNote: false
isDispatchQueued: false }, props.note)
} }
this.dispatchTimer = null this.dispatchTimer = null
} }
@@ -43,14 +44,9 @@ class MarkdownNoteDetail extends React.Component {
} }
componentWillReceiveProps (nextProps) { componentWillReceiveProps (nextProps) {
if (nextProps.note.key !== this.props.note.key) { if (nextProps.note.key !== this.props.note.key && !this.isMovingNote) {
if (this.state.isDispatchQueued) {
this.cancelDispatchQueue()
this.dispatch()
}
this.setState({ this.setState({
note: Object.assign({}, nextProps.note), note: Object.assign({}, nextProps.note)
isDispatchQueued: false
}, () => { }, () => {
this.refs.content.reload() this.refs.content.reload()
this.refs.tags.reset() this.refs.tags.reset()
@@ -114,7 +110,36 @@ class MarkdownNoteDetail extends React.Component {
} }
handleFolderChange (e) { handleFolderChange (e) {
let { note } = this.state
let value = this.refs.folder.value
let splitted = value.split('-')
let newStorageKey = splitted.shift()
let newFolderKey = splitted.shift()
dataApi
.moveNote(note.storage, note.folder, note.key, newStorageKey, newFolderKey)
.then((newNote) => {
this.setState({
isMovingNote: true,
note: Object.assign({}, newNote)
}, () => {
let { dispatch, location } = this.props
dispatch({
type: 'MOVE_NOTE',
note: note,
newNote: newNote
})
hashHistory.replace({
pathname: location.pathname,
query: {
key: newNote.uniqueKey
}
})
this.setState({
isMovingNote: false
})
})
})
} }
handleStarButtonClick (e) { handleStarButtonClick (e) {

View File

@@ -8,6 +8,7 @@ import FolderSelect from './FolderSelect'
import Commander from 'browser/main/lib/Commander' import Commander from 'browser/main/lib/Commander'
import dataApi from 'browser/main/lib/dataApi' import dataApi from 'browser/main/lib/dataApi'
import modes from 'browser/lib/modes' import modes from 'browser/lib/modes'
import { hashHistory } from 'react-router'
const electron = require('electron') const electron = require('electron')
const { remote } = electron const { remote } = electron
@@ -118,7 +119,36 @@ class SnippetNoteDetail extends React.Component {
} }
handleFolderChange (e) { handleFolderChange (e) {
let { note } = this.state
let value = this.refs.folder.value
let splitted = value.split('-')
let newStorageKey = splitted.shift()
let newFolderKey = splitted.shift()
dataApi
.moveNote(note.storage, note.folder, note.key, newStorageKey, newFolderKey)
.then((newNote) => {
this.setState({
isMovingNote: true,
note: Object.assign({}, newNote)
}, () => {
let { dispatch, location } = this.props
dispatch({
type: 'MOVE_NOTE',
note: note,
newNote: newNote
})
hashHistory.replace({
pathname: location.pathname,
query: {
key: newNote.uniqueKey
}
})
this.setState({
isMovingNote: false
})
})
})
} }
handleStarButtonClick (e) { handleStarButtonClick (e) {

View File

@@ -4,6 +4,7 @@ import styles from './Detail.styl'
import _ from 'lodash' import _ from 'lodash'
import MarkdownNoteDetail from './MarkdownNoteDetail' import MarkdownNoteDetail from './MarkdownNoteDetail'
import SnippetNoteDetail from './SnippetNoteDetail' import SnippetNoteDetail from './SnippetNoteDetail'
import dataApi from 'browser/main/lib/dataApi'
const electron = require('electron') const electron = require('electron')
@@ -51,7 +52,8 @@ class Detail extends React.Component {
'dispatch', 'dispatch',
'storages', 'storages',
'style', 'style',
'ignorePreviewPointerEvents' 'ignorePreviewPointerEvents',
'location'
])} ])}
/> />
) )
@@ -65,7 +67,8 @@ class Detail extends React.Component {
'dispatch', 'dispatch',
'storages', 'storages',
'style', 'style',
'ignorePreviewPointerEvents' 'ignorePreviewPointerEvents',
'location'
])} ])}
/> />
) )

View File

@@ -18,12 +18,15 @@ function queueSaveFolder (storageKey, folderKey) {
clearTimeout(task.timer) clearTimeout(task.timer)
}) })
queuedTasks = queuedTasks.filter((task) => task.storage !== storageKey || task.folder !== folderKey) queuedTasks = queuedTasks.filter((task) => task.storage !== storageKey || task.folder !== folderKey)
let newTimer = setTimeout(() => { let newTimer = setTimeout(() => {
let folderNotes = notes.filter((note) => note.storage === storageKey && note.folder === folderKey) let folderNotes = notes.filter((note) => note.storage === storageKey && note.folder === folderKey)
sander sander
.writeFile(path.join(storage.cache.path, folderKey, 'data.json'), JSON.stringify({ .writeFile(path.join(storage.cache.path, folderKey, 'data.json'), JSON.stringify({
notes: folderNotes notes: folderNotes.map((note) => {
let json = note.toJSON()
delete json.storage
return json
})
})) }))
}, 1500) }, 1500)
@@ -398,6 +401,35 @@ function removeNote (storageKey, folderKey, noteKey, input) {
} }
function moveNote (storageKey, folderKey, noteKey, newStorageKey, newFolderKey) {
let note = _.find(notes, {
key: noteKey,
storage: storageKey,
folder: folderKey
})
if (note == null) throw new Error('Note doesn\'t exist.')
let storage = _.find(storages, {key: newStorageKey})
if (storage == null) throw new Error('Storage doesn\'t exist.')
let folder = _.find(storage.data.folders, {key: newFolderKey})
if (folder == null) throw new Error('Folder doesn\'t exist.')
note.storage = storage.key
note.data.storage = storage.key
note.folder = folder.key
note.data.folder = folder.key
let key = note.key
while (notes.some((note) => note.storage === storage.key && note.folder === folder.key && note.key === key)) {
key = keygen()
}
note.key = key
note.data.key = key
note.uniqueKey = `${note.storage}-${note.folder}-${note.key}`
console.log(note.uniqueKey)
queueSaveFolder(storageKey, folderKey)
return note.save()
.then(() => note.toJSON())
}
export default { export default {
init, init,
addStorage, addStorage,
@@ -408,5 +440,6 @@ export default {
createMarkdownNote, createMarkdownNote,
createSnippetNote, createSnippetNote,
updateNote, updateNote,
removeNote removeNote,
moveNote
} }

View File

@@ -79,6 +79,13 @@ function notes (state = [], action) {
notes.push(action.note) notes.push(action.note)
return notes return notes
} }
case 'MOVE_NOTE':
{
let notes = state.slice()
notes = notes.filter((note) => note.key !== action.note.key || note.folder !== action.note.folder || note.storage !== action.note.storage)
notes.push(action.newNote)
return notes
}
} }
return state return state
} }