Introduce autosave feature

Fix #8.
This commit is contained in:
Seongjae Lee
2015-04-27 22:24:43 -07:00
parent 7e66ac8f88
commit 49c081ed6e
+30 -1
View File
@@ -1,4 +1,6 @@
{CompositeDisposable} = require 'atom'
path = require 'path'
fs = require 'fs-plus'
{CompositeDisposable, Disposable} = require 'atom'
module.exports =
config:
@@ -11,6 +13,8 @@ module.exports =
notationalVelocityView: null
activate: (state) ->
@rootDirectory = fs.realpathSync(atom.config.get('notational-velocity.directory'))
# Events subscribed to in atom's system can be easily cleaned up with a
# CompositeDisposable
@subscriptions = new CompositeDisposable
@@ -19,6 +23,20 @@ module.exports =
@subscriptions.add atom.commands.add 'atom-workspace',
'notational-velocity:toggle': => @createView(state).toggle()
handleBeforeUnload = @autosaveAll.bind(this)
window.addEventListener('beforeunload', handleBeforeUnload, true)
@subscriptions.add new Disposable -> window.removeEventListener('beforeunload', handleBeforeUnload, true)
handleBlur = (event) =>
if event.target is window
@autosaveAll()
else if event.target.matches('atom-text-editor:not([mini])') and not event.target.contains(event.relatedTarget)
@autosave(event.target.getModel())
window.addEventListener('blur', handleBlur, true)
@subscriptions.add new Disposable -> window.removeEventListener('blur', handleBlur, true)
@subscriptions.add atom.workspace.onWillDestroyPaneItem ({item}) => @autosave(item)
deactivate: ->
@subscriptions.dispose()
@notationalVelocityView.destroy()
@@ -31,3 +49,14 @@ module.exports =
NotationalVelocityView = require './notational-velocity-view'
@notationalVelocityView = new NotationalVelocityView(state.notationalVelocityViewState)
@notationalVelocityView
autosave: (paneItem) ->
return unless paneItem?.getURI?()?
return unless paneItem?.isModified?()
uri = paneItem.getURI()
return unless uri.indexOf(@rootDirectory) == 0
return unless fs.isMarkdownExtension(path.extname(uri))
paneItem?.save?()
autosaveAll: ->
@autosave(paneItem) for paneItem in atom.workspace.getPaneItems()