mirror of
https://github.com/atom/atom.git
synced 2026-01-23 05:48:10 -05:00
Editor is now a plugin! This means everything is pretty modular now (and the code is a little event heavy)
This commit is contained in:
24
plugins/editor/editor-pane.coffee
Normal file
24
plugins/editor/editor-pane.coffee
Normal file
@@ -0,0 +1,24 @@
|
||||
$ = require 'jquery'
|
||||
|
||||
Pane = require 'pane'
|
||||
|
||||
module.exports =
|
||||
class EditorPane extends Pane
|
||||
position: 'main'
|
||||
|
||||
html: $ '<div id="editor"></div>'
|
||||
|
||||
# You know what I don't like? This constructor.
|
||||
constructor: (@window, @editor) ->
|
||||
super @window
|
||||
|
||||
# Resize editor when panes are added/removed
|
||||
el = document.body
|
||||
el.addEventListener 'DOMNodeInsertedIntoDocument', => @resize()
|
||||
el.addEventListener 'DOMNodeRemovedFromDocument', => @resize()
|
||||
|
||||
resize: (timeout=1) ->
|
||||
setTimeout =>
|
||||
@editor.ace.focus()
|
||||
@editor.ace.resize()
|
||||
, timeout
|
||||
@@ -2,7 +2,8 @@ _ = require 'underscore'
|
||||
|
||||
File = require 'fs'
|
||||
Chrome = require 'chrome'
|
||||
Pane = require 'pane'
|
||||
Plugin = require 'plugin'
|
||||
EditorPane = require 'editor/editor-pane'
|
||||
|
||||
{bindKey} = require 'keybinder'
|
||||
|
||||
@@ -12,15 +13,15 @@ ace = require 'ace/ace'
|
||||
{UndoManager} = require 'ace/undomanager'
|
||||
|
||||
module.exports =
|
||||
class Editor extends Pane
|
||||
class Editor extends Plugin
|
||||
filename: null
|
||||
|
||||
position: 'main'
|
||||
html: '<div id="editor"></div>'
|
||||
|
||||
keymap: ->
|
||||
# Maybe these don't go here?
|
||||
'Command-S' : 'save'
|
||||
'Command-Shift-S' : 'saveAs'
|
||||
|
||||
# These are cool though.
|
||||
'Command-C' : 'copy'
|
||||
'Command-X' : 'cut'
|
||||
'Command-R' : 'eval'
|
||||
@@ -34,30 +35,49 @@ class Editor extends Pane
|
||||
'Alt-Shift-.' : 'end'
|
||||
'Ctrl-L' : 'consolelog'
|
||||
|
||||
modeMap:
|
||||
js: 'javascript'
|
||||
c: 'c_cpp'
|
||||
cpp: 'c_cpp'
|
||||
h: 'c_cpp'
|
||||
m: 'c_cpp'
|
||||
md: 'markdown'
|
||||
cs: 'csharp'
|
||||
rb: 'ruby'
|
||||
|
||||
sessions: {}
|
||||
|
||||
constructor: (args...) ->
|
||||
super(args...)
|
||||
|
||||
for shortcut, method of @keymap()
|
||||
bindKey @, shortcut, method
|
||||
@pane = new EditorPane @window
|
||||
@pane.add()
|
||||
|
||||
@ace = ace.edit "editor"
|
||||
|
||||
# This stuff should all be grabbed from the .atomicity dir
|
||||
@ace.setTheme require "ace/theme/twilight"
|
||||
@ace.getSession().setUseSoftTabs true
|
||||
@ace.getSession().setTabSize 2
|
||||
@ace.pane = this
|
||||
@ace.setShowInvisibles(true)
|
||||
@ace.setPrintMarginColumn 78
|
||||
|
||||
@ace.getSession().on 'change', ->
|
||||
@window.setDirty true
|
||||
@ace.getSession().on 'change', -> @window.setDirty true
|
||||
@window.on 'open', ({filename}) => @open filename
|
||||
@window.on 'close', ({filename}) => @close filename
|
||||
|
||||
el = document.body
|
||||
el.addEventListener 'DOMNodeInsertedIntoDocument', =>
|
||||
@resize()
|
||||
el.addEventListener 'DOMNodeRemovedFromDocument', =>
|
||||
@resize()
|
||||
modeForLanguage: (language) ->
|
||||
language = language.toLowerCase()
|
||||
modeName = @modeMap[language] or language
|
||||
|
||||
try
|
||||
require("ace/mode/#{modeName}").Mode
|
||||
catch e
|
||||
null
|
||||
|
||||
setMode: ->
|
||||
if mode = @modeForLanguage _.last @filename.split '.'
|
||||
@ace.getSession().setMode new mode
|
||||
|
||||
save: ->
|
||||
return @saveAs() if not @filename
|
||||
@@ -66,7 +86,7 @@ class Editor extends Pane
|
||||
File.write @filename, @code()
|
||||
@sessions[@filename] = @ace.getSession()
|
||||
@window.setDirty false
|
||||
@window._emit 'save', { @filename }
|
||||
@window._emit 'save', { filename: @filename }
|
||||
|
||||
open: (path) ->
|
||||
path = Chrome.openPanel() if not path
|
||||
@@ -86,12 +106,10 @@ class Editor extends Pane
|
||||
@sessions[@filename] or= @newSession File.read @filename
|
||||
@ace.setSession @sessions[@filename]
|
||||
@window.setDirty false
|
||||
|
||||
@window._emit 'open', { @filename }
|
||||
@setMode()
|
||||
|
||||
close: (path) ->
|
||||
@deleteSession path
|
||||
@window._emit 'close', { filename : path }
|
||||
|
||||
saveAs: ->
|
||||
if file = Chrome.savePanel()
|
||||
@@ -109,17 +127,6 @@ class Editor extends Pane
|
||||
regExp: true
|
||||
wrap: true
|
||||
|
||||
resize: (timeout=1) ->
|
||||
setTimeout =>
|
||||
@ace.focus()
|
||||
@ace.resize()
|
||||
, timeout
|
||||
|
||||
switchToSession: (path) ->
|
||||
if @sessions[path]
|
||||
@filename = path
|
||||
@ace.setSession @sessions[path]
|
||||
|
||||
deleteSession: (path) ->
|
||||
if path is @filename
|
||||
@filename = null
|
||||
1
plugins/editor/index.coffee
Normal file
1
plugins/editor/index.coffee
Normal file
@@ -0,0 +1 @@
|
||||
module.exports = require 'editor/editor'
|
||||
@@ -1 +0,0 @@
|
||||
module.exports = require 'modes/modes'
|
||||
@@ -1,32 +0,0 @@
|
||||
_ = require 'underscore'
|
||||
|
||||
Plugin = require 'plugin'
|
||||
|
||||
module.exports =
|
||||
class Modes extends Plugin
|
||||
load: ->
|
||||
@window.on 'open', ({filename}) => @setMode(filename)
|
||||
@window.on 'save', ({filename}) => @setMode(filename)
|
||||
|
||||
modeMap:
|
||||
js: 'javascript'
|
||||
c: 'c_cpp'
|
||||
cpp: 'c_cpp'
|
||||
h: 'c_cpp'
|
||||
m: 'c_cpp'
|
||||
md: 'markdown'
|
||||
cs: 'csharp'
|
||||
rb: 'ruby'
|
||||
|
||||
modeForLanguage: (language) ->
|
||||
language = language.toLowerCase()
|
||||
modeName = @modeMap[language] or language
|
||||
|
||||
try
|
||||
require("ace/mode/#{modeName}").Mode
|
||||
catch e
|
||||
null
|
||||
|
||||
setMode: (filename) ->
|
||||
if mode = @modeForLanguage _.last filename.split '.'
|
||||
@window.document.ace.getSession().setMode new mode
|
||||
@@ -39,8 +39,7 @@ class Tabs extends Plugin
|
||||
# Move all of this methods below to pane? I think so
|
||||
closeActiveTab: ->
|
||||
activeTab = $('#tabs ul .active')
|
||||
# I don't like how this is talking directly to the document/editor
|
||||
@window.document.close(activeTab.data 'path')
|
||||
@window.close activeTab.data 'path'
|
||||
|
||||
nextTab: ->
|
||||
@pane.switchToTab $('#tabs ul .active').next()
|
||||
|
||||
@@ -30,7 +30,7 @@ class TabsPane extends Pane
|
||||
|
||||
$("#tabs ul .active").removeClass("active")
|
||||
$(tab).addClass 'active'
|
||||
@window.document.switchToSession $(tab).data 'path'
|
||||
@window.open $(tab).data 'path'
|
||||
|
||||
addTab: (path) ->
|
||||
existing = $("#tabs [data-path='#{path}']")
|
||||
|
||||
@@ -10,8 +10,9 @@ oop = require "pilot/oop"
|
||||
module.exports =
|
||||
class Window
|
||||
controller: null
|
||||
document: null
|
||||
|
||||
nswindow: null
|
||||
|
||||
panes: []
|
||||
|
||||
keymap: ->
|
||||
@@ -35,12 +36,9 @@ class Window
|
||||
@._emit "loaded"
|
||||
|
||||
loadPlugins: ->
|
||||
Editor = require 'editor'
|
||||
@document = new Editor @
|
||||
|
||||
@open @path if @path?
|
||||
|
||||
@plugins = []
|
||||
|
||||
# Ewwww, don't do this
|
||||
App = require 'app'
|
||||
for pluginPath in File.list(App.root + "/plugins")
|
||||
if File.isDirectory pluginPath
|
||||
@@ -51,6 +49,8 @@ class Window
|
||||
console.warn "Plugin Failed: #{File.base pluginPath}"
|
||||
console.warn error
|
||||
|
||||
@open @path if @path?
|
||||
|
||||
# After all the plugins are created, load them.
|
||||
for plugin in @plugins
|
||||
try
|
||||
@@ -59,19 +59,9 @@ class Window
|
||||
console.warn "Plugin Loading Failed: #{plugin.constructor.name}"
|
||||
console.warn error
|
||||
|
||||
close: ->
|
||||
@controller.close
|
||||
|
||||
reload: ->
|
||||
Chrome.newWindow()
|
||||
@close()
|
||||
|
||||
isDirty: ->
|
||||
@nswindow.isDocumentEdited()
|
||||
|
||||
# Set the active window's dirty status.
|
||||
setDirty: (bool) ->
|
||||
@nswindow.setDocumentEdited bool
|
||||
@controller.close
|
||||
|
||||
inspector: ->
|
||||
@_inspector ?= WindowController.webView.inspector
|
||||
@@ -79,14 +69,6 @@ class Window
|
||||
new: ->
|
||||
Chrome.newWindow()
|
||||
|
||||
open: (path) ->
|
||||
@document?.open path
|
||||
|
||||
openURL: (url) ->
|
||||
if url = prompt "Enter URL:"
|
||||
Chrome = require 'app'
|
||||
Chrome.openURL url
|
||||
|
||||
showConsole: ->
|
||||
@inspector().showConsole(1)
|
||||
|
||||
@@ -95,3 +77,22 @@ class Window
|
||||
|
||||
setTitle: (title) ->
|
||||
@nswindow.title = title
|
||||
|
||||
# Do these get moved into document?
|
||||
isDirty: ->
|
||||
@nswindow.isDocumentEdited()
|
||||
|
||||
# Set the active window's dirty status.
|
||||
setDirty: (bool) ->
|
||||
@nswindow.setDocumentEdited bool
|
||||
|
||||
open: (path) ->
|
||||
@_emit 'open', { filename: path }
|
||||
|
||||
close: (path) ->
|
||||
@_emit 'close', { filename: path }
|
||||
|
||||
openURL: (url) ->
|
||||
if url = prompt "Enter URL:"
|
||||
Chrome = require 'app'
|
||||
Chrome.openURL url
|
||||
|
||||
Reference in New Issue
Block a user