Skip to content
57 changes: 31 additions & 26 deletions browser/main/NoteList/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ class NoteList extends React.Component {

this.state = {
}

this.contextNotes = []
}

componentDidMount () {
Expand Down Expand Up @@ -90,6 +92,7 @@ class NoteList extends React.Component {

if (this.notes.length > 0 && location.query.key == null) {
let { router } = this.context
if (!location.pathname.match(/\/searched/)) this.contextNotes = this.getContextNotes()
router.replace({
pathname: location.pathname,
query: {
Expand Down Expand Up @@ -234,48 +237,50 @@ class NoteList extends React.Component {
let { router } = this.context

if (location.pathname.match(/\/home/)) {
return data.noteMap.map((note) => note)
const allNotes = data.noteMap.map((note) => note)
this.contextNotes = allNotes
return allNotes
}

if (location.pathname.match(/\/starred/)) {
return data.starredSet.toJS()
.map((uniqueKey) => data.noteMap.get(uniqueKey))
const starredNotes = data.starredSet.toJS().map((uniqueKey) => data.noteMap.get(uniqueKey))
this.contextNotes = starredNotes
return starredNotes
}

if (location.pathname.match(/\/searched/)) {
const searchInputText = document.getElementsByClassName('searchInput')[0].value
if (searchInputText === '') {
router.push('/home')
return this.contextNotes
}
return searchFromNotes(this.notes, searchInputText)
return searchFromNotes(this.contextNotes, searchInputText)
}

if (location.pathname.match(/\/trashed/)) {
return data.trashedSet.toJS()
.map((uniqueKey) => data.noteMap.get(uniqueKey))
const trashedNotes = data.trashedSet.toJS().map((uniqueKey) => data.noteMap.get(uniqueKey))
this.contextNotes = trashedNotes
return trashedNotes
}

let storageKey = params.storageKey
let folderKey = params.folderKey
let storage = data.storageMap.get(storageKey)
if (storage == null) return []

let folder = _.find(storage.folders, {key: folderKey})
if (folder == null) {
let storageNoteSet = data.storageNoteMap
.get(storage.key)
if (storageNoteSet == null) storageNoteSet = []
return storageNoteSet
.map((uniqueKey) => data.noteMap.get(uniqueKey))
}
return this.getContextNotes()
}

let folderNoteKeyList = data.folderNoteMap
.get(storage.key + '-' + folder.key)
// get notes in the current folder
getContextNotes () {
const { data, params } = this.props
const storageKey = params.storageKey
const folderKey = params.folderKey
const storage = data.storageMap.get(storageKey)
if (storage === undefined) return []

const folder = _.find(storage.folders, {key: folderKey})
if (folder === undefined) {
const storageNoteSet = data.storageNoteMap.get(storage.key) || []
return storageNoteSet.map((uniqueKey) => data.noteMap.get(uniqueKey))
}

return folderNoteKeyList != null
? folderNoteKeyList
.map((uniqueKey) => data.noteMap.get(uniqueKey))
: []
const folderNoteKeyList = data.folderNoteMap.get(`${storage.key}-${folder.key}`) || []
return folderNoteKeyList.map((uniqueKey) => data.noteMap.get(uniqueKey))
}

handleNoteClick (e, uniqueKey) {
Expand Down
54 changes: 51 additions & 3 deletions browser/main/TopBar/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@ class TopBar extends React.Component {
this.state = {
search: '',
searchOptions: [],
isSearching: false
isSearching: false,
isAlphabet: false,
isIME: false,
isConfirmTranslation: false
}

this.focusSearchHandler = () => {
Expand All @@ -34,9 +37,52 @@ class TopBar extends React.Component {
ee.off('top:focus-search', this.focusSearchHandler)
}

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

// When the key is an alphabet, del, enter or ctr
if (e.keyCode <= 90) {
Copy link
Contributor Author

@asmsuechan asmsuechan Oct 12, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This does not work on various keyCodes... Need to change.

German: üöä
French: ù
Spanish: ñ
Russian: (work fine)
Chinese: (work fine by using pinyin keyboard)

Copy link
Contributor Author

@asmsuechan asmsuechan Oct 12, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I decided to extend the keyCode scope to e.keyCode<=90 || 186<=e.keyCode<=222 according to http://www.javascripter.net/faq/keycodes.htm.

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)
if (this.state.isIME && e.keyCode === 13) {
this.setState({
isConfirmTranslation: true
})
router.push('/searched')
this.setState({
search: this.refs.searchInput.value
})
}
}

handleSearchChange (e) {
let { router } = this.context
router.push('/searched')
const { router } = this.context
if (this.state.isAlphabet || this.state.isConfirmTranslation) {
router.push('/searched')
} else {
e.preventDefault()
}
this.setState({
search: this.refs.searchInput.value
})
Expand Down Expand Up @@ -93,6 +139,8 @@ class TopBar extends React.Component {
ref='searchInput'
value={this.state.search}
onChange={(e) => this.handleSearchChange(e)}
onKeyDown={(e) => this.handleKeyDown(e)}
onKeyUp={(e) => this.handleKeyUp(e)}
placeholder='Search'
type='text'
className='searchInput'
Expand Down