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

Function drawSelectionRange

static/editor.md/lib/codemirror/lib/codemirror.js:2238–2310  ·  view source on GitHub ↗
(cm, range, output)

Source from the content-addressed store, hash-verified

2236
2237 // Draws the given range as a highlighted selection
2238 function drawSelectionRange(cm, range, output) {
2239 var display = cm.display, doc = cm.doc;
2240 var fragment = document.createDocumentFragment();
2241 var padding = paddingH(cm.display), leftSide = padding.left;
2242 var rightSide = Math.max(display.sizerWidth, displayWidth(cm) - display.sizer.offsetLeft) - padding.right;
2243
2244 function add(left, top, width, bottom) {
2245 if (top < 0) top = 0;
2246 top = Math.round(top);
2247 bottom = Math.round(bottom);
2248 fragment.appendChild(elt("div", null, "CodeMirror-selected", "position: absolute; left: " + left +
2249 "px; top: " + top + "px; width: " + (width == null ? rightSide - left : width) +
2250 "px; height: " + (bottom - top) + "px"));
2251 }
2252
2253 function drawForLine(line, fromArg, toArg) {
2254 var lineObj = getLine(doc, line);
2255 var lineLen = lineObj.text.length;
2256 var start, end;
2257 function coords(ch, bias) {
2258 return charCoords(cm, Pos(line, ch), "div", lineObj, bias);
2259 }
2260
2261 iterateBidiSections(getOrder(lineObj), fromArg || 0, toArg == null ? lineLen : toArg, function(from, to, dir) {
2262 var leftPos = coords(from, "left"), rightPos, left, right;
2263 if (from == to) {
2264 rightPos = leftPos;
2265 left = right = leftPos.left;
2266 } else {
2267 rightPos = coords(to - 1, "right");
2268 if (dir == "rtl") { var tmp = leftPos; leftPos = rightPos; rightPos = tmp; }
2269 left = leftPos.left;
2270 right = rightPos.right;
2271 }
2272 if (fromArg == null && from == 0) left = leftSide;
2273 if (rightPos.top - leftPos.top > 3) { // Different lines, draw top part
2274 add(left, leftPos.top, null, leftPos.bottom);
2275 left = leftSide;
2276 if (leftPos.bottom < rightPos.top) add(left, leftPos.bottom, null, rightPos.top);
2277 }
2278 if (toArg == null && to == lineLen) right = rightSide;
2279 if (!start || leftPos.top < start.top || leftPos.top == start.top && leftPos.left < start.left)
2280 start = leftPos;
2281 if (!end || rightPos.bottom > end.bottom || rightPos.bottom == end.bottom && rightPos.right > end.right)
2282 end = rightPos;
2283 if (left < leftSide + 1) left = leftSide;
2284 add(left, rightPos.top, right - left, rightPos.bottom);
2285 });
2286 return {start: start, end: end};
2287 }
2288
2289 var sFrom = range.from(), sTo = range.to();
2290 if (sFrom.line == sTo.line) {
2291 drawForLine(sFrom.line, sFrom.ch, sTo.ch);
2292 } else {
2293 var fromLine = getLine(doc, sFrom.line), toLine = getLine(doc, sTo.line);
2294 var singleVLine = visualLine(fromLine) == visualLine(toLine);
2295 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