Save on focus lost


#1

That’s one of the features that I’m missing from SL and I really got used to it… It is a great feature and I think it should be in the core settings.


Latex Live Preview
#2

It is part of the core settings. It lives in the Atom package called Autosave.


Event to focus and unfocus to a tab (EditorView)?
#3

Maybe I should go through all the packages… thanks :smile:


#4

Problem is it saves on any pane lost focus. Would be nice if option to save on lost focus of the IDE rather than the pane itself. This is because if you’re using live reload or browser sync etc. you probably don’t want the page reloading until you’re done editing all files needed if that makes sense.


#5

If you want to autosave only when the window loses focus, you can disable the autosave package and put the following in your init.coffee:

atom.getCurrentWindow().on 'blur', ->
  for pane in atom.workspace.getPanes()
    pane.saveItems()

#6

Excellent! Exactly the desired behavior I’d like!


#7

You could even wrap it up into your own package to publish for other people to use.


#8

Thanks olmokramer. One slight improvement can be made: only save items that have been modified.

atom.getCurrentWindow().on 'blur', ->
  for pane in atom.workspace.getPanes()
    for item in pane.items
      if item.isModified and item.isModified()
        item.save()

#9

One problem with this approach is that pane items that do not implement an isModified method will never be saved. I’d change that to

if item.isModified?
  item.save() if item.isModified()
else
  item.save()

#11

This is almost perfect other than the fact that it assumes all pane items are TextEditor pane items. TextEditor pane items are the only ones that implement the save method out of the box, thus you get an error when UI tabs such as setting are open. Adding a check to see if pane items are TextEditor objects the snippet becomes

atom.getCurrentWindow().on 'blur', ->
  for pane in atom.workspace.getPanes()
      for item in pane.items
        if atom.workspace.isTextEditor(item)
          if item.isModified?
            item.save() if item.isModified()
          else
            item.save()