Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix the search note functionality #2676

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
55 changes: 18 additions & 37 deletions browser/main/TopBar/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,7 @@ class TopBar extends React.Component {
search: '',
searchOptions: [],
isSearching: false,
isAlphabet: false,
isIME: false,
isConfirmTranslation: false
onComposition: false
}

this.focusSearchHandler = () => {
Expand Down Expand Up @@ -57,12 +55,6 @@ class TopBar extends React.Component {
}

handleKeyDown (e) {
// reset states
this.setState({
isAlphabet: false,
isIME: false
})

// Clear search on ESC
if (e.keyCode === 27) {
return this.handleSearchClearButton(e)
Expand All @@ -79,52 +71,34 @@ class TopBar extends React.Component {
ee.emit('list:prior')
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) {
const { router } = this.context
// 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
})
// When the IMEI is done input or Enter or Space is entered
if (!this.state.onComposition || (e.keyCode === 32 || e.keyCode === 13)) {
const keyword = this.refs.searchInput.value
router.push(`/searched/${encodeURIComponent(keyword)}`)
this.setState({
search: keyword
search: keyword,
onComposition: false
})
}
}

handleSearchChange (e) {
const { router } = this.context
const keyword = this.refs.searchInput.value
if (this.state.isAlphabet || this.state.isConfirmTranslation) {
this.setState({
search: keyword
})
if (!this.state.onComposition) {
router.push(`/searched/${encodeURIComponent(keyword)}`)

ee.emit('top:search', keyword)
} else {
e.preventDefault()
}
this.setState({
search: keyword
})
ee.emit('top:search', keyword)
}

handleSearchFocus (e) {
Expand Down Expand Up @@ -164,6 +138,11 @@ class TopBar extends React.Component {
ee.emit('top:search', this.refs.searchInput.value)
}

handleComposition (event) {
const onComposition = !(event.type === 'compositionend')
this.setState({onComposition})
}

render () {
const { config, style, location } = this.props
return (
Expand All @@ -185,6 +164,8 @@ class TopBar extends React.Component {
onChange={(e) => this.handleSearchChange(e)}
onKeyDown={(e) => this.handleKeyDown(e)}
onKeyUp={(e) => this.handleKeyUp(e)}
onCompositionStart={(e) => this.handleComposition(e)}
onCompositionEnd={(e) => this.handleComposition(e)}
placeholder={i18n.__('Search')}
type='text'
className='searchInput'
Expand Down