Skip to content

Commit b4251a7

Browse files
authored
Merge pull request #3037 from AWolf81/fix-search-issue-unicode
Simplified search input & fixed chinese character input
2 parents 6736a08 + 639bfbe commit b4251a7

File tree

4 files changed

+52
-67
lines changed

4 files changed

+52
-67
lines changed

browser/main/Detail/MarkdownNoteDetail.js

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,6 @@ class MarkdownNoteDetail extends React.Component {
6767
})
6868
ee.on('hotkey:deletenote', this.handleDeleteNote.bind(this))
6969
ee.on('code:generate-toc', this.generateToc)
70-
71-
// Focus content if using blur or double click
72-
if (this.state.switchPreview === 'BLUR' || this.state.switchPreview === 'DBL_CLICK') this.focus()
7370
}
7471

7572
componentWillReceiveProps (nextProps) {
@@ -84,6 +81,20 @@ class MarkdownNoteDetail extends React.Component {
8481
if (this.refs.tags) this.refs.tags.reset()
8582
})
8683
}
84+
85+
// Focus content if using blur or double click
86+
// --> Moved here from componentDidMount so a re-render during search won't set focus to the editor
87+
const {switchPreview} = nextProps.config.editor
88+
89+
if (this.state.switchPreview !== switchPreview) {
90+
this.setState({
91+
switchPreview
92+
})
93+
if (switchPreview === 'BLUR' || switchPreview === 'DBL_CLICK') {
94+
console.log('setting focus', switchPreview)
95+
this.focus()
96+
}
97+
}
8798
}
8899

89100
componentWillUnmount () {

browser/main/TopBar/index.js

Lines changed: 29 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ import ee from 'browser/main/lib/eventEmitter'
77
import NewNoteButton from 'browser/main/NewNoteButton'
88
import i18n from 'browser/lib/i18n'
99
import debounce from 'lodash/debounce'
10+
import CInput from 'react-composition-input'
1011
import { push } from 'connected-react-router'
11-
import queryString from 'query-string'
1212

1313
class TopBar extends React.Component {
1414
constructor (props) {
@@ -17,21 +17,29 @@ class TopBar extends React.Component {
1717
this.state = {
1818
search: '',
1919
searchOptions: [],
20-
isSearching: false,
21-
isAlphabet: false,
22-
isIME: false,
23-
isConfirmTranslation: false
20+
isSearching: false
2421
}
2522

23+
const { dispatch } = this.props
24+
2625
this.focusSearchHandler = () => {
2726
this.handleOnSearchFocus()
2827
}
2928

3029
this.codeInitHandler = this.handleCodeInit.bind(this)
31-
this.updateKeyword = this.updateKeyword.bind(this)
30+
this.handleKeyDown = this.handleKeyDown.bind(this)
31+
this.handleSearchFocus = this.handleSearchFocus.bind(this)
32+
this.handleSearchBlur = this.handleSearchBlur.bind(this)
33+
this.handleSearchChange = this.handleSearchChange.bind(this)
3234
this.handleSearchClearButton = this.handleSearchClearButton.bind(this)
3335

34-
this.updateKeyword = debounce(this.updateKeyword, 1000 / 60, {
36+
this.debouncedUpdateKeyword = debounce((keyword) => {
37+
dispatch(push(`/searched/${encodeURIComponent(keyword)}`))
38+
this.setState({
39+
search: keyword
40+
})
41+
ee.emit('top:search', keyword)
42+
}, 1000 / 60, {
3543
maxWait: 1000 / 8
3644
})
3745
}
@@ -66,11 +74,10 @@ class TopBar extends React.Component {
6674
}
6775

6876
handleKeyDown (e) {
69-
// reset states
70-
this.setState({
71-
isAlphabet: false,
72-
isIME: false
73-
})
77+
// Re-apply search field on ENTER key
78+
if (e.keyCode === 13) {
79+
this.debouncedUpdateKeyword(e.target.value)
80+
}
7481

7582
// Clear search on ESC
7683
if (e.keyCode === 27) {
@@ -88,59 +95,19 @@ class TopBar extends React.Component {
8895
ee.emit('list:prior')
8996
e.preventDefault()
9097
}
91-
92-
// When the key is an alphabet, del, enter or ctr
93-
if (e.keyCode <= 90 || e.keyCode >= 186 && e.keyCode <= 222) {
94-
this.setState({
95-
isAlphabet: true
96-
})
97-
// When the key is an IME input (Japanese, Chinese)
98-
} else if (e.keyCode === 229) {
99-
this.setState({
100-
isIME: true
101-
})
102-
}
103-
}
104-
105-
handleKeyUp (e) {
106-
// reset states
107-
this.setState({
108-
isConfirmTranslation: false
109-
})
110-
111-
// When the key is translation confirmation (Enter, Space)
112-
if (this.state.isIME && (e.keyCode === 32 || e.keyCode === 13)) {
113-
this.setState({
114-
isConfirmTranslation: true
115-
})
116-
const keyword = this.refs.searchInput.value
117-
this.updateKeyword(keyword)
118-
}
11998
}
12099

121100
handleSearchChange (e) {
122-
if (this.state.isAlphabet || this.state.isConfirmTranslation) {
123-
const keyword = this.refs.searchInput.value
124-
this.updateKeyword(keyword)
125-
} else {
126-
e.preventDefault()
127-
}
128-
}
129-
130-
updateKeyword (keyword) {
131-
const { dispatch } = this.props
132-
dispatch(push(`/searched/${encodeURIComponent(keyword)}`))
133-
this.setState({
134-
search: keyword
135-
})
136-
ee.emit('top:search', keyword)
101+
const keyword = e.target.value
102+
this.debouncedUpdateKeyword(keyword)
137103
}
138104

139105
handleSearchFocus (e) {
140106
this.setState({
141107
isSearching: true
142108
})
143109
}
110+
144111
handleSearchBlur (e) {
145112
e.stopPropagation()
146113

@@ -170,7 +137,7 @@ class TopBar extends React.Component {
170137
}
171138

172139
handleCodeInit () {
173-
ee.emit('top:search', this.refs.searchInput.value)
140+
ee.emit('top:search', this.refs.searchInput.value || '')
174141
}
175142

176143
render () {
@@ -183,24 +150,23 @@ class TopBar extends React.Component {
183150
<div styleName='control'>
184151
<div styleName='control-search'>
185152
<div styleName='control-search-input'
186-
onFocus={(e) => this.handleSearchFocus(e)}
187-
onBlur={(e) => this.handleSearchBlur(e)}
153+
onFocus={this.handleSearchFocus}
154+
onBlur={this.handleSearchBlur}
188155
tabIndex='-1'
189156
ref='search'
190157
>
191-
<input
158+
<CInput
192159
ref='searchInput'
193160
value={this.state.search}
194-
onChange={(e) => this.handleSearchChange(e)}
195-
onKeyDown={(e) => this.handleKeyDown(e)}
196-
onKeyUp={(e) => this.handleKeyUp(e)}
161+
onInputChange={this.handleSearchChange}
162+
onKeyDown={this.handleKeyDown}
197163
placeholder={i18n.__('Search')}
198164
type='text'
199165
className='searchInput'
200166
/>
201167
{this.state.search !== '' &&
202168
<button styleName='control-search-input-clear'
203-
onClick={(e) => this.handleSearchClearButton(e)}
169+
onClick={this.handleSearchClearButton}
204170
>
205171
<i className='fa fa-fw fa-times' />
206172
<span styleName='control-search-input-clear-tooltip'>{i18n.__('Clear Search')}</span>

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "boost",
33
"productName": "Boostnote",
4-
"version": "0.11.17",
4+
"version": "0.11.17",
55
"main": "index.js",
66
"description": "Boostnote",
77
"license": "GPL-3.0",
@@ -105,6 +105,7 @@
105105
"react-autosuggest": "^9.4.0",
106106
"react-codemirror": "^1.0.0",
107107
"react-color": "^2.2.2",
108+
"react-composition-input": "^1.1.1",
108109
"react-debounce-render": "^4.0.1",
109110
"react-dom": "^16.8.6",
110111
"react-image-carousel": "^2.0.18",

yarn.lock

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7746,6 +7746,13 @@ react-color@^2.2.2:
77467746
reactcss "^1.2.0"
77477747
tinycolor2 "^1.4.1"
77487748

7749+
react-composition-input@^1.1.1:
7750+
version "1.1.1"
7751+
resolved "https://registry.yarnpkg.com/react-composition-input/-/react-composition-input-1.1.1.tgz#51fc711f8b1c7d11e39210639175f0b48de44aff"
7752+
integrity sha512-xzRAUvsrEdSjI1tQXu3ouPHkHVZnunx6OoAFsv4YxVV6fIBHc9XZuxkmJwoxSatPxJ6WN94k91PBWQTsL6h/ZA==
7753+
dependencies:
7754+
prop-types "^15.6.2"
7755+
77497756
react-css-modules@^4.7.9:
77507757
version "4.7.9"
77517758
resolved "https://registry.yarnpkg.com/react-css-modules/-/react-css-modules-4.7.9.tgz#459235e149a0df7a62b092ae079d53cb0b6154ee"

0 commit comments

Comments
 (0)