0863f552d99e9b2a565e82c47f18f0da9c54d799a1c2a78a149a8025333f49ee10655eb0d2bb97d8d71145d3b4c6738224ca115f9b26f67b42a42bdc111113 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. import { loadMode } from "../display/mode_state.js"
  2. import { runInOp } from "../display/operations.js"
  3. import { regChange } from "../display/view_tracking.js"
  4. import { Line, updateLine } from "../line/line_data.js"
  5. import { findMaxLine } from "../line/spans.js"
  6. import { getLine } from "../line/utils_line.js"
  7. import { estimateLineHeights } from "../measurement/position_measurement.js"
  8. import { addClass, rmClass } from "../util/dom.js"
  9. import { lst } from "../util/misc.js"
  10. import { signalLater } from "../util/operation_group.js"
  11. // DOCUMENT DATA STRUCTURE
  12. // By default, updates that start and end at the beginning of a line
  13. // are treated specially, in order to make the association of line
  14. // widgets and marker elements with the text behave more intuitive.
  15. export function isWholeLineUpdate(doc, change) {
  16. return change.from.ch == 0 && change.to.ch == 0 && lst(change.text) == "" &&
  17. (!doc.cm || doc.cm.options.wholeLineUpdateBefore)
  18. }
  19. // Perform a change on the document data structure.
  20. export function updateDoc(doc, change, markedSpans, estimateHeight) {
  21. function spansFor(n) {return markedSpans ? markedSpans[n] : null}
  22. function update(line, text, spans) {
  23. updateLine(line, text, spans, estimateHeight)
  24. signalLater(line, "change", line, change)
  25. }
  26. function linesFor(start, end) {
  27. let result = []
  28. for (let i = start; i < end; ++i)
  29. result.push(new Line(text[i], spansFor(i), estimateHeight))
  30. return result
  31. }
  32. let from = change.from, to = change.to, text = change.text
  33. let firstLine = getLine(doc, from.line), lastLine = getLine(doc, to.line)
  34. let lastText = lst(text), lastSpans = spansFor(text.length - 1), nlines = to.line - from.line
  35. // Adjust the line structure
  36. if (change.full) {
  37. doc.insert(0, linesFor(0, text.length))
  38. doc.remove(text.length, doc.size - text.length)
  39. } else if (isWholeLineUpdate(doc, change)) {
  40. // This is a whole-line replace. Treated specially to make
  41. // sure line objects move the way they are supposed to.
  42. let added = linesFor(0, text.length - 1)
  43. update(lastLine, lastLine.text, lastSpans)
  44. if (nlines) doc.remove(from.line, nlines)
  45. if (added.length) doc.insert(from.line, added)
  46. } else if (firstLine == lastLine) {
  47. if (text.length == 1) {
  48. update(firstLine, firstLine.text.slice(0, from.ch) + lastText + firstLine.text.slice(to.ch), lastSpans)
  49. } else {
  50. let added = linesFor(1, text.length - 1)
  51. added.push(new Line(lastText + firstLine.text.slice(to.ch), lastSpans, estimateHeight))
  52. update(firstLine, firstLine.text.slice(0, from.ch) + text[0], spansFor(0))
  53. doc.insert(from.line + 1, added)
  54. }
  55. } else if (text.length == 1) {
  56. update(firstLine, firstLine.text.slice(0, from.ch) + text[0] + lastLine.text.slice(to.ch), spansFor(0))
  57. doc.remove(from.line + 1, nlines)
  58. } else {
  59. update(firstLine, firstLine.text.slice(0, from.ch) + text[0], spansFor(0))
  60. update(lastLine, lastText + lastLine.text.slice(to.ch), lastSpans)
  61. let added = linesFor(1, text.length - 1)
  62. if (nlines > 1) doc.remove(from.line + 1, nlines - 1)
  63. doc.insert(from.line + 1, added)
  64. }
  65. signalLater(doc, "change", doc, change)
  66. }
  67. // Call f for all linked documents.
  68. export function linkedDocs(doc, f, sharedHistOnly) {
  69. function propagate(doc, skip, sharedHist) {
  70. if (doc.linked) for (let i = 0; i < doc.linked.length; ++i) {
  71. let rel = doc.linked[i]
  72. if (rel.doc == skip) continue
  73. let shared = sharedHist && rel.sharedHist
  74. if (sharedHistOnly && !shared) continue
  75. f(rel.doc, shared)
  76. propagate(rel.doc, doc, shared)
  77. }
  78. }
  79. propagate(doc, null, true)
  80. }
  81. // Attach a document to an editor.
  82. export function attachDoc(cm, doc) {
  83. if (doc.cm) throw new Error("This document is already in use.")
  84. cm.doc = doc
  85. doc.cm = cm
  86. estimateLineHeights(cm)
  87. loadMode(cm)
  88. setDirectionClass(cm)
  89. cm.options.direction = doc.direction
  90. if (!cm.options.lineWrapping) findMaxLine(cm)
  91. cm.options.mode = doc.modeOption
  92. regChange(cm)
  93. }
  94. function setDirectionClass(cm) {
  95. ;(cm.doc.direction == "rtl" ? addClass : rmClass)(cm.display.lineDiv, "CodeMirror-rtl")
  96. }
  97. export function directionChanged(cm) {
  98. runInOp(cm, () => {
  99. setDirectionClass(cm)
  100. regChange(cm)
  101. })
  102. }