From b4f5913a803945fe86c65edb2a38a1e8f760f9ae Mon Sep 17 00:00:00 2001 From: asmsuechan Date: Wed, 11 Oct 2017 00:13:44 +0900 Subject: [PATCH 01/13] Fix to work inputting by IME on search --- browser/main/TopBar/index.js | 52 +++++++++++++++++++++++++++++++++--- 1 file changed, 49 insertions(+), 3 deletions(-) diff --git a/browser/main/TopBar/index.js b/browser/main/TopBar/index.js index c469adb4..0b193623 100644 --- a/browser/main/TopBar/index.js +++ b/browser/main/TopBar/index.js @@ -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 = () => { @@ -34,9 +37,50 @@ class TopBar extends React.Component { ee.off('top:focus-search', this.focusSearchHandler) } + handleKeyDown (e) { + // reset states + this.setState({ + isAlphabet: false, + isIME: false + }) + + if (e.keyCode <= 90) { + this.setState({ + isAlphabet: true, + isIME: false + }) + } else if (e.keyCode === 229) { + this.setState({ + isIME: true + }) + } + } + + + handleKeyUp (e) { + if (this.state.isIME && e.keyCode === 13) { + this.setState({ + isConfirmTranslation: true + }) + let { router } = this.context + router.push('/searched') + this.setState({ + search: this.refs.searchInput.value + }) + } else { + this.setState({ + isConfirmTranslation: false + }) + } + } + handleSearchChange (e) { - let { router } = this.context - router.push('/searched') + if (this.state.isAlphabet || this.state.isConfirmTranslation) { + let { router } = this.context + router.push('/searched') + } else { + e.preventDefault() + } this.setState({ search: this.refs.searchInput.value }) @@ -93,6 +137,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' From 6e480ba14619da4c8187c4c8da6ae2cfdc0740f7 Mon Sep 17 00:00:00 2001 From: asmsuechan Date: Wed, 11 Oct 2017 08:03:08 +0900 Subject: [PATCH 02/13] Add comments --- browser/main/TopBar/index.js | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/browser/main/TopBar/index.js b/browser/main/TopBar/index.js index 0b193623..2688a159 100644 --- a/browser/main/TopBar/index.js +++ b/browser/main/TopBar/index.js @@ -44,11 +44,12 @@ class TopBar extends React.Component { isIME: false }) + // When the key is an alphabet, del, enter or ctr if (e.keyCode <= 90) { this.setState({ - isAlphabet: true, - isIME: false + isAlphabet: true }) + // When the key is an IME input (Japanese, Chinese) } else if (e.keyCode === 229) { this.setState({ isIME: true @@ -58,6 +59,12 @@ class TopBar extends React.Component { handleKeyUp (e) { + // reset states + this.setState({ + isConfirmTranslation: false + }) + + // When the key is translation confirmation (Enter) if (this.state.isIME && e.keyCode === 13) { this.setState({ isConfirmTranslation: true @@ -67,10 +74,6 @@ class TopBar extends React.Component { this.setState({ search: this.refs.searchInput.value }) - } else { - this.setState({ - isConfirmTranslation: false - }) } } From 4418617d3be0c79290f325bb35da4a21851c1c16 Mon Sep 17 00:00:00 2001 From: asmsuechan Date: Wed, 11 Oct 2017 08:22:57 +0900 Subject: [PATCH 03/13] Delete a unnecessary break --- browser/main/TopBar/index.js | 1 - 1 file changed, 1 deletion(-) diff --git a/browser/main/TopBar/index.js b/browser/main/TopBar/index.js index 2688a159..89ab1688 100644 --- a/browser/main/TopBar/index.js +++ b/browser/main/TopBar/index.js @@ -57,7 +57,6 @@ class TopBar extends React.Component { } } - handleKeyUp (e) { // reset states this.setState({ From 2511512d9439dd44c861febc9f3cab821d099234 Mon Sep 17 00:00:00 2001 From: asmsuechan Date: Wed, 11 Oct 2017 09:53:37 +0900 Subject: [PATCH 04/13] Improve context search --- browser/main/NoteList/index.js | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/browser/main/NoteList/index.js b/browser/main/NoteList/index.js index 682c755d..d6c96e7f 100644 --- a/browser/main/NoteList/index.js +++ b/browser/main/NoteList/index.js @@ -51,6 +51,7 @@ class NoteList extends React.Component { this.jumpNoteByHash = this.jumpNoteByHashHandler.bind(this) this.state = { + contextNotes: [] } } @@ -90,6 +91,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.setState({contextNotes: this.getContextNotes()}) router.replace({ pathname: location.pathname, query: { @@ -245,9 +247,9 @@ class NoteList extends React.Component { if (location.pathname.match(/\/searched/)) { const searchInputText = document.getElementsByClassName('searchInput')[0].value if (searchInputText === '') { - router.push('/home') + return this.state.contextNotes } - return searchFromNotes(this.notes, searchInputText) + return searchFromNotes(this.state.contextNotes, searchInputText) } if (location.pathname.match(/\/trashed/)) { @@ -255,6 +257,12 @@ class NoteList extends React.Component { .map((uniqueKey) => data.noteMap.get(uniqueKey)) } + return this.getContextNotes() + } + + // get notes in a specific folder + getContextNotes () { + let { data, params } = this.props let storageKey = params.storageKey let folderKey = params.folderKey let storage = data.storageMap.get(storageKey) From 70468b6b7d79d215e01a4de4167e01631d22dc4f Mon Sep 17 00:00:00 2001 From: asmsuechan Date: Wed, 11 Oct 2017 10:57:30 +0900 Subject: [PATCH 05/13] Change contextNotes a state to a variable because of lifesycle loop --- browser/main/NoteList/index.js | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/browser/main/NoteList/index.js b/browser/main/NoteList/index.js index d6c96e7f..2473f8de 100644 --- a/browser/main/NoteList/index.js +++ b/browser/main/NoteList/index.js @@ -51,8 +51,9 @@ class NoteList extends React.Component { this.jumpNoteByHash = this.jumpNoteByHashHandler.bind(this) this.state = { - contextNotes: [] } + + this.contextNotes = [] } componentDidMount () { @@ -91,7 +92,8 @@ class NoteList extends React.Component { if (this.notes.length > 0 && location.query.key == null) { let { router } = this.context - if (!location.pathname.match(/\/searched/)) this.setState({contextNotes: this.getContextNotes()}) + if (!location.pathname.match(/\/searched/)) this.contextNotes = this.getContextNotes() + console.log(this.contextNotes) router.replace({ pathname: location.pathname, query: { @@ -236,6 +238,7 @@ class NoteList extends React.Component { let { router } = this.context if (location.pathname.match(/\/home/)) { + this.contextNotes = data.noteMap.map((note) => note) return data.noteMap.map((note) => note) } @@ -247,9 +250,9 @@ class NoteList extends React.Component { if (location.pathname.match(/\/searched/)) { const searchInputText = document.getElementsByClassName('searchInput')[0].value if (searchInputText === '') { - return this.state.contextNotes + return this.contextNotes } - return searchFromNotes(this.state.contextNotes, searchInputText) + return searchFromNotes(this.contextNotes, searchInputText) } if (location.pathname.match(/\/trashed/)) { From da6b8c30a01c585ec8d441c6c70fa35c5c9bbfd4 Mon Sep 17 00:00:00 2001 From: asmsuechan Date: Thu, 12 Oct 2017 17:01:25 +0900 Subject: [PATCH 06/13] Improve some --- browser/main/NoteList/index.js | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/browser/main/NoteList/index.js b/browser/main/NoteList/index.js index 2473f8de..3e301da2 100644 --- a/browser/main/NoteList/index.js +++ b/browser/main/NoteList/index.js @@ -238,8 +238,9 @@ class NoteList extends React.Component { let { router } = this.context if (location.pathname.match(/\/home/)) { - this.contextNotes = data.noteMap.map((note) => note) - return data.noteMap.map((note) => note) + const allNotes = data.noteMap.map((note) => note) + this.contextNotes = allNotes + return allNotes } if (location.pathname.match(/\/starred/)) { @@ -263,7 +264,7 @@ class NoteList extends React.Component { return this.getContextNotes() } - // get notes in a specific folder + // get notes in the current folder getContextNotes () { let { data, params } = this.props let storageKey = params.storageKey From df3195fc1e9fce473155c195d6d5600369e8de1b Mon Sep 17 00:00:00 2001 From: asmsuechan Date: Thu, 12 Oct 2017 17:48:57 +0900 Subject: [PATCH 07/13] Refactor getContextNotes() --- browser/main/NoteList/index.js | 31 +++++++++++-------------------- browser/main/TopBar/index.js | 4 ++-- 2 files changed, 13 insertions(+), 22 deletions(-) diff --git a/browser/main/NoteList/index.js b/browser/main/NoteList/index.js index 3e301da2..3f57fdc9 100644 --- a/browser/main/NoteList/index.js +++ b/browser/main/NoteList/index.js @@ -93,7 +93,6 @@ 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() - console.log(this.contextNotes) router.replace({ pathname: location.pathname, query: { @@ -266,28 +265,20 @@ class NoteList extends React.Component { // get notes in the current folder getContextNotes () { - let { data, params } = this.props - let storageKey = params.storageKey - let folderKey = params.folderKey - let storage = data.storageMap.get(storageKey) - if (storage == null) return [] + const { data, params } = this.props + const storageKey = params.storageKey + const folderKey = params.folderKey + const storage = data.storageMap.get(storageKey) + if (storage === undefined) 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)) + 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)) } - let folderNoteKeyList = data.folderNoteMap - .get(storage.key + '-' + folder.key) - - 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) { diff --git a/browser/main/TopBar/index.js b/browser/main/TopBar/index.js index 89ab1688..83bfd582 100644 --- a/browser/main/TopBar/index.js +++ b/browser/main/TopBar/index.js @@ -58,6 +58,7 @@ class TopBar extends React.Component { } handleKeyUp (e) { + const { router } = this.context // reset states this.setState({ isConfirmTranslation: false @@ -68,7 +69,6 @@ class TopBar extends React.Component { this.setState({ isConfirmTranslation: true }) - let { router } = this.context router.push('/searched') this.setState({ search: this.refs.searchInput.value @@ -77,8 +77,8 @@ class TopBar extends React.Component { } handleSearchChange (e) { + const { router } = this.context if (this.state.isAlphabet || this.state.isConfirmTranslation) { - let { router } = this.context router.push('/searched') } else { e.preventDefault() From 93e188d11800f7f704183fd60d4138b832f2b4a2 Mon Sep 17 00:00:00 2001 From: asmsuechan Date: Thu, 12 Oct 2017 20:28:23 +0900 Subject: [PATCH 08/13] Change to be able to search on starred --- browser/main/NoteList/index.js | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/browser/main/NoteList/index.js b/browser/main/NoteList/index.js index 3f57fdc9..6f3860eb 100644 --- a/browser/main/NoteList/index.js +++ b/browser/main/NoteList/index.js @@ -243,8 +243,9 @@ class NoteList extends React.Component { } 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/)) { @@ -256,8 +257,9 @@ class NoteList extends React.Component { } 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 } return this.getContextNotes() From 49243a8010ab2c600f554ebb670d163b45d6a4b1 Mon Sep 17 00:00:00 2001 From: asmsuechan Date: Thu, 12 Oct 2017 23:15:03 +0900 Subject: [PATCH 09/13] Add to handle space key on search --- browser/main/TopBar/index.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/browser/main/TopBar/index.js b/browser/main/TopBar/index.js index 83bfd582..6dd299fe 100644 --- a/browser/main/TopBar/index.js +++ b/browser/main/TopBar/index.js @@ -64,8 +64,8 @@ class TopBar extends React.Component { isConfirmTranslation: false }) - // When the key is translation confirmation (Enter) - if (this.state.isIME && e.keyCode === 13) { + // When the key is translation confirmation (Enter, Space) + if (this.state.isIME && (e.keyCode === 32 || e.keyCode === 13)) { this.setState({ isConfirmTranslation: true }) From 15dc424ade209b6544813712180e8d7870879593 Mon Sep 17 00:00:00 2001 From: asmsuechan Date: Thu, 12 Oct 2017 23:24:31 +0900 Subject: [PATCH 10/13] Change keycode handling scope due to global keyboard support --- browser/main/TopBar/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/browser/main/TopBar/index.js b/browser/main/TopBar/index.js index 6dd299fe..2c34ebf2 100644 --- a/browser/main/TopBar/index.js +++ b/browser/main/TopBar/index.js @@ -45,7 +45,7 @@ class TopBar extends React.Component { }) // When the key is an alphabet, del, enter or ctr - if (e.keyCode <= 90) { + if (e.keyCode <= 90 || e.keyCode >= 186 && e.keyCode <= 222) { this.setState({ isAlphabet: true }) From caa5deac4eb4989ef38646bf10c16552626bc4e4 Mon Sep 17 00:00:00 2001 From: asmsuechan Date: Fri, 13 Oct 2017 10:37:43 +0900 Subject: [PATCH 11/13] Change searching by tag --- browser/lib/search.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/browser/lib/search.js b/browser/lib/search.js index 1914bb81..65ec33a9 100644 --- a/browser/lib/search.js +++ b/browser/lib/search.js @@ -2,15 +2,15 @@ import _ from 'lodash' export default function searchFromNotes (notes, search) { if (search.trim().length === 0) return [] - let searchBlocks = search.split(' ') + const searchBlocks = search.split(' ').filter(block => { return block !== '' }) + let foundNotes = [] searchBlocks.forEach((block) => { + foundNotes = findByWord(notes, block) if (block.match(/^#.+/)) { - notes = findByTag(notes, block) - } else { - notes = findByWord(notes, block) + foundNotes = foundNotes.concat(findByTag(notes, block)) } }) - return notes + return foundNotes } function findByTag (notes, block) { From 78b12ae686c0464672648fe5b0391c15273c0dcd Mon Sep 17 00:00:00 2001 From: asmsuechan Date: Fri, 13 Oct 2017 10:48:58 +0900 Subject: [PATCH 12/13] Add test --- tests/lib/search-test.js | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/tests/lib/search-test.js b/tests/lib/search-test.js index e99a3612..1550a08f 100644 --- a/tests/lib/search-test.js +++ b/tests/lib/search-test.js @@ -6,26 +6,30 @@ import _ from 'lodash' const pickContents = (notes) => notes.map((note) => { return note.content }) let notes = [] -let note1, note2 +let note1, note2, note3 test.before(t => { const data1 = { type: 'MARKDOWN_NOTE', content: 'content1', tags: ['tag1'] } const data2 = { type: 'MARKDOWN_NOTE', content: 'content1\ncontent2', tags: ['tag1', 'tag2'] } + const data3 = { type: 'MARKDOWN_NOTE', content: '#content4', tags: ['tag1'] } + note1 = dummyNote(data1) note2 = dummyNote(data2) + note3 = dummyNote(data3) - notes = [note1, note2] + notes = [note1, note2, note3] }) test('it can find notes by tags or words', t => { // [input, expected content (Array)] const testCases = [ - ['#tag1', [note1.content, note2.content]], + ['#tag1', [note1.content, note2.content, note3.content]], ['#tag1 #tag2', [note2.content]], ['#tag1 #tag2 #tag3', []], ['content1', [note1.content, note2.content]], ['content1 content2', [note2.content]], - ['content1 content2 content3', []] + ['content1 content2 content3', []], + ['#content4', [note3.content]] ] testCases.forEach((testCase) => { From 7ab482184bc4d1dea9cfbd79e03f36c562c7b752 Mon Sep 17 00:00:00 2001 From: asmsuechan Date: Fri, 13 Oct 2017 11:40:10 +0900 Subject: [PATCH 13/13] Improve tag search by changing the logic --- browser/lib/search.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/browser/lib/search.js b/browser/lib/search.js index 65ec33a9..d102e381 100644 --- a/browser/lib/search.js +++ b/browser/lib/search.js @@ -3,9 +3,10 @@ import _ from 'lodash' export default function searchFromNotes (notes, search) { if (search.trim().length === 0) return [] const searchBlocks = search.split(' ').filter(block => { return block !== '' }) - let foundNotes = [] + + let foundNotes = findByWord(notes, searchBlocks[0]) searchBlocks.forEach((block) => { - foundNotes = findByWord(notes, block) + foundNotes = findByWord(foundNotes, block) if (block.match(/^#.+/)) { foundNotes = foundNotes.concat(findByTag(notes, block)) }