From 89b5a1be6789912f917fc13521d6d0d5079ca6bf Mon Sep 17 00:00:00 2001 From: Seong Jae Lee Date: Sat, 11 Apr 2015 00:40:52 -0700 Subject: [PATCH] Add query title selection feature and read notes in the subdirectories If there is a match in the title, it automatically selects the title. However, this does not considers auto-filling. Without this auto-filling, we cannot discern between a note selection and a note creating. If we implement auto-filling later, then the non-initial partial title matching filter should be turned off. Other changes that is not related to this CL. - Sort notes by modified time. - Delete debug messages. --- lib/notational-velocity-view.coffee | 71 ++++++++++++++++------------- 1 file changed, 40 insertions(+), 31 deletions(-) diff --git a/lib/notational-velocity-view.coffee b/lib/notational-velocity-view.coffee index 6338929..28bd586 100644 --- a/lib/notational-velocity-view.coffee +++ b/lib/notational-velocity-view.coffee @@ -14,12 +14,21 @@ class NotationalVelocityView extends SelectListView if filterQuery.length == 0 return null - titleQuery = filterQuery.toLowerCase() - titleItems = @items - .filter (x) -> x.title.toLowerCase()[...titleQuery.length] is titleQuery - .sort (x, y) -> x.modified > y.modified - titleItem = if titleItems.length > 0 then titleItems[0] else null - return titleItem + titlePatterns = [ + ///^#{filterQuery}$///i, + ///^#{filterQuery}///i, + ///(\s|\\|\/)#{filterQuery}///i, + ] + + for titlePattern in titlePatterns + titleItems = @items + .filter (x) -> x.title.match(titlePattern) != null + .sort (x, y) -> if x.modified.getTime() <= y.modified.getTime() then 1 else -1 + titleItem = if titleItems.length > 0 then titleItems[0] else null + if titleItem != null + return titleItem + + return null filter: (filterQuery) -> if filterQuery.length == 0 @@ -31,44 +40,53 @@ class NotationalVelocityView extends SelectListView contentItems = @items .filter (x) -> queries - .map (q) -> q.test(x.filetext) || q.test(x.test) + .map (q) -> q.test(x.filetext) || q.test(x.title) .reduce (x, y) -> x && y return contentItems + getSubPath: (baseDir, dir)-> + ret = [] + fullDir = path.join(baseDir, dir) + for filename in fs.readdirSync(fullDir) + filePath = path.join(dir, filename) + fullPath = path.join(baseDir, filePath) + if fs.statSync(fullPath).isDirectory() + ret = ret.concat(@getSubPath(baseDir, filePath)) + else + if !fsPlus.isMarkdownExtension(path.extname(filename)) + continue + ret.push(filePath) + return ret + loadData: -> @data = [] - directory = atom.config.get('notational-velocity.directory') + basedir = atom.config.get('notational-velocity.directory') - for filename in fs.readdirSync(directory) - if !fsPlus.isMarkdownExtension(path.extname(filename)) - continue - - filepath = path.join(directory, filename) - filetext = fs.readFileSync(filepath, 'utf8') - modified = fs.statSync(filepath).mtime - - title = '' - result = filetext.match(/^#\s.+/) - if result != null - title = result[0].slice(2, result[0].length) + for filepath in @getSubPath(basedir, '') + fullpath = path.join(basedir, filepath) + filename = path.basename(filepath, path.extname(filepath)) + filetext = fs.readFileSync(fullpath, 'utf8') + title = path.join(path.dirname(filepath), filename) + modified = fs.statSync(fullpath).mtime item = { 'title': title, 'modified': modified, 'filetext': filetext, 'filename': filename, - 'filepath': filepath + 'filepath': fullpath } @data.push(item) + @data = @data.sort (x, y) -> if x.modified.getTime() <= y.modified.getTime() then 1 else -1 + @setItems(@data) getFilterKey: -> 'filetext' toggle: -> - console.log 'toggle' if @panel?.isVisible() @hide() else @@ -76,8 +94,6 @@ class NotationalVelocityView extends SelectListView @show() viewForItem: (item) -> - console.log 'viewForItem #{item}' - index = item.filetext.search /\n/ content = item.filetext.slice(index, item.filetext.length) @@ -94,23 +110,17 @@ class NotationalVelocityView extends SelectListView @confirmed(item) else query = @getFilterQuery() - if query? - # TODO(seongjae): It should create a new document. - console.log(query) @cancel() confirmed: (item) -> - console.log 'confirmed #{item}' atom.workspace.open(item.filepath) @cancel() destroy: -> - console.log 'destroy' @cancel() @panel?.destroy() show: -> - console.log 'show' @storeFocusedElement() @panel ?= atom.workspace.addModalPanel(item: this) @panel.show() @@ -123,7 +133,6 @@ class NotationalVelocityView extends SelectListView @panel?.hide() populateList: -> - console.log 'populateList' return unless @items? filterQuery = @getFilterQuery()