mirror of
https://github.com/BoostIo/Boostnote
synced 2025-12-13 09:46:22 +00:00
300 lines
7.5 KiB
JavaScript
300 lines
7.5 KiB
JavaScript
import React, { PropTypes } from 'react'
|
|
import CSSModules from 'browser/lib/CSSModules'
|
|
import styles from './MarkdownNoteDetail.styl'
|
|
import MarkdownEditor from 'browser/components/MarkdownEditor'
|
|
import StarButton from './StarButton'
|
|
import TagSelect from './TagSelect'
|
|
import FolderSelect from './FolderSelect'
|
|
import dataApi from 'browser/main/lib/dataApi'
|
|
import { hashHistory } from 'react-router'
|
|
import ee from 'browser/main/lib/eventEmitter'
|
|
import markdown from 'browser/lib/markdown'
|
|
|
|
const electron = require('electron')
|
|
const { remote } = electron
|
|
const { Menu, MenuItem, dialog } = remote
|
|
|
|
class MarkdownNoteDetail extends React.Component {
|
|
constructor (props) {
|
|
super(props)
|
|
|
|
this.state = {
|
|
isMovingNote: false,
|
|
note: Object.assign({
|
|
title: '',
|
|
content: ''
|
|
}, props.note)
|
|
}
|
|
this.dispatchTimer = null
|
|
}
|
|
|
|
focus () {
|
|
this.refs.content.focus()
|
|
}
|
|
|
|
componentWillReceiveProps (nextProps) {
|
|
if (nextProps.note.key !== this.props.note.key && !this.isMovingNote) {
|
|
if (this.saveQueue != null) this.saveNow()
|
|
this.setState({
|
|
note: Object.assign({}, nextProps.note)
|
|
}, () => {
|
|
this.refs.content.reload()
|
|
this.refs.tags.reset()
|
|
})
|
|
}
|
|
}
|
|
|
|
componentWillUnmount () {
|
|
if (this.saveQueue != null) this.saveNow()
|
|
}
|
|
|
|
findTitle (value) {
|
|
let splitted = value.split('\n')
|
|
let title = null
|
|
|
|
for (let i = 0; i < splitted.length; i++) {
|
|
let trimmedLine = splitted[i].trim()
|
|
if (trimmedLine.match(/^# .+/)) {
|
|
title = trimmedLine.substring(1, trimmedLine.length).trim()
|
|
break
|
|
}
|
|
}
|
|
|
|
if (title == null) {
|
|
for (let i = 0; i < splitted.length; i++) {
|
|
let trimmedLine = splitted[i].trim()
|
|
if (trimmedLine.length > 0) {
|
|
title = trimmedLine
|
|
break
|
|
}
|
|
}
|
|
if (title == null) {
|
|
title = ''
|
|
}
|
|
}
|
|
|
|
title = markdown.strip(title)
|
|
|
|
return title
|
|
}
|
|
|
|
handleChange (e) {
|
|
let { note } = this.state
|
|
|
|
note.content = this.refs.content.value
|
|
note.tags = this.refs.tags.value
|
|
note.title = this.findTitle(note.content)
|
|
note.updatedAt = new Date()
|
|
|
|
this.setState({
|
|
note
|
|
}, () => {
|
|
this.save()
|
|
})
|
|
}
|
|
|
|
save () {
|
|
clearTimeout(this.saveQueue)
|
|
this.saveQueue = setTimeout(() => {
|
|
this.saveNow()
|
|
}, 1000)
|
|
}
|
|
|
|
saveNow () {
|
|
let { note, dispatch } = this.props
|
|
clearTimeout(this.saveQueue)
|
|
this.saveQueue = null
|
|
|
|
dataApi
|
|
.updateNote(note.storage, note.key, this.state.note)
|
|
.then((note) => {
|
|
dispatch({
|
|
type: 'UPDATE_NOTE',
|
|
note: note
|
|
})
|
|
})
|
|
}
|
|
|
|
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.key, newStorageKey, newFolderKey)
|
|
.then((newNote) => {
|
|
this.setState({
|
|
isMovingNote: true,
|
|
note: Object.assign({}, newNote)
|
|
}, () => {
|
|
let { dispatch, location } = this.props
|
|
dispatch({
|
|
type: 'MOVE_NOTE',
|
|
originNote: note,
|
|
note: newNote
|
|
})
|
|
hashHistory.replace({
|
|
pathname: location.pathname,
|
|
query: {
|
|
key: newNote.storage + '-' + newNote.key
|
|
}
|
|
})
|
|
this.setState({
|
|
isMovingNote: false
|
|
})
|
|
})
|
|
})
|
|
}
|
|
|
|
handleStarButtonClick (e) {
|
|
let { note } = this.state
|
|
|
|
note.isStarred = !note.isStarred
|
|
|
|
this.setState({
|
|
note
|
|
}, () => {
|
|
this.save()
|
|
})
|
|
}
|
|
|
|
exportAsFile () {
|
|
|
|
}
|
|
|
|
handleShareButtonClick (e) {
|
|
let menu = new Menu()
|
|
menu.append(new MenuItem({
|
|
label: 'Export as a File',
|
|
click: (e) => this.handlePreferencesButtonClick(e)
|
|
}))
|
|
menu.append(new MenuItem({
|
|
label: 'Export to Web',
|
|
disabled: true,
|
|
click: (e) => this.handlePreferencesButtonClick(e)
|
|
}))
|
|
menu.popup(remote.getCurrentWindow())
|
|
}
|
|
|
|
handleContextButtonClick (e) {
|
|
let menu = new Menu()
|
|
menu.append(new MenuItem({
|
|
label: 'Delete',
|
|
click: (e) => this.handleDeleteMenuClick(e)
|
|
}))
|
|
menu.popup(remote.getCurrentWindow())
|
|
}
|
|
|
|
handleDeleteMenuClick (e) {
|
|
let index = dialog.showMessageBox(remote.getCurrentWindow(), {
|
|
type: 'warning',
|
|
message: 'Delete a note',
|
|
detail: 'This work cannot be undone.',
|
|
buttons: ['Confirm', 'Cancel']
|
|
})
|
|
if (index === 0) {
|
|
let { note, dispatch } = this.props
|
|
dataApi
|
|
.deleteNote(note.storage, note.key)
|
|
.then((data) => {
|
|
let dispatchHandler = () => {
|
|
dispatch({
|
|
type: 'DELETE_NOTE',
|
|
storageKey: data.storageKey,
|
|
noteKey: data.noteKey
|
|
})
|
|
}
|
|
ee.once('list:moved', dispatchHandler)
|
|
ee.emit('list:next')
|
|
})
|
|
}
|
|
}
|
|
|
|
handleDeleteKeyDown (e) {
|
|
if (e.keyCode === 27) this.handleDeleteCancelButtonClick(e)
|
|
}
|
|
|
|
render () {
|
|
let { data, config } = this.props
|
|
let { note } = this.state
|
|
|
|
return (
|
|
<div className='NoteDetail'
|
|
style={this.props.style}
|
|
styleName='root'
|
|
>
|
|
<div styleName='info'>
|
|
<div styleName='info-left'>
|
|
<div styleName='info-left-top'>
|
|
<FolderSelect styleName='info-left-top-folderSelect'
|
|
value={this.state.note.storage + '-' + this.state.note.folder}
|
|
ref='folder'
|
|
data={data}
|
|
onChange={(e) => this.handleFolderChange(e)}
|
|
/>
|
|
</div>
|
|
<div styleName='info-left-bottom'>
|
|
<TagSelect
|
|
styleName='info-left-bottom-tagSelect'
|
|
ref='tags'
|
|
value={this.state.note.tags}
|
|
onChange={(e) => this.handleChange(e)}
|
|
/>
|
|
</div>
|
|
</div>
|
|
<div styleName='info-right'>
|
|
<StarButton styleName='info-right-button'
|
|
onClick={(e) => this.handleStarButtonClick(e)}
|
|
isActive={note.isStarred}
|
|
/>
|
|
<button styleName='info-right-button'
|
|
onClick={(e) => this.handleShareButtonClick(e)}
|
|
disabled
|
|
>
|
|
<i className='fa fa-share-alt fa-fw'/>
|
|
<span styleName='info-right-button-tooltip'
|
|
style={{right: 20}}
|
|
>Share Note</span>
|
|
</button>
|
|
<button styleName='info-right-button'
|
|
onClick={(e) => this.handleContextButtonClick(e)}
|
|
>
|
|
<i className='fa fa-ellipsis-v'/>
|
|
<span styleName='info-right-button-tooltip'
|
|
style={{right: 5}}
|
|
>More Options</span>
|
|
</button>
|
|
</div>
|
|
</div>
|
|
|
|
<div styleName='body'>
|
|
<MarkdownEditor
|
|
ref='content'
|
|
styleName='body-noteEditor'
|
|
config={config}
|
|
value={this.state.note.content}
|
|
onChange={(e) => this.handleChange(e)}
|
|
ignorePreviewPointerEvents={this.props.ignorePreviewPointerEvents}
|
|
/>
|
|
</div>
|
|
</div>
|
|
)
|
|
}
|
|
}
|
|
|
|
MarkdownNoteDetail.propTypes = {
|
|
dispatch: PropTypes.func,
|
|
repositories: PropTypes.array,
|
|
note: PropTypes.shape({
|
|
|
|
}),
|
|
style: PropTypes.shape({
|
|
left: PropTypes.number
|
|
}),
|
|
ignorePreviewPointerEvents: PropTypes.bool
|
|
}
|
|
|
|
export default CSSModules(MarkdownNoteDetail, styles)
|