Skip to content

Commit

Permalink
Merge pull request #24 from jonmagic/powered-by-docquery
Browse files Browse the repository at this point in the history
First attempt with DocQuery
  • Loading branch information
seongjaelee committed Aug 29, 2015
2 parents 03654ac + c15e7e0 commit ae435d7
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 306 deletions.
97 changes: 45 additions & 52 deletions lib/notational-velocity-view.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -2,100 +2,90 @@ path = require 'path'
fs = require 'fs-plus'
_ = require 'underscore-plus'
{$, $$, SelectListView} = require 'atom-space-pen-views'
NoteDirectory = require './note-directory'
Note = require './note'
DocQuery = require 'DocQuery'

module.exports =
class NotationalVelocityView extends SelectListView
initialize: ->
initialize: (state) ->
@initializedAt = new Date()
super
@addClass('notational-velocity from-top overlay')
@rootDirectory = atom.config.get('notational-velocity.directory')
if !fs.existsSync(@rootDirectory)
throw new Error("The given directory #{@rootDirectory} does not exist. "
+ "Set the note directory to the existing one from Settings.")
@noteDirectory = new NoteDirectory(@rootDirectory, null, () => @updateNotes())
@updateNotes()
@prevFilterQuery = ''
@prevCursorPosition = 0

updateNotes: () ->
@notes = @noteDirectory.getNotes()
@setItems(@notes)
@documentsLoaded = false
@docQuery = new DocQuery(@rootDirectory, {recursive: true})
@docQuery.on "ready", () =>
@documentsLoaded = true
@setLoading()
@populateList()
@docQuery.on "added", (fileDetails) =>
@populateList() if @documentsLoaded
@docQuery.on "updated", (fileDetails) =>
@populateList() if @documentsLoaded
@docQuery.on "removed", (fileDetails) =>
@populateList() if @documentsLoaded

selectItem: (filterQuery) ->
if filterQuery.length == 0
@prevCursorPosition = 0
return null

titlePatterns = [
///^#{filterQuery}$///i,
///^#{filterQuery}///i,
]

titleItem = null
for titlePattern in titlePatterns
titleItems = @notes
.filter (x) -> x.getTitle().match(titlePattern) != null
titleItem = if titleItems.length > 0 then titleItems[0] else null
if titleItem != null
break
titleItem = @docQuery.search(filterQuery)[0]

# If title item is not null, auto-fill the search panel.
# But we don't want to fill it when deleting.
editor = @filterEditorView.model
currCursorPosition = editor.getCursorBufferPosition().column
if titleItem != null && @prevCursorPosition < currCursorPosition
@prevFilterQuery = titleItem.getTitle()
editor.setText(filterQuery + titleItem.getTitle().slice(filterQuery.length))
editor.selectLeft(titleItem.getTitle().length - filterQuery.length)
if titleItem != undefined && @prevCursorPosition < currCursorPosition
@prevFilterQuery = titleItem.title
editor.setText(filterQuery + titleItem.title.slice(filterQuery.length))
editor.selectLeft(titleItem.title.length - filterQuery.length)
@prevCursorPosition = currCursorPosition

return titleItem

filter: (filterQuery) ->
if filterQuery.length == 0
return @notes

queries = filterQuery.split(' ')
.filter (x) -> x.length > 0
.map (x) -> new RegExp(x, 'gi')
return @notes
.filter (x) ->
queries
.map (q) -> q.test(x.getText()) || q.test(x.getTitle())
.reduce (x, y) -> x && y
return @docQuery.search(filterQuery)

getFilterKey: ->
'filetext'

toggle: ->
if @panel?.isVisible()
@hide()
else
else if @documentsLoaded
@populateList()
@show()
else
@setLoading("Loading documents")
@show()

viewForItem: (item) ->
content = item.getText()[0...100]
content = item.body[0...100]

$$ ->
@li class: 'two-lines', =>
@div class: 'primary-line', =>
@span "#{item.getTitle()}"
@div class: 'metadata', "#{item.getModified().toLocaleDateString()}"
@span "#{item.title}"
@div class: 'metadata', "#{item.modifiedAt.toLocaleDateString()}"
@div class: 'secondary-line', "#{content}"

confirmSelection: ->
item = @getSelectedItem()
filePath = null
item = @getSelectedItem()
filePath = null
sanitizedQuery = @getFilterQuery().replace(/\s+$/, '')
calculatedPath = path.join(@rootDirectory, sanitizedQuery + '.md')
if item?
filePath = item.getFilePath()
else
sanitizedQuery = @getFilterQuery().replace(/\s+$/, '')
if sanitizedQuery.length > 0
filePath = path.join(@rootDirectory, sanitizedQuery + '.md')
fs.writeFileSync(filePath, '')
filePath = item.filePath
else if fs.existsSync(calculatedPath)
filePath = calculatedPath
else if sanitizedQuery.length > 0
filePath = calculatedPath
fs.writeFileSync(filePath, '')

if filePath
atom.workspace.open(filePath).then (editor) ->
Expand Down Expand Up @@ -126,10 +116,13 @@ class NotationalVelocityView extends SelectListView
@panel?.hide()

populateList: ->
return unless @notes?

filterQuery = @getFilterQuery()
filteredItems = @filter(filterQuery)
filteredItems = null
if filterQuery == "" || filterQuery == undefined
filteredItems = @docQuery.documents
else
filteredItems = @filter(filterQuery)

selectedItem = @selectItem(filterQuery)

@list.empty()
Expand All @@ -147,7 +140,7 @@ class NotationalVelocityView extends SelectListView
@selectItemView(@list.find("li:nth-child(#{n})"))

else
@setError(@getEmptyMessage(@notes.length, filteredItems.length))
@setError(@getEmptyMessage(@docQuery.documents.length, filteredItems.length))

schedulePopulateList: ->
# We can skip it when we are just moving the position of the cursor.
Expand Down
2 changes: 1 addition & 1 deletion lib/notational-velocity.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ module.exports =
serialize: ->
notationalVelocityViewState: @notationalVelocityView.serialize()

createView: (state) ->
createView: (state, docQuery) ->
unless @notationalVelocityView?
NotationalVelocityView = require './notational-velocity-view'
@notationalVelocityView = new NotationalVelocityView(state.notationalVelocityViewState)
Expand Down
58 changes: 0 additions & 58 deletions lib/note-directory.coffee

This file was deleted.

37 changes: 0 additions & 37 deletions lib/note.coffee

This file was deleted.

8 changes: 3 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,14 @@
},
"homepage": "https://github.com/seongjaelee/notational-velocity",
"dependencies": {
"fs-plus": "2.x",
"atom-space-pen-views": "^2.0.3",
"pathwatcher": "^4.2",
"docquery": "^1.1.0",
"fs-plus": "2.x",
"underscore-plus": "^1.6.6"
},
"devDependencies": {
"fs-plus": "2.x",
"atom-space-pen-views": "^2.0.3",
"pathwatcher": "^4.2",
"underscore-plus": "^1.6.6",
"temp": "~0.7.0"
"underscore-plus": "^1.6.6"
}
}
Loading

0 comments on commit ae435d7

Please sign in to comment.