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

Function updateDoc

static/editor.md/lib/codemirror/lib/codemirror.js:6945–6993  ·  view source on GitHub ↗
(doc, change, markedSpans, estimateHeight)

Source from the content-addressed store, hash-verified

6943
6944 // Perform a change on the document data structure.
6945 function updateDoc(doc, change, markedSpans, estimateHeight) {
6946 function spansFor(n) {return markedSpans ? markedSpans[n] : null;}
6947 function update(line, text, spans) {
6948 updateLine(line, text, spans, estimateHeight);
6949 signalLater(line, "change", line, change);
6950 }
6951 function linesFor(start, end) {
6952 for (var i = start, result = []; i < end; ++i)
6953 result.push(new Line(text[i], spansFor(i), estimateHeight));
6954 return result;
6955 }
6956
6957 var from = change.from, to = change.to, text = change.text;
6958 var firstLine = getLine(doc, from.line), lastLine = getLine(doc, to.line);
6959 var lastText = lst(text), lastSpans = spansFor(text.length - 1), nlines = to.line - from.line;
6960
6961 // Adjust the line structure
6962 if (change.full) {
6963 doc.insert(0, linesFor(0, text.length));
6964 doc.remove(text.length, doc.size - text.length);
6965 } else if (isWholeLineUpdate(doc, change)) {
6966 // This is a whole-line replace. Treated specially to make
6967 // sure line objects move the way they are supposed to.
6968 var added = linesFor(0, text.length - 1);
6969 update(lastLine, lastLine.text, lastSpans);
6970 if (nlines) doc.remove(from.line, nlines);
6971 if (added.length) doc.insert(from.line, added);
6972 } else if (firstLine == lastLine) {
6973 if (text.length == 1) {
6974 update(firstLine, firstLine.text.slice(0, from.ch) + lastText + firstLine.text.slice(to.ch), lastSpans);
6975 } else {
6976 var added = linesFor(1, text.length - 1);
6977 added.push(new Line(lastText + firstLine.text.slice(to.ch), lastSpans, estimateHeight));
6978 update(firstLine, firstLine.text.slice(0, from.ch) + text[0], spansFor(0));
6979 doc.insert(from.line + 1, added);
6980 }
6981 } else if (text.length == 1) {
6982 update(firstLine, firstLine.text.slice(0, from.ch) + text[0] + lastLine.text.slice(to.ch), spansFor(0));
6983 doc.remove(from.line + 1, nlines);
6984 } else {
6985 update(firstLine, firstLine.text.slice(0, from.ch) + text[0], spansFor(0));
6986 update(lastLine, lastText + lastLine.text.slice(to.ch), lastSpans);
6987 var added = linesFor(1, text.length - 1);
6988 if (nlines > 1) doc.remove(from.line + 1, nlines - 1);
6989 doc.insert(from.line + 1, added);
6990 }
6991
6992 signalLater(doc, "change", doc, change);
6993 }
6994
6995 // The document is represented as a BTree consisting of leaves, with
6996 // 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