Attach and populate the ace editor with buffer text when an editor is created.

This commit is contained in:
Corey Johnson & Nathan Sobo
2011-12-15 14:13:34 -08:00
parent 32631468e7
commit 27dd97069a
5 changed files with 65 additions and 2 deletions

View File

@@ -1,4 +1,5 @@
App = require 'app'
fs = require 'fs'
describe "App", ->
app = null
@@ -18,7 +19,8 @@ describe "App", ->
expect(app.windows().length).toBe 1
newWindow = app.windows()[0]
expect(newWindow.editor).toBeDefined()
expect(newWindow.editor.buffer).toBeDefined()
expect(newWindow.editor.buffer.url).toEqual filePath
expect(newWindow.editor.buffer.text).toEqual fs.read(filePath)

View File

@@ -0,0 +1,9 @@
Buffer = require 'buffer'
fs = require 'fs'
describe 'Buffer', ->
describe 'constructor', ->
it "loads the contents of the given url", ->
filePath = require.resolve 'fixtures/sample.txt'
buffer = new Buffer filePath
expect(buffer.text).toBe fs.read(filePath)

View File

@@ -0,0 +1,34 @@
Editor = require 'editor'
$ = require 'jquery'
ck = require 'coffeekup'
describe "Editor", ->
mainDiv = null; editor = null; filePath = null
beforeEach ->
filePath = require.resolve 'fixtures/sample.txt'
mainDiv = $("<div id='main'>")
$("#jasmine-content").append(mainDiv)
editor = new Editor filePath
afterEach ->
editor.destroy()
describe "constructor", ->
it "attaches itself to the #main element and opens a buffer with the given url", ->
expect(editor.buffer.url).toEqual filePath
expect(mainDiv.children('#editor').html()).not.toBe ''
it "populates the editor with the contents of the buffer", ->
expect(editor.aceEditor.getSession().getValue()).toBe editor.buffer.text
describe 'destroy', ->
it 'destroys the ace editor and removes #editor from the dom.', ->
spyOn editor.aceEditor, 'destroy'
editor.destroy()
expect(editor.aceEditor.destroy).toHaveBeenCalled()
expect(mainDiv.children('#editor').length).toBe 0
describe "when the text is changed via the ace editor", ->
it "updates the buffer text", ->

View File

@@ -1,6 +1,9 @@
fs = require 'fs'
module.exports =
class Buffer
text: null
url: null
constructor: (@url) ->
@text = fs.read @url

View File

@@ -1,9 +1,24 @@
Buffer = require 'buffer'
ace = require 'ace/ace'
$ = require 'jquery'
module.exports =
class Editor
aceEditor: null
buffer: null
constructor: (url) ->
@buffer = new Buffer(url)
@buildAceEditor()
@setText @buffer.text
setText: (text) ->
@aceEditor.getSession().setValue @buffer.text
buildAceEditor: ->
$('#main').append("<div id='editor'>")
@aceEditor = ace.edit 'editor'
destroy: ->
@aceEditor.destroy()