add support for "gutter mouse events"

This commit is contained in:
Fabian Jakobs 2010-05-07 16:00:05 +02:00
commit 7f5ea3d277
2 changed files with 58 additions and 15 deletions

View file

@ -35,6 +35,32 @@ ace.Editor = function(renderer, doc) {
(function(){ (function(){
ace.implement(this, ace.MEventEmitter);
this.$forwardEvents = {
gutterclick: 1,
gutterdblclick: 1
};
this.$originalAddEventListener = this.addEventListener;
this.$originalRemoveEventListener = this.removeEventListener;
this.addEventListener = function(eventName, callback) {
if (this.$forwardEvents[eventName]) {
return this.renderer.addEventListener(eventName, callback);
} else {
return this.$originalAddEventListener(eventName, callback);
}
};
this.removeEventListener = function(eventName, callback) {
if (this.$forwardEvents[eventName]) {
return this.renderer.removeEventListener(eventName, callback);
} else {
return this.$originalRemoveEventListener(eventName, callback);
}
};
this.setDocument = function(doc) { this.setDocument = function(doc) {
if (this.doc == doc) return; if (this.doc == doc) return;
@ -227,8 +253,7 @@ ace.Editor = function(renderer, doc) {
if (mousePageX === undefined || mousePageY === undefined) if (mousePageX === undefined || mousePageY === undefined)
return; return;
selectionLead = _self.renderer.screenToTextCoordinates(mousePageX, selectionLead = _self.renderer.screenToTextCoordinates(mousePageX, mousePageY);
mousePageY);
_self.selection.selectToPosition(selectionLead); _self.selection.selectToPosition(selectionLead);
_self.renderer.scrollCursorIntoView(); _self.renderer.scrollCursorIntoView();

View file

@ -8,15 +8,15 @@ ace.VirtualRenderer = function(container) {
this.scroller.className = "scroller"; this.scroller.className = "scroller";
this.container.appendChild(this.scroller); this.container.appendChild(this.scroller);
this.gutter = document.createElement("div"); this.$gutter = document.createElement("div");
this.gutter.className = "gutter"; this.$gutter.className = "gutter";
this.container.appendChild(this.gutter); this.container.appendChild(this.$gutter);
this.content = document.createElement("div"); this.content = document.createElement("div");
this.scroller.appendChild(this.content); this.scroller.appendChild(this.content);
this.gutterLayer = new ace.layer.Gutter(this.gutter); this.$gutterLayer = new ace.layer.Gutter(this.$gutter);
this.markerLayer = new ace.layer.Marker(this.content); this.$markerLayer = new ace.layer.Marker(this.content);
var textLayer = this.textLayer = new ace.layer.Text(this.content); var textLayer = this.textLayer = new ace.layer.Text(this.content);
this.canvas = textLayer.element; this.canvas = textLayer.element;
@ -26,7 +26,7 @@ ace.VirtualRenderer = function(container) {
this.cursorLayer = new ace.layer.Cursor(this.content); this.cursorLayer = new ace.layer.Cursor(this.content);
this.layers = [ this.markerLayer, textLayer, this.cursorLayer ]; this.layers = [ this.$markerLayer, textLayer, this.cursorLayer ];
this.scrollBar = new ace.ScrollBar(container); this.scrollBar = new ace.ScrollBar(container);
this.scrollBar.addEventListener("scroll", ace.bind(this.onScroll, this)); this.scrollBar.addEventListener("scroll", ace.bind(this.onScroll, this));
@ -40,14 +40,19 @@ ace.VirtualRenderer = function(container) {
this.$updatePrintMargin(); this.$updatePrintMargin();
this.onResize(); this.onResize();
ace.addListener(this.$gutter, "click", ace.bind(this.$onGutterClick, this));
ace.addListener(this.$gutter, "dblclick", ace.bind(this.$onGutterClick, this));
}; };
(function() { (function() {
ace.implement(this, ace.MEventEmitter);
this.setDocument = function(doc) { this.setDocument = function(doc) {
this.lines = doc.lines; this.lines = doc.lines;
this.doc = doc; this.doc = doc;
this.markerLayer.setDocument(doc); this.$markerLayer.setDocument(doc);
this.textLayer.setDocument(doc); this.textLayer.setDocument(doc);
}; };
@ -55,6 +60,19 @@ ace.VirtualRenderer = function(container) {
this.textLayer.setTokenizer(tokenizer); this.textLayer.setTokenizer(tokenizer);
}; };
this.$onGutterClick = function(e) {
var pageX = ace.getDocumentX(e);
var pageY = ace.getDocumentY(e);
var event = {
row: this.screenToTextCoordinates(pageX, pageY).row,
htmlEvent: e
};
var type = "gutter" + e.type;
this.$dispatchEvent(type, event);
};
this.$showInvisibles = true; this.$showInvisibles = true;
this.setShowInvisibles = function(showInvisibles) { this.setShowInvisibles = function(showInvisibles) {
this.$showInvisibles = showInvisibles; this.$showInvisibles = showInvisibles;
@ -92,7 +110,7 @@ ace.VirtualRenderer = function(container) {
if (!this.$printMarginEl) { if (!this.$printMarginEl) {
this.$printMarginEl = document.createElement("div"); this.$printMarginEl = document.createElement("div");
this.$printMarginEl.className = "printMargin"; this.$printMarginEl.className = "printMargin";
this.content.insertBefore(this.$printMarginEl, this.gutter.element); this.content.insertBefore(this.$printMarginEl, this.$gutter.element);
} }
var style = this.$printMarginEl.style; var style = this.$printMarginEl.style;
@ -123,7 +141,7 @@ ace.VirtualRenderer = function(container) {
this.scrollBar.setHeight(height); this.scrollBar.setHeight(height);
var width = ace.getInnerWidth(this.container); var width = ace.getInnerWidth(this.container);
var gutterWidth = this.gutter.offsetWidth; var gutterWidth = this.$gutter.offsetWidth;
this.scroller.style.left = gutterWidth + "px"; this.scroller.style.left = gutterWidth + "px";
this.scroller.style.width = Math.max(0, width - gutterWidth - this.scrollBar.getWidth()) + "px"; this.scroller.style.width = Math.max(0, width - gutterWidth - this.scrollBar.getWidth()) + "px";
@ -194,8 +212,8 @@ ace.VirtualRenderer = function(container) {
layer.update(layerConfig); layer.update(layerConfig);
}; };
this.gutterLayer.element.style.marginTop = (-offset) + "px"; this.$gutterLayer.element.style.marginTop = (-offset) + "px";
this.gutterLayer.update(layerConfig); this.$gutterLayer.update(layerConfig);
this.$updateScrollBar(); this.$updateScrollBar();
}; };
@ -203,11 +221,11 @@ ace.VirtualRenderer = function(container) {
this.addMarker = function(range, clazz, type) { this.addMarker = function(range, clazz, type) {
range.start = this.$documentToScreenPosition(range.start); range.start = this.$documentToScreenPosition(range.start);
range.end = this.$documentToScreenPosition(range.end); range.end = this.$documentToScreenPosition(range.end);
return this.markerLayer.addMarker(range, clazz, type); return this.$markerLayer.addMarker(range, clazz, type);
}; };
this.removeMarker = function(markerId) { this.removeMarker = function(markerId) {
this.markerLayer.removeMarker(markerId); this.$markerLayer.removeMarker(markerId);
}; };
this.updateCursor = function(position, overwrite) { this.updateCursor = function(position, overwrite) {