MCPcopy Create free account
hub / github.com/OpenEndedGroup/Field2 / drawSelectionRange

Function drawSelectionRange

lib/web/CodeMirror/src/display/selection.js:53–145  ·  view source on GitHub ↗
(cm, range, output)

Source from the content-addressed store, hash-verified

51
52// Draws the given range as a highlighted selection
53function drawSelectionRange(cm, range, output) {
54 let display = cm.display, doc = cm.doc
55 let fragment = document.createDocumentFragment()
56 let padding = paddingH(cm.display), leftSide = padding.left
57 let rightSide = Math.max(display.sizerWidth, displayWidth(cm) - display.sizer.offsetLeft) - padding.right
58 let docLTR = doc.direction == "ltr"
59
60 function add(left, top, width, bottom) {
61 if (top < 0) top = 0
62 top = Math.round(top)
63 bottom = Math.round(bottom)
64 fragment.appendChild(elt("div", null, "CodeMirror-selected", `position: absolute; left: ${left}px;
65 top: ${top}px; width: ${width == null ? rightSide - left : width}px;
66 height: ${bottom - top}px`))
67 }
68
69 function drawForLine(line, fromArg, toArg) {
70 let lineObj = getLine(doc, line)
71 let lineLen = lineObj.text.length
72 let start, end
73 function coords(ch, bias) {
74 return charCoords(cm, Pos(line, ch), "div", lineObj, bias)
75 }
76
77 function wrapX(pos, dir, side) {
78 let extent = wrappedLineExtentChar(cm, lineObj, null, pos)
79 let prop = (dir == "ltr") == (side == "after") ? "left" : "right"
80 let ch = side == "after" ? extent.begin : extent.end - (/\s/.test(lineObj.text.charAt(extent.end - 1)) ? 2 : 1)
81 return coords(ch, prop)[prop]
82 }
83
84 let order = getOrder(lineObj, doc.direction)
85 iterateBidiSections(order, fromArg || 0, toArg == null ? lineLen : toArg, (from, to, dir, i) => {
86 let ltr = dir == "ltr"
87 let fromPos = coords(from, ltr ? "left" : "right")
88 let toPos = coords(to - 1, ltr ? "right" : "left")
89
90 let openStart = fromArg == null && from == 0, openEnd = toArg == null && to == lineLen
91 let first = i == 0, last = !order || i == order.length - 1
92 if (toPos.top - fromPos.top <= 3) { // Single line
93 let openLeft = (docLTR ? openStart : openEnd) && first
94 let openRight = (docLTR ? openEnd : openStart) && last
95 let left = openLeft ? leftSide : (ltr ? fromPos : toPos).left
96 let right = openRight ? rightSide : (ltr ? toPos : fromPos).right
97 add(left, fromPos.top, right - left, fromPos.bottom)
98 } else { // Multiple lines
99 let topLeft, topRight, botLeft, botRight
100 if (ltr) {
101 topLeft = docLTR && openStart && first ? leftSide : fromPos.left
102 topRight = docLTR ? rightSide : wrapX(from, dir, "before")
103 botLeft = docLTR ? leftSide : wrapX(to, dir, "after")
104 botRight = docLTR && openEnd && last ? rightSide : toPos.right
105 } else {
106 topLeft = !docLTR ? leftSide : wrapX(from, dir, "before")
107 topRight = !docLTR && openStart && first ? rightSide : fromPos.right
108 botLeft = !docLTR && openEnd && last ? leftSide : toPos.left
109 botRight = !docLTR ? rightSide : wrapX(to, dir, "after")
110 }

Callers 1

prepareSelectionFunction · 0.70

Calls 9

paddingHFunction · 0.90
displayWidthFunction · 0.90
getLineFunction · 0.90
visualLineFunction · 0.90
fromMethod · 0.80
toMethod · 0.80
drawForLineFunction · 0.70
addFunction · 0.70
maxMethod · 0.45

Tested by

no test coverage detected