Mercurial
comparison .cms/lib/codemirror/src/display/selection.js @ 0:78edf6b517a0 draft
24.10
author | Coffee CMS <info@coffee-cms.ru> |
---|---|
date | Fri, 11 Oct 2024 22:40:23 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:78edf6b517a0 |
---|---|
1 import { Pos } from "../line/pos.js" | |
2 import { visualLine } from "../line/spans.js" | |
3 import { getLine } from "../line/utils_line.js" | |
4 import { charCoords, cursorCoords, displayWidth, paddingH, wrappedLineExtentChar } from "../measurement/position_measurement.js" | |
5 import { getOrder, iterateBidiSections } from "../util/bidi.js" | |
6 import { elt } from "../util/dom.js" | |
7 import { onBlur } from "./focus.js" | |
8 | |
9 export function updateSelection(cm) { | |
10 cm.display.input.showSelection(cm.display.input.prepareSelection()) | |
11 } | |
12 | |
13 export function prepareSelection(cm, primary = true) { | |
14 let doc = cm.doc, result = {} | |
15 let curFragment = result.cursors = document.createDocumentFragment() | |
16 let selFragment = result.selection = document.createDocumentFragment() | |
17 | |
18 let customCursor = cm.options.$customCursor | |
19 if (customCursor) primary = true | |
20 for (let i = 0; i < doc.sel.ranges.length; i++) { | |
21 if (!primary && i == doc.sel.primIndex) continue | |
22 let range = doc.sel.ranges[i] | |
23 if (range.from().line >= cm.display.viewTo || range.to().line < cm.display.viewFrom) continue | |
24 let collapsed = range.empty() | |
25 if (customCursor) { | |
26 let head = customCursor(cm, range) | |
27 if (head) drawSelectionCursor(cm, head, curFragment) | |
28 } else if (collapsed || cm.options.showCursorWhenSelecting) { | |
29 drawSelectionCursor(cm, range.head, curFragment) | |
30 } | |
31 if (!collapsed) | |
32 drawSelectionRange(cm, range, selFragment) | |
33 } | |
34 return result | |
35 } | |
36 | |
37 // Draws a cursor for the given range | |
38 export function drawSelectionCursor(cm, head, output) { | |
39 let pos = cursorCoords(cm, head, "div", null, null, !cm.options.singleCursorHeightPerLine) | |
40 | |
41 let cursor = output.appendChild(elt("div", "\u00a0", "CodeMirror-cursor")) | |
42 cursor.style.left = pos.left + "px" | |
43 cursor.style.top = pos.top + "px" | |
44 cursor.style.height = Math.max(0, pos.bottom - pos.top) * cm.options.cursorHeight + "px" | |
45 | |
46 if (/\bcm-fat-cursor\b/.test(cm.getWrapperElement().className)) { | |
47 let charPos = charCoords(cm, head, "div", null, null) | |
48 let width = charPos.right - charPos.left | |
49 cursor.style.width = (width > 0 ? width : cm.defaultCharWidth()) + "px" | |
50 } | |
51 | |
52 if (pos.other) { | |
53 // Secondary cursor, shown when on a 'jump' in bi-directional text | |
54 let otherCursor = output.appendChild(elt("div", "\u00a0", "CodeMirror-cursor CodeMirror-secondarycursor")) | |
55 otherCursor.style.display = "" | |
56 otherCursor.style.left = pos.other.left + "px" | |
57 otherCursor.style.top = pos.other.top + "px" | |
58 otherCursor.style.height = (pos.other.bottom - pos.other.top) * .85 + "px" | |
59 } | |
60 } | |
61 | |
62 function cmpCoords(a, b) { return a.top - b.top || a.left - b.left } | |
63 | |
64 // Draws the given range as a highlighted selection | |
65 function drawSelectionRange(cm, range, output) { | |
66 let display = cm.display, doc = cm.doc | |
67 let fragment = document.createDocumentFragment() | |
68 let padding = paddingH(cm.display), leftSide = padding.left | |
69 let rightSide = Math.max(display.sizerWidth, displayWidth(cm) - display.sizer.offsetLeft) - padding.right | |
70 let docLTR = doc.direction == "ltr" | |
71 | |
72 function add(left, top, width, bottom) { | |
73 if (top < 0) top = 0 | |
74 top = Math.round(top) | |
75 bottom = Math.round(bottom) | |
76 fragment.appendChild(elt("div", null, "CodeMirror-selected", `position: absolute; left: ${left}px; | |
77 top: ${top}px; width: ${width == null ? rightSide - left : width}px; | |
78 height: ${bottom - top}px`)) | |
79 } | |
80 | |
81 function drawForLine(line, fromArg, toArg) { | |
82 let lineObj = getLine(doc, line) | |
83 let lineLen = lineObj.text.length | |
84 let start, end | |
85 function coords(ch, bias) { | |
86 return charCoords(cm, Pos(line, ch), "div", lineObj, bias) | |
87 } | |
88 | |
89 function wrapX(pos, dir, side) { | |
90 let extent = wrappedLineExtentChar(cm, lineObj, null, pos) | |
91 let prop = (dir == "ltr") == (side == "after") ? "left" : "right" | |
92 let ch = side == "after" ? extent.begin : extent.end - (/\s/.test(lineObj.text.charAt(extent.end - 1)) ? 2 : 1) | |
93 return coords(ch, prop)[prop] | |
94 } | |
95 | |
96 let order = getOrder(lineObj, doc.direction) | |
97 iterateBidiSections(order, fromArg || 0, toArg == null ? lineLen : toArg, (from, to, dir, i) => { | |
98 let ltr = dir == "ltr" | |
99 let fromPos = coords(from, ltr ? "left" : "right") | |
100 let toPos = coords(to - 1, ltr ? "right" : "left") | |
101 | |
102 let openStart = fromArg == null && from == 0, openEnd = toArg == null && to == lineLen | |
103 let first = i == 0, last = !order || i == order.length - 1 | |
104 if (toPos.top - fromPos.top <= 3) { // Single line | |
105 let openLeft = (docLTR ? openStart : openEnd) && first | |
106 let openRight = (docLTR ? openEnd : openStart) && last | |
107 let left = openLeft ? leftSide : (ltr ? fromPos : toPos).left | |
108 let right = openRight ? rightSide : (ltr ? toPos : fromPos).right | |
109 add(left, fromPos.top, right - left, fromPos.bottom) | |
110 } else { // Multiple lines | |
111 let topLeft, topRight, botLeft, botRight | |
112 if (ltr) { | |
113 topLeft = docLTR && openStart && first ? leftSide : fromPos.left | |
114 topRight = docLTR ? rightSide : wrapX(from, dir, "before") | |
115 botLeft = docLTR ? leftSide : wrapX(to, dir, "after") | |
116 botRight = docLTR && openEnd && last ? rightSide : toPos.right | |
117 } else { | |
118 topLeft = !docLTR ? leftSide : wrapX(from, dir, "before") | |
119 topRight = !docLTR && openStart && first ? rightSide : fromPos.right | |
120 botLeft = !docLTR && openEnd && last ? leftSide : toPos.left | |
121 botRight = !docLTR ? rightSide : wrapX(to, dir, "after") | |
122 } | |
123 add(topLeft, fromPos.top, topRight - topLeft, fromPos.bottom) | |
124 if (fromPos.bottom < toPos.top) add(leftSide, fromPos.bottom, null, toPos.top) | |
125 add(botLeft, toPos.top, botRight - botLeft, toPos.bottom) | |
126 } | |
127 | |
128 if (!start || cmpCoords(fromPos, start) < 0) start = fromPos | |
129 if (cmpCoords(toPos, start) < 0) start = toPos | |
130 if (!end || cmpCoords(fromPos, end) < 0) end = fromPos | |
131 if (cmpCoords(toPos, end) < 0) end = toPos | |
132 }) | |
133 return {start: start, end: end} | |
134 } | |
135 | |
136 let sFrom = range.from(), sTo = range.to() | |
137 if (sFrom.line == sTo.line) { | |
138 drawForLine(sFrom.line, sFrom.ch, sTo.ch) | |
139 } else { | |
140 let fromLine = getLine(doc, sFrom.line), toLine = getLine(doc, sTo.line) | |
141 let singleVLine = visualLine(fromLine) == visualLine(toLine) | |
142 let leftEnd = drawForLine(sFrom.line, sFrom.ch, singleVLine ? fromLine.text.length + 1 : null).end | |
143 let rightStart = drawForLine(sTo.line, singleVLine ? 0 : null, sTo.ch).start | |
144 if (singleVLine) { | |
145 if (leftEnd.top < rightStart.top - 2) { | |
146 add(leftEnd.right, leftEnd.top, null, leftEnd.bottom) | |
147 add(leftSide, rightStart.top, rightStart.left, rightStart.bottom) | |
148 } else { | |
149 add(leftEnd.right, leftEnd.top, rightStart.left - leftEnd.right, leftEnd.bottom) | |
150 } | |
151 } | |
152 if (leftEnd.bottom < rightStart.top) | |
153 add(leftSide, leftEnd.bottom, null, rightStart.top) | |
154 } | |
155 | |
156 output.appendChild(fragment) | |
157 } | |
158 | |
159 // Cursor-blinking | |
160 export function restartBlink(cm) { | |
161 if (!cm.state.focused) return | |
162 let display = cm.display | |
163 clearInterval(display.blinker) | |
164 let on = true | |
165 display.cursorDiv.style.visibility = "" | |
166 if (cm.options.cursorBlinkRate > 0) | |
167 display.blinker = setInterval(() => { | |
168 if (!cm.hasFocus()) onBlur(cm) | |
169 display.cursorDiv.style.visibility = (on = !on) ? "" : "hidden" | |
170 }, cm.options.cursorBlinkRate) | |
171 else if (cm.options.cursorBlinkRate < 0) | |
172 display.cursorDiv.style.visibility = "hidden" | |
173 } |