MCPcopy Create free account
hub / github.com/TextGeneratorio/text-generator.io / updateDoc

Function updateDoc

static/libs/codemirror.js:4650–4699  ·  view source on GitHub ↗
(doc, change, markedSpans, estimateHeight$$1)

Source from the content-addressed store, hash-verified

4648
4649// Perform a change on the document data structure.
4650function updateDoc(doc, change, markedSpans, estimateHeight$$1) {
4651 function spansFor(n) {return markedSpans ? markedSpans[n] : null}
4652 function update(line, text, spans) {
4653 updateLine(line, text, spans, estimateHeight$$1);
4654 signalLater(line, "change", line, change);
4655 }
4656 function linesFor(start, end) {
4657 var result = [];
4658 for (var i = start; i < end; ++i)
4659 { result.push(new Line(text[i], spansFor(i), estimateHeight$$1)); }
4660 return result
4661 }
4662
4663 var from = change.from, to = change.to, text = change.text;
4664 var firstLine = getLine(doc, from.line), lastLine = getLine(doc, to.line);
4665 var lastText = lst(text), lastSpans = spansFor(text.length - 1), nlines = to.line - from.line;
4666
4667 // Adjust the line structure
4668 if (change.full) {
4669 doc.insert(0, linesFor(0, text.length));
4670 doc.remove(text.length, doc.size - text.length);
4671 } else if (isWholeLineUpdate(doc, change)) {
4672 // This is a whole-line replace. Treated specially to make
4673 // sure line objects move the way they are supposed to.
4674 var added = linesFor(0, text.length - 1);
4675 update(lastLine, lastLine.text, lastSpans);
4676 if (nlines) { doc.remove(from.line, nlines); }
4677 if (added.length) { doc.insert(from.line, added); }
4678 } else if (firstLine == lastLine) {
4679 if (text.length == 1) {
4680 update(firstLine, firstLine.text.slice(0, from.ch) + lastText + firstLine.text.slice(to.ch), lastSpans);
4681 } else {
4682 var added$1 = linesFor(1, text.length - 1);
4683 added$1.push(new Line(lastText + firstLine.text.slice(to.ch), lastSpans, estimateHeight$$1));
4684 update(firstLine, firstLine.text.slice(0, from.ch) + text[0], spansFor(0));
4685 doc.insert(from.line + 1, added$1);
4686 }
4687 } else if (text.length == 1) {
4688 update(firstLine, firstLine.text.slice(0, from.ch) + text[0] + lastLine.text.slice(to.ch), spansFor(0));
4689 doc.remove(from.line + 1, nlines);
4690 } else {
4691 update(firstLine, firstLine.text.slice(0, from.ch) + text[0], spansFor(0));
4692 update(lastLine, lastText + lastLine.text.slice(to.ch), lastSpans);
4693 var added$2 = linesFor(1, text.length - 1);
4694 if (nlines > 1) { doc.remove(from.line + 1, nlines - 1); }
4695 doc.insert(from.line + 1, added$2);
4696 }
4697
4698 signalLater(doc, "change", doc, change);
4699}
4700
4701// Call f for all linked documents.
4702function linkedDocs(doc, f, sharedHistOnly) {

Callers 3

makeChangeSingleDocFunction · 0.85
DocFunction · 0.85

Calls 7

getLineFunction · 0.85
lstFunction · 0.85
spansForFunction · 0.85
linesForFunction · 0.85
isWholeLineUpdateFunction · 0.85
updateFunction · 0.85
signalLaterFunction · 0.85

Tested by

no test coverage detected