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

Function wrapRange

static/editor.md/lib/codemirror/addon/wrap/hardwrap.js:42–93  ·  view source on GitHub ↗
(cm, from, to, options)

Source from the content-addressed store, hash-verified

40 }
41
42 function wrapRange(cm, from, to, options) {
43 from = cm.clipPos(from); to = cm.clipPos(to);
44 var column = options.column || 80;
45 var wrapOn = options.wrapOn || /\s\S|-[^\.\d]/;
46 var killTrailing = options.killTrailingSpace !== false;
47 var changes = [], curLine = "", curNo = from.line;
48 var lines = cm.getRange(from, to, false);
49 if (!lines.length) return null;
50 var leadingSpace = lines[0].match(/^[ \t]*/)[0];
51
52 for (var i = 0; i < lines.length; ++i) {
53 var text = lines[i], oldLen = curLine.length, spaceInserted = 0;
54 if (curLine && text && !wrapOn.test(curLine.charAt(curLine.length - 1) + text.charAt(0))) {
55 curLine += " ";
56 spaceInserted = 1;
57 }
58 var spaceTrimmed = "";
59 if (i) {
60 spaceTrimmed = text.match(/^\s*/)[0];
61 text = text.slice(spaceTrimmed.length);
62 }
63 curLine += text;
64 if (i) {
65 var firstBreak = curLine.length > column && leadingSpace == spaceTrimmed &&
66 findBreakPoint(curLine, column, wrapOn, killTrailing);
67 // If this isn't broken, or is broken at a different point, remove old break
68 if (!firstBreak || firstBreak.from != oldLen || firstBreak.to != oldLen + spaceInserted) {
69 changes.push({text: [spaceInserted ? " " : ""],
70 from: Pos(curNo, oldLen),
71 to: Pos(curNo + 1, spaceTrimmed.length)});
72 } else {
73 curLine = leadingSpace + text;
74 ++curNo;
75 }
76 }
77 while (curLine.length > column) {
78 var bp = findBreakPoint(curLine, column, wrapOn, killTrailing);
79 changes.push({text: ["", leadingSpace],
80 from: Pos(curNo, bp.from),
81 to: Pos(curNo, bp.to)});
82 curLine = leadingSpace + curLine.slice(bp.to);
83 ++curNo;
84 }
85 }
86 if (changes.length) cm.operation(function() {
87 for (var i = 0; i < changes.length; ++i) {
88 var change = changes[i];
89 cm.replaceRange(change.text, change.from, change.to);
90 }
91 });
92 return changes.length ? {from: changes[0].from, to: CodeMirror.changeEnd(changes[changes.length - 1])} : null;
93 }
94
95 CodeMirror.defineExtension("wrapParagraph", function(pos, options) {
96 options = options || {};

Callers 1

hardwrap.jsFile · 0.85

Calls 1

findBreakPointFunction · 0.85

Tested by

no test coverage detected