add ability to draw show cursors

This commit is contained in:
nightwing 2012-03-13 22:04:45 +04:00
commit c431f6a550

View file

@ -47,11 +47,10 @@ var Cursor = function(parentEl) {
this.element.className = "ace_layer ace_cursor-layer"; this.element.className = "ace_layer ace_cursor-layer";
parentEl.appendChild(this.element); parentEl.appendChild(this.element);
this.cursor = dom.createElement("div");
this.cursor.className = "ace_cursor ace_hidden";
this.element.appendChild(this.cursor);
this.isVisible = false; this.isVisible = false;
this.cursors = [];
this.cursor = this.addCursor();
}; };
(function() { (function() {
@ -65,30 +64,54 @@ var Cursor = function(parentEl) {
this.session = session; this.session = session;
}; };
this.addCursor = function() {
var el = dom.createElement("div");
var className = "ace_cursor"
if (!this.isVisible)
className += " ace_hidden";
if (this.overwrite)
className += " ace_overwrite";
el.className = className;
this.element.appendChild(el);
this.cursors.push(el);
return el;
};
this.removeCursor = function() {
if (this.cursors.length > 1) {
var el = this.cursors.pop();
el.parentNode.removeChild(el)
return el
}
};
this.hideCursor = function() { this.hideCursor = function() {
this.isVisible = false; this.isVisible = false;
dom.addCssClass(this.cursor, "ace_hidden"); for (var i = this.cursors.length; i--; )
dom.addCssClass(this.cursors[i], "ace_hidden");
clearInterval(this.blinkId); clearInterval(this.blinkId);
}; };
this.showCursor = function() { this.showCursor = function() {
this.isVisible = true; this.isVisible = true;
dom.removeCssClass(this.cursor, "ace_hidden"); for (var i = this.cursors.length; i--; )
this.cursor.style.visibility = "visible"; dom.removeCssClass(this.cursors[i], "ace_hidden");
this.element.style.visibility = "";
this.restartTimer(); this.restartTimer();
}; };
this.restartTimer = function() { this.restartTimer = function() {
clearInterval(this.blinkId); clearInterval(this.blinkId);
if (!this.isVisible) { if (!this.isVisible)
return; return;
}
var cursor = this.cursor; var element = this.element;
this.blinkId = setInterval(function() { this.blinkId = setInterval(function() {
cursor.style.visibility = "hidden"; element.style.visibility = "hidden";
setTimeout(function() { setTimeout(function() {
cursor.style.visibility = "visible"; element.style.visibility = "visible";
}, 400); }, 400);
}, 1000); }, 1000);
}; };
@ -118,25 +141,56 @@ var Cursor = function(parentEl) {
this.update = function(config) { this.update = function(config) {
this.config = config; this.config = config;
if (!this.session.selection.multiSelectMode) {
this.pixelPos = this.getPixelPosition(null, true); this.pixelPos = this.getPixelPosition(null, true);
var style = this.cursor.style;
style.left = this.pixelPos.left + "px";
style.top = this.pixelPos.top + "px";
style.width = config.characterWidth + "px";
style.height = config.lineHeight + "px";
this.cursor.style.left = this.pixelPos.left + "px"; while (this.cursors.length > 1)
this.cursor.style.top = this.pixelPos.top + "px"; this.removeCursor();
this.cursor.style.width = config.characterWidth + "px"; } else {
this.cursor.style.height = config.lineHeight + "px"; var selections = this.session.selection.allRanges
var i = 0, sel, cursorIndex = 0;
var overwrite = this.session.getOverwrite() while ((sel = selections[i]) && sel.cursor.row < config.firstRow)
if (overwrite != this.overwrite) { i++;
this.overwrite = overwrite;
if (overwrite) while ((sel = selections[i]) && sel.cursor.row < config.lastRow) {
dom.addCssClass(this.cursor, "ace_overwrite"); this.pixelPos = this.getPixelPosition(sel.cursor, true);
else
dom.removeCssClass(this.cursor, "ace_overwrite"); var style = (this.cursors[cursorIndex++] || this.addCursor()).style;
style.left = this.pixelPos.left + "px";
style.top = this.pixelPos.top + "px";
style.width = config.characterWidth + "px";
style.height = config.lineHeight + "px";
i++;
}
while (cursorIndex < this.cursors.length)
this.removeCursor();
} }
var overwrite = this.session.getOverwrite();
if (overwrite != this.overwrite)
this.$setOverite(overwrite);
this.restartTimer(); this.restartTimer();
}; };
this.$setOverite = function(overwrite) {
this.overwrite = overwrite;
for (var i = this.cursors.length; i--; ) {
if (overwrite)
dom.addCssClass(this.cursors[i], "ace_overwrite");
else
dom.removeCssClass(this.cursors[i], "ace_overwrite");
}
};
this.destroy = function() { this.destroy = function() {
clearInterval(this.blinkId); clearInterval(this.blinkId);
} }