all files / document-editor/implementation/writer/ text-export.js

99.12% Statements 113/114
86.84% Branches 33/38
100% Functions 20/20
99.12% Lines 113/114
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162   146× 146× 146×   14780×           47× 47×   47× 47× 47× 47× 47× 55× 55× 55× 55× 55×   47× 55× 55×     288× 288× 288× 543× 543× 499× 499×   44× 11×     33×       499× 756× 756× 90×   666× 452×     499× 499× 452×     33× 89× 89× 212× 212×       55× 55×     55× 55× 55× 55× 55× 55×   330× 10×     55×   507×     505×     452×     451×     47× 47× 47× 47×   47× 47× 47× 181× 158×     47× 47× 47×       202× 202× 202× 202× 202× 202×        
define(["require", "exports", "@syncfusion/ej2-file-utils", "@syncfusion/ej2-base"], function (require, exports, ej2_file_utils_1, ej2_base_1) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    var TextExport = (function () {
        function TextExport() {
            this.pageContent = '';
            this.curSectionIndex = 0;
            this.inField = false;
        }
        TextExport.prototype.getModuleName = function () {
            return 'TextExport';
        };
        TextExport.prototype.save = function (documentHelper, fileName) {
            this.serialize(documentHelper);
            var writer = new ej2_file_utils_1.StreamWriter();
            this.writeInternal(writer);
            writer.save(fileName + '.txt');
        };
        TextExport.prototype.saveAsBlob = function (documentHelper) {
            this.serialize(documentHelper);
            var streamWriter = new ej2_file_utils_1.StreamWriter();
            this.writeInternal(streamWriter);
            var blob = streamWriter.buffer;
            streamWriter.destroy();
            return new Promise(function (resolve, reject) {
                resolve(blob);
            });
        };
        TextExport.prototype.serialize = function (documentHelper) {
            var document = documentHelper.owner.sfdtExportModule.write(0);
            this.setDocument(document);
        };
        TextExport.prototype.setDocument = function (document) {
            this.document = document;
            this.mSections = document.sections;
        };
        TextExport.prototype.writeInternal = function (streamWriter) {
            var section = undefined;
            var sectionCount = this.document.sections.length - 1;
            var isLastSection = false;
            this.updateLastParagraph();
            for (var i = 0; i <= sectionCount; i++) {
                section = this.document.sections[i];
                isLastSection = (i === sectionCount) ? true : false;
                this.writeBody(streamWriter, section.blocks, false);
                this.writeNewLine(streamWriter);
                this.writeSectionEnd(section, isLastSection);
            }
            for (var j = 0; j <= sectionCount; j++) {
                section = this.document.sections[j];
                this.writeHeadersFooters(streamWriter, section);
            }
        };
        TextExport.prototype.writeBody = function (streamWriter, body, isHeaderFooter) {
            var bodyItemsCount = body.length - 1;
            var bodyItem = undefined;
            for (var i = 0; i <= bodyItemsCount; i++) {
                bodyItem = body[i];
                if (bodyItem.hasOwnProperty('inlines')) {
                    var isLastPara = (bodyItem === this.lastPara) ? true : false;
                    this.writeParagraph(streamWriter, bodyItem, isLastPara, isHeaderFooter);
                }
                else if (bodyItem.blocks) {
                    this.writeBody(streamWriter, bodyItem.blocks, isHeaderFooter);
                }
                else {
                    this.writeTable(streamWriter, bodyItem);
                }
            }
        };
        TextExport.prototype.writeParagraph = function (streamWriter, paragraph, isLastPara, isHeaderFooter) {
            for (var i = 0; i < paragraph.inlines.length; i++) {
                var item = paragraph.inlines[i];
                if (item.hasOwnProperty('fieldType')) {
                    this.inField = item.fieldType === 0;
                }
                else if (item.hasOwnProperty('text') && !this.inField) {
                    this.writeText(streamWriter, item.text);
                }
            }
            Eif (!isHeaderFooter || paragraph.inlines.length > 0)
                if (!isLastPara) {
                    this.writeNewLine(streamWriter);
                }
        };
        TextExport.prototype.writeTable = function (streamWriter, table) {
            for (var i = 0; i < table.rows.length; i++) {
                var row = table.rows[i];
                for (var j = 0; j < row.cells.length; j++) {
                    var cell = row.cells[j];
                    this.writeBody(streamWriter, cell.blocks, false);
                }
            }
        };
        TextExport.prototype.writeHeadersFooters = function (streamWriter, section) {
            var headersFooters = section.headersFooters;
            Iif (ej2_base_1.isNullOrUndefined(headersFooters)) {
                return;
            }
            this.writeHeaderFooter(streamWriter, section.headersFooters.header);
            this.writeHeaderFooter(streamWriter, section.headersFooters.footer);
            this.writeHeaderFooter(streamWriter, section.headersFooters.evenFooter);
            this.writeHeaderFooter(streamWriter, section.headersFooters.evenHeader);
            this.writeHeaderFooter(streamWriter, section.headersFooters.firstPageHeader);
            this.writeHeaderFooter(streamWriter, section.headersFooters.firstPageFooter);
        };
        TextExport.prototype.writeHeaderFooter = function (streamWriter, headerFooter) {
            if (headerFooter && headerFooter.blocks) {
                this.writeBody(streamWriter, headerFooter.blocks, true);
            }
        };
        TextExport.prototype.writeSectionEnd = function (section, lastSection) {
            this.curSectionIndex++;
        };
        TextExport.prototype.writeNewLine = function (writer) {
            if (!ej2_base_1.isNullOrUndefined(writer)) {
                writer.writeLine('');
            }
            else {
                this.pageContent = this.pageContent + ' ';
            }
        };
        TextExport.prototype.writeText = function (writer, text) {
            if (!ej2_base_1.isNullOrUndefined(writer)) {
                writer.write(text);
            }
            else {
                this.pageContent += text;
            }
        };
        TextExport.prototype.updateLastParagraph = function () {
            var cnt = this.document.sections.length;
            var sec;
            Eif (cnt > 0) {
                sec = this.document.sections[cnt - 1];
            }
            Eif (!ej2_base_1.isNullOrUndefined(sec)) {
                var paragraphs = [];
                for (var i = 0; i < sec.blocks.length; i++) {
                    if (sec.blocks[i].hasOwnProperty('inlines')) {
                        paragraphs.push(sec.blocks[i]);
                    }
                }
                var pCount = paragraphs.length;
                Eif (pCount > 0) {
                    this.lastPara = paragraphs[pCount - 1];
                }
            }
        };
        TextExport.prototype.destroy = function () {
            this.document = undefined;
            this.lastPara = undefined;
            this.mSections = undefined;
            this.sections = [];
            this.sections = undefined;
            this.pageContent = undefined;
        };
        return TextExport;
    }());
    exports.TextExport = TextExport;
});