codemirror 3.14
This commit is contained in:
+200
-110
@@ -1,57 +1,168 @@
|
||||
CodeMirror.showHint = function(cm, getHints, options) {
|
||||
if (!options) options = {};
|
||||
var startCh = cm.getCursor().ch, continued = false;
|
||||
var closeOn = options.closeCharacters || /[\s()\[\]{};:]/;
|
||||
(function() {
|
||||
"use strict";
|
||||
|
||||
function startHinting() {
|
||||
CodeMirror.showHint = function(cm, getHints, options) {
|
||||
// We want a single cursor position.
|
||||
if (cm.somethingSelected()) return;
|
||||
|
||||
if (options.async)
|
||||
getHints(cm, showHints, options);
|
||||
if (cm.state.completionActive) cm.state.completionActive.close();
|
||||
|
||||
var completion = cm.state.completionActive = new Completion(cm, getHints, options || {});
|
||||
CodeMirror.signal(cm, "startCompletion", cm);
|
||||
if (completion.options.async)
|
||||
getHints(cm, function(hints) { completion.showHints(hints); }, completion.options);
|
||||
else
|
||||
return showHints(getHints(cm, options));
|
||||
return completion.showHints(getHints(cm, completion.options));
|
||||
};
|
||||
|
||||
function Completion(cm, getHints, options) {
|
||||
this.cm = cm;
|
||||
this.getHints = getHints;
|
||||
this.options = options;
|
||||
this.widget = this.onClose = null;
|
||||
}
|
||||
|
||||
Completion.prototype = {
|
||||
close: function() {
|
||||
if (!this.active()) return;
|
||||
|
||||
if (this.widget) this.widget.close();
|
||||
if (this.onClose) this.onClose();
|
||||
this.cm.state.completionActive = null;
|
||||
CodeMirror.signal(this.cm, "endCompletion", this.cm);
|
||||
},
|
||||
|
||||
active: function() {
|
||||
return this.cm.state.completionActive == this;
|
||||
},
|
||||
|
||||
pick: function(data, i) {
|
||||
var completion = data.list[i];
|
||||
if (completion.hint) completion.hint(this.cm, data, completion);
|
||||
else this.cm.replaceRange(getText(completion), data.from, data.to);
|
||||
this.close();
|
||||
},
|
||||
|
||||
showHints: function(data) {
|
||||
if (!data || !data.list.length || !this.active()) return this.close();
|
||||
|
||||
if (this.options.completeSingle != false && data.list.length == 1)
|
||||
this.pick(data, 0);
|
||||
else
|
||||
this.showWidget(data);
|
||||
},
|
||||
|
||||
showWidget: function(data) {
|
||||
this.widget = new Widget(this, data);
|
||||
CodeMirror.signal(data, "shown");
|
||||
|
||||
var debounce = null, completion = this, finished;
|
||||
var closeOn = this.options.closeCharacters || /[\s()\[\]{};:>,]/;
|
||||
var startPos = this.cm.getCursor(), startLen = this.cm.getLine(startPos.line).length;
|
||||
|
||||
function done() {
|
||||
if (finished) return;
|
||||
finished = true;
|
||||
completion.close();
|
||||
completion.cm.off("cursorActivity", activity);
|
||||
CodeMirror.signal(data, "close");
|
||||
}
|
||||
function isDone() {
|
||||
if (finished) return true;
|
||||
if (!completion.widget) { done(); return true; }
|
||||
}
|
||||
|
||||
function update() {
|
||||
if (isDone()) return;
|
||||
if (completion.options.async)
|
||||
completion.getHints(completion.cm, finishUpdate, completion.options);
|
||||
else
|
||||
finishUpdate(completion.getHints(completion.cm, completion.options));
|
||||
}
|
||||
function finishUpdate(data) {
|
||||
if (isDone()) return;
|
||||
if (!data || !data.list.length) return done();
|
||||
completion.widget.close();
|
||||
completion.widget = new Widget(completion, data);
|
||||
}
|
||||
|
||||
function activity() {
|
||||
clearTimeout(debounce);
|
||||
var pos = completion.cm.getCursor(), line = completion.cm.getLine(pos.line);
|
||||
if (pos.line != startPos.line || line.length - pos.ch != startLen - startPos.ch ||
|
||||
pos.ch < startPos.ch || completion.cm.somethingSelected() ||
|
||||
(pos.ch && closeOn.test(line.charAt(pos.ch - 1))))
|
||||
completion.close();
|
||||
else
|
||||
debounce = setTimeout(update, 170);
|
||||
}
|
||||
this.cm.on("cursorActivity", activity);
|
||||
this.onClose = done;
|
||||
}
|
||||
};
|
||||
|
||||
function getText(completion) {
|
||||
if (typeof completion == "string") return completion;
|
||||
else return completion.text;
|
||||
}
|
||||
|
||||
function pickCompletion(cm, data, completion) {
|
||||
if (completion.hint) completion.hint(cm, data, completion);
|
||||
else cm.replaceRange(getText(completion), data.from, data.to);
|
||||
function buildKeyMap(options, handle) {
|
||||
var baseMap = {
|
||||
Up: function() {handle.moveFocus(-1);},
|
||||
Down: function() {handle.moveFocus(1);},
|
||||
PageUp: function() {handle.moveFocus(-handle.menuSize());},
|
||||
PageDown: function() {handle.moveFocus(handle.menuSize());},
|
||||
Home: function() {handle.setFocus(0);},
|
||||
End: function() {handle.setFocus(handle.length);},
|
||||
Enter: handle.pick,
|
||||
Tab: handle.pick,
|
||||
Esc: handle.close
|
||||
};
|
||||
var ourMap = options.customKeys ? {} : baseMap;
|
||||
function addBinding(key, val) {
|
||||
var bound;
|
||||
if (typeof val != "string")
|
||||
bound = function(cm) { return val(cm, handle); };
|
||||
// This mechanism is deprecated
|
||||
else if (baseMap.hasOwnProperty(val))
|
||||
bound = baseMap[val];
|
||||
else
|
||||
bound = val;
|
||||
ourMap[key] = bound;
|
||||
}
|
||||
if (options.customKeys)
|
||||
for (var key in options.customKeys) if (options.customKeys.hasOwnProperty(key))
|
||||
addBinding(key, options.customKeys[key]);
|
||||
if (options.extraKeys)
|
||||
for (var key in options.extraKeys) if (options.extraKeys.hasOwnProperty(key))
|
||||
addBinding(key, options.extraKeys[key]);
|
||||
return ourMap;
|
||||
}
|
||||
|
||||
function showHints(data) {
|
||||
if (!data || !data.list.length) return;
|
||||
var completions = data.list;
|
||||
// When there is only one completion, use it directly.
|
||||
if (!continued && options.completeSingle !== false && completions.length == 1) {
|
||||
pickCompletion(cm, data, completions[0]);
|
||||
CodeMirror.signal(data, "close");
|
||||
return true;
|
||||
}
|
||||
function Widget(completion, data) {
|
||||
this.completion = completion;
|
||||
this.data = data;
|
||||
var widget = this, cm = completion.cm, options = completion.options;
|
||||
|
||||
// Build the select widget
|
||||
var hints = document.createElement("ul"), selectedHint = 0;
|
||||
var hints = this.hints = document.createElement("ul");
|
||||
hints.className = "CodeMirror-hints";
|
||||
this.selectedHint = 0;
|
||||
|
||||
var completions = data.list;
|
||||
for (var i = 0; i < completions.length; ++i) {
|
||||
var elt = hints.appendChild(document.createElement("li")), completion = completions[i];
|
||||
var elt = hints.appendChild(document.createElement("li")), cur = completions[i];
|
||||
var className = "CodeMirror-hint" + (i ? "" : " CodeMirror-hint-active");
|
||||
if (completion.className != null) className = completion.className + " " + className;
|
||||
if (cur.className != null) className = cur.className + " " + className;
|
||||
elt.className = className;
|
||||
if (completion.render) completion.render(elt, data, completion);
|
||||
else elt.appendChild(document.createTextNode(completion.displayText || getText(completion)));
|
||||
if (cur.render) cur.render(elt, data, cur);
|
||||
else elt.appendChild(document.createTextNode(cur.displayText || getText(cur)));
|
||||
elt.hintId = i;
|
||||
}
|
||||
|
||||
var pos = cm.cursorCoords(options.alignWithWord !== false ? data.from : null);
|
||||
var left = pos.left, top = pos.bottom, below = true;
|
||||
hints.style.left = left + "px";
|
||||
hints.style.top = top + "px";
|
||||
document.body.appendChild(hints);
|
||||
CodeMirror.signal(data, "shown");
|
||||
|
||||
// If we're at the edge of the screen, then we want the menu to appear on the left of the cursor.
|
||||
var winW = window.innerWidth || Math.max(document.body.offsetWidth, document.documentElement.offsetWidth);
|
||||
var winH = window.innerHeight || Math.max(document.body.offsetHeight, document.documentElement.offsetHeight);
|
||||
@@ -75,106 +186,85 @@ CodeMirror.showHint = function(cm, getHints, options) {
|
||||
}
|
||||
hints.style.top = (top = pos.bottom - overlapY) + "px";
|
||||
}
|
||||
(options.container || document.body).appendChild(hints);
|
||||
|
||||
function changeActive(i) {
|
||||
i = Math.max(0, Math.min(i, completions.length - 1));
|
||||
if (selectedHint == i) return;
|
||||
var node = hints.childNodes[selectedHint];
|
||||
node.className = node.className.replace(" CodeMirror-hint-active", "");
|
||||
node = hints.childNodes[selectedHint = i];
|
||||
node.className += " CodeMirror-hint-active";
|
||||
if (node.offsetTop < hints.scrollTop)
|
||||
hints.scrollTop = node.offsetTop - 3;
|
||||
else if (node.offsetTop + node.offsetHeight > hints.scrollTop + hints.clientHeight)
|
||||
hints.scrollTop = node.offsetTop + node.offsetHeight - hints.clientHeight + 3;
|
||||
CodeMirror.signal(data, "select", completions[selectedHint], node);
|
||||
cm.addKeyMap(this.keyMap = buildKeyMap(options, {
|
||||
moveFocus: function(n) { widget.changeActive(widget.selectedHint + n); },
|
||||
setFocus: function(n) { widget.changeActive(n); },
|
||||
menuSize: function() { return widget.screenAmount(); },
|
||||
length: completions.length,
|
||||
close: function() { completion.close(); },
|
||||
pick: function() { widget.pick(); }
|
||||
}));
|
||||
|
||||
if (options.closeOnUnfocus !== false) {
|
||||
var closingOnBlur;
|
||||
cm.on("blur", this.onBlur = function() { closingOnBlur = setTimeout(function() { completion.close(); }, 100); });
|
||||
cm.on("focus", this.onFocus = function() { clearTimeout(closingOnBlur); });
|
||||
}
|
||||
|
||||
function screenAmount() {
|
||||
return Math.floor(hints.clientHeight / hints.firstChild.offsetHeight) || 1;
|
||||
}
|
||||
|
||||
var ourMap, baseMap = {
|
||||
Up: function() {changeActive(selectedHint - 1);},
|
||||
Down: function() {changeActive(selectedHint + 1);},
|
||||
PageUp: function() {changeActive(selectedHint - screenAmount());},
|
||||
PageDown: function() {changeActive(selectedHint + screenAmount());},
|
||||
Home: function() {changeActive(0);},
|
||||
End: function() {changeActive(completions.length - 1);},
|
||||
Enter: pick,
|
||||
Tab: pick,
|
||||
Esc: close
|
||||
};
|
||||
if (options.customKeys) {
|
||||
ourMap = {};
|
||||
for (var key in options.customKeys) if (options.customKeys.hasOwnProperty(key)) {
|
||||
var val = options.customKeys[key];
|
||||
if (baseMap.hasOwnProperty(val)) val = baseMap[val];
|
||||
ourMap[key] = val;
|
||||
}
|
||||
} else ourMap = baseMap;
|
||||
|
||||
cm.addKeyMap(ourMap);
|
||||
cm.on("cursorActivity", cursorActivity);
|
||||
var closingOnBlur;
|
||||
function onBlur(){ closingOnBlur = setTimeout(close, 100); };
|
||||
function onFocus(){ clearTimeout(closingOnBlur); };
|
||||
cm.on("blur", onBlur);
|
||||
cm.on("focus", onFocus);
|
||||
var startScroll = cm.getScrollInfo();
|
||||
function onScroll() {
|
||||
cm.on("scroll", this.onScroll = function() {
|
||||
var curScroll = cm.getScrollInfo(), editor = cm.getWrapperElement().getBoundingClientRect();
|
||||
var newTop = top + startScroll.top - curScroll.top, point = newTop;
|
||||
var newTop = top + startScroll.top - curScroll.top;
|
||||
var point = newTop - (window.pageYOffset || (document.documentElement || document.body).scrollTop);
|
||||
if (!below) point += hints.offsetHeight;
|
||||
if (point <= editor.top || point >= editor.bottom) return close();
|
||||
if (point <= editor.top || point >= editor.bottom) return completion.close();
|
||||
hints.style.top = newTop + "px";
|
||||
hints.style.left = (left + startScroll.left - curScroll.left) + "px";
|
||||
}
|
||||
cm.on("scroll", onScroll);
|
||||
});
|
||||
|
||||
CodeMirror.on(hints, "dblclick", function(e) {
|
||||
var t = e.target || e.srcElement;
|
||||
if (t.hintId != null) {selectedHint = t.hintId; pick();}
|
||||
if (t.hintId != null) {widget.changeActive(t.hintId); widget.pick();}
|
||||
});
|
||||
CodeMirror.on(hints, "click", function(e) {
|
||||
var t = e.target || e.srcElement;
|
||||
if (t.hintId != null) changeActive(t.hintId);
|
||||
if (t.hintId != null) widget.changeActive(t.hintId);
|
||||
});
|
||||
CodeMirror.on(hints, "mousedown", function() {
|
||||
setTimeout(function(){cm.focus();}, 20);
|
||||
});
|
||||
|
||||
var done = false, once;
|
||||
function close(willContinue) {
|
||||
if (done) return;
|
||||
done = true;
|
||||
clearTimeout(once);
|
||||
hints.parentNode.removeChild(hints);
|
||||
cm.removeKeyMap(ourMap);
|
||||
cm.off("cursorActivity", cursorActivity);
|
||||
cm.off("blur", onBlur);
|
||||
cm.off("focus", onFocus);
|
||||
cm.off("scroll", onScroll);
|
||||
if (willContinue !== true) CodeMirror.signal(data, "close");
|
||||
}
|
||||
function pick() {
|
||||
pickCompletion(cm, data, completions[selectedHint]);
|
||||
close();
|
||||
}
|
||||
var once, lastPos = cm.getCursor(), lastLen = cm.getLine(lastPos.line).length;
|
||||
function cursorActivity() {
|
||||
clearTimeout(once);
|
||||
|
||||
var pos = cm.getCursor(), line = cm.getLine(pos.line);
|
||||
if (pos.line != lastPos.line || line.length - pos.ch != lastLen - lastPos.ch ||
|
||||
pos.ch < startCh || cm.somethingSelected() ||
|
||||
(pos.ch && closeOn.test(line.charAt(pos.ch - 1))))
|
||||
close();
|
||||
else
|
||||
once = setTimeout(function(){close(true); continued = true; startHinting();}, 70);
|
||||
}
|
||||
CodeMirror.signal(data, "select", completions[0], hints.firstChild);
|
||||
return true;
|
||||
}
|
||||
|
||||
return startHinting();
|
||||
};
|
||||
Widget.prototype = {
|
||||
close: function() {
|
||||
if (this.completion.widget != this) return;
|
||||
this.completion.widget = null;
|
||||
this.hints.parentNode.removeChild(this.hints);
|
||||
this.completion.cm.removeKeyMap(this.keyMap);
|
||||
|
||||
var cm = this.completion.cm;
|
||||
if (this.completion.options.closeOnUnfocus !== false) {
|
||||
cm.off("blur", this.onBlur);
|
||||
cm.off("focus", this.onFocus);
|
||||
}
|
||||
cm.off("scroll", this.onScroll);
|
||||
},
|
||||
|
||||
pick: function() {
|
||||
this.completion.pick(this.data, this.selectedHint);
|
||||
},
|
||||
|
||||
changeActive: function(i) {
|
||||
i = Math.max(0, Math.min(i, this.data.list.length - 1));
|
||||
if (this.selectedHint == i) return;
|
||||
var node = this.hints.childNodes[this.selectedHint];
|
||||
node.className = node.className.replace(" CodeMirror-hint-active", "");
|
||||
node = this.hints.childNodes[this.selectedHint = i];
|
||||
node.className += " CodeMirror-hint-active";
|
||||
if (node.offsetTop < this.hints.scrollTop)
|
||||
this.hints.scrollTop = node.offsetTop - 3;
|
||||
else if (node.offsetTop + node.offsetHeight > this.hints.scrollTop + this.hints.clientHeight)
|
||||
this.hints.scrollTop = node.offsetTop + node.offsetHeight - this.hints.clientHeight + 3;
|
||||
CodeMirror.signal(this.data, "select", this.data.list[this.selectedHint], node);
|
||||
},
|
||||
|
||||
screenAmount: function() {
|
||||
return Math.floor(this.hints.clientHeight / this.hints.firstChild.offsetHeight) || 1;
|
||||
}
|
||||
};
|
||||
})();
|
||||
|
||||
Reference in New Issue
Block a user