changing the document is now possible
This commit is contained in:
parent
328dfacf47
commit
aecaea3ef1
6 changed files with 276 additions and 63 deletions
|
|
@ -1,13 +1,10 @@
|
|||
ace.provide("ace.Editor");
|
||||
|
||||
ace.Editor = function(renderer, doc, mode) {
|
||||
ace.Editor = function(renderer, doc) {
|
||||
var container = renderer.getContainerElement();
|
||||
this.container = container;
|
||||
this.renderer = renderer;
|
||||
|
||||
this.setMode(mode || new ace.mode.Text());
|
||||
this.setDocument(doc || new ace.TextDocument(""));
|
||||
|
||||
this.textInput = new ace.TextInput(container, this);
|
||||
new ace.KeyBinding(container, this);
|
||||
|
||||
|
|
@ -20,68 +17,59 @@ ace.Editor = function(renderer, doc, mode) {
|
|||
this._highlightLineMarker = null;
|
||||
this._blockScrolling = false;
|
||||
|
||||
this.renderer.draw();
|
||||
this.onCursorChange();
|
||||
this.onSelectionChange();
|
||||
|
||||
this._initialized = true;
|
||||
this.setDocument(doc || new ace.TextDocument(""));
|
||||
};
|
||||
|
||||
|
||||
ace.Editor.prototype.setDocument = function(doc) {
|
||||
// TODO: document change is not yet supported
|
||||
if (this.doc == doc) return;
|
||||
|
||||
if (this.doc) {
|
||||
throw new Error("TODO: document change is not yet supported");
|
||||
this.doc.removeEventListener("change", this._onDocumentChange);
|
||||
this.doc.removeEventListener("changeMode", this._onDocumentModeChange);
|
||||
this.doc.removeEventListener("changeTabSize", this._onDocumentChangeTabSize);
|
||||
|
||||
var selection = this.doc.getSelection();
|
||||
this.selection.removeEventListener("changeCursor", this._onCursorChange);
|
||||
this.selection.removeEventListener("changeSelection", this._onSelectionChange);
|
||||
}
|
||||
|
||||
this.doc = doc;
|
||||
|
||||
doc.addEventListener("change", ace.bind(this.onDocumentChange, this));
|
||||
this._onDocumentChange = ace.bind(this.onDocumentChange, this);
|
||||
doc.addEventListener("change", this._onDocumentChange);
|
||||
this.renderer.setDocument(doc);
|
||||
|
||||
var self = this;
|
||||
doc.addEventListener("changeTabSize", function() {
|
||||
self.renderer.draw();
|
||||
});
|
||||
this._onDocumentModeChange = ace.bind(this.onDocumentModeChange, this);
|
||||
doc.addEventListener("changeMode", this._onDocumentModeChange);
|
||||
|
||||
this._onDocumentChangeTabSize = ace.bind(this.renderer.draw, this.renderer);
|
||||
doc.addEventListener("changeTabSize", this._onDocumentChangeTabSize);
|
||||
|
||||
this.selection = doc.getSelection();
|
||||
|
||||
var onCursorChange = ace.bind(this.onCursorChange, this);
|
||||
this.selection.addEventListener("changeCursor", onCursorChange);
|
||||
|
||||
var onSelectionChange = ace.bind(this.onSelectionChange, this);
|
||||
this.selection.addEventListener("changeSelection", onSelectionChange);
|
||||
this._onCursorChange = ace.bind(this.onCursorChange, this);
|
||||
this.selection.addEventListener("changeCursor", this._onCursorChange);
|
||||
|
||||
this._onSelectionChange = ace.bind(this.onSelectionChange, this);
|
||||
this.selection.addEventListener("changeSelection", this._onSelectionChange);
|
||||
|
||||
this.onDocumentModeChange();
|
||||
this.bgTokenizer.setLines(this.doc.lines);
|
||||
|
||||
this.renderer.draw();
|
||||
this.onCursorChange();
|
||||
this.onSelectionChange();
|
||||
};
|
||||
|
||||
ace.Editor.prototype.getDocument = function() {
|
||||
return this.doc;
|
||||
};
|
||||
|
||||
ace.Editor.prototype.getSelection = function() {
|
||||
return this.selection;
|
||||
};
|
||||
|
||||
ace.Editor.prototype.setMode = function(mode) {
|
||||
if (this.mode == mode) return;
|
||||
|
||||
this.mode = mode;
|
||||
var tokenizer = mode.getTokenizer();
|
||||
|
||||
if (!this.bgTokenizer) {
|
||||
var onUpdate = ace.bind(this.onTokenizerUpdate, this);
|
||||
this.bgTokenizer = new ace.BackgroundTokenizer(tokenizer);
|
||||
this.bgTokenizer.addEventListener("update", onUpdate);
|
||||
} else {
|
||||
this.bgTokenizer.setTokenizer(tokenizer);
|
||||
}
|
||||
|
||||
this.renderer.setTokenizer(this.bgTokenizer);
|
||||
|
||||
if (this._initialized) {
|
||||
this.renderer.draw();
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
ace.Editor.prototype.resize = function() {
|
||||
this.renderer.onResize();
|
||||
};
|
||||
|
|
@ -116,6 +104,9 @@ ace.Editor.prototype._highlightBrackets = function() {
|
|||
}, 10);
|
||||
};
|
||||
|
||||
ace.Editor.prototype.focus = function() {
|
||||
this.textInput.focus();
|
||||
};
|
||||
|
||||
ace.Editor.prototype.onFocus = function() {
|
||||
this.renderer.showCursor();
|
||||
|
|
@ -185,8 +176,27 @@ ace.Editor.prototype.onSelectionChange = function() {
|
|||
this.onCursorChange();
|
||||
};
|
||||
|
||||
ace.Editor.prototype.onDocumentModeChange = function() {
|
||||
var mode = this.doc.getMode();
|
||||
|
||||
this.mode = mode;
|
||||
var tokenizer = mode.getTokenizer();
|
||||
|
||||
if (!this.bgTokenizer) {
|
||||
var onUpdate = ace.bind(this.onTokenizerUpdate, this);
|
||||
this.bgTokenizer = new ace.BackgroundTokenizer(tokenizer);
|
||||
this.bgTokenizer.addEventListener("update", onUpdate);
|
||||
} else {
|
||||
this.bgTokenizer.setTokenizer(tokenizer);
|
||||
}
|
||||
|
||||
this.renderer.setTokenizer(this.bgTokenizer);
|
||||
this.renderer.draw();
|
||||
};
|
||||
|
||||
|
||||
ace.Editor.prototype.onMouseDown = function(e) {
|
||||
this.textInput.focus();
|
||||
this.focus();
|
||||
|
||||
var pageX = ace.getDocumentX(e);
|
||||
var pageY = ace.getDocumentY(e);
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@ ace.MEventEmitter.$initEvents = function() {
|
|||
|
||||
ace.MEventEmitter.$dispatchEvent = function(eventName, e) {
|
||||
var listeners = this._eventRegistry[eventName];
|
||||
if (!listeners) return;
|
||||
if (!listeners || !listeners.length) return;
|
||||
|
||||
var e = e || {};
|
||||
e.type = eventName;
|
||||
|
|
@ -24,4 +24,15 @@ ace.MEventEmitter.addEventListener = function(eventName, callback) {
|
|||
if (ace.arrayIndexOf(listeners, callback) == -1) {
|
||||
listeners.push(callback);
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
ace.MEventEmitter.removeEventListener = function(eventName, callback) {
|
||||
var listeners = this._eventRegistry[eventName];
|
||||
if (!listeners) {
|
||||
return;
|
||||
}
|
||||
var index = ace.arrayIndexOf(listeners, callback);
|
||||
if (index !== -1) {
|
||||
listeners.splice(index, 1);
|
||||
}
|
||||
};
|
||||
|
|
|
|||
|
|
@ -1,16 +1,19 @@
|
|||
ace.provide("ace.TextDocument");
|
||||
|
||||
ace.TextDocument = function(text) {
|
||||
ace.TextDocument = function(text, mode) {
|
||||
this.$initEvents();
|
||||
|
||||
this.lines = this._split(text);
|
||||
this.modified = true;
|
||||
this.selection = new ace.Selection(this);
|
||||
|
||||
this.listeners = [];
|
||||
|
||||
this.$initEvents();
|
||||
if (mode) {
|
||||
this.setMode(mode);
|
||||
}
|
||||
};
|
||||
ace.mixin(ace.TextDocument.prototype, ace.MEventEmitter);
|
||||
|
||||
ace.mixin(ace.TextDocument.prototype, ace.MEventEmitter);
|
||||
|
||||
ace.TextDocument.prototype._split = function(text) {
|
||||
return text.split(/\r\n|\r|\n/);
|
||||
|
|
@ -62,6 +65,21 @@ ace.TextDocument.prototype.getTabSize = function() {
|
|||
return this._tabSize;
|
||||
};
|
||||
|
||||
ace.TextDocument.prototype._mode = null;
|
||||
ace.TextDocument.prototype.setMode = function(mode) {
|
||||
if (this._mode === mode) return;
|
||||
|
||||
this._mode = mode;
|
||||
this.$dispatchEvent("changeMode");
|
||||
};
|
||||
|
||||
ace.TextDocument.prototype.getMode = function() {
|
||||
if (!this._mode) {
|
||||
this._mode = new ace.mode.Text();
|
||||
}
|
||||
return this._mode;
|
||||
};
|
||||
|
||||
ace.TextDocument.prototype.getWidth = function() {
|
||||
if (this.modified) {
|
||||
this.modified = false;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue