PageRenderTime 48ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/ajax/libs/codemirror/3.21.0/addon/hint/show-hint.js

https://gitlab.com/Blueprint-Marketing/cdnjs
JavaScript | 335 lines | 291 code | 41 blank | 3 comment | 113 complexity | 039edfd21c07dd440934bb5d241940a8 MD5 | raw file
  1. (function() {
  2. "use strict";
  3. var HINT_ELEMENT_CLASS = "CodeMirror-hint";
  4. var ACTIVE_HINT_ELEMENT_CLASS = "CodeMirror-hint-active";
  5. CodeMirror.showHint = function(cm, getHints, options) {
  6. // We want a single cursor position.
  7. if (cm.somethingSelected()) return;
  8. if (getHints == null) {
  9. if (options && options.async) return;
  10. else getHints = CodeMirror.hint.auto;
  11. }
  12. if (cm.state.completionActive) cm.state.completionActive.close();
  13. var completion = cm.state.completionActive = new Completion(cm, getHints, options || {});
  14. CodeMirror.signal(cm, "startCompletion", cm);
  15. if (completion.options.async)
  16. getHints(cm, function(hints) { completion.showHints(hints); }, completion.options);
  17. else
  18. return completion.showHints(getHints(cm, completion.options));
  19. };
  20. function Completion(cm, getHints, options) {
  21. this.cm = cm;
  22. this.getHints = getHints;
  23. this.options = options;
  24. this.widget = this.onClose = null;
  25. }
  26. Completion.prototype = {
  27. close: function() {
  28. if (!this.active()) return;
  29. this.cm.state.completionActive = null;
  30. if (this.widget) this.widget.close();
  31. if (this.onClose) this.onClose();
  32. CodeMirror.signal(this.cm, "endCompletion", this.cm);
  33. },
  34. active: function() {
  35. return this.cm.state.completionActive == this;
  36. },
  37. pick: function(data, i) {
  38. var completion = data.list[i];
  39. if (completion.hint) completion.hint(this.cm, data, completion);
  40. else this.cm.replaceRange(getText(completion), data.from, data.to);
  41. CodeMirror.signal(data, "pick", completion);
  42. this.close();
  43. },
  44. showHints: function(data) {
  45. if (!data || !data.list.length || !this.active()) return this.close();
  46. if (this.options.completeSingle != false && data.list.length == 1)
  47. this.pick(data, 0);
  48. else
  49. this.showWidget(data);
  50. },
  51. showWidget: function(data) {
  52. this.widget = new Widget(this, data);
  53. CodeMirror.signal(data, "shown");
  54. var debounce = 0, completion = this, finished;
  55. var closeOn = this.options.closeCharacters || /[\s()\[\]{};:>,]/;
  56. var startPos = this.cm.getCursor(), startLen = this.cm.getLine(startPos.line).length;
  57. var requestAnimationFrame = window.requestAnimationFrame || function(fn) {
  58. return setTimeout(fn, 1000/60);
  59. };
  60. var cancelAnimationFrame = window.cancelAnimationFrame || clearTimeout;
  61. function done() {
  62. if (finished) return;
  63. finished = true;
  64. completion.close();
  65. completion.cm.off("cursorActivity", activity);
  66. if (data) CodeMirror.signal(data, "close");
  67. }
  68. function update() {
  69. if (finished) return;
  70. CodeMirror.signal(data, "update");
  71. if (completion.options.async)
  72. completion.getHints(completion.cm, finishUpdate, completion.options);
  73. else
  74. finishUpdate(completion.getHints(completion.cm, completion.options));
  75. }
  76. function finishUpdate(data_) {
  77. data = data_;
  78. if (finished) return;
  79. if (!data || !data.list.length) return done();
  80. completion.widget = new Widget(completion, data);
  81. }
  82. function clearDebounce() {
  83. if (debounce) {
  84. cancelAnimationFrame(debounce);
  85. debounce = 0;
  86. }
  87. }
  88. function activity() {
  89. clearDebounce();
  90. var pos = completion.cm.getCursor(), line = completion.cm.getLine(pos.line);
  91. if (pos.line != startPos.line || line.length - pos.ch != startLen - startPos.ch ||
  92. pos.ch < startPos.ch || completion.cm.somethingSelected() ||
  93. (pos.ch && closeOn.test(line.charAt(pos.ch - 1)))) {
  94. completion.close();
  95. } else {
  96. debounce = requestAnimationFrame(update);
  97. if (completion.widget) completion.widget.close();
  98. }
  99. }
  100. this.cm.on("cursorActivity", activity);
  101. this.onClose = done;
  102. }
  103. };
  104. function getText(completion) {
  105. if (typeof completion == "string") return completion;
  106. else return completion.text;
  107. }
  108. function buildKeyMap(options, handle) {
  109. var baseMap = {
  110. Up: function() {handle.moveFocus(-1);},
  111. Down: function() {handle.moveFocus(1);},
  112. PageUp: function() {handle.moveFocus(-handle.menuSize() + 1, true);},
  113. PageDown: function() {handle.moveFocus(handle.menuSize() - 1, true);},
  114. Home: function() {handle.setFocus(0);},
  115. End: function() {handle.setFocus(handle.length - 1);},
  116. Enter: handle.pick,
  117. Tab: handle.pick,
  118. Esc: handle.close
  119. };
  120. var ourMap = options.customKeys ? {} : baseMap;
  121. function addBinding(key, val) {
  122. var bound;
  123. if (typeof val != "string")
  124. bound = function(cm) { return val(cm, handle); };
  125. // This mechanism is deprecated
  126. else if (baseMap.hasOwnProperty(val))
  127. bound = baseMap[val];
  128. else
  129. bound = val;
  130. ourMap[key] = bound;
  131. }
  132. if (options.customKeys)
  133. for (var key in options.customKeys) if (options.customKeys.hasOwnProperty(key))
  134. addBinding(key, options.customKeys[key]);
  135. if (options.extraKeys)
  136. for (var key in options.extraKeys) if (options.extraKeys.hasOwnProperty(key))
  137. addBinding(key, options.extraKeys[key]);
  138. return ourMap;
  139. }
  140. function getHintElement(hintsElement, el) {
  141. while (el && el != hintsElement) {
  142. if (el.nodeName.toUpperCase() === "LI" && el.parentNode == hintsElement) return el;
  143. el = el.parentNode;
  144. }
  145. }
  146. function Widget(completion, data) {
  147. this.completion = completion;
  148. this.data = data;
  149. var widget = this, cm = completion.cm, options = completion.options;
  150. var hints = this.hints = document.createElement("ul");
  151. hints.className = "CodeMirror-hints";
  152. this.selectedHint = options.getDefaultSelection ? options.getDefaultSelection(cm,options,data) : 0;
  153. var completions = data.list;
  154. for (var i = 0; i < completions.length; ++i) {
  155. var elt = hints.appendChild(document.createElement("li")), cur = completions[i];
  156. var className = HINT_ELEMENT_CLASS + (i != this.selectedHint ? "" : " " + ACTIVE_HINT_ELEMENT_CLASS);
  157. if (cur.className != null) className = cur.className + " " + className;
  158. elt.className = className;
  159. if (cur.render) cur.render(elt, data, cur);
  160. else elt.appendChild(document.createTextNode(cur.displayText || getText(cur)));
  161. elt.hintId = i;
  162. }
  163. var pos = cm.cursorCoords(options.alignWithWord !== false ? data.from : null);
  164. var left = pos.left, top = pos.bottom, below = true;
  165. hints.style.left = left + "px";
  166. hints.style.top = top + "px";
  167. // If we're at the edge of the screen, then we want the menu to appear on the left of the cursor.
  168. var winW = window.innerWidth || Math.max(document.body.offsetWidth, document.documentElement.offsetWidth);
  169. var winH = window.innerHeight || Math.max(document.body.offsetHeight, document.documentElement.offsetHeight);
  170. (options.container || document.body).appendChild(hints);
  171. var box = hints.getBoundingClientRect();
  172. var overlapX = box.right - winW, overlapY = box.bottom - winH;
  173. if (overlapX > 0) {
  174. if (box.right - box.left > winW) {
  175. hints.style.width = (winW - 5) + "px";
  176. overlapX -= (box.right - box.left) - winW;
  177. }
  178. hints.style.left = (left = pos.left - overlapX) + "px";
  179. }
  180. if (overlapY > 0) {
  181. var height = box.bottom - box.top;
  182. if (box.top - (pos.bottom - pos.top) - height > 0) {
  183. overlapY = height + (pos.bottom - pos.top);
  184. below = false;
  185. } else if (height > winH) {
  186. hints.style.height = (winH - 5) + "px";
  187. overlapY -= height - winH;
  188. }
  189. hints.style.top = (top = pos.bottom - overlapY) + "px";
  190. }
  191. cm.addKeyMap(this.keyMap = buildKeyMap(options, {
  192. moveFocus: function(n, avoidWrap) { widget.changeActive(widget.selectedHint + n, avoidWrap); },
  193. setFocus: function(n) { widget.changeActive(n); },
  194. menuSize: function() { return widget.screenAmount(); },
  195. length: completions.length,
  196. close: function() { completion.close(); },
  197. pick: function() { widget.pick(); }
  198. }));
  199. if (options.closeOnUnfocus !== false) {
  200. var closingOnBlur;
  201. cm.on("blur", this.onBlur = function() { closingOnBlur = setTimeout(function() { completion.close(); }, 100); });
  202. cm.on("focus", this.onFocus = function() { clearTimeout(closingOnBlur); });
  203. }
  204. var startScroll = cm.getScrollInfo();
  205. cm.on("scroll", this.onScroll = function() {
  206. var curScroll = cm.getScrollInfo(), editor = cm.getWrapperElement().getBoundingClientRect();
  207. var newTop = top + startScroll.top - curScroll.top;
  208. var point = newTop - (window.pageYOffset || (document.documentElement || document.body).scrollTop);
  209. if (!below) point += hints.offsetHeight;
  210. if (point <= editor.top || point >= editor.bottom) return completion.close();
  211. hints.style.top = newTop + "px";
  212. hints.style.left = (left + startScroll.left - curScroll.left) + "px";
  213. });
  214. CodeMirror.on(hints, "dblclick", function(e) {
  215. var t = getHintElement(hints, e.target || e.srcElement);
  216. if (t && t.hintId != null) {widget.changeActive(t.hintId); widget.pick();}
  217. });
  218. CodeMirror.on(hints, "click", function(e) {
  219. var t = getHintElement(hints, e.target || e.srcElement);
  220. if (t && t.hintId != null) {
  221. widget.changeActive(t.hintId);
  222. if (options.completeOnSingleClick) widget.pick();
  223. }
  224. });
  225. CodeMirror.on(hints, "mousedown", function() {
  226. setTimeout(function(){cm.focus();}, 20);
  227. });
  228. CodeMirror.signal(data, "select", completions[0], hints.firstChild);
  229. return true;
  230. }
  231. Widget.prototype = {
  232. close: function() {
  233. if (this.completion.widget != this) return;
  234. this.completion.widget = null;
  235. this.hints.parentNode.removeChild(this.hints);
  236. this.completion.cm.removeKeyMap(this.keyMap);
  237. var cm = this.completion.cm;
  238. if (this.completion.options.closeOnUnfocus !== false) {
  239. cm.off("blur", this.onBlur);
  240. cm.off("focus", this.onFocus);
  241. }
  242. cm.off("scroll", this.onScroll);
  243. },
  244. pick: function() {
  245. this.completion.pick(this.data, this.selectedHint);
  246. },
  247. changeActive: function(i, avoidWrap) {
  248. if (i >= this.data.list.length)
  249. i = avoidWrap ? this.data.list.length - 1 : 0;
  250. else if (i < 0)
  251. i = avoidWrap ? 0 : this.data.list.length - 1;
  252. if (this.selectedHint == i) return;
  253. var node = this.hints.childNodes[this.selectedHint];
  254. node.className = node.className.replace(" " + ACTIVE_HINT_ELEMENT_CLASS, "");
  255. node = this.hints.childNodes[this.selectedHint = i];
  256. node.className += " " + ACTIVE_HINT_ELEMENT_CLASS;
  257. if (node.offsetTop < this.hints.scrollTop)
  258. this.hints.scrollTop = node.offsetTop - 3;
  259. else if (node.offsetTop + node.offsetHeight > this.hints.scrollTop + this.hints.clientHeight)
  260. this.hints.scrollTop = node.offsetTop + node.offsetHeight - this.hints.clientHeight + 3;
  261. CodeMirror.signal(this.data, "select", this.data.list[this.selectedHint], node);
  262. },
  263. screenAmount: function() {
  264. return Math.floor(this.hints.clientHeight / this.hints.firstChild.offsetHeight) || 1;
  265. }
  266. };
  267. CodeMirror.registerHelper("hint", "auto", function(cm, options) {
  268. var helpers = cm.getHelpers(cm.getCursor(), "hint");
  269. if (helpers.length) {
  270. for (var i = 0; i < helpers.length; i++) {
  271. var cur = helpers[i](cm, options);
  272. if (cur && cur.list.length) return cur;
  273. }
  274. } else {
  275. var words = cm.getHelper(cm.getCursor(), "hintWords");
  276. if (words) return CodeMirror.hint.fromList(cm, {words: words});
  277. }
  278. });
  279. CodeMirror.registerHelper("hint", "fromList", function(cm, options) {
  280. var cur = cm.getCursor(), token = cm.getTokenAt(cur);
  281. var found = [];
  282. for (var i = 0; i < options.words.length; i++) {
  283. var word = options.words[i];
  284. if (word.slice(0, token.string.length) == token.string)
  285. found.push(word);
  286. }
  287. if (found.length) return {
  288. list: found,
  289. from: CodeMirror.Pos(cur.line, token.start),
  290. to: CodeMirror.Pos(cur.line, token.end)
  291. };
  292. });
  293. CodeMirror.commands.autocomplete = CodeMirror.showHint;
  294. })();