6f4892e816f61ddff5400a615baf2d67c53d1c23d36dee2763784cbd6a231f956af602cbd2f909c66a9337847e3d6e81134055f4d3e984391f4f156b24d91e 7.3 KB

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