import React, { PropTypes } from 'react'
import ReactDOM from 'react-dom'
import moment from 'moment'
import _ from 'lodash'
import MarkdownPreview from 'browser/components/MarkdownPreview'
import CodeEditor from 'browser/components/CodeEditor'
import {
switchFolder,
cacheArticle,
saveArticle
} from '../../actions'
import linkState from 'browser/lib/linkState'
import TagSelect from 'browser/components/TagSelect'
import ModeSelect from 'browser/components/ModeSelect'
import activityRecord from 'browser/lib/activityRecord'
import ShareButton from './ShareButton'
import { openModal, isModalOpen } from 'browser/lib/modal'
import DeleteArticleModal from '../../modal/DeleteArticleModal'
const electron = require('electron')
const clipboard = electron.clipboard
const ipc = electron.ipcRenderer
const OSX = global.process.platform === 'darwin'
const BRAND_COLOR = '#18AF90'
const editDeleteTutorialElement = (
)
const tagSelectTutorialElement = (
Attach some tags here!
)
const modeSelectTutorialElement = (
Select code syntax!!
)
function notify (...args) {
return new window.Notification(...args)
}
export default class ArticleDetail extends React.Component {
constructor (props) {
super(props)
this.saveHandler = e => {
if (isModalOpen()) return true
this.handleSaveButtonClick()
}
this.deleteHandler = e => {
if (isModalOpen()) return true
this.handleDeleteButtonClick()
}
this.togglePreviewHandler = e => {
if (isModalOpen()) return true
this.handleTogglePreviewButtonClick()
}
this.editHandler = e => {
if (isModalOpen()) return true
this.editArticle()
}
this.state = {
article: Object.assign({content: ''}, props.activeArticle),
previewMode: true,
openShareDropdown: false
}
if (props.activeArticle != null && props.activeArticle.content.trim().length > 0 && props.activeArticle.mode === 'markdown') this.state.previewMode = true
}
componentDidMount () {
this.refreshTimer = setInterval(() => this.forceUpdate(), 60 * 1000)
this.shareDropdownInterceptor = e => {
e.stopPropagation()
}
ipc.on('detail-save', this.saveHandler)
ipc.on('detail-delete', this.deleteHandler)
ipc.on('detail-toggle-preview', this.togglePreviewHandler)
ipc.on('detail-edit', this.editHandler)
}
componentWillUnmount () {
clearInterval(this.refreshTimer)
ipc.removeListener('detail-save', this.saveHandler)
ipc.removeListener('detail-delete', this.deleteHandler)
ipc.removeListener('detail-toggle-preview', this.togglePreviewHandler)
ipc.removeListener('detail-on', this.editHandler)
}
componentWillReceiveProps (nextProps) {
let nextArticle = nextProps.activeArticle
let nextModified = nextArticle != null ? _.findWhere(nextProps.modified, {key: nextArticle.key}) : null
let article = Object.assign({content: ''}, nextProps.activeArticle, nextModified)
let nextState = {
article
}
this.setState(nextState)
}
editArticle () {
ReactDOM.findDOMNode(this.refs.title).focus()
ReactDOM.findDOMNode(this.refs.title).select()
}
cacheArticle () {
let { dispatch } = this.props
dispatch(cacheArticle(this.props.activeArticle.key, this.state.article))
}
renderEmpty () {
return (
Command(⌘) + N
to create a new post
)
}
handleClipboardButtonClick (e) {
activityRecord.emit('MAIN_DETAIL_COPY')
clipboard.writeText(this.props.activeArticle.content)
notify('Saved to Clipboard!', {
body: 'Paste it wherever you want!'
})
}
handleSaveButtonClick (e) {
let { dispatch, folders, status } = this.props
let targetFolderKey = this.state.article.FolderKey
dispatch(saveArticle(this.props.activeArticle.key, this.state.article), true)
if (status.targetFolders.length > 0) {
let targetFolder = _.findWhere(folders, {key: targetFolderKey})
dispatch(switchFolder(targetFolder.name))
}
}
handleFolderKeyChange (e) {
let article = this.state.article
article.FolderKey = e.target.value
this.setState({article: article}, () => this.cacheArticle())
}
handleTitleChange (e) {
let { article } = this.state
article.title = e.target.value
this.setState({
article
}, () => this.cacheArticle())
}
handleTagsChange (newTag, tags) {
let article = this.state.article
article.tags = tags
this.setState({
article
}, () => this.cacheArticle())
}
handleModeChange (value) {
let { article } = this.state
article.mode = value
this.setState({
article
}, () => this.cacheArticle())
}
handleContentChange (e, value) {
let { article } = this.state
article.content = value
this.setState({
article
}, () => this.cacheArticle())
}
handleCodeEditorBlur (e) {
if (this.state.article.mode === 'markdown' && !this.state.previewMode) {
this.setState({
previewMode: true
})
}
}
handleDeleteButtonClick (e) {
if (this.props.activeArticle) {
openModal(DeleteArticleModal, {articleKey: this.props.activeArticle.key})
}
}
handleTitleKeyDown (e) {
if (e.keyCode === 9 && !e.shiftKey) {
e.preventDefault()
this.refs.mode.handleIdleSelectClick()
}
}
handleModeSelectKeyDown (e) {
if (e.keyCode === 9 && !e.shiftKey) {
e.preventDefault()
this.setState({previewMode: false}, function () {
this.refs.code.editor.focus()
})
}
if (e.keyCode === 9 && e.shiftKey) {
e.preventDefault()
ReactDOM.findDOMNode(this.refs.title).focus()
}
}
handlePreviewDoubleClick (e) {
this.setState({
previewMode: false
}, function () {
this.refs.code.editor.focus()
})
}
render () {
let { folders, status, tags, activeArticle, modified, user } = this.props
if (activeArticle == null) return this.renderEmpty()
let folderOptions = folders.map(folder => {
return (
)
})
let isUnsaved = !!_.findWhere(modified, {key: activeArticle.key})
return (
● Unsaved
: `Created : ${moment(this.state.article.createdAt).format('YYYY/MM/DD')} Updated : ${moment(this.state.article.updatedAt).format('YYYY/MM/DD')}`
}
/>
{
this.state.article.mode === 'markdown'
?
: null
}
{status.isTutorialOpen ? editDeleteTutorialElement : null}
this.handleTagsChange(tags, tag)}
suggestTags={tags}
/>
{status.isTutorialOpen ? tagSelectTutorialElement : null}
{status.isTutorialOpen ? modeSelectTutorialElement : null}
{this.state.article.mode === 'markdown' && this.state.previewMode
? (
this.handlePreviewDoubleClick(e)}
content={this.state.article.content}
/>)
: ( this.handleContentChange(e, value)}
onBlur={e => this.handleCodeEditorBlur(e)}
readOnly={false}
mode={this.state.article.mode}
code={this.state.article.content}
/>)}
{
this.state.article.mode === 'markdown' && this.state.previewMode
? Double click to edit post
: null
}
)
}
}
ArticleDetail.propTypes = {
dispatch: PropTypes.func,
status: PropTypes.shape(),
tags: PropTypes.array,
user: PropTypes.shape(),
folders: PropTypes.array,
modified: PropTypes.array,
activeArticle: PropTypes.shape()
}
ArticleDetail.prototype.linkState = linkState