mirror of
https://github.com/BoostIo/Boostnote
synced 2025-12-13 09:46:22 +00:00
simplified search input & fixed focus loss issue
This commit is contained in:
@@ -66,9 +66,6 @@ class MarkdownNoteDetail extends React.Component {
|
|||||||
})
|
})
|
||||||
ee.on('hotkey:deletenote', this.handleDeleteNote.bind(this))
|
ee.on('hotkey:deletenote', this.handleDeleteNote.bind(this))
|
||||||
ee.on('code:generate-toc', this.generateToc)
|
ee.on('code:generate-toc', this.generateToc)
|
||||||
|
|
||||||
// Focus content if using blur or double click
|
|
||||||
if (this.state.switchPreview === 'BLUR' || this.state.switchPreview === 'DBL_CLICK') this.focus()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
componentWillReceiveProps (nextProps) {
|
componentWillReceiveProps (nextProps) {
|
||||||
@@ -83,6 +80,16 @@ class MarkdownNoteDetail extends React.Component {
|
|||||||
if (this.refs.tags) this.refs.tags.reset()
|
if (this.refs.tags) this.refs.tags.reset()
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Focus content if using blur or double click
|
||||||
|
// --> Moved here from componentDidMount so a re-render during search won't set focus to the editor
|
||||||
|
const {switchPreview} = nextProps.config.editor
|
||||||
|
this.setState({
|
||||||
|
switchPreview
|
||||||
|
})
|
||||||
|
if (switchPreview === 'BLUR' || switchPreview === 'DBL_CLICK') {
|
||||||
|
this.focus()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
componentWillUnmount () {
|
componentWillUnmount () {
|
||||||
|
|||||||
@@ -26,8 +26,18 @@ class TopBar extends React.Component {
|
|||||||
}
|
}
|
||||||
|
|
||||||
this.codeInitHandler = this.handleCodeInit.bind(this)
|
this.codeInitHandler = this.handleCodeInit.bind(this)
|
||||||
|
this.handleKeyDown = this.handleKeyDown.bind(this)
|
||||||
|
this.handleSearchFocus = this.handleSearchFocus.bind(this)
|
||||||
|
this.handleSearchBlur = this.handleSearchBlur.bind(this)
|
||||||
|
this.handleSearchChange = this.handleSearchChange.bind(this)
|
||||||
|
|
||||||
this.updateKeyword = debounce(this.updateKeyword, 1000 / 60, {
|
this.debouncedUpdateKeyword = debounce((keyword) => {
|
||||||
|
this.context.router.push(`/searched/${encodeURIComponent(keyword)}`)
|
||||||
|
this.setState({
|
||||||
|
search: keyword
|
||||||
|
})
|
||||||
|
ee.emit('top:search', keyword)
|
||||||
|
}, 1000 / 60, {
|
||||||
maxWait: 1000 / 8
|
maxWait: 1000 / 8
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@@ -62,12 +72,6 @@ class TopBar extends React.Component {
|
|||||||
}
|
}
|
||||||
|
|
||||||
handleKeyDown (e) {
|
handleKeyDown (e) {
|
||||||
// reset states
|
|
||||||
this.setState({
|
|
||||||
isAlphabet: false,
|
|
||||||
isIME: false
|
|
||||||
})
|
|
||||||
|
|
||||||
// Clear search on ESC
|
// Clear search on ESC
|
||||||
if (e.keyCode === 27) {
|
if (e.keyCode === 27) {
|
||||||
return this.handleSearchClearButton(e)
|
return this.handleSearchClearButton(e)
|
||||||
@@ -84,51 +88,11 @@ class TopBar extends React.Component {
|
|||||||
ee.emit('list:prior')
|
ee.emit('list:prior')
|
||||||
e.preventDefault()
|
e.preventDefault()
|
||||||
}
|
}
|
||||||
|
|
||||||
// When the key is an alphabet, del, enter or ctr
|
|
||||||
if (e.keyCode <= 90 || e.keyCode >= 186 && e.keyCode <= 222) {
|
|
||||||
this.setState({
|
|
||||||
isAlphabet: true
|
|
||||||
})
|
|
||||||
// When the key is an IME input (Japanese, Chinese)
|
|
||||||
} else if (e.keyCode === 229) {
|
|
||||||
this.setState({
|
|
||||||
isIME: true
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
handleKeyUp (e) {
|
|
||||||
// reset states
|
|
||||||
this.setState({
|
|
||||||
isConfirmTranslation: false
|
|
||||||
})
|
|
||||||
|
|
||||||
// When the key is translation confirmation (Enter, Space)
|
|
||||||
if (this.state.isIME && (e.keyCode === 32 || e.keyCode === 13)) {
|
|
||||||
this.setState({
|
|
||||||
isConfirmTranslation: true
|
|
||||||
})
|
|
||||||
const keyword = this.refs.searchInput.value
|
|
||||||
this.updateKeyword(keyword)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
handleSearchChange (e) {
|
handleSearchChange (e) {
|
||||||
if (this.state.isAlphabet || this.state.isConfirmTranslation) {
|
const keyword = this.refs.searchInput.value
|
||||||
const keyword = this.refs.searchInput.value
|
this.debouncedUpdateKeyword(keyword)
|
||||||
this.updateKeyword(keyword)
|
|
||||||
} else {
|
|
||||||
e.preventDefault()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
updateKeyword (keyword) {
|
|
||||||
this.context.router.push(`/searched/${encodeURIComponent(keyword)}`)
|
|
||||||
this.setState({
|
|
||||||
search: keyword
|
|
||||||
})
|
|
||||||
ee.emit('top:search', keyword)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
handleSearchFocus (e) {
|
handleSearchFocus (e) {
|
||||||
@@ -136,6 +100,7 @@ class TopBar extends React.Component {
|
|||||||
isSearching: true
|
isSearching: true
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
handleSearchBlur (e) {
|
handleSearchBlur (e) {
|
||||||
e.stopPropagation()
|
e.stopPropagation()
|
||||||
|
|
||||||
@@ -178,24 +143,23 @@ class TopBar extends React.Component {
|
|||||||
<div styleName='control'>
|
<div styleName='control'>
|
||||||
<div styleName='control-search'>
|
<div styleName='control-search'>
|
||||||
<div styleName='control-search-input'
|
<div styleName='control-search-input'
|
||||||
onFocus={(e) => this.handleSearchFocus(e)}
|
onFocus={this.handleSearchFocus}
|
||||||
onBlur={(e) => this.handleSearchBlur(e)}
|
onBlur={this.handleSearchBlur}
|
||||||
tabIndex='-1'
|
tabIndex='-1'
|
||||||
ref='search'
|
ref='search'
|
||||||
>
|
>
|
||||||
<input
|
<input
|
||||||
ref='searchInput'
|
ref='searchInput'
|
||||||
value={this.state.search}
|
value={this.state.search}
|
||||||
onChange={(e) => this.handleSearchChange(e)}
|
onChange={(e) => this.debouncedUpdateKeyword(this.refs.searchInput.value)}
|
||||||
onKeyDown={(e) => this.handleKeyDown(e)}
|
onKeyDown={this.handleKeyDown}
|
||||||
onKeyUp={(e) => this.handleKeyUp(e)}
|
|
||||||
placeholder={i18n.__('Search')}
|
placeholder={i18n.__('Search')}
|
||||||
type='text'
|
type='text'
|
||||||
className='searchInput'
|
className='searchInput'
|
||||||
/>
|
/>
|
||||||
{this.state.search !== '' &&
|
{this.state.search !== '' &&
|
||||||
<button styleName='control-search-input-clear'
|
<button styleName='control-search-input-clear'
|
||||||
onClick={(e) => this.handleSearchClearButton(e)}
|
onClick={this.handleSearchChange}
|
||||||
>
|
>
|
||||||
<i className='fa fa-fw fa-times' />
|
<i className='fa fa-fw fa-times' />
|
||||||
<span styleName='control-search-input-clear-tooltip'>{i18n.__('Clear Search')}</span>
|
<span styleName='control-search-input-clear-tooltip'>{i18n.__('Clear Search')}</span>
|
||||||
|
|||||||
Reference in New Issue
Block a user