1
0
mirror of https://github.com/BoostIo/Boostnote synced 2025-12-13 17:56:25 +00:00

preview on blur

This commit is contained in:
Dick Choi
2016-07-19 02:38:23 +09:00
parent 4f45ba1680
commit 9a5653f1e3
3 changed files with 62 additions and 12 deletions

View File

@@ -13,18 +13,19 @@ export default class CodeEditor extends React.Component {
this.changeHandler = (e) => this.handleChange(e) this.changeHandler = (e) => this.handleChange(e)
this.blurHandler = (e) => { this.blurHandler = (e) => {
if (e.relatedTarget === null) { e.stopPropagation()
return let el = e.relatedTarget
let isStillFocused = false
while (el != null) {
if (el === this.refs.root) {
isStillFocused = true
break
} }
el = el.parentNode
let isFocusingToSearch = e.relatedTarget.className && e.relatedTarget.className.split(' ').some((clss) => {
return clss === 'ace_search_field' || clss === 'ace_searchbtn' || clss === 'ace_replacebtn' || clss === 'ace_searchbtn_close' || clss === 'ace_text-input'
})
if (isFocusingToSearch) {
return
} }
console.log(isStillFocused)
if (this.props.onBlur) this.props.onBlur(e) if (!isStillFocused && this.props.onBlur != null) this.props.onBlur(e)
} }
this.killedBuffer = '' this.killedBuffer = ''
@@ -230,6 +231,8 @@ export default class CodeEditor extends React.Component {
? 'CodeEditor' ? 'CodeEditor'
: `CodeEditor ${className}` : `CodeEditor ${className}`
} }
ref='root'
tabIndex='-1'
style={{ style={{
fontFamily: fontFamily.join(', ') fontFamily: fontFamily.join(', ')
}} }}

View File

@@ -9,7 +9,7 @@ class MarkdownEditor extends React.Component {
super(props) super(props)
this.state = { this.state = {
status: 'CODE' status: 'PREVIEW'
} }
} }
@@ -45,6 +45,32 @@ class MarkdownEditor extends React.Component {
} }
} }
handleBlur (e) {
let { config } = this.props
if (config.editor.switchPreview === 'BLUR') {
this.setState({
status: 'PREVIEW'
}, () => {
this.refs.preview.focus()
})
}
}
handlePreviewMouseDown (e) {
this.previewMouseDownedAt = new Date()
}
handlePreviewMouseUp (e) {
let { config } = this.props
if (config.editor.switchPreview === 'BLUR' && new Date() - this.previewMouseDownedAt < 150) {
this.setState({
status: 'CODE'
}, () => {
this.refs.code.focus()
})
}
}
focus () { focus () {
if (this.state.status === 'PREVIEW') { if (this.state.status === 'PREVIEW') {
this.setState({ this.setState({
@@ -77,6 +103,7 @@ class MarkdownEditor extends React.Component {
: `MarkdownEditor ${className}` : `MarkdownEditor ${className}`
} }
onContextMenu={(e) => this.handleContextMenu(e)} onContextMenu={(e) => this.handleContextMenu(e)}
tabIndex='-1'
> >
<CodeEditor styleName='codeEditor' <CodeEditor styleName='codeEditor'
ref='code' ref='code'
@@ -88,6 +115,7 @@ class MarkdownEditor extends React.Component {
indentType={config.editor.indentType} indentType={config.editor.indentType}
indentSize={editorIndentSize} indentSize={editorIndentSize}
onChange={(e) => this.handleChange(e)} onChange={(e) => this.handleChange(e)}
onBlur={(e) => this.handleBlur(e)}
/> />
<MarkdownPreview styleName={this.state.status === 'PREVIEW' <MarkdownPreview styleName={this.state.status === 'PREVIEW'
? 'preview' ? 'preview'
@@ -103,6 +131,8 @@ class MarkdownEditor extends React.Component {
onContextMenu={(e) => this.handleContextMenu(e)} onContextMenu={(e) => this.handleContextMenu(e)}
tabIndex='0' tabIndex='0'
value={value} value={value}
onMouseUp={(e) => this.handlePreviewMouseUp(e)}
onMouseDown={(e) => this.handlePreviewMouseDown(e)}
/> />
</div> </div>
) )

View File

@@ -7,6 +7,7 @@ const markdownStyle = require('!!css!stylus?sourceMap!./markdown.styl')[0][1]
const { shell } = require('electron') const { shell } = require('electron')
const goExternal = function (e) { const goExternal = function (e) {
e.preventDefault() e.preventDefault()
e.stopPropagation()
shell.openExternal(e.target.href) shell.openExternal(e.target.href)
} }
@@ -24,20 +25,35 @@ export default class MarkdownPreview extends React.Component {
super(props) super(props)
this.contextMenuHandler = (e) => this.handleContextMenu(e) this.contextMenuHandler = (e) => this.handleContextMenu(e)
this.mouseDownHandler = (e) => this.handleMouseDown(e)
this.mouseUpHandler = (e) => this.handleMouseUp(e)
} }
handleContextMenu (e) { handleContextMenu (e) {
this.props.onContextMenu(e) this.props.onContextMenu(e)
} }
handleMouseDown (e) {
if (this.props.onMouseDown != null) this.props.onMouseDown(e)
}
handleMouseUp (e) {
if (this.props.onMouseUp != null) this.props.onMouseUp(e)
}
componentDidMount () { componentDidMount () {
this.refs.root.setAttribute('sandbox', 'allow-same-origin') this.refs.root.setAttribute('sandbox', 'allow-same-origin')
this.refs.root.contentWindow.document.body.addEventListener('contextmenu', this.contextMenuHandler) this.refs.root.contentWindow.document.body.addEventListener('contextmenu', this.contextMenuHandler)
this.rewriteIframe() this.rewriteIframe()
this.refs.root.contentWindow.document.addEventListener('mousedown', this.mouseDownHandler)
this.refs.root.contentWindow.document.addEventListener('mouseup', this.mouseUpHandler)
} }
componentWillUnmount () { componentWillUnmount () {
this.refs.root.contentWindow.document.body.removeEventListener('contextmenu', this.contextMenuHandler) this.refs.root.contentWindow.document.body.removeEventListener('contextmenu', this.contextMenuHandler)
this.refs.root.contentWindow.document.removeEventListener('mousedown', this.mouseDownHandler)
this.refs.root.contentWindow.document.removeEventListener('mouseup', this.mouseUpHandler)
} }
componentDidUpdate (prevProps) { componentDidUpdate (prevProps) {
@@ -55,6 +71,7 @@ export default class MarkdownPreview extends React.Component {
el.removeEventListener('click', goExternal) el.removeEventListener('click', goExternal)
}) })
let { value, fontFamily, fontSize, codeBlockFontFamily, lineNumber, codeBlockTheme } = this.props let { value, fontFamily, fontSize, codeBlockFontFamily, lineNumber, codeBlockTheme } = this.props
fontFamily = _.isString(fontFamily) && fontFamily.trim().length > 0 fontFamily = _.isString(fontFamily) && fontFamily.trim().length > 0
? [fontFamily].concat(defaultFontFamily) ? [fontFamily].concat(defaultFontFamily)
@@ -95,7 +112,7 @@ export default class MarkdownPreview extends React.Component {
this.refs.root.contentWindow.document.body.innerHTML = markdown(value) this.refs.root.contentWindow.document.body.innerHTML = markdown(value)
Array.prototype.forEach.call(this.refs.root.contentWindow.document.querySelectorAll('a'), (el) => { Array.prototype.forEach.call(this.refs.root.contentWindow.document.querySelectorAll('a'), (el) => {
el.addEventListener('click', goExternal) el.addEventListener('mousedown', goExternal)
}) })
} }