mirror of
https://github.com/atom/atom.git
synced 2026-04-06 03:02:13 -04:00
Dirty and window title added back. Dirty close prompts user.
This commit is contained in:
@@ -11,12 +11,13 @@ Native = require 'native'
|
||||
|
||||
module.exports =
|
||||
class Editor
|
||||
filename: null
|
||||
activePath: null
|
||||
|
||||
sessions: {}
|
||||
|
||||
constructor: (path) ->
|
||||
KeyBinder.register "editor", @
|
||||
|
||||
# Resize editor when panes are added/removed
|
||||
el = document.body
|
||||
el.addEventListener 'DOMNodeInsertedIntoDocument', => @resize()
|
||||
@@ -36,7 +37,7 @@ class Editor
|
||||
Event.on 'window:open', (e) => @open e.details
|
||||
Event.on 'window:close', (e) => @close e.details
|
||||
|
||||
@open path if path
|
||||
@open path
|
||||
|
||||
modeMap:
|
||||
js: 'javascript'
|
||||
@@ -58,37 +59,74 @@ class Editor
|
||||
null
|
||||
|
||||
setMode: ->
|
||||
return unless @activePath
|
||||
if mode = @modeForLanguage _.last @activePath.split '.'
|
||||
@ace.getSession().setMode new mode
|
||||
|
||||
save: ->
|
||||
return @saveAs() if not @activePath
|
||||
|
||||
@removeTrailingWhitespace()
|
||||
fs.write @activePath, @code()
|
||||
@sessions[@activePath] = @ace.getSession()
|
||||
#@window.setDirty false
|
||||
#@window._emit 'save', { filename: @activePath }
|
||||
|
||||
open: (path) ->
|
||||
return unless fs.isFile path
|
||||
@activePath = path
|
||||
if not path or fs.isDirectory path
|
||||
@activePath = null
|
||||
@ace.setSession @newSession()
|
||||
else
|
||||
@activePath = path
|
||||
|
||||
@sessions[@activePath] ?= @newSession fs.read @activePath
|
||||
@ace.setSession @sessions[@activePath]
|
||||
@setMode()
|
||||
session = @sessions[path]
|
||||
if not session
|
||||
@sessions[path] = session = @newSession fs.read path
|
||||
session.on 'change', -> session.$atom_dirty = true
|
||||
|
||||
@ace.setSession session
|
||||
@setMode()
|
||||
|
||||
Event.trigger "editor:open", path
|
||||
|
||||
close: (path) ->
|
||||
@activePath = null if path is @activePath
|
||||
path ?= @activePath
|
||||
|
||||
# ICK, clean this up... too many assumptions being made
|
||||
session = @sessions[path]
|
||||
if not session or session.$atom_dirty
|
||||
detailedMessage = if @activePath
|
||||
"#{@activePath} has changes."
|
||||
else
|
||||
"An untitled file has changes."
|
||||
|
||||
canceled = Native.alert "Do you want to save the changes you made?", detailedMessage,
|
||||
"Save": =>
|
||||
path = @save()
|
||||
not path # if save fails/cancels, consider it canceled
|
||||
"Cancel": => true
|
||||
"Don't Save": => false
|
||||
|
||||
return if canceled
|
||||
|
||||
delete @sessions[path]
|
||||
@ace.setSession @newSession()
|
||||
|
||||
if path is @activePath
|
||||
@activePath = null
|
||||
@ace.setSession @newSession()
|
||||
|
||||
Event.trigger "editor:close", path
|
||||
|
||||
save: (path) ->
|
||||
path ?= @activePath
|
||||
|
||||
return @saveAs() if not path
|
||||
|
||||
@removeTrailingWhitespace()
|
||||
fs.write path, @code()
|
||||
if @sessions[path]
|
||||
@sessions[path].$atom_dirty = false
|
||||
|
||||
path
|
||||
|
||||
saveAs: ->
|
||||
if path = Native.savePanel()
|
||||
@activePath = path
|
||||
#@window.setTitle _.last @activePath.split '/'
|
||||
@save()
|
||||
path = Native.savePanel()?.toString()
|
||||
if path
|
||||
@save path
|
||||
@open path
|
||||
|
||||
path
|
||||
|
||||
code: ->
|
||||
@ace.getSession().getValue()
|
||||
|
||||
@@ -1,5 +1,17 @@
|
||||
module.exports =
|
||||
class Native
|
||||
@alert: (message, detailedMessage, buttons) ->
|
||||
alert = OSX.NSAlert.alloc.init
|
||||
alert.setMessageText message
|
||||
alert.setInformativeText detailedMessage
|
||||
callbacks = {}
|
||||
for label, callback of buttons
|
||||
button = alert.addButtonWithTitle label
|
||||
callbacks[button.tag] = callback
|
||||
|
||||
buttonTag = alert.runModal
|
||||
return callbacks[buttonTag]()
|
||||
|
||||
# path - Optional. The String path to the file to base it on.
|
||||
@newWindow: (path) ->
|
||||
controller = OSX.NSApp.createController path
|
||||
|
||||
@@ -22,15 +22,22 @@ windowAdditions =
|
||||
|
||||
KeyBinder.register "window", window
|
||||
|
||||
@editor = if fs.isFile atomController.path
|
||||
@editor = if atomController.path and fs.isFile atomController.path
|
||||
new Editor atomController.path
|
||||
else
|
||||
new Editor @tmpFile()
|
||||
new Editor
|
||||
|
||||
@loadExtensions()
|
||||
@loadKeyBindings()
|
||||
|
||||
KeyBinder.load "#{@appRoot}/static/key-bindings.coffee"
|
||||
KeyBinder.load "~/.atomicity/key-bindings.coffee"
|
||||
Event.on "editor:open", (e) =>
|
||||
path = e.details
|
||||
console.log path
|
||||
basename = fs.base path
|
||||
@setTitle basename
|
||||
|
||||
Event.on "editor:close", (e) =>
|
||||
@setTitle "untitled"
|
||||
|
||||
loadExtensions: ->
|
||||
extension.shutdown() for extension in @extensions
|
||||
@@ -53,18 +60,22 @@ windowAdditions =
|
||||
console.warn "window: Extension #{extension.constructor.name} failed to startup."
|
||||
console.warn error
|
||||
|
||||
loadKeyBindings: ->
|
||||
KeyBinder.load "#{@appRoot}/static/key-bindings.coffee"
|
||||
KeyBinder.load "~/.atomicity/key-bindings.coffee"
|
||||
|
||||
recentPath: ->
|
||||
localStorage.lastOpenedPath ? @tmpFile()
|
||||
localStorage.lastOpenedPath
|
||||
|
||||
setRecentPath: (path) ->
|
||||
localStorage.lastOpenedPath = path
|
||||
|
||||
tmpFile: ->
|
||||
"/tmp/atom"
|
||||
|
||||
showConsole: ->
|
||||
atomController.webView.inspector.showConsole true
|
||||
|
||||
setTitle: (title) ->
|
||||
atomController.window.title = title
|
||||
|
||||
reload: ->
|
||||
@close()
|
||||
Native.newWindow atomController.path
|
||||
|
||||
@@ -4,10 +4,10 @@ app:
|
||||
|
||||
window:
|
||||
'cmd-shift-i': (window) -> window.showConsole()
|
||||
'cmd-w': (window) -> window.close()
|
||||
'cmd-r': (window) -> window.reload()
|
||||
|
||||
editor:
|
||||
'cmd-w': 'close'
|
||||
'cmd-s': 'save'
|
||||
'cmd-shift-s': 'saveAs'
|
||||
'cmd-c': 'copy'
|
||||
|
||||
Reference in New Issue
Block a user