MCPcopy Create free account
hub / github.com/TruthHun/BookStack / updateDoc

Function updateDoc

static/mergely/lib/codemirror.js:7160–7208  ·  view source on GitHub ↗
(doc, change, markedSpans, estimateHeight)

Source from the content-addressed store, hash-verified

7158
7159 // Perform a change on the document data structure.
7160 function updateDoc(doc, change, markedSpans, estimateHeight) {
7161 function spansFor(n) {return markedSpans ? markedSpans[n] : null;}
7162 function update(line, text, spans) {
7163 updateLine(line, text, spans, estimateHeight);
7164 signalLater(line, "change", line, change);
7165 }
7166 function linesFor(start, end) {
7167 for (var i = start, result = []; i < end; ++i)
7168 result.push(new Line(text[i], spansFor(i), estimateHeight));
7169 return result;
7170 }
7171
7172 var from = change.from, to = change.to, text = change.text;
7173 var firstLine = getLine(doc, from.line), lastLine = getLine(doc, to.line);
7174 var lastText = lst(text), lastSpans = spansFor(text.length - 1), nlines = to.line - from.line;
7175
7176 // Adjust the line structure
7177 if (change.full) {
7178 doc.insert(0, linesFor(0, text.length));
7179 doc.remove(text.length, doc.size - text.length);
7180 } else if (isWholeLineUpdate(doc, change)) {
7181 // This is a whole-line replace. Treated specially to make
7182 // sure line objects move the way they are supposed to.
7183 var added = linesFor(0, text.length - 1);
7184 update(lastLine, lastLine.text, lastSpans);
7185 if (nlines) doc.remove(from.line, nlines);
7186 if (added.length) doc.insert(from.line, added);
7187 } else if (firstLine == lastLine) {
7188 if (text.length == 1) {
7189 update(firstLine, firstLine.text.slice(0, from.ch) + lastText + firstLine.text.slice(to.ch), lastSpans);
7190 } else {
7191 var added = linesFor(1, text.length - 1);
7192 added.push(new Line(lastText + firstLine.text.slice(to.ch), lastSpans, estimateHeight));
7193 update(firstLine, firstLine.text.slice(0, from.ch) + text[0], spansFor(0));
7194 doc.insert(from.line + 1, added);
7195 }
7196 } else if (text.length == 1) {
7197 update(firstLine, firstLine.text.slice(0, from.ch) + text[0] + lastLine.text.slice(to.ch), spansFor(0));
7198 doc.remove(from.line + 1, nlines);
7199 } else {
7200 update(firstLine, firstLine.text.slice(0, from.ch) + text[0], spansFor(0));
7201 update(lastLine, lastText + lastLine.text.slice(to.ch), lastSpans);
7202 var added = linesFor(1, text.length - 1);
7203 if (nlines > 1) doc.remove(from.line + 1, nlines - 1);
7204 doc.insert(from.line + 1, added);
7205 }
7206
7207 signalLater(doc, "change", doc, change);
7208 }
7209
7210 // The document is represented as a BTree consisting of leaves, with
7211 // chunk of lines in them, and branches, with up to ten leaves or

Callers 3

makeChangeSingleDocFunction · 0.70
codemirror.jsFile · 0.70

Calls 7

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

Tested by

no test coverage detected