add doc tooltip
This commit is contained in:
parent
e894b5f9e0
commit
953e452f70
4 changed files with 106 additions and 11 deletions
|
|
@ -36,6 +36,7 @@ var AcePopup = require("./autocomplete/popup").AcePopup;
|
||||||
var util = require("./autocomplete/util");
|
var util = require("./autocomplete/util");
|
||||||
var event = require("./lib/event");
|
var event = require("./lib/event");
|
||||||
var lang = require("./lib/lang");
|
var lang = require("./lib/lang");
|
||||||
|
var dom = require("./lib/dom");
|
||||||
var snippetManager = require("./snippets").snippetManager;
|
var snippetManager = require("./snippets").snippetManager;
|
||||||
|
|
||||||
var Autocomplete = function() {
|
var Autocomplete = function() {
|
||||||
|
|
@ -52,6 +53,8 @@ var Autocomplete = function() {
|
||||||
this.changeTimer = lang.delayedCall(function() {
|
this.changeTimer = lang.delayedCall(function() {
|
||||||
this.updateCompletions(true);
|
this.updateCompletions(true);
|
||||||
}.bind(this));
|
}.bind(this));
|
||||||
|
|
||||||
|
this.tooltipTimer = lang.delayedCall(this.updateDocTooltip.bind(this), 50);
|
||||||
};
|
};
|
||||||
|
|
||||||
(function() {
|
(function() {
|
||||||
|
|
@ -64,6 +67,13 @@ var Autocomplete = function() {
|
||||||
e.stop();
|
e.stop();
|
||||||
}.bind(this));
|
}.bind(this));
|
||||||
this.popup.focus = this.editor.focus.bind(this.editor);
|
this.popup.focus = this.editor.focus.bind(this.editor);
|
||||||
|
this.popup.on("select", this.tooltipTimer.bind(null, null));
|
||||||
|
this.popup.on("changeHoverMarker", this.tooltipTimer.bind(null, null));
|
||||||
|
return this.popup;
|
||||||
|
};
|
||||||
|
|
||||||
|
this.getPopup = function() {
|
||||||
|
return this.popup || this.$init();
|
||||||
};
|
};
|
||||||
|
|
||||||
this.openPopup = function(editor, prefix, keepPopupPosition) {
|
this.openPopup = function(editor, prefix, keepPopupPosition) {
|
||||||
|
|
@ -101,6 +111,7 @@ var Autocomplete = function() {
|
||||||
this.editor.off("mousedown", this.mousedownListener);
|
this.editor.off("mousedown", this.mousedownListener);
|
||||||
this.editor.off("mousewheel", this.mousewheelListener);
|
this.editor.off("mousewheel", this.mousewheelListener);
|
||||||
this.changeTimer.cancel();
|
this.changeTimer.cancel();
|
||||||
|
this.hideDocTooltip();
|
||||||
|
|
||||||
if (this.popup && this.popup.isOpen) {
|
if (this.popup && this.popup.isOpen) {
|
||||||
this.gatherCompletionsId += 1;
|
this.gatherCompletionsId += 1;
|
||||||
|
|
@ -124,12 +135,17 @@ var Autocomplete = function() {
|
||||||
this.detach();
|
this.detach();
|
||||||
};
|
};
|
||||||
|
|
||||||
this.blurListener = function() {
|
this.blurListener = function(e) {
|
||||||
// we have to check if activeElement is a child of popup because
|
// we have to check if activeElement is a child of popup because
|
||||||
// on IE preventDefault doesn't stop scrollbar from being focussed
|
// on IE preventDefault doesn't stop scrollbar from being focussed
|
||||||
var el = document.activeElement;
|
var el = document.activeElement;
|
||||||
if (el != this.editor.textInput.getElement() && el.parentNode != this.popup.container)
|
var text = this.editor.textInput.getElement()
|
||||||
|
if (el != text && el.parentNode != this.popup.container
|
||||||
|
&& el != this.tooltipNode && e.relatedTarget != this.tooltipNode
|
||||||
|
&& e.relatedTarget != text
|
||||||
|
) {
|
||||||
this.detach();
|
this.detach();
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
this.mousedownListener = function(e) {
|
this.mousedownListener = function(e) {
|
||||||
|
|
@ -178,6 +194,7 @@ var Autocomplete = function() {
|
||||||
this.detach();
|
this.detach();
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
this.commands = {
|
this.commands = {
|
||||||
"Up": function(editor) { editor.completer.goTo("up"); },
|
"Up": function(editor) { editor.completer.goTo("up"); },
|
||||||
"Down": function(editor) { editor.completer.goTo("down"); },
|
"Down": function(editor) { editor.completer.goTo("down"); },
|
||||||
|
|
@ -310,6 +327,74 @@ var Autocomplete = function() {
|
||||||
this.cancelContextMenu = function() {
|
this.cancelContextMenu = function() {
|
||||||
this.editor.$mouseHandler.cancelContextMenu();
|
this.editor.$mouseHandler.cancelContextMenu();
|
||||||
};
|
};
|
||||||
|
|
||||||
|
this.updateDocTooltip = function() {
|
||||||
|
var popup = this.popup;
|
||||||
|
var all = popup.data;
|
||||||
|
var selected = all && (all[popup.getHoveredRow()] || all[popup.getRow()]);
|
||||||
|
var doc = null;
|
||||||
|
if (!selected || !this.editor || !this.popup.isOpen)
|
||||||
|
return this.hideDocTooltip();
|
||||||
|
this.editor.completers.some(function(completer) {
|
||||||
|
if (completer.getDocTooltip)
|
||||||
|
doc = completer.getDocTooltip(selected);
|
||||||
|
return doc;
|
||||||
|
});
|
||||||
|
if (!doc)
|
||||||
|
doc = selected;
|
||||||
|
|
||||||
|
if (typeof doc == "string")
|
||||||
|
doc = {tooltipText: doc}
|
||||||
|
if (!doc || !(doc.docHTML || doc.docText))
|
||||||
|
return this.hideDocTooltip();
|
||||||
|
this.showDocTooltip(doc);
|
||||||
|
};
|
||||||
|
|
||||||
|
this.showDocTooltip = function(item) {
|
||||||
|
if (!this.tooltipNode) {
|
||||||
|
this.tooltipNode = dom.createElement("pre");
|
||||||
|
this.tooltipNode.className = "ace_tooltip ace_doc-tooltip";
|
||||||
|
this.tooltipNode.style.margin = 0;
|
||||||
|
this.tooltipNode.style.pointerEvents = "auto";
|
||||||
|
this.tooltipNode.tabIndex = -1;
|
||||||
|
this.tooltipNode.onblur = this.blurListener.bind(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
var tooltipNode = this.tooltipNode;
|
||||||
|
if (item.docHTML) {
|
||||||
|
tooltipNode.innerHTML = item.docHTML;
|
||||||
|
} else if (item.docText) {
|
||||||
|
tooltipNode.textContent = item.docText;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!tooltipNode.parentNode)
|
||||||
|
document.body.appendChild(tooltipNode);
|
||||||
|
var popup = this.popup;
|
||||||
|
var rect = popup.container.getBoundingClientRect();
|
||||||
|
tooltipNode.style.top = popup.container.style.top;
|
||||||
|
tooltipNode.style.bottom = popup.container.style.bottom;
|
||||||
|
|
||||||
|
if (window.innerWidth - rect.right < 320) {
|
||||||
|
tooltipNode.style.right = window.innerWidth - rect.left + "px";
|
||||||
|
tooltipNode.style.left = "";
|
||||||
|
} else {
|
||||||
|
tooltipNode.style.left = (rect.right + 1) + "px";
|
||||||
|
tooltipNode.style.right = "";
|
||||||
|
}
|
||||||
|
// tooltipNode.style.height = rect.height + "px";
|
||||||
|
tooltipNode.style.display = "block";
|
||||||
|
};
|
||||||
|
|
||||||
|
this.hideDocTooltip = function() {
|
||||||
|
this.tooltipTimer.cancel();
|
||||||
|
if (!this.tooltipNode) return;
|
||||||
|
var el = this.tooltipNode;
|
||||||
|
if (!this.editor.isFocused() && document.activeElement == el)
|
||||||
|
this.editor.focus();
|
||||||
|
this.tooltipNode = null;
|
||||||
|
if (el.parentNode)
|
||||||
|
el.parentNode.removeChild(el);
|
||||||
|
};
|
||||||
|
|
||||||
}).call(Autocomplete.prototype);
|
}).call(Autocomplete.prototype);
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -662,13 +662,13 @@ var Editor = function(renderer, session) {
|
||||||
*
|
*
|
||||||
*
|
*
|
||||||
**/
|
**/
|
||||||
this.onFocus = function() {
|
this.onFocus = function(e) {
|
||||||
if (this.$isFocused)
|
if (this.$isFocused)
|
||||||
return;
|
return;
|
||||||
this.$isFocused = true;
|
this.$isFocused = true;
|
||||||
this.renderer.showCursor();
|
this.renderer.showCursor();
|
||||||
this.renderer.visualizeFocus();
|
this.renderer.visualizeFocus();
|
||||||
this._emit("focus");
|
this._emit("focus", e);
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -677,13 +677,13 @@ var Editor = function(renderer, session) {
|
||||||
*
|
*
|
||||||
*
|
*
|
||||||
**/
|
**/
|
||||||
this.onBlur = function() {
|
this.onBlur = function(e) {
|
||||||
if (!this.$isFocused)
|
if (!this.$isFocused)
|
||||||
return;
|
return;
|
||||||
this.$isFocused = false;
|
this.$isFocused = false;
|
||||||
this.renderer.hideCursor();
|
this.renderer.hideCursor();
|
||||||
this.renderer.visualizeBlur();
|
this.renderer.visualizeBlur();
|
||||||
this._emit("blur");
|
this._emit("blur", e);
|
||||||
};
|
};
|
||||||
|
|
||||||
this.$cursorChange = function() {
|
this.$cursorChange = function() {
|
||||||
|
|
|
||||||
|
|
@ -34,6 +34,7 @@ define(function(require, exports, module) {
|
||||||
var snippetManager = require("../snippets").snippetManager;
|
var snippetManager = require("../snippets").snippetManager;
|
||||||
var Autocomplete = require("../autocomplete").Autocomplete;
|
var Autocomplete = require("../autocomplete").Autocomplete;
|
||||||
var config = require("../config");
|
var config = require("../config");
|
||||||
|
var lang = require("../lib/lang");
|
||||||
var util = require("../autocomplete/util");
|
var util = require("../autocomplete/util");
|
||||||
|
|
||||||
var textCompleter = require("../autocomplete/text_completer");
|
var textCompleter = require("../autocomplete/text_completer");
|
||||||
|
|
@ -59,11 +60,20 @@ var snippetCompleter = {
|
||||||
completions.push({
|
completions.push({
|
||||||
caption: caption,
|
caption: caption,
|
||||||
snippet: s.content,
|
snippet: s.content,
|
||||||
meta: s.tabTrigger && !s.name ? s.tabTrigger + "\u21E5 " : "snippet"
|
meta: s.tabTrigger && !s.name ? s.tabTrigger + "\u21E5 " : "snippet",
|
||||||
|
type: "snippet"
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}, this);
|
}, this);
|
||||||
callback(null, completions);
|
callback(null, completions);
|
||||||
|
},
|
||||||
|
getDocTooltip: function(item) {
|
||||||
|
if (item.type == "snippet" && !item.docHTML) {
|
||||||
|
item.docHTML = [
|
||||||
|
"<b>", lang.escapeHTML(item.caption), "</b>", "<hr></hr>",
|
||||||
|
lang.escapeHTML(item.snippet)
|
||||||
|
].join("");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -66,13 +66,13 @@ var TextInput = function(parentNode, host) {
|
||||||
// ie9 throws error if document.activeElement is accessed too soon
|
// ie9 throws error if document.activeElement is accessed too soon
|
||||||
try { var isFocused = document.activeElement === text; } catch(e) {}
|
try { var isFocused = document.activeElement === text; } catch(e) {}
|
||||||
|
|
||||||
event.addListener(text, "blur", function() {
|
event.addListener(text, "blur", function(e) {
|
||||||
host.onBlur();
|
host.onBlur(e);
|
||||||
isFocused = false;
|
isFocused = false;
|
||||||
});
|
});
|
||||||
event.addListener(text, "focus", function() {
|
event.addListener(text, "focus", function(e) {
|
||||||
isFocused = true;
|
isFocused = true;
|
||||||
host.onFocus();
|
host.onFocus(e);
|
||||||
resetSelection();
|
resetSelection();
|
||||||
});
|
});
|
||||||
this.focus = function() { text.focus(); };
|
this.focus = function() { text.focus(); };
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue