diff --git a/lib/ace/editor.js b/lib/ace/editor.js index 15330f73..9a27c9b2 100644 --- a/lib/ace/editor.js +++ b/lib/ace/editor.js @@ -125,6 +125,8 @@ var Editor = function(windowView, buffer) { this.setSession = function(session) { if (this.session == session) return; + + this.windowModel.setBuffer(session); if (this.session) { var oldSession = this.session; @@ -937,11 +939,11 @@ var Editor = function(windowView, buffer) { this.getFirstVisibleRow = function() { - return this.renderer.getFirstVisibleRow(); + return this.windowModel.getFirstVisibleRow(); }; this.getLastVisibleRow = function() { - return this.renderer.getLastVisibleRow(); + return this.windowModel.getLastVisibleRow(); }; this.isRowVisible = function(row) { diff --git a/lib/ace/model/window.js b/lib/ace/model/window.js index 7928af4c..73e65a03 100644 --- a/lib/ace/model/window.js +++ b/lib/ace/model/window.js @@ -51,7 +51,7 @@ var Window = exports.Window = function(theme) { this.theme = null; this.setTheme(theme); - this._buffer = null; + this.buffer = null; this.layerConfig = { width : 1, @@ -91,12 +91,52 @@ var Window = exports.Window = function(theme) { oop.implement(this, EventEmitter); + // VIEWPORT COMPUTATIONS + + this.getFirstVisibleRow = function() { + return this.layerConfig.firstRow; + }; + + this.getLastVisibleRow = function() { + return this.layerConfig.lastRow; + }; + + this.getFirstFullyVisibleRow = function() { + return this.layerConfig.firstRow + (this.layerConfig.offset === 0 ? 0 : 1); + }; + + this.getLastFullyVisibleRow = function() { + var flint = Math.floor((this.layerConfig.height + this.layerConfig.offset) / this.characterSize.height); + return this.layerConfig.firstRow - 1 + flint; + }; + + this.getCursorPixelPosition = function(onScreen) { + if (!this.buffer) { + return { + left: 0, + top: 0 + } + } + var position = this.buffer.selection.getCursor(); + var pos = this.buffer.documentToScreenPosition(position); + var cursorLeft = Math.round( + this.padding + + pos.column * this.characterSize.width + ); + var cursorTop = (pos.row - (onScreen ? this.layerConfig.firstRowScreen : 0)) + * this.characterSize.height; + + return { + left : cursorLeft, + top : cursorTop + }; + }; this.setBuffer = function(buffer) { - if (this._buffer === buffer) + if (this.buffer === buffer) return; - var oldValue = this._buffer; - this._buffer = buffer; + var oldValue = this.buffer; + this.buffer = buffer; this._emit("changeBuffer", {oldValue: oldValue, value: buffer}); }; diff --git a/lib/ace/view/layer/cursor.js b/lib/ace/view/layer/cursor.js index 14816cf6..ee482254 100644 --- a/lib/ace/view/layer/cursor.js +++ b/lib/ace/view/layer/cursor.js @@ -89,33 +89,8 @@ var Cursor = function(model, parentEl) { }, 1000); }; - this.getPixelPosition = function(onScreen) { - if (!this.config || !this.session) { - return { - left : 0, - top : 0 - }; - } - - var position = this.session.selection.getCursor(); - var pos = this.session.documentToScreenPosition(position); - var cursorLeft = Math.round( - this.model.padding - + pos.column * this.config.characterWidth - ); - var cursorTop = (pos.row - (onScreen ? this.config.firstRowScreen : 0)) - * this.config.lineHeight; - - return { - left : cursorLeft, - top : cursorTop - }; - }; - this.update = function(config) { - this.config = config; - - this.pixelPos = this.getPixelPosition(true); + this.pixelPos = this.model.getCursorPixelPosition(true); this.cursor.style.left = this.pixelPos.left + "px"; this.cursor.style.top = this.pixelPos.top + "px"; diff --git a/lib/ace/view/window_view.js b/lib/ace/view/window_view.js index c96be8fb..b66facc9 100644 --- a/lib/ace/view/window_view.js +++ b/lib/ace/view/window_view.js @@ -89,8 +89,6 @@ var WindowView = function(windowModel, container) { windowModel.setComputedCharacterSize(measureText.getCharacterSize()); this.$cursorLayer = new CursorLayer(windowModel, this.content); - this.$cursorPadding = 8; - this.$horizScroll = true; this.scrollBar = new ScrollBar(container); @@ -278,7 +276,7 @@ var WindowView = function(windowModel, container) { if (useragent.isIE) return; - var pos = this.$cursorLayer.getPixelPosition(); + var pos = this.model.getCursorPixelPosition(); if (!pos) return; @@ -289,23 +287,6 @@ var WindowView = function(windowModel, container) { textarea.style.top = (bounds.top + pos.top - this.scrollTop + offset) + "px"; }; - this.getFirstVisibleRow = function() { - return this.model.layerConfig.firstRow; - }; - - this.getFirstFullyVisibleRow = function() { - return this.model.layerConfig.firstRow + (this.model.layerConfig.offset === 0 ? 0 : 1); - }; - - this.getLastFullyVisibleRow = function() { - var flint = Math.floor((this.model.layerConfig.height + this.model.layerConfig.offset) / this.model.layerConfig.lineHeight); - return this.model.layerConfig.firstRow - 1 + flint; - }; - - this.getLastVisibleRow = function() { - return this.model.layerConfig.lastRow; - }; - this.updatePadding = function() { this.$textLayer.element.style.padding = "0 " + this.model.padding + "px"; this.$loop.schedule(this.CHANGE_FULL); @@ -565,7 +546,7 @@ var WindowView = function(windowModel, container) { if (this.model.size.scrollerHeight === 0) return; - var pos = this.$cursorLayer.getPixelPosition(); + var pos = this.model.getCursorPixelPosition(); var left = pos.left + this.model.padding; var top = pos.top; @@ -688,7 +669,7 @@ var WindowView = function(windowModel, container) { this.$composition.innerHTML = " "; - var pos = this.$cursorLayer.getPixelPosition(); + var pos = this.model.getCursorPixelPosition(); var style = this.$composition.style; style.top = pos.top + "px"; style.left = (pos.left + this.model.padding) + "px"; diff --git a/lib/ace/view/window_view_mock.js b/lib/ace/view/window_view_mock.js index b215a449..dc41554f 100644 --- a/lib/ace/view/window_view_mock.js +++ b/lib/ace/view/window_view_mock.js @@ -42,28 +42,10 @@ var Window = require("ace/model/window").Window; exports.MockRenderer = exports.WindowViewMock = WindowViewMock = function(visibleRowCount) { this.container = document.createElement("div"); - this.visibleRowCount = visibleRowCount || 20; - this.model = null; - - this.layerConfig = { - firstVisibleRow : 0, - lastVisibleRow : this.visibleRowCount - }; - this.isWindowViewMock = true; - this.model = new Window(); }; - -WindowViewMock.prototype.getFirstVisibleRow = function() { - return this.layerConfig.firstVisibleRow; -}; - -WindowViewMock.prototype.getLastVisibleRow = function() { - return this.layerConfig.lastVisibleRow; -}; - WindowViewMock.prototype.getContainerElement = function() { return this.container; }; @@ -76,9 +58,6 @@ WindowViewMock.prototype.getTextAreaContainer = function() { return this.container; }; -WindowViewMock.prototype.moveTextAreaToCursor = function() { -}; - WindowViewMock.prototype.setModel = function(model) { this.model = model; }; @@ -91,51 +70,14 @@ WindowViewMock.prototype.getSession = function(session) { return this.session; }; -WindowViewMock.prototype.setTokenizer = function() { -}; - -WindowViewMock.prototype.updateCursor = function() { -}; - -WindowViewMock.prototype.scrollToLine = function(line, center) { - var lineHeight = { lineHeight: 16 }; - var row = 0; - for (var l = 1; l < line; l++) { - row += this.session.getRowHeight(lineHeight, l-1) / lineHeight.lineHeight; - } - - if (center) { - row -= this.visibleRowCount / 2; - } - this.scrollToRow(row); -}; - -WindowViewMock.prototype.scrollCursorIntoView = function() { - var cursor = this.session.getSelection().getCursor(); - if (cursor.row < this.layerConfig.firstVisibleRow) { - this.scrollToRow(cursor.row); - } - else if (cursor.row > this.layerConfig.lastVisibleRow) { - this.scrollToRow(cursor.row); - } -}; - -WindowViewMock.prototype.scrollToRow = function(row) { - var row = Math.min(this.session.getLength() - this.visibleRowCount, Math.max(0, - row)); - this.layerConfig.firstVisibleRow = row; - this.layerConfig.lastVisibleRow = row + this.visibleRowCount; -}; - -WindowViewMock.prototype.getScrollTopRow = function() { - return this.layerConfig.firstVisibleRow; -}; - [ + "moveTextAreaToCursor", + "setTokenizer", "draw", "updateLines", "updateBackMarkers", "updateFrontMarkers", + "updateCursor", "updateShowInvisibles", "updatePrintMargin", "updateShowGutter",