MCPcopy Create free account
hub / github.com/breck7/scroll / updateDoc

Function updateDoc

external/.scrollLibs.js:6491–6554  ·  view source on GitHub ↗
(doc, change, markedSpans, estimateHeight$$1)

Source from the content-addressed store, hash-verified

6489
6490 // Perform a change on the document data structure.
6491 function updateDoc(doc, change, markedSpans, estimateHeight$$1) {
6492 function spansFor(n) {
6493 return markedSpans ? markedSpans[n] : null
6494 }
6495 function update(line, text, spans) {
6496 updateLine(line, text, spans, estimateHeight$$1)
6497 signalLater(line, "change", line, change)
6498 }
6499 function linesFor(start, end) {
6500 var result = []
6501 for (var i = start; i < end; ++i) {
6502 result.push(new Line(text[i], spansFor(i), estimateHeight$$1))
6503 }
6504 return result
6505 }
6506
6507 var from = change.from,
6508 to = change.to,
6509 text = change.text
6510 var firstLine = getLine(doc, from.line),
6511 lastLine = getLine(doc, to.line)
6512 var lastText = lst(text),
6513 lastSpans = spansFor(text.length - 1),
6514 nlines = to.line - from.line
6515
6516 // Adjust the line structure
6517 if (change.full) {
6518 doc.insert(0, linesFor(0, text.length))
6519 doc.remove(text.length, doc.size - text.length)
6520 } else if (isWholeLineUpdate(doc, change)) {
6521 // This is a whole-line replace. Treated specially to make
6522 // sure line objects move the way they are supposed to.
6523 var added = linesFor(0, text.length - 1)
6524 update(lastLine, lastLine.text, lastSpans)
6525 if (nlines) {
6526 doc.remove(from.line, nlines)
6527 }
6528 if (added.length) {
6529 doc.insert(from.line, added)
6530 }
6531 } else if (firstLine == lastLine) {
6532 if (text.length == 1) {
6533 update(firstLine, firstLine.text.slice(0, from.ch) + lastText + firstLine.text.slice(to.ch), lastSpans)
6534 } else {
6535 var added$1 = linesFor(1, text.length - 1)
6536 added$1.push(new Line(lastText + firstLine.text.slice(to.ch), lastSpans, estimateHeight$$1))
6537 update(firstLine, firstLine.text.slice(0, from.ch) + text[0], spansFor(0))
6538 doc.insert(from.line + 1, added$1)
6539 }
6540 } else if (text.length == 1) {
6541 update(firstLine, firstLine.text.slice(0, from.ch) + text[0] + lastLine.text.slice(to.ch), spansFor(0))
6542 doc.remove(from.line + 1, nlines)
6543 } else {
6544 update(firstLine, firstLine.text.slice(0, from.ch) + text[0], spansFor(0))
6545 update(lastLine, lastText + lastLine.text.slice(to.ch), lastSpans)
6546 var added$2 = linesFor(1, text.length - 1)
6547 if (nlines > 1) {
6548 doc.remove(from.line + 1, nlines - 1)

Callers 3

makeChangeSingleDocFunction · 0.85
DocFunction · 0.85

Calls 9

getLineFunction · 0.85
lstFunction · 0.85
spansForFunction · 0.85
linesForFunction · 0.85
isWholeLineUpdateFunction · 0.85
updateFunction · 0.85
signalLaterFunction · 0.85
removeMethod · 0.80
sliceMethod · 0.80

Tested by

no test coverage detected