Skip to content

Commit 9f4dd90

Browse files
committed
right click to delete a note
1 parent 2b85aa1 commit 9f4dd90

1 file changed

Lines changed: 57 additions & 11 deletions

File tree

browser/main/NoteList/index.js

Lines changed: 57 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ import styles from './NoteList.styl'
44
import moment from 'moment'
55
import _ from 'lodash'
66
import ee from 'browser/main/lib/eventEmitter'
7+
import dataApi from 'browser/main/lib/dataApi'
8+
9+
const { remote } = require('electron')
10+
const { Menu, MenuItem, dialog } = remote
711

812
class NoteList extends React.Component {
913
constructor (props) {
@@ -213,17 +217,58 @@ class NoteList extends React.Component {
213217
: []
214218
}
215219

216-
handleNoteClick (uniqueKey) {
217-
return (e) => {
218-
let { router } = this.context
219-
let { location } = this.props
220+
handleNoteClick (e, uniqueKey) {
221+
let { router } = this.context
222+
let { location } = this.props
220223

221-
router.push({
222-
pathname: location.pathname,
223-
query: {
224-
key: uniqueKey
225-
}
226-
})
224+
router.push({
225+
pathname: location.pathname,
226+
query: {
227+
key: uniqueKey
228+
}
229+
})
230+
}
231+
232+
handleNoteContextMenu (e, uniqueKey) {
233+
let menu = new Menu()
234+
menu.append(new MenuItem({
235+
label: 'Delete Note',
236+
click: (e) => this.handleDeleteNote(e, uniqueKey)
237+
}))
238+
menu.popup()
239+
}
240+
241+
handleDeleteNote (e, uniqueKey) {
242+
let index = dialog.showMessageBox(remote.getCurrentWindow(), {
243+
type: 'warning',
244+
message: 'Delete a note',
245+
detail: 'This work cannot be undone.',
246+
buttons: ['Confirm', 'Cancel']
247+
})
248+
if (index === 0) {
249+
let { dispatch, location } = this.props
250+
let splitted = uniqueKey.split('-')
251+
let storageKey = splitted.shift()
252+
let noteKey = splitted.shift()
253+
254+
dataApi
255+
.deleteNote(storageKey, noteKey)
256+
.then((data) => {
257+
let dispatchHandler = () => {
258+
dispatch({
259+
type: 'DELETE_NOTE',
260+
storageKey: data.storageKey,
261+
noteKey: data.noteKey
262+
})
263+
}
264+
265+
if (location.query.key === uniqueKey) {
266+
ee.once('list:moved', dispatchHandler)
267+
ee.emit('list:next')
268+
} else {
269+
dispatchHandler()
270+
}
271+
})
227272
}
228273
}
229274

@@ -254,7 +299,8 @@ class NoteList extends React.Component {
254299
: 'item'
255300
}
256301
key={note.storage + '-' + note.key}
257-
onClick={(e) => this.handleNoteClick(note.storage + '-' + note.key)(e)}
302+
onClick={(e) => this.handleNoteClick(e, note.storage + '-' + note.key)}
303+
onContextMenu={(e) => this.handleNoteContextMenu(e, note.storage + '-' + note.key)}
258304
>
259305
<div styleName='item-border'/>
260306
<div styleName='item-info'>

0 commit comments

Comments
 (0)