mirror of
https://github.com/BoostIo/Boostnote
synced 2025-12-13 17:56:25 +00:00
Merge pull request #283 from asmsuechan/add-lock-to-CodeEditor
Enable lock in MarkdownEditor
This commit is contained in:
@@ -3,6 +3,7 @@ import CSSModules from 'browser/lib/CSSModules'
|
|||||||
import styles from './MarkdownEditor.styl'
|
import styles from './MarkdownEditor.styl'
|
||||||
import CodeEditor from 'browser/components/CodeEditor'
|
import CodeEditor from 'browser/components/CodeEditor'
|
||||||
import MarkdownPreview from 'browser/components/MarkdownPreview'
|
import MarkdownPreview from 'browser/components/MarkdownPreview'
|
||||||
|
import eventEmitter from 'browser/main/lib/eventEmitter'
|
||||||
|
|
||||||
class MarkdownEditor extends React.Component {
|
class MarkdownEditor extends React.Component {
|
||||||
constructor (props) {
|
constructor (props) {
|
||||||
@@ -13,12 +14,16 @@ class MarkdownEditor extends React.Component {
|
|||||||
this.state = {
|
this.state = {
|
||||||
status: 'PREVIEW',
|
status: 'PREVIEW',
|
||||||
renderValue: props.value,
|
renderValue: props.value,
|
||||||
keyPressed: {}
|
keyPressed: {},
|
||||||
|
isLocked: false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
this.lockEditorCode = () => this.handleLockEditor()
|
||||||
}
|
}
|
||||||
|
|
||||||
componentDidMount () {
|
componentDidMount () {
|
||||||
this.value = this.refs.code.value
|
this.value = this.refs.code.value
|
||||||
|
eventEmitter.on('editor:lock', this.lockEditorCode)
|
||||||
}
|
}
|
||||||
|
|
||||||
componentDidUpdate () {
|
componentDidUpdate () {
|
||||||
@@ -33,6 +38,7 @@ class MarkdownEditor extends React.Component {
|
|||||||
|
|
||||||
componentWillUnmount () {
|
componentWillUnmount () {
|
||||||
this.cancelQueue()
|
this.cancelQueue()
|
||||||
|
eventEmitter.off('editor:lock', this.lockEditorCode)
|
||||||
}
|
}
|
||||||
|
|
||||||
queueRendering (value) {
|
queueRendering (value) {
|
||||||
@@ -72,11 +78,13 @@ class MarkdownEditor extends React.Component {
|
|||||||
this.refs.code.blur()
|
this.refs.code.blur()
|
||||||
this.refs.preview.focus()
|
this.refs.preview.focus()
|
||||||
}
|
}
|
||||||
|
eventEmitter.emit('topbar:showlockbutton')
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
handleBlur (e) {
|
handleBlur (e) {
|
||||||
|
if (this.state.isLocked) return
|
||||||
this.setState({ keyPressed: [] })
|
this.setState({ keyPressed: [] })
|
||||||
let { config } = this.props
|
let { config } = this.props
|
||||||
if (config.editor.switchPreview === 'BLUR') {
|
if (config.editor.switchPreview === 'BLUR') {
|
||||||
@@ -87,6 +95,7 @@ class MarkdownEditor extends React.Component {
|
|||||||
this.refs.preview.focus()
|
this.refs.preview.focus()
|
||||||
this.refs.preview.scrollTo(cursorPosition.line)
|
this.refs.preview.scrollTo(cursorPosition.line)
|
||||||
})
|
})
|
||||||
|
eventEmitter.emit('topbar:showlockbutton')
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -102,6 +111,7 @@ class MarkdownEditor extends React.Component {
|
|||||||
}, () => {
|
}, () => {
|
||||||
this.refs.code.focus()
|
this.refs.code.focus()
|
||||||
})
|
})
|
||||||
|
eventEmitter.emit('topbar:showlockbutton')
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -138,6 +148,7 @@ class MarkdownEditor extends React.Component {
|
|||||||
} else {
|
} else {
|
||||||
this.refs.code.focus()
|
this.refs.code.focus()
|
||||||
}
|
}
|
||||||
|
eventEmitter.emit('topbar:showlockbutton')
|
||||||
}
|
}
|
||||||
|
|
||||||
reload () {
|
reload () {
|
||||||
@@ -152,7 +163,7 @@ class MarkdownEditor extends React.Component {
|
|||||||
})
|
})
|
||||||
this.setState({ keyPressed })
|
this.setState({ keyPressed })
|
||||||
let isNoteHandlerKey = (el) => { return this.state.keyPressed[el] }
|
let isNoteHandlerKey = (el) => { return this.state.keyPressed[el] }
|
||||||
if (this.state.status === 'CODE' && this.escapeFromEditor.every(isNoteHandlerKey)) {
|
if (!this.state.isLocked && this.state.status === 'CODE' && this.escapeFromEditor.every(isNoteHandlerKey)) {
|
||||||
document.activeElement.blur()
|
document.activeElement.blur()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -164,6 +175,10 @@ class MarkdownEditor extends React.Component {
|
|||||||
this.setState({ keyPressed })
|
this.setState({ keyPressed })
|
||||||
}
|
}
|
||||||
|
|
||||||
|
handleLockEditor () {
|
||||||
|
this.setState({ isLocked: !this.state.isLocked })
|
||||||
|
}
|
||||||
|
|
||||||
render () {
|
render () {
|
||||||
let { className, value, config } = this.props
|
let { className, value, config } = this.props
|
||||||
|
|
||||||
|
|||||||
@@ -25,15 +25,23 @@ class MarkdownNoteDetail extends React.Component {
|
|||||||
note: Object.assign({
|
note: Object.assign({
|
||||||
title: '',
|
title: '',
|
||||||
content: ''
|
content: ''
|
||||||
}, props.note)
|
}, props.note),
|
||||||
|
editorStatus: false,
|
||||||
|
isLocked: false
|
||||||
}
|
}
|
||||||
this.dispatchTimer = null
|
this.dispatchTimer = null
|
||||||
|
|
||||||
|
this.showLockButton = this.handleShowLockButton.bind(this)
|
||||||
}
|
}
|
||||||
|
|
||||||
focus () {
|
focus () {
|
||||||
this.refs.content.focus()
|
this.refs.content.focus()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
componentDidMount () {
|
||||||
|
ee.on('topbar:showlockbutton', this.showLockButton)
|
||||||
|
}
|
||||||
|
|
||||||
componentWillReceiveProps (nextProps) {
|
componentWillReceiveProps (nextProps) {
|
||||||
if (nextProps.note.key !== this.props.note.key && !this.isMovingNote) {
|
if (nextProps.note.key !== this.props.note.key && !this.isMovingNote) {
|
||||||
if (this.saveQueue != null) this.saveNow()
|
if (this.saveQueue != null) this.saveNow()
|
||||||
@@ -50,6 +58,10 @@ class MarkdownNoteDetail extends React.Component {
|
|||||||
if (this.saveQueue != null) this.saveNow()
|
if (this.saveQueue != null) this.saveNow()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
componentDidUnmount () {
|
||||||
|
ee.off('topbar:lock', this.showLockButton)
|
||||||
|
}
|
||||||
|
|
||||||
findTitle (value) {
|
findTitle (value) {
|
||||||
let splitted = value.split('\n')
|
let splitted = value.split('\n')
|
||||||
let title = null
|
let title = null
|
||||||
@@ -200,10 +212,28 @@ class MarkdownNoteDetail extends React.Component {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
handleLockButtonMouseDown (e) {
|
||||||
|
e.preventDefault()
|
||||||
|
ee.emit('editor:lock')
|
||||||
|
this.setState({ isLocked: !this.state.isLocked })
|
||||||
|
}
|
||||||
|
|
||||||
|
getToggleLockButton () {
|
||||||
|
return this.state.isLocked ? 'fa-lock' : 'fa-unlock-alt'
|
||||||
|
}
|
||||||
|
|
||||||
handleDeleteKeyDown (e) {
|
handleDeleteKeyDown (e) {
|
||||||
if (e.keyCode === 27) this.handleDeleteCancelButtonClick(e)
|
if (e.keyCode === 27) this.handleDeleteCancelButtonClick(e)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
handleShowLockButton () {
|
||||||
|
this.setState({editorStatus: this.refs.content.state.status})
|
||||||
|
}
|
||||||
|
|
||||||
|
handleFocus (e) {
|
||||||
|
this.focus()
|
||||||
|
}
|
||||||
|
|
||||||
render () {
|
render () {
|
||||||
let { data, config } = this.props
|
let { data, config } = this.props
|
||||||
let { note } = this.state
|
let { note } = this.state
|
||||||
@@ -235,6 +265,19 @@ class MarkdownNoteDetail extends React.Component {
|
|||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
<div styleName='info-right'>
|
<div styleName='info-right'>
|
||||||
|
{(() => {
|
||||||
|
const faClassName=`fa ${this.getToggleLockButton()}`
|
||||||
|
const lockButtonComponent =
|
||||||
|
<button styleName='info-right-button'
|
||||||
|
onFocus={(e) => this.handleFocus(e)}
|
||||||
|
onMouseDown={(e) => this.handleLockButtonMouseDown(e)}
|
||||||
|
>
|
||||||
|
<i className={faClassName}/>
|
||||||
|
</button>
|
||||||
|
return (
|
||||||
|
this.state.editorStatus === 'CODE' ? lockButtonComponent : ''
|
||||||
|
)
|
||||||
|
})()}
|
||||||
<button styleName='info-right-button'
|
<button styleName='info-right-button'
|
||||||
onClick={(e) => this.handleContextButtonClick(e)}
|
onClick={(e) => this.handleContextButtonClick(e)}
|
||||||
>
|
>
|
||||||
|
|||||||
Reference in New Issue
Block a user