diff --git a/browser/lib/search.js b/browser/lib/search.js new file mode 100644 index 00000000..38a1ef0b --- /dev/null +++ b/browser/lib/search.js @@ -0,0 +1,62 @@ +export default function searchFromNotes(data, search) { + let notes = data.noteMap.map((note) => note) + if (search.trim().length === 0) return [] + let searchBlocks = search.split(' ') + searchBlocks.forEach((block) => { + if (block.match(/^!#.+/)) { + let tag = block.match(/^!#(.+)/)[1] + let regExp = new RegExp(_.escapeRegExp(tag), 'i') + notes = notes + .filter((note) => { + if (!_.isArray(note.tags)) return false + return note.tags.some((_tag) => { + return _tag.match(regExp) + }) + }) + } else if (block.match(/^!.+/)) { + let block = block.match(/^!(.+)/)[1] + let regExp = new RegExp(_.escapeRegExp(block), 'i') + notes = notes.filter((note) => { + if (!_.isArray(note.tags) || !note.tags.some((_tag) => { + return _tag.match(regExp) + })) { + return true + } + if (note.type === 'SNIPPET_NOTE') { + return !note.description.match(regExp) + } else if (note.type === 'MARKDOWN_NOTE') { + return !note.content.match(regExp) + } + return false + }) + } else if (block.match(/^#.+/)) { + let tag = block.match(/#(.+)/)[1] + let regExp = new RegExp(_.escapeRegExp(tag), 'i') + notes = notes + .filter((note) => { + if (!_.isArray(note.tags)) return false + return note.tags.some((_tag) => { + return _tag.match(regExp) + }) + }) + } else { + let regExp = new RegExp(_.escapeRegExp(block), 'i') + notes = notes.filter((note) => { + if (_.isArray(note.tags) && note.tags.some((_tag) => { + return _tag.match(regExp) + })) { + return true + } + if (note.type === 'SNIPPET_NOTE') { + return note.description.match(regExp) + } else if (note.type === 'MARKDOWN_NOTE') { + return note.content.match(regExp) + } + return false + }) + } +}) + + return notes + +} diff --git a/browser/main/NoteList/index.js b/browser/main/NoteList/index.js index c8fafd1a..59e50b99 100644 --- a/browser/main/NoteList/index.js +++ b/browser/main/NoteList/index.js @@ -8,6 +8,7 @@ import dataApi from 'browser/main/lib/dataApi' import ConfigManager from 'browser/main/lib/ConfigManager' import NoteItem from 'browser/components/NoteItem' import NoteItemSimple from 'browser/components/NoteItemSimple' +import searchFromNotes from 'browser/lib/search' const { remote } = require('electron') const { Menu, MenuItem, dialog } = remote @@ -213,7 +214,7 @@ class NoteList extends React.Component { } if (location.pathname.match(/\/searched/)) { - return this.getSearchNotes() + return searchFromNotes(this.props.data, document.getElementsByClassName('TopBar__control-search-input___browser-main-TopBar-')[0].childNodes[0].value) } let storageKey = params.storageKey @@ -353,71 +354,6 @@ class NoteList extends React.Component { }) } - getSearchNotes () { - let { data } = this.props - let search = document.getElementsByClassName('TopBar__control-search-input___browser-main-TopBar-')[0].childNodes[0].value - let notes = data.noteMap.map((note) => note) - if (search.trim().length === 0) return [] - let searchBlocks = search.split(' ') - searchBlocks.forEach((block) => { - if (block.match(/^!#.+/)) { - let tag = block.match(/^!#(.+)/)[1] - let regExp = new RegExp(_.escapeRegExp(tag), 'i') - notes = notes - .filter((note) => { - if (!_.isArray(note.tags)) return false - return note.tags.some((_tag) => { - return _tag.match(regExp) - }) - }) - } else if (block.match(/^!.+/)) { - let block = block.match(/^!(.+)/)[1] - let regExp = new RegExp(_.escapeRegExp(block), 'i') - notes = notes.filter((note) => { - if (!_.isArray(note.tags) || !note.tags.some((_tag) => { - return _tag.match(regExp) - })) { - return true - } - if (note.type === 'SNIPPET_NOTE') { - return !note.description.match(regExp) - } else if (note.type === 'MARKDOWN_NOTE') { - return !note.content.match(regExp) - } - return false - }) - } else if (block.match(/^#.+/)) { - let tag = block.match(/#(.+)/)[1] - let regExp = new RegExp(_.escapeRegExp(tag), 'i') - notes = notes - .filter((note) => { - if (!_.isArray(note.tags)) return false - return note.tags.some((_tag) => { - return _tag.match(regExp) - }) - }) - } else { - let regExp = new RegExp(_.escapeRegExp(block), 'i') - notes = notes.filter((note) => { - if (_.isArray(note.tags) && note.tags.some((_tag) => { - return _tag.match(regExp) - })) { - return true - } - if (note.type === 'SNIPPET_NOTE') { - return note.description.match(regExp) - } else if (note.type === 'MARKDOWN_NOTE') { - return note.content.match(regExp) - } - return false - }) - } - }) - - return notes - - } - render () { let { location, notes, config } = this.props let sortFunc = config.sortBy === 'CREATED_AT'