MCPcopy Create free account
hub / github.com/TextGeneratorio/text-generator.io / drawSelectionRange

Function drawSelectionRange

static/libs/codemirror.js:3100–3190  ·  view source on GitHub ↗
(cm, range$$1, output)

Source from the content-addressed store, hash-verified

3098
3099// Draws the given range as a highlighted selection
3100function drawSelectionRange(cm, range$$1, output) {
3101 var display = cm.display, doc = cm.doc;
3102 var fragment = document.createDocumentFragment();
3103 var padding = paddingH(cm.display), leftSide = padding.left;
3104 var rightSide = Math.max(display.sizerWidth, displayWidth(cm) - display.sizer.offsetLeft) - padding.right;
3105 var docLTR = doc.direction == "ltr";
3106
3107 function add(left, top, width, bottom) {
3108 if (top < 0) { top = 0; }
3109 top = Math.round(top);
3110 bottom = Math.round(bottom);
3111 fragment.appendChild(elt("div", null, "CodeMirror-selected", ("position: absolute; left: " + left + "px;\n top: " + top + "px; width: " + (width == null ? rightSide - left : width) + "px;\n height: " + (bottom - top) + "px")));
3112 }
3113
3114 function drawForLine(line, fromArg, toArg) {
3115 var lineObj = getLine(doc, line);
3116 var lineLen = lineObj.text.length;
3117 var start, end;
3118 function coords(ch, bias) {
3119 return charCoords(cm, Pos(line, ch), "div", lineObj, bias)
3120 }
3121
3122 function wrapX(pos, dir, side) {
3123 var extent = wrappedLineExtentChar(cm, lineObj, null, pos);
3124 var prop = (dir == "ltr") == (side == "after") ? "left" : "right";
3125 var ch = side == "after" ? extent.begin : extent.end - (/\s/.test(lineObj.text.charAt(extent.end - 1)) ? 2 : 1);
3126 return coords(ch, prop)[prop]
3127 }
3128
3129 var order = getOrder(lineObj, doc.direction);
3130 iterateBidiSections(order, fromArg || 0, toArg == null ? lineLen : toArg, function (from, to, dir, i) {
3131 var ltr = dir == "ltr";
3132 var fromPos = coords(from, ltr ? "left" : "right");
3133 var toPos = coords(to - 1, ltr ? "right" : "left");
3134
3135 var openStart = fromArg == null && from == 0, openEnd = toArg == null && to == lineLen;
3136 var first = i == 0, last = !order || i == order.length - 1;
3137 if (toPos.top - fromPos.top <= 3) { // Single line
3138 var openLeft = (docLTR ? openStart : openEnd) && first;
3139 var openRight = (docLTR ? openEnd : openStart) && last;
3140 var left = openLeft ? leftSide : (ltr ? fromPos : toPos).left;
3141 var right = openRight ? rightSide : (ltr ? toPos : fromPos).right;
3142 add(left, fromPos.top, right - left, fromPos.bottom);
3143 } else { // Multiple lines
3144 var topLeft, topRight, botLeft, botRight;
3145 if (ltr) {
3146 topLeft = docLTR && openStart && first ? leftSide : fromPos.left;
3147 topRight = docLTR ? rightSide : wrapX(from, dir, "before");
3148 botLeft = docLTR ? leftSide : wrapX(to, dir, "after");
3149 botRight = docLTR && openEnd && last ? rightSide : toPos.right;
3150 } else {
3151 topLeft = !docLTR ? leftSide : wrapX(from, dir, "before");
3152 topRight = !docLTR && openStart && first ? rightSide : fromPos.right;
3153 botLeft = !docLTR && openEnd && last ? leftSide : toPos.left;
3154 botRight = !docLTR ? rightSide : wrapX(to, dir, "after");
3155 }
3156 add(topLeft, fromPos.top, topRight - topLeft, fromPos.bottom);
3157 if (fromPos.bottom < toPos.top) { add(leftSide, fromPos.bottom, null, toPos.top); }

Callers 1

prepareSelectionFunction · 0.85

Calls 7

paddingHFunction · 0.85
displayWidthFunction · 0.85
drawForLineFunction · 0.85
getLineFunction · 0.85
visualLineFunction · 0.85
toMethod · 0.80
addFunction · 0.70

Tested by

no test coverage detected