ace Editor uses Document held by Buffer.

This commit is contained in:
Corey Johnson & Nathan Sobo 2011-12-15 15:06:34 -08:00
parent c2c17f846e
commit c16c25b69b
5 changed files with 25 additions and 13 deletions

View File

@ -23,4 +23,4 @@ describe "App", ->
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)
expect(newWindow.editor.buffer.getText()).toEqual fs.read(filePath)

View File

@ -6,4 +6,4 @@ describe 'Buffer', ->
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)
expect(buffer.getText()).toBe fs.read(filePath)

View File

@ -17,10 +17,10 @@ describe "Editor", ->
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 ''
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
expect(editor.aceEditor.getSession().getValue()).toBe editor.buffer.getText()
describe 'destroy', ->
it 'destroys the ace editor and removes #editor from the dom.', ->
@ -28,7 +28,10 @@ describe "Editor", ->
editor.destroy()
expect(editor.aceEditor.destroy).toHaveBeenCalled()
expect(mainDiv.children('#editor').length).toBe 0
expect(mainDiv.children('.editor').length).toBe 0
describe "when the text is changed via the ace editor", ->
it "updates the buffer text", ->
expect(editor.buffer.getText()).not.toMatch /^.ooo/
editor.aceEditor.getSession().insert {row: 0, column: 1}, 'ooo'
expect(editor.buffer.getText()).toMatch /^.ooo/

View File

@ -1,9 +1,14 @@
fs = require 'fs'
{Document} = require 'ace/document'
module.exports =
class Buffer
text: null
aceDocument: null
url: null
constructor: (@url) ->
@text = fs.read @url
@aceDocument = new Document fs.read(@url)
getText: ->
@aceDocument.getValue()

View File

@ -1,4 +1,5 @@
Buffer = require 'buffer'
{EditSession} = require 'ace/edit_session'
ace = require 'ace/ace'
$ = require 'jquery'
@ -10,14 +11,17 @@ class Editor
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'
editorElement = $("<div class='editor'>")
$('#main').append(editorElement)
@aceEditor = ace.edit editorElement[0]
@aceEditor.setSession(new EditSession(@buffer.aceDocument))
@aceEditor.setTheme(require "ace/theme/twilight")
window.x = this
getAceSession: ->
@aceEditor.getSession()
destroy: ->
@aceEditor.destroy()