Skip to content

Commit a6e3dbd

Browse files
committed
change folder
1 parent 3882df4 commit a6e3dbd

6 files changed

Lines changed: 118 additions & 20 deletions

File tree

browser/main/Detail/FolderSelect.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -166,20 +166,20 @@ class FolderSelect extends React.Component {
166166
}
167167
}
168168

169-
handleOptionClick (folderKey) {
169+
handleOptionClick (storageKey, folderKey) {
170170
return (e) => {
171171
e.stopPropagation()
172172
this.setState({
173173
status: 'FOCUS'
174174
}, () => {
175-
this.setValue(folderKey)
175+
this.setValue(storageKey + '-' + folderKey)
176176
this.refs.root.focus()
177177
})
178178
}
179179
}
180180

181-
setValue (folderKey) {
182-
this.value = folderKey
181+
setValue (value) {
182+
this.value = value
183183
this.props.onChange()
184184
}
185185

@@ -208,7 +208,7 @@ class FolderSelect extends React.Component {
208208
: 'search-optionList-item'
209209
}
210210
key={option.storage.key + '-' + option.folder.key}
211-
onClick={(e) => this.handleOptionClick(option.folder.key)(e)}
211+
onClick={(e) => this.handleOptionClick(option.storage.key, option.folder.key)(e)}
212212
>
213213
<span styleName='search-optionList-item-name'
214214
style={{borderColor: option.folder.color}}

browser/main/Detail/MarkdownNoteDetail.js

Lines changed: 35 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import TagSelect from './TagSelect'
77
import FolderSelect from './FolderSelect'
88
import Commander from 'browser/main/lib/Commander'
99
import dataApi from 'browser/main/lib/dataApi'
10+
import { hashHistory } from 'react-router'
1011

1112
const electron = require('electron')
1213
const { remote } = electron
@@ -20,9 +21,9 @@ class MarkdownNoteDetail extends React.Component {
2021
this.state = {
2122
note: Object.assign({
2223
title: '',
23-
content: ''
24-
}, props.note),
25-
isDispatchQueued: false
24+
content: '',
25+
isMovingNote: false
26+
}, props.note)
2627
}
2728
this.dispatchTimer = null
2829
}
@@ -43,14 +44,9 @@ class MarkdownNoteDetail extends React.Component {
4344
}
4445

4546
componentWillReceiveProps (nextProps) {
46-
if (nextProps.note.key !== this.props.note.key) {
47-
if (this.state.isDispatchQueued) {
48-
this.cancelDispatchQueue()
49-
this.dispatch()
50-
}
47+
if (nextProps.note.key !== this.props.note.key && !this.isMovingNote) {
5148
this.setState({
52-
note: Object.assign({}, nextProps.note),
53-
isDispatchQueued: false
49+
note: Object.assign({}, nextProps.note)
5450
}, () => {
5551
this.refs.content.reload()
5652
this.refs.tags.reset()
@@ -114,7 +110,36 @@ class MarkdownNoteDetail extends React.Component {
114110
}
115111

116112
handleFolderChange (e) {
113+
let { note } = this.state
114+
let value = this.refs.folder.value
115+
let splitted = value.split('-')
116+
let newStorageKey = splitted.shift()
117+
let newFolderKey = splitted.shift()
117118

119+
dataApi
120+
.moveNote(note.storage, note.folder, note.key, newStorageKey, newFolderKey)
121+
.then((newNote) => {
122+
this.setState({
123+
isMovingNote: true,
124+
note: Object.assign({}, newNote)
125+
}, () => {
126+
let { dispatch, location } = this.props
127+
dispatch({
128+
type: 'MOVE_NOTE',
129+
note: note,
130+
newNote: newNote
131+
})
132+
hashHistory.replace({
133+
pathname: location.pathname,
134+
query: {
135+
key: newNote.uniqueKey
136+
}
137+
})
138+
this.setState({
139+
isMovingNote: false
140+
})
141+
})
142+
})
118143
}
119144

120145
handleStarButtonClick (e) {

browser/main/Detail/SnippetNoteDetail.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import FolderSelect from './FolderSelect'
88
import Commander from 'browser/main/lib/Commander'
99
import dataApi from 'browser/main/lib/dataApi'
1010
import modes from 'browser/lib/modes'
11+
import { hashHistory } from 'react-router'
1112

1213
const electron = require('electron')
1314
const { remote } = electron
@@ -118,7 +119,36 @@ class SnippetNoteDetail extends React.Component {
118119
}
119120

120121
handleFolderChange (e) {
122+
let { note } = this.state
123+
let value = this.refs.folder.value
124+
let splitted = value.split('-')
125+
let newStorageKey = splitted.shift()
126+
let newFolderKey = splitted.shift()
121127

128+
dataApi
129+
.moveNote(note.storage, note.folder, note.key, newStorageKey, newFolderKey)
130+
.then((newNote) => {
131+
this.setState({
132+
isMovingNote: true,
133+
note: Object.assign({}, newNote)
134+
}, () => {
135+
let { dispatch, location } = this.props
136+
dispatch({
137+
type: 'MOVE_NOTE',
138+
note: note,
139+
newNote: newNote
140+
})
141+
hashHistory.replace({
142+
pathname: location.pathname,
143+
query: {
144+
key: newNote.uniqueKey
145+
}
146+
})
147+
this.setState({
148+
isMovingNote: false
149+
})
150+
})
151+
})
122152
}
123153

124154
handleStarButtonClick (e) {

browser/main/Detail/index.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import styles from './Detail.styl'
44
import _ from 'lodash'
55
import MarkdownNoteDetail from './MarkdownNoteDetail'
66
import SnippetNoteDetail from './SnippetNoteDetail'
7+
import dataApi from 'browser/main/lib/dataApi'
78

89
const electron = require('electron')
910

@@ -51,7 +52,8 @@ class Detail extends React.Component {
5152
'dispatch',
5253
'storages',
5354
'style',
54-
'ignorePreviewPointerEvents'
55+
'ignorePreviewPointerEvents',
56+
'location'
5557
])}
5658
/>
5759
)
@@ -65,7 +67,8 @@ class Detail extends React.Component {
6567
'dispatch',
6668
'storages',
6769
'style',
68-
'ignorePreviewPointerEvents'
70+
'ignorePreviewPointerEvents',
71+
'location'
6972
])}
7073
/>
7174
)

browser/main/lib/dataApi.js

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,15 @@ function queueSaveFolder (storageKey, folderKey) {
1818
clearTimeout(task.timer)
1919
})
2020
queuedTasks = queuedTasks.filter((task) => task.storage !== storageKey || task.folder !== folderKey)
21-
2221
let newTimer = setTimeout(() => {
2322
let folderNotes = notes.filter((note) => note.storage === storageKey && note.folder === folderKey)
2423
sander
2524
.writeFile(path.join(storage.cache.path, folderKey, 'data.json'), JSON.stringify({
26-
notes: folderNotes
25+
notes: folderNotes.map((note) => {
26+
let json = note.toJSON()
27+
delete json.storage
28+
return json
29+
})
2730
}))
2831
}, 1500)
2932

@@ -398,6 +401,35 @@ function removeNote (storageKey, folderKey, noteKey, input) {
398401

399402
}
400403

404+
function moveNote (storageKey, folderKey, noteKey, newStorageKey, newFolderKey) {
405+
let note = _.find(notes, {
406+
key: noteKey,
407+
storage: storageKey,
408+
folder: folderKey
409+
})
410+
if (note == null) throw new Error('Note doesn\'t exist.')
411+
412+
let storage = _.find(storages, {key: newStorageKey})
413+
if (storage == null) throw new Error('Storage doesn\'t exist.')
414+
let folder = _.find(storage.data.folders, {key: newFolderKey})
415+
if (folder == null) throw new Error('Folder doesn\'t exist.')
416+
note.storage = storage.key
417+
note.data.storage = storage.key
418+
note.folder = folder.key
419+
note.data.folder = folder.key
420+
let key = note.key
421+
while (notes.some((note) => note.storage === storage.key && note.folder === folder.key && note.key === key)) {
422+
key = keygen()
423+
}
424+
note.key = key
425+
note.data.key = key
426+
note.uniqueKey = `${note.storage}-${note.folder}-${note.key}`
427+
console.log(note.uniqueKey)
428+
queueSaveFolder(storageKey, folderKey)
429+
return note.save()
430+
.then(() => note.toJSON())
431+
}
432+
401433
export default {
402434
init,
403435
addStorage,
@@ -408,5 +440,6 @@ export default {
408440
createMarkdownNote,
409441
createSnippetNote,
410442
updateNote,
411-
removeNote
443+
removeNote,
444+
moveNote
412445
}

browser/main/store.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,13 @@ function notes (state = [], action) {
7979
notes.push(action.note)
8080
return notes
8181
}
82+
case 'MOVE_NOTE':
83+
{
84+
let notes = state.slice()
85+
notes = notes.filter((note) => note.key !== action.note.key || note.folder !== action.note.folder || note.storage !== action.note.storage)
86+
notes.push(action.newNote)
87+
return notes
88+
}
8289
}
8390
return state
8491
}

0 commit comments

Comments
 (0)