Turn editor.coffee into an Editor class which subclasses Pane

This isn't great but I think it's a step in the right direction.
This commit is contained in:
Chris Wanstrath
2011-09-04 16:36:56 -07:00
parent 18b8e14134
commit b9689679e5
8 changed files with 125 additions and 75 deletions

View File

@@ -1,6 +1,6 @@
_ = require 'underscore'
{ace} = require 'editor'
{activeWindow} = require 'app'
modeMap =
js: 'javascript'
@@ -26,10 +26,11 @@ setMode = ({filename}) ->
console.log "setMode(#{filename})"
if mode = modeForLanguage _.last filename.split '.'
console.dir mode
ace.getSession().setMode new mode
activeWindow.document.ace?.getSession().setMode new mode
exports.init = ->
ace.on 'open', setMode
ace.on 'save', setMode
if ace = activeWindow.document.ace
ace.on 'open', setMode
ace.on 'save', setMode
exports.modeMap = modeMap

View File

@@ -4,27 +4,26 @@ _ = require 'underscore'
{activeWindow} = require 'app'
File = require 'fs'
Editor = require 'editor'
bindKey = Editor.bindKey
{bindKey} = require 'keybinder'
exports.init = ->
@html = require "project/project.html"
bindKey 'toggleProjectDrawer', 'Command-Ctrl-N', (env) =>
bindKey 'toggleProjectDrawer', 'Command-Ctrl-N', =>
@toggle()
Editor.ace.on 'open', =>
activeWindow.document.ace.on 'open', =>
@reload() if @dir? and File.workingDirectory() isnt @dir
$('#project .cwd').live 'click', (event) =>
Editor.open @dir.replace _.last(@dir.split '/'), ''
activeWindow.open @dir.replace _.last(@dir.split '/'), ''
$('#project li').live 'click', (event) =>
$('#project .active').removeClass 'active'
el = $(event.currentTarget)
el.addClass 'active'
path = decodeURIComponent el.attr 'path'
Editor.open path
activeWindow.open path
exports.toggle = ->
if @showing

View File

@@ -2,7 +2,7 @@ $ = require 'jquery'
_ = require 'underscore'
{activeWindow} = require 'app'
{bindKey} = require 'editor'
{bindKey} = require 'keybinder'
# click tab
@@ -16,7 +16,7 @@ $(document).delegate '#tabs .add a', 'click', ->
false
# toggle
bindKey 'toggleTabs', 'Command-Ctrl-T', (env) ->
bindKey 'toggleTabs', 'Command-Ctrl-T', ->
if $('#tabs').length
tabs.hideTabs()
else

View File

@@ -5,82 +5,99 @@ _ = require 'underscore'
File = require 'fs'
App = require 'app'
Pane = require 'pane'
activeWindow = App.activeWindow
{bindKey} = require 'keybinder'
ace = require 'ace/ace'
canon = require 'pilot/canon'
exports.ace = editor = ace.edit "editor"
editor.setTheme require "ace/theme/twilight"
editor.getSession().setUseSoftTabs true
editor.getSession().setTabSize 2
module.exports =
class Editor extends Pane
filename: null
filename = null
editor.getSession().on 'change', ->
activeWindow.setDirty true
save = ->
File.write filename, editor.getSession().getValue()
activeWindow.setDirty false
editor._emit 'save', { filename }
exports.open = open = (path) ->
filename = path
constructor: ->
@ace = ace.edit "editor"
@ace.setTheme require "ace/theme/twilight"
@ace.getSession().setUseSoftTabs true
@ace.getSession().setTabSize 2
@ace.pane = this
if File.isDirectory filename
File.changeWorkingDirectory filename
activeWindow.setTitle _.last filename.split '/'
editor.getSession().setValue ""
@ace.getSession().on 'change', ->
activeWindow.setDirty true
el = document.body
el.addEventListener 'DOMNodeInsertedIntoDocument', =>
@resize()
el.addEventListener 'DOMNodeRemovedFromDocument', =>
@resize()
# bug #13
@resize 200
save: ->
File.write @filename, @ace.getSession().getValue()
activeWindow.setDirty false
else
if /png|jpe?g|gif/i.test filename
App.openURL filename
else
activeWindow.setTitle _.last filename.split '/'
editor.getSession().setValue File.read filename
activeWindow.setDirty false
editor._emit 'open', { filename }
saveAs = ->
if file = App.savePanel()
filename = file
activeWindow.setTitle _.last filename.split '/'
save()
exports.bindKey = bindKey = (name, shortcut, callback) ->
canon.addCommand
name: name
exec: callback
bindKey:
win: null
mac: shortcut
sender: 'editor'
exports.resize = (timeout=1) ->
setTimeout ->
editor.focus()
editor.resize()
, timeout
@ace._emit 'save', { @filename }
exports.resize(200)
open: (path) ->
@filename = path
if File.isDirectory @filename
File.changeWorkingDirectory @filename
activeWindow.setTitle _.last @filename.split '/'
@ace.getSession().setValue ""
activeWindow.setDirty false
else
if /png|jpe?g|gif/i.test @filename
App.openURL @filename
else
activeWindow.setTitle _.last @filename.split '/'
@ace.getSession().setValue File.read @filename
activeWindow.setDirty false
@ace._emit 'open', { @filename }
saveAs: ->
if file = App.savePanel()
@filename = file
activeWindow.setTitle _.last @filename.split '/'
@save()
resize: (timeout=1) ->
setTimeout =>
@ace.focus()
@ace.resize()
, timeout
#
# keybindings
#
bindKey 'open', 'Command-O', (env, args, request) ->
if file = App.openPanel()
open file
env.editor.pane.open file
bindKey 'openURL', 'Command-Shift-O', (env, args, request) ->
if url = prompt "Enter URL:"
App.openURL url
bindKey 'saveAs', 'Command-Shift-S', (env, args, request) ->
saveAs()
env.editor.pane.saveAs()
bindKey 'save', 'Command-S', (env, args, request) ->
if filename then save() else saveAs()
doc = env.editor.pane
if doc.filename then doc.save() else doc.saveAs()
bindKey 'new', 'Command-N', (env, args, request) ->
App.newWindow()
bindKey 'copy', 'Command-C', (env, args, request) ->
editor = env.editor
text = editor.getSession().doc.getTextRange editor.getSelectionRange()
App.writeToPasteboard text
bindKey 'cut', 'Command-X', (env, args, request) ->
editor = env.editor
text = editor.getSession().doc.getTextRange editor.getSelectionRange()
App.writeToPasteboard text
editor.session.remove editor.getSelectionRange()

11
src/keybinder.coffee Normal file
View File

@@ -0,0 +1,11 @@
ace = require 'ace/ace'
canon = require 'pilot/canon'
exports.bindKey = (name, shortcut, callback) ->
canon.addCommand
name: name
exec: callback
bindKey:
win: null
mac: shortcut
sender: 'editor'

2
src/pane.coffee Normal file
View File

@@ -0,0 +1,2 @@
module.exports =
class Pane

View File

@@ -1,6 +1,9 @@
# yay!
App = require 'app'
Window = require 'window'
App.setActiveWindow new Window controller: WindowController
require 'editor'
Editor = require 'editor'
App.activeWindow.document = new Editor
require 'plugins'

View File

@@ -1,20 +1,26 @@
$ = require 'jquery'
App = require 'app'
Pane = require 'pane'
module.exports =
class Window
class Window extends Pane
controller: null
document: null
nswindow: null
panes: []
constructor: (options={}) ->
@controller = options.controller
@document = options.document
@nswindow = options.controller?.window
keymap:
'Command-N' : 'new'
'Command-O' : 'open'
'Command-Shift-O' : 'openURL'
'Command-Ctrl-K' : 'showConsole'
'Command-Ctrl-R' : 'reload'
addPane: (position, html) ->
Editor = require 'editor'
initialize: ->
@nswindow = @controller?.window
addPane: ({position, html}) ->
verticalDiv = $('#app-vertical')
horizontalDiv = $('#app-horizontal')
@@ -22,12 +28,6 @@ class Window
el.setAttribute 'class', "pane " + position
el.innerHTML = html
el.addEventListener 'DOMNodeInsertedIntoDocument', ->
Editor.resize()
el.addEventListener 'DOMNodeRemovedFromDocument', ->
Editor.resize()
switch position
when 'top', 'main'
verticalDiv.prepend el
@@ -43,6 +43,10 @@ class Window
close: ->
@controller.close()
reload: ->
App.newWindow()
@close()
isDirty: ->
@nswindow.isDocumentEdited()
@@ -50,9 +54,22 @@ class Window
setDirty: (bool) ->
@nswindow.setDocumentEdited bool
inspector:->
inspector: ->
@_inspector ?= WindowController.webView.inspector
new: ->
App.newWindow()
open: (path) ->
@document?.open path
openURL: (url) ->
if url = prompt "Enter URL:"
App.openURL url
showConsole: ->
@inspector().showConsole(1)
title: ->
@nswindow.title