Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions browser/components/MarkdownEditor.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import CSSModules from 'browser/lib/CSSModules'
import styles from './MarkdownEditor.styl'
import CodeEditor from 'browser/components/CodeEditor'
import MarkdownPreview from 'browser/components/MarkdownPreview'
import eventEmitter from 'browser/main/lib/eventEmitter'

class MarkdownEditor extends React.Component {
constructor (props) {
Expand All @@ -13,12 +14,18 @@ class MarkdownEditor extends React.Component {
this.state = {
status: 'PREVIEW',
renderValue: props.value,
keyPressed: {}
keyPressed: {},
locked: false
Copy link
Contributor Author

@asmsuechan asmsuechan Feb 11, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

@asmsuechan asmsuechan Feb 11, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

However, I'm not sure how it connects each other.

}

this.lockEditorCode = () => this.handleLockEditor()
this.getEditorStatus = () => this.handleGetEditorStatus()
}

componentDidMount () {
this.value = this.refs.code.value
eventEmitter.on('editor:lock', this.lockEditorCode)
eventEmitter.on('editor:status', this.getEditorStatus)
}

componentDidUpdate () {
Expand All @@ -33,6 +40,8 @@ class MarkdownEditor extends React.Component {

componentWillUnmount () {
this.cancelQueue()
eventEmitter.off('editor:lock', this.lockEditorCode)
eventEmitter.off('editor:status', this.getEditorStatus)
}

queueRendering (value) {
Expand Down Expand Up @@ -77,6 +86,7 @@ class MarkdownEditor extends React.Component {
}

handleBlur (e) {
if (this.state.locked) return
this.setState({ keyPressed: [] })
let { config } = this.props
if (config.editor.switchPreview === 'BLUR') {
Expand Down Expand Up @@ -152,7 +162,7 @@ class MarkdownEditor extends React.Component {
})
this.setState({ keyPressed })
let isNoteHandlerKey = (el) => { return this.state.keyPressed[el] }
if (this.state.status === 'CODE' && this.escapeFromEditor.every(isNoteHandlerKey)) {
if (!this.state.locked && this.state.status === 'CODE' && this.escapeFromEditor.every(isNoteHandlerKey)) {
document.activeElement.blur()
}
}
Expand All @@ -164,6 +174,10 @@ class MarkdownEditor extends React.Component {
this.setState({ keyPressed })
}

handleLockEditor () {
this.setState({ locked: !this.state.locked })
}

render () {
let { className, value, config } = this.props

Expand Down
27 changes: 26 additions & 1 deletion browser/main/Detail/MarkdownNoteDetail.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ class MarkdownNoteDetail extends React.Component {
note: Object.assign({
title: '',
content: ''
}, props.note)
}, props.note),
locked: false
}
this.dispatchTimer = null
}
Expand Down Expand Up @@ -200,6 +201,16 @@ class MarkdownNoteDetail extends React.Component {
}
}

handleLockButtonClick () {
ee.emit('editor:lock')
this.focus()
this.setState({ locked: !this.state.locked })
}

toggleLockButton () {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think getToggleLockButtonClassName is better.

return this.state.locked ? 'fa-lock' : 'fa-unlock-alt'
Copy link
Contributor Author

@asmsuechan asmsuechan Feb 11, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This does not collaborate with the actual a state of MarkdownEditor.

}

handleDeleteKeyDown (e) {
if (e.keyCode === 27) this.handleDeleteCancelButtonClick(e)
}
Expand Down Expand Up @@ -235,6 +246,20 @@ class MarkdownNoteDetail extends React.Component {
/>
</div>
<div styleName='info-right'>
{(() => {
// TODO: get a state of MarkdownEditor somehow
const editorStatus='CODE'
let faClassName=`fa ${this.toggleLockButton()}`
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wanna get the current status PREVIEW/CODE of the MarkdownEditor.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you use const?

if (editorStatus === 'CODE') {
return(
<button styleName='info-right-button'
onClick={(e) => this.handleLockButtonClick(e)}
>
<i className={faClassName}/>
</button>
)
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Writing this function before return is more readable.

const lockButtonComponent =
  <button styleName='info-right-button'
    onFocus={(e) => this.handleFocus(e)}
    onMouseDown={(e) => this.handleLockButtonMouseDown(e)}
  >
    <i className={`fa ${this.toggleLockButton()}`}/>
  </button>;

/** */

return (
  {this.state.editorStatus === 'CODE' ? lockButtonComponent : '' }
);

})()}
<button styleName='info-right-button'
onClick={(e) => this.handleContextButtonClick(e)}
>
Expand Down