MCPcopy Create free account
hub / github.com/TruthHun/BookStack / Widget

Function Widget

static/editor.md/lib/codemirror/addon/hint/show-hint.js:206–306  ·  view source on GitHub ↗
(completion, data)

Source from the content-addressed store, hash-verified

204 }
205
206 function Widget(completion, data) {
207 this.completion = completion;
208 this.data = data;
209 var widget = this, cm = completion.cm;
210
211 var hints = this.hints = document.createElement("ul");
212 hints.className = "CodeMirror-hints";
213 this.selectedHint = data.selectedHint || 0;
214
215 var completions = data.list;
216 for (var i = 0; i < completions.length; ++i) {
217 var elt = hints.appendChild(document.createElement("li")), cur = completions[i];
218 var className = HINT_ELEMENT_CLASS + (i != this.selectedHint ? "" : " " + ACTIVE_HINT_ELEMENT_CLASS);
219 if (cur.className != null) className = cur.className + " " + className;
220 elt.className = className;
221 if (cur.render) cur.render(elt, data, cur);
222 else elt.appendChild(document.createTextNode(cur.displayText || getText(cur)));
223 elt.hintId = i;
224 }
225
226 var pos = cm.cursorCoords(completion.options.alignWithWord ? data.from : null);
227 var left = pos.left, top = pos.bottom, below = true;
228 hints.style.left = left + "px";
229 hints.style.top = top + "px";
230 // If we're at the edge of the screen, then we want the menu to appear on the left of the cursor.
231 var winW = window.innerWidth || Math.max(document.body.offsetWidth, document.documentElement.offsetWidth);
232 var winH = window.innerHeight || Math.max(document.body.offsetHeight, document.documentElement.offsetHeight);
233 (completion.options.container || document.body).appendChild(hints);
234 var box = hints.getBoundingClientRect(), overlapY = box.bottom - winH;
235 if (overlapY > 0) {
236 var height = box.bottom - box.top, curTop = pos.top - (pos.bottom - box.top);
237 if (curTop - height > 0) { // Fits above cursor
238 hints.style.top = (top = pos.top - height) + "px";
239 below = false;
240 } else if (height > winH) {
241 hints.style.height = (winH - 5) + "px";
242 hints.style.top = (top = pos.bottom - box.top) + "px";
243 var cursor = cm.getCursor();
244 if (data.from.ch != cursor.ch) {
245 pos = cm.cursorCoords(cursor);
246 hints.style.left = (left = pos.left) + "px";
247 box = hints.getBoundingClientRect();
248 }
249 }
250 }
251 var overlapX = box.right - winW;
252 if (overlapX > 0) {
253 if (box.right - box.left > winW) {
254 hints.style.width = (winW - 5) + "px";
255 overlapX -= (box.right - box.left) - winW;
256 }
257 hints.style.left = (left = pos.left - overlapX) + "px";
258 }
259
260 cm.addKeyMap(this.keyMap = buildKeyMap(completion, {
261 moveFocus: function(n, avoidWrap) { widget.changeActive(widget.selectedHint + n, avoidWrap); },
262 setFocus: function(n) { widget.changeActive(n); },
263 menuSize: function() { return widget.screenAmount(); },

Callers

nothing calls this directly

Calls 3

buildKeyMapFunction · 0.85
getHintElementFunction · 0.85
getTextFunction · 0.70

Tested by

no test coverage detected