mirror of
https://github.com/atom/atom.git
synced 2026-02-08 13:45:09 -05:00
refactor editor and browser
This commit is contained in:
@@ -6,22 +6,13 @@ module.exports =
|
||||
class Browser extends Document
|
||||
@register (path) -> /^https?:/.test path
|
||||
|
||||
buffers: {}
|
||||
|
||||
path: null
|
||||
html: $ "<div id='browser'></div>"
|
||||
iframe: ->
|
||||
$ "<iframe src='#{@path}' style='width:100%;height:100%'></iframe>"
|
||||
|
||||
@isPathUrl: (path) ->
|
||||
/^https?:\/\//.test path
|
||||
constructor: (@path) ->
|
||||
@html.html @iframe()
|
||||
@show()
|
||||
|
||||
constructor: ->
|
||||
atom.on "window:open", (e) =>
|
||||
path = e.details
|
||||
return unless @constructor.isPathUrl path
|
||||
|
||||
@buffers[path] ?= $ "<iframe src='#{path}' style='width:100%;height:100%'></iframe>"
|
||||
|
||||
@html.html @buffers[path]
|
||||
|
||||
@show()
|
||||
|
||||
atom.trigger "browser:focus", path
|
||||
atom.trigger 'browser:load', this
|
||||
|
||||
@@ -11,13 +11,12 @@ module.exports =
|
||||
class Editor extends Document
|
||||
@register (path) -> fs.isFile path
|
||||
|
||||
activePath: null
|
||||
|
||||
buffers: {}
|
||||
|
||||
dirty: false
|
||||
path: null
|
||||
html: $ "<div id='ace-editor'></div>"
|
||||
|
||||
constructor: ->
|
||||
constructor: (@path) ->
|
||||
super()
|
||||
atom.keybinder.register "editor", @
|
||||
|
||||
@show()
|
||||
@@ -31,13 +30,6 @@ class Editor extends Document
|
||||
@ace.setShowInvisibles(true)
|
||||
@ace.setPrintMarginColumn 78
|
||||
|
||||
atom.on 'window:open', (e) =>
|
||||
path = e.details
|
||||
@addBuffer e.details if fs.isFile path
|
||||
|
||||
atom.on 'window:close', (e) => @removeBuffer e.details
|
||||
atom.on 'editor:bufferFocus', (e) => @resize()
|
||||
|
||||
# Resize editor when panes are added/removed
|
||||
el = document.body
|
||||
el.addEventListener 'DOMNodeInsertedIntoDocument', => @resize()
|
||||
@@ -46,6 +38,19 @@ class Editor extends Document
|
||||
@resize()
|
||||
, 500
|
||||
|
||||
# Setup the session
|
||||
code = if @path then fs.read @path else ''
|
||||
session = new EditSession code
|
||||
session.setUndoManager new UndoManager
|
||||
session.setUseSoftTabs useSoftTabs = @usesSoftTabs code
|
||||
session.setTabSize if useSoftTabs then @guessTabSize code else 8
|
||||
mode = @modeForPath()
|
||||
session.setMode new mode if mode
|
||||
session.on 'change', => @dirty = true
|
||||
@ace.setSession session
|
||||
|
||||
atom.trigger 'editor:load', this
|
||||
|
||||
modeMap:
|
||||
js: 'javascript'
|
||||
c: 'c_cpp'
|
||||
@@ -62,7 +67,7 @@ class Editor extends Document
|
||||
Gemfile: 'ruby'
|
||||
Rakefile: 'ruby'
|
||||
|
||||
modeForPath: (path) ->
|
||||
modeForPath: (path=@path) ->
|
||||
return null if not path
|
||||
|
||||
if not modeName = @modeFileMap[ _.last path.split '/' ]
|
||||
@@ -75,93 +80,36 @@ class Editor extends Document
|
||||
catch e
|
||||
null
|
||||
|
||||
addBuffer: (path) ->
|
||||
throw "#{@constructor.name}: Cannot create buffer from a directory `#{path}`" if fs.isDirectory path
|
||||
|
||||
buffer = @buffers[path]
|
||||
if not buffer
|
||||
code = if path then fs.read path else ''
|
||||
buffer = new EditSession code
|
||||
buffer.setUndoManager new UndoManager
|
||||
buffer.setUseSoftTabs useSoftTabs = @usesSoftTabs code
|
||||
buffer.setTabSize if useSoftTabs then @guessTabSize code else 8
|
||||
|
||||
mode = @modeForPath path
|
||||
buffer.setMode new mode if mode
|
||||
|
||||
@buffers[path] = buffer
|
||||
|
||||
buffer.on 'change', -> buffer.$atom_dirty = true
|
||||
atom.trigger "editor:bufferAdd", path
|
||||
|
||||
@focusBuffer path
|
||||
|
||||
removeBuffer: (path) ->
|
||||
path ?= @activePath
|
||||
|
||||
return if not path
|
||||
|
||||
buffer = @buffers[path]
|
||||
return if not buffer
|
||||
|
||||
if buffer.$atom_dirty
|
||||
# This should be thrown into it's own method, but I can't think of a good
|
||||
# name.
|
||||
detailedMessage = if @activePath
|
||||
"#{@activePath} has changes."
|
||||
close: ->
|
||||
if @dirty
|
||||
detailedMessage = if @path
|
||||
"#{@path} has changes."
|
||||
else
|
||||
"An untitled file has changes."
|
||||
|
||||
canceled = atom.native.alert "Do you want to save the changes you made?", detailedMessage,
|
||||
canceled = atom.native.alert "Do you want to save your changes?",
|
||||
detailedMessage,
|
||||
"Save": =>
|
||||
path = @save()
|
||||
not path # if save modal fails/cancels, consider it canceled
|
||||
# if save modal fails/cancels, consider it cancelled
|
||||
not @save()
|
||||
"Cancel": => true
|
||||
"Don't Save": => false
|
||||
|
||||
return if canceled
|
||||
|
||||
delete @buffers[path]
|
||||
|
||||
atom.trigger "editor:bufferRemove", path
|
||||
|
||||
if path is @activePath
|
||||
newActivePath = Object.keys(@buffers)[0]
|
||||
if newActivePath
|
||||
@focusBuffer newActivePath
|
||||
else
|
||||
@ace.setSession new EditSession ''
|
||||
|
||||
focusBuffer: (path) ->
|
||||
return if not path
|
||||
|
||||
@show()
|
||||
@activePath = path
|
||||
|
||||
buffer = @buffers[path] or @addBuffer path
|
||||
@ace.setSession buffer
|
||||
|
||||
atom.trigger "editor:bufferFocus", path
|
||||
|
||||
save: (path) ->
|
||||
path ?= @activePath
|
||||
|
||||
return @saveAs() if not path
|
||||
save: ->
|
||||
return @saveAs() if not @path
|
||||
|
||||
@removeTrailingWhitespace()
|
||||
fs.write path, @code()
|
||||
if @buffers[path]
|
||||
@buffers[path].$atom_dirty = false
|
||||
fs.write @path, @code()
|
||||
@dirty = false
|
||||
|
||||
path
|
||||
@path
|
||||
|
||||
saveAs: ->
|
||||
path = atom.native.savePanel()?.toString()
|
||||
if path
|
||||
if path = atom.native.savePanel()?.toString()
|
||||
@path = path
|
||||
@save path
|
||||
@addBuffer path
|
||||
|
||||
path
|
||||
|
||||
code: ->
|
||||
@ace.getSession().getValue()
|
||||
|
||||
@@ -1,16 +1,9 @@
|
||||
Browser = require 'browser'
|
||||
Editor = require 'editor'
|
||||
|
||||
fs = require 'fs'
|
||||
_ = require 'underscore'
|
||||
|
||||
# This a weirdo file. We don't create a Window class, we just add stuff to
|
||||
# the DOM window.
|
||||
windowAdditions =
|
||||
editor: null
|
||||
|
||||
browser: null
|
||||
|
||||
appRoot: OSX.NSBundle.mainBundle.resourcePath
|
||||
|
||||
path: null
|
||||
@@ -18,8 +11,8 @@ windowAdditions =
|
||||
startup: ->
|
||||
atom.keybinder.register "window", window
|
||||
|
||||
@path = $atomController.path
|
||||
@setTitle _.last @path.split '/'
|
||||
@path = $atomController.path.toString()
|
||||
@setTitle (_.last @path.split '/') or 'Untitled Document'
|
||||
|
||||
# Remember sizing!
|
||||
defaultFrame = x: 0, y: 0, width: 600, height: 800
|
||||
@@ -27,9 +20,6 @@ windowAdditions =
|
||||
rect = OSX.CGRectMake(frame.x, frame.y, frame.width, frame.height)
|
||||
$atomController.window.setFrame_display rect, true
|
||||
|
||||
@editor = new Editor
|
||||
@browser = new Browser
|
||||
|
||||
$atomController.window.makeKeyWindow
|
||||
atom.trigger 'window:load'
|
||||
|
||||
|
||||
@@ -2,6 +2,9 @@
|
||||
window.atom = {}
|
||||
|
||||
App = require 'app'
|
||||
Browser = require 'browser'
|
||||
Document = require 'document'
|
||||
Editor = require 'editor'
|
||||
Event = require 'event'
|
||||
ExtensionManager = require 'extension-manager'
|
||||
KeyBinder = require 'key-binder'
|
||||
@@ -26,5 +29,8 @@ atom.app = new App
|
||||
for name, method of atom.app
|
||||
atom[name] = atom.app[name]
|
||||
|
||||
atom.path = $atomController.path.toString()
|
||||
atom.document = new Document.handler atom.path
|
||||
|
||||
require 'window'
|
||||
window.startup()
|
||||
window.startup()
|
||||
|
||||
@@ -21,9 +21,10 @@ class KeyBinder
|
||||
load: (path) ->
|
||||
try
|
||||
Watcher.watch path, =>
|
||||
# Should we keep track of which file bindings are associated with? That
|
||||
# way we could clear bindings when the file is changed or deleted. I
|
||||
# think the answer is yes, but I don't want to do this right now.
|
||||
# Should we keep track of which file bindings are associated with?
|
||||
# That way we could clear bindings when the file is changed
|
||||
# or deleted. I think the answer is yes, but I don't want to
|
||||
# do this right now.
|
||||
console.log "#{@name}: Reloading #{path}"
|
||||
@load path
|
||||
|
||||
|
||||
Reference in New Issue
Block a user