UNPKG

4.13 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.TrimSpacesTransform = void 0;
4var nodes_1 = require("../nodes");
5/**
6 * Implementation of DocNodeTransforms.trimSpacesInParagraphNodes()
7 */
8var TrimSpacesTransform = /** @class */ (function () {
9 function TrimSpacesTransform() {
10 }
11 TrimSpacesTransform.transform = function (docParagraph) {
12 var transformedNodes = [];
13 // Whether the next nonempty node to be added needs a space before it
14 var pendingSpace = false;
15 // The DocPlainText node that we're currently accumulating
16 var accumulatedTextChunks = [];
17 var accumulatedNodes = [];
18 // We always trim leading whitespace for a paragraph. This flag gets set to true
19 // as soon as nonempty content is encountered.
20 var finishedSkippingLeadingSpaces = false;
21 for (var _i = 0, _a = docParagraph.nodes; _i < _a.length; _i++) {
22 var node = _a[_i];
23 switch (node.kind) {
24 case nodes_1.DocNodeKind.PlainText:
25 var docPlainText = node;
26 var text = docPlainText.text;
27 var startedWithSpace = /^\s/.test(text);
28 var endedWithSpace = /\s$/.test(text);
29 var collapsedText = text.replace(/\s+/g, ' ').trim();
30 if (startedWithSpace && finishedSkippingLeadingSpaces) {
31 pendingSpace = true;
32 }
33 if (collapsedText.length > 0) {
34 if (pendingSpace) {
35 accumulatedTextChunks.push(' ');
36 pendingSpace = false;
37 }
38 accumulatedTextChunks.push(collapsedText);
39 accumulatedNodes.push(node);
40 finishedSkippingLeadingSpaces = true;
41 }
42 if (endedWithSpace && finishedSkippingLeadingSpaces) {
43 pendingSpace = true;
44 }
45 break;
46 case nodes_1.DocNodeKind.SoftBreak:
47 if (finishedSkippingLeadingSpaces) {
48 pendingSpace = true;
49 }
50 accumulatedNodes.push(node);
51 break;
52 default:
53 if (pendingSpace) {
54 accumulatedTextChunks.push(' ');
55 pendingSpace = false;
56 }
57 // Push the accumulated text
58 if (accumulatedTextChunks.length > 0) {
59 // TODO: We should probably track the accumulatedNodes somehow, e.g. so we can map them back to the
60 // original excerpts. But we need a developer scenario before we can design this API.
61 transformedNodes.push(new nodes_1.DocPlainText({
62 configuration: docParagraph.configuration,
63 text: accumulatedTextChunks.join('')
64 }));
65 accumulatedTextChunks.length = 0;
66 accumulatedNodes.length = 0;
67 }
68 transformedNodes.push(node);
69 finishedSkippingLeadingSpaces = true;
70 }
71 }
72 // Push the accumulated text
73 if (accumulatedTextChunks.length > 0) {
74 transformedNodes.push(new nodes_1.DocPlainText({
75 configuration: docParagraph.configuration,
76 text: accumulatedTextChunks.join('')
77 }));
78 accumulatedTextChunks.length = 0;
79 accumulatedNodes.length = 0;
80 }
81 var transformedParagraph = new nodes_1.DocParagraph({
82 configuration: docParagraph.configuration
83 });
84 transformedParagraph.appendNodes(transformedNodes);
85 return transformedParagraph;
86 };
87 return TrimSpacesTransform;
88}());
89exports.TrimSpacesTransform = TrimSpacesTransform;
90//# sourceMappingURL=TrimSpacesTransform.js.map
\No newline at end of file