- don't store state in the cursor layer

- move overwrite setting to the session
This commit is contained in:
Fabian Jakobs 2011-02-21 08:24:58 +01:00
commit 9e8b49224f
4 changed files with 58 additions and 57 deletions

View file

@ -177,6 +177,22 @@ var EditSession = function(text, mode) {
return this.$useSoftTabs && (position.column % this.$tabSize == 0); return this.$useSoftTabs && (position.column % this.$tabSize == 0);
}; };
this.$overwrite = false;
this.setOverwrite = function(overwrite) {
if (this.$overwrite == overwrite) return;
this.$overwrite = overwrite;
this._dispatchEvent("changeOverwrite");
};
this.getOverwrite = function() {
return this.$overwrite;
};
this.toggleOverwrite = function() {
this.setOverwrite(!this.$overwrite);
};
this.getBreakpoints = function() { this.getBreakpoints = function() {
return this.$breakpoints; return this.$breakpoints;
}; };

View file

@ -121,14 +121,15 @@ var Editor =function(renderer, session) {
if (this.session) { if (this.session) {
var oldSession = this.session; var oldSession = this.session;
this.session.removeEventListener("change", this.$onDocumentChange); this.session.removeEventListener("change", this.$onDocumentChange);
this.session.removeEventListener("changeMode", this.$onDocumentModeChange); this.session.removeEventListener("changeMode", this.$onModeChange);
this.session.removeEventListener("changeTabSize", this.$onDocumentChangeTabSize); this.session.removeEventListener("changeTabSize", this.$onChangeTabSize);
this.session.removeEventListener("changeWrapLimit", this.$onDocumentChangeWrapLimit); this.session.removeEventListener("changeWrapLimit", this.$onChangeWrapLimit);
this.session.removeEventListener("changeWrapMode", this.$onDocumentChangeWrapMode); this.session.removeEventListener("changeWrapMode", this.$onChangeWrapMode);
this.session.removeEventListener("changeFrontMarker", this.$onChangeFrontMarker); this.session.removeEventListener("changeFrontMarker", this.$onChangeFrontMarker);
this.session.removeEventListener("changeBackMarker", this.$onChangeBackMarker); this.session.removeEventListener("changeBackMarker", this.$onChangeBackMarker);
this.session.removeEventListener("changeBreakpoint", this.$onDocumentChangeBreakpoint); this.session.removeEventListener("changeBreakpoint", this.$onChangeBreakpoint);
this.session.removeEventListener("changeAnnotation", this.$onDocumentChangeAnnotation); this.session.removeEventListener("changeAnnotation", this.$onChangeAnnotation);
this.session.removeEventListener("changeOverwrite", this.$onCursorChange);
var selection = this.session.getSelection(); var selection = this.session.getSelection();
selection.removeEventListener("changeCursor", this.$onCursorChange); selection.removeEventListener("changeCursor", this.$onCursorChange);
@ -143,17 +144,17 @@ var Editor =function(renderer, session) {
session.addEventListener("change", this.$onDocumentChange); session.addEventListener("change", this.$onDocumentChange);
this.renderer.setSession(session); this.renderer.setSession(session);
this.$onDocumentModeChange = this.onDocumentModeChange.bind(this); this.$onModeChange = this.onModeChange.bind(this);
session.addEventListener("changeMode", this.$onDocumentModeChange); session.addEventListener("changeMode", this.$onModeChange);
this.$onDocumentChangeTabSize = this.renderer.updateText.bind(this.renderer); this.$onChangeTabSize = this.renderer.updateText.bind(this.renderer);
session.addEventListener("changeTabSize", this.$onDocumentChangeTabSize); session.addEventListener("changeTabSize", this.$onChangeTabSize);
this.$onDocumentChangeWrapLimit = this.onDocumentChangeWrapLimit.bind(this); this.$onChangeWrapLimit = this.onChangeWrapLimit.bind(this);
session.addEventListener("changeWrapLimit", this.$onDocumentChangeWrapLimit); session.addEventListener("changeWrapLimit", this.$onChangeWrapLimit);
this.$onDocumentChangeWrapMode = this.onDocumentChangeWrapMode.bind(this); this.$onChangeWrapMode = this.onChangeWrapMode.bind(this);
session.addEventListener("changeWrapMode", this.$onDocumentChangeWrapMode); session.addEventListener("changeWrapMode", this.$onChangeWrapMode);
this.$onChangeFrontMarker = this.onChangeFrontMarker.bind(this); this.$onChangeFrontMarker = this.onChangeFrontMarker.bind(this);
this.session.addEventListener("changeFrontMarker", this.$onChangeFrontMarker); this.session.addEventListener("changeFrontMarker", this.$onChangeFrontMarker);
@ -161,21 +162,22 @@ var Editor =function(renderer, session) {
this.$onChangeBackMarker = this.onChangeBackMarker.bind(this); this.$onChangeBackMarker = this.onChangeBackMarker.bind(this);
this.session.addEventListener("changeBackMarker", this.$onChangeBackMarker); this.session.addEventListener("changeBackMarker", this.$onChangeBackMarker);
this.$onDocumentChangeBreakpoint = this.onDocumentChangeBreakpoint.bind(this); this.$onChangeBreakpoint = this.onChangeBreakpoint.bind(this);
this.session.addEventListener("changeBreakpoint", this.$onDocumentChangeBreakpoint); this.session.addEventListener("changeBreakpoint", this.$onChangeBreakpoint);
this.$onDocumentChangeAnnotation = this.onDocumentChangeAnnotation.bind(this); this.$onChangeAnnotation = this.onChangeAnnotation.bind(this);
this.session.addEventListener("changeAnnotation", this.$onDocumentChangeAnnotation); this.session.addEventListener("changeAnnotation", this.$onChangeAnnotation);
this.$onCursorChange = this.onCursorChange.bind(this);
this.session.addEventListener("changeOverwrite", this.$onCursorChange);
this.selection = session.getSelection(); this.selection = session.getSelection();
this.$onCursorChange = this.onCursorChange.bind(this);
this.selection.addEventListener("changeCursor", this.$onCursorChange); this.selection.addEventListener("changeCursor", this.$onCursorChange);
this.$onSelectionChange = this.onSelectionChange.bind(this); this.$onSelectionChange = this.onSelectionChange.bind(this);
this.selection.addEventListener("changeSelection", this.$onSelectionChange); this.selection.addEventListener("changeSelection", this.$onSelectionChange);
this.onDocumentModeChange(); this.onModeChange();
this.bgTokenizer.setDocument(session.getDocument()); this.bgTokenizer.setDocument(session.getDocument());
this.bgTokenizer.start(0); this.bgTokenizer.start(0);
@ -183,8 +185,8 @@ var Editor =function(renderer, session) {
this.onSelectionChange(); this.onSelectionChange();
this.onChangeFrontMarker(); this.onChangeFrontMarker();
this.onChangeBackMarker(); this.onChangeBackMarker();
this.onDocumentChangeBreakpoint(); this.onChangeBreakpoint();
this.onDocumentChangeAnnotation(); this.onChangeAnnotation();
this.renderer.scrollToRow(session.getScrollTopRow()); this.renderer.scrollToRow(session.getScrollTopRow());
this.renderer.updateFull(); this.renderer.updateFull();
@ -281,7 +283,7 @@ var Editor =function(renderer, session) {
this.renderer.updateLines(range.start.row, lastRow); this.renderer.updateLines(range.start.row, lastRow);
// update cursor because tab characters can influence the cursor position // update cursor because tab characters can influence the cursor position
this.renderer.updateCursor(this.getCursorPosition(), this.$overwrite); this.renderer.updateCursor();
}; };
this.onTokenizerUpdate = function(e) { this.onTokenizerUpdate = function(e) {
@ -290,7 +292,7 @@ var Editor =function(renderer, session) {
}; };
this.onCursorChange = function(e) { this.onCursorChange = function(e) {
this.renderer.updateCursor(this.getCursorPosition(), this.$overwrite); this.renderer.updateCursor();
if (!this.$blockScrolling) { if (!this.$blockScrolling) {
this.renderer.scrollCursorIntoView(); this.renderer.scrollCursorIntoView();
@ -347,15 +349,15 @@ var Editor =function(renderer, session) {
this.renderer.updateBackMarkers(); this.renderer.updateBackMarkers();
}; };
this.onDocumentChangeBreakpoint = function() { this.onChangeBreakpoint = function() {
this.renderer.setBreakpoints(this.session.getBreakpoints()); this.renderer.setBreakpoints(this.session.getBreakpoints());
}; };
this.onDocumentChangeAnnotation = function() { this.onChangeAnnotation = function() {
this.renderer.setAnnotations(this.session.getAnnotations()); this.renderer.setAnnotations(this.session.getAnnotations());
}; };
this.onDocumentModeChange = function() { this.onModeChange = function() {
var mode = this.session.getMode(); var mode = this.session.getMode();
if (this.mode == mode) if (this.mode == mode)
return; return;
@ -374,12 +376,11 @@ var Editor =function(renderer, session) {
this.renderer.setTokenizer(this.bgTokenizer); this.renderer.setTokenizer(this.bgTokenizer);
}; };
this.onDocumentChangeWrapLimit = function() { this.onChangeWrapLimit = function() {
this.renderer.updateCursor(this.getCursorPosition(), this.$overwrite);
this.renderer.updateFull(); this.renderer.updateFull();
}; };
this.onDocumentChangeWrapMode = function() { this.onChangeWrapMode = function() {
this.renderer.onResize(true); this.renderer.onResize(true);
}; };
@ -413,7 +414,8 @@ var Editor =function(renderer, session) {
if (!this.selection.isEmpty()) { if (!this.selection.isEmpty()) {
var cursor = this.session.remove(this.getSelectionRange()); var cursor = this.session.remove(this.getSelectionRange());
this.clearSelection(); this.clearSelection();
} else if (this.$overwrite){ }
else if (this.session.getOverwrite()) {
var range = new Range.fromPoints(cursor, cursor); var range = new Range.fromPoints(cursor, cursor);
range.end.column += text.length; range.end.column += text.length;
this.session.remove(range); this.session.remove(range);
@ -481,25 +483,16 @@ var Editor =function(renderer, session) {
this.keyBinding.onCommandKey(e, hashId, keyCode); this.keyBinding.onCommandKey(e, hashId, keyCode);
}; };
this.$overwrite = false;
this.setOverwrite = function(overwrite) { this.setOverwrite = function(overwrite) {
if (this.$overwrite == overwrite) return; this.session.setOverwrite();
this.$overwrite = overwrite;
this.$blockScrolling += 1;
this.onCursorChange();
this.$blockScrolling -= 1;
this._dispatchEvent("changeOverwrite", {data: overwrite});
}; };
this.getOverwrite = function() { this.getOverwrite = function() {
return this.$overwrite; return this.session.getOverwrite();
}; };
this.toggleOverwrite = function() { this.toggleOverwrite = function() {
this.setOverwrite(!this.$overwrite); this.session.toggleOverwrite();
}; };
this.setScrollSpeed = function(speed) { this.setScrollSpeed = function(speed) {

View file

@ -57,11 +57,6 @@ var Cursor = function(parentEl) {
this.session = session; this.session = session;
}; };
this.setCursor = function(position, overwrite) {
this.position = position;
this.overwrite = overwrite;
};
this.hideCursor = function() { this.hideCursor = function() {
this.isVisible = false; this.isVisible = false;
if (this.cursor.parentNode) { if (this.cursor.parentNode) {
@ -95,14 +90,15 @@ var Cursor = function(parentEl) {
}; };
this.getPixelPosition = function(onScreen) { this.getPixelPosition = function(onScreen) {
if (!this.config || !this.position) { if (!this.config || !this.session) {
return { return {
left : 0, left : 0,
top : 0 top : 0
}; };
} }
var pos = this.session.documentToScreenPosition(this.position); var position = this.session.selection.getCursor();
var pos = this.session.documentToScreenPosition(position);
var cursorLeft = Math.round(pos.column * this.config.characterWidth); var cursorLeft = Math.round(pos.column * this.config.characterWidth);
var cursorTop = (pos.row - (onScreen ? this.config.firstRowScreen : 0)) * var cursorTop = (pos.row - (onScreen ? this.config.firstRowScreen : 0)) *
this.config.lineHeight; this.config.lineHeight;
@ -114,9 +110,6 @@ var Cursor = function(parentEl) {
}; };
this.update = function(config) { this.update = function(config) {
if (!this.position)
return;
this.config = config; this.config = config;
this.pixelPos = this.getPixelPosition(true); this.pixelPos = this.getPixelPosition(true);
@ -130,7 +123,7 @@ var Cursor = function(parentEl) {
this.element.appendChild(this.cursor); this.element.appendChild(this.cursor);
} }
if (this.overwrite) { if (this.session.getOverwrite()) {
dom.addCssClass(this.cursor, "ace_overwrite"); dom.addCssClass(this.cursor, "ace_overwrite");
} else { } else {
dom.removeCssClass(this.cursor, "ace_overwrite"); dom.removeCssClass(this.cursor, "ace_overwrite");

View file

@ -568,8 +568,7 @@ var VirtualRenderer = function(container, theme) {
this.$loop.schedule(this.CHANGE_GUTTER); this.$loop.schedule(this.CHANGE_GUTTER);
}; };
this.updateCursor = function(position, overwrite) { this.updateCursor = function() {
this.$cursorLayer.setCursor(position, overwrite);
this.$loop.schedule(this.CHANGE_CURSOR); this.$loop.schedule(this.CHANGE_CURSOR);
}; };