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

Function drawSelectionRange

static/mergely/lib/codemirror.js:2341–2413  ·  view source on GitHub ↗
(cm, range, output)

Source from the content-addressed store, hash-verified

2339
2340 // Draws the given range as a highlighted selection
2341 function drawSelectionRange(cm, range, output) {
2342 var display = cm.display, doc = cm.doc;
2343 var fragment = document.createDocumentFragment();
2344 var padding = paddingH(cm.display), leftSide = padding.left;
2345 var rightSide = Math.max(display.sizerWidth, displayWidth(cm) - display.sizer.offsetLeft) - padding.right;
2346
2347 function add(left, top, width, bottom) {
2348 if (top < 0) top = 0;
2349 top = Math.round(top);
2350 bottom = Math.round(bottom);
2351 fragment.appendChild(elt("div", null, "CodeMirror-selected", "position: absolute; left: " + left +
2352 "px; top: " + top + "px; width: " + (width == null ? rightSide - left : width) +
2353 "px; height: " + (bottom - top) + "px"));
2354 }
2355
2356 function drawForLine(line, fromArg, toArg) {
2357 var lineObj = getLine(doc, line);
2358 var lineLen = lineObj.text.length;
2359 var start, end;
2360 function coords(ch, bias) {
2361 return charCoords(cm, Pos(line, ch), "div", lineObj, bias);
2362 }
2363
2364 iterateBidiSections(getOrder(lineObj), fromArg || 0, toArg == null ? lineLen : toArg, function(from, to, dir) {
2365 var leftPos = coords(from, "left"), rightPos, left, right;
2366 if (from == to) {
2367 rightPos = leftPos;
2368 left = right = leftPos.left;
2369 } else {
2370 rightPos = coords(to - 1, "right");
2371 if (dir == "rtl") { var tmp = leftPos; leftPos = rightPos; rightPos = tmp; }
2372 left = leftPos.left;
2373 right = rightPos.right;
2374 }
2375 if (fromArg == null && from == 0) left = leftSide;
2376 if (rightPos.top - leftPos.top > 3) { // Different lines, draw top part
2377 add(left, leftPos.top, null, leftPos.bottom);
2378 left = leftSide;
2379 if (leftPos.bottom < rightPos.top) add(left, leftPos.bottom, null, rightPos.top);
2380 }
2381 if (toArg == null && to == lineLen) right = rightSide;
2382 if (!start || leftPos.top < start.top || leftPos.top == start.top && leftPos.left < start.left)
2383 start = leftPos;
2384 if (!end || rightPos.bottom > end.bottom || rightPos.bottom == end.bottom && rightPos.right > end.right)
2385 end = rightPos;
2386 if (left < leftSide + 1) left = leftSide;
2387 add(left, rightPos.top, right - left, rightPos.bottom);
2388 });
2389 return {start: start, end: end};
2390 }
2391
2392 var sFrom = range.from(), sTo = range.to();
2393 if (sFrom.line == sTo.line) {
2394 drawForLine(sFrom.line, sFrom.ch, sTo.ch);
2395 } else {
2396 var fromLine = getLine(doc, sFrom.line), toLine = getLine(doc, sTo.line);
2397 var singleVLine = visualLine(fromLine) == visualLine(toLine);
2398 var leftEnd = drawForLine(sFrom.line, sFrom.ch, singleVLine ? fromLine.text.length + 1 : null).end;

Callers 1

prepareSelectionFunction · 0.70

Calls 6

paddingHFunction · 0.70
displayWidthFunction · 0.70
drawForLineFunction · 0.70
getLineFunction · 0.70
visualLineFunction · 0.70
addFunction · 0.70

Tested by

no test coverage detected