operations.js 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. import { clipPos } from "../line/pos.js"
  2. import { findMaxLine } from "../line/spans.js"
  3. import { displayWidth, measureChar, scrollGap } from "../measurement/position_measurement.js"
  4. import { signal } from "../util/event.js"
  5. import { activeElt } from "../util/dom.js"
  6. import { finishOperation, pushOperation } from "../util/operation_group.js"
  7. import { ensureFocus } from "./focus.js"
  8. import { measureForScrollbars, updateScrollbars } from "./scrollbars.js"
  9. import { restartBlink } from "./selection.js"
  10. import { maybeScrollWindow, scrollPosIntoView, setScrollLeft, setScrollTop } from "./scrolling.js"
  11. import { DisplayUpdate, maybeClipScrollbars, postUpdateDisplay, setDocumentHeight, updateDisplayIfNeeded } from "./update_display.js"
  12. import { updateHeightsInViewport } from "./update_lines.js"
  13. // Operations are used to wrap a series of changes to the editor
  14. // state in such a way that each change won't have to update the
  15. // cursor and display (which would be awkward, slow, and
  16. // error-prone). Instead, display updates are batched and then all
  17. // combined and executed at once.
  18. let nextOpId = 0
  19. // Start a new operation.
  20. export function startOperation(cm) {
  21. cm.curOp = {
  22. cm: cm,
  23. viewChanged: false, // Flag that indicates that lines might need to be redrawn
  24. startHeight: cm.doc.height, // Used to detect need to update scrollbar
  25. forceUpdate: false, // Used to force a redraw
  26. updateInput: null, // Whether to reset the input textarea
  27. typing: false, // Whether this reset should be careful to leave existing text (for compositing)
  28. changeObjs: null, // Accumulated changes, for firing change events
  29. cursorActivityHandlers: null, // Set of handlers to fire cursorActivity on
  30. cursorActivityCalled: 0, // Tracks which cursorActivity handlers have been called already
  31. selectionChanged: false, // Whether the selection needs to be redrawn
  32. updateMaxLine: false, // Set when the widest line needs to be determined anew
  33. scrollLeft: null, scrollTop: null, // Intermediate scroll position, not pushed to DOM yet
  34. scrollToPos: null, // Used to scroll to a specific position
  35. focus: false,
  36. id: ++nextOpId // Unique ID
  37. }
  38. pushOperation(cm.curOp)
  39. }
  40. // Finish an operation, updating the display and signalling delayed events
  41. export function endOperation(cm) {
  42. let op = cm.curOp
  43. finishOperation(op, group => {
  44. for (let i = 0; i < group.ops.length; i++)
  45. group.ops[i].cm.curOp = null
  46. endOperations(group)
  47. })
  48. }
  49. // The DOM updates done when an operation finishes are batched so
  50. // that the minimum number of relayouts are required.
  51. function endOperations(group) {
  52. let ops = group.ops
  53. for (let i = 0; i < ops.length; i++) // Read DOM
  54. endOperation_R1(ops[i])
  55. for (let i = 0; i < ops.length; i++) // Write DOM (maybe)
  56. endOperation_W1(ops[i])
  57. for (let i = 0; i < ops.length; i++) // Read DOM
  58. endOperation_R2(ops[i])
  59. for (let i = 0; i < ops.length; i++) // Write DOM (maybe)
  60. endOperation_W2(ops[i])
  61. for (let i = 0; i < ops.length; i++) // Read DOM
  62. endOperation_finish(ops[i])
  63. }
  64. function endOperation_R1(op) {
  65. let cm = op.cm, display = cm.display
  66. maybeClipScrollbars(cm)
  67. if (op.updateMaxLine) findMaxLine(cm)
  68. op.mustUpdate = op.viewChanged || op.forceUpdate || op.scrollTop != null ||
  69. op.scrollToPos && (op.scrollToPos.from.line < display.viewFrom ||
  70. op.scrollToPos.to.line >= display.viewTo) ||
  71. display.maxLineChanged && cm.options.lineWrapping
  72. op.update = op.mustUpdate &&
  73. new DisplayUpdate(cm, op.mustUpdate && {top: op.scrollTop, ensure: op.scrollToPos}, op.forceUpdate)
  74. }
  75. function endOperation_W1(op) {
  76. op.updatedDisplay = op.mustUpdate && updateDisplayIfNeeded(op.cm, op.update)
  77. }
  78. function endOperation_R2(op) {
  79. let cm = op.cm, display = cm.display
  80. if (op.updatedDisplay) updateHeightsInViewport(cm)
  81. op.barMeasure = measureForScrollbars(cm)
  82. // If the max line changed since it was last measured, measure it,
  83. // and ensure the document's width matches it.
  84. // updateDisplay_W2 will use these properties to do the actual resizing
  85. if (display.maxLineChanged && !cm.options.lineWrapping) {
  86. op.adjustWidthTo = measureChar(cm, display.maxLine, display.maxLine.text.length).left + 3
  87. cm.display.sizerWidth = op.adjustWidthTo
  88. op.barMeasure.scrollWidth =
  89. Math.max(display.scroller.clientWidth, display.sizer.offsetLeft + op.adjustWidthTo + scrollGap(cm) + cm.display.barWidth)
  90. op.maxScrollLeft = Math.max(0, display.sizer.offsetLeft + op.adjustWidthTo - displayWidth(cm))
  91. }
  92. if (op.updatedDisplay || op.selectionChanged)
  93. op.preparedSelection = display.input.prepareSelection()
  94. }
  95. function endOperation_W2(op) {
  96. let cm = op.cm
  97. if (op.adjustWidthTo != null) {
  98. cm.display.sizer.style.minWidth = op.adjustWidthTo + "px"
  99. if (op.maxScrollLeft < cm.doc.scrollLeft)
  100. setScrollLeft(cm, Math.min(cm.display.scroller.scrollLeft, op.maxScrollLeft), true)
  101. cm.display.maxLineChanged = false
  102. }
  103. let takeFocus = op.focus && op.focus == activeElt()
  104. if (op.preparedSelection)
  105. cm.display.input.showSelection(op.preparedSelection, takeFocus)
  106. if (op.updatedDisplay || op.startHeight != cm.doc.height)
  107. updateScrollbars(cm, op.barMeasure)
  108. if (op.updatedDisplay)
  109. setDocumentHeight(cm, op.barMeasure)
  110. if (op.selectionChanged) restartBlink(cm)
  111. if (cm.state.focused && op.updateInput)
  112. cm.display.input.reset(op.typing)
  113. if (takeFocus) ensureFocus(op.cm)
  114. }
  115. function endOperation_finish(op) {
  116. let cm = op.cm, display = cm.display, doc = cm.doc
  117. if (op.updatedDisplay) postUpdateDisplay(cm, op.update)
  118. // Abort mouse wheel delta measurement, when scrolling explicitly
  119. if (display.wheelStartX != null && (op.scrollTop != null || op.scrollLeft != null || op.scrollToPos))
  120. display.wheelStartX = display.wheelStartY = null
  121. // Propagate the scroll position to the actual DOM scroller
  122. if (op.scrollTop != null) setScrollTop(cm, op.scrollTop, op.forceScroll)
  123. if (op.scrollLeft != null) setScrollLeft(cm, op.scrollLeft, true, true)
  124. // If we need to scroll a specific position into view, do so.
  125. if (op.scrollToPos) {
  126. let rect = scrollPosIntoView(cm, clipPos(doc, op.scrollToPos.from),
  127. clipPos(doc, op.scrollToPos.to), op.scrollToPos.margin)
  128. maybeScrollWindow(cm, rect)
  129. }
  130. // Fire events for markers that are hidden/unidden by editing or
  131. // undoing
  132. let hidden = op.maybeHiddenMarkers, unhidden = op.maybeUnhiddenMarkers
  133. if (hidden) for (let i = 0; i < hidden.length; ++i)
  134. if (!hidden[i].lines.length) signal(hidden[i], "hide")
  135. if (unhidden) for (let i = 0; i < unhidden.length; ++i)
  136. if (unhidden[i].lines.length) signal(unhidden[i], "unhide")
  137. if (display.wrapper.offsetHeight)
  138. doc.scrollTop = cm.display.scroller.scrollTop
  139. // Fire change events, and delayed event handlers
  140. if (op.changeObjs)
  141. signal(cm, "changes", cm, op.changeObjs)
  142. if (op.update)
  143. op.update.finish()
  144. }
  145. // Run the given function in an operation
  146. export function runInOp(cm, f) {
  147. if (cm.curOp) return f()
  148. startOperation(cm)
  149. try { return f() }
  150. finally { endOperation(cm) }
  151. }
  152. // Wraps a function in an operation. Returns the wrapped function.
  153. export function operation(cm, f) {
  154. return function() {
  155. if (cm.curOp) return f.apply(cm, arguments)
  156. startOperation(cm)
  157. try { return f.apply(cm, arguments) }
  158. finally { endOperation(cm) }
  159. }
  160. }
  161. // Used to add methods to editor and doc instances, wrapping them in
  162. // operations.
  163. export function methodOp(f) {
  164. return function() {
  165. if (this.curOp) return f.apply(this, arguments)
  166. startOperation(this)
  167. try { return f.apply(this, arguments) }
  168. finally { endOperation(this) }
  169. }
  170. }
  171. export function docMethodOp(f) {
  172. return function() {
  173. let cm = this.cm
  174. if (!cm || cm.curOp) return f.apply(this, arguments)
  175. startOperation(cm)
  176. try { return f.apply(this, arguments) }
  177. finally { endOperation(cm) }
  178. }
  179. }