all files / pdfviewer/annotation/ redaction-overlay-text.js

6.86% Statements 14/204
0% Branches 0/103
18.18% Functions 2/11
6.86% Lines 14/204
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 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 2701×   1× 1× 1×       1×                                                                                                                                                                                                                       1×                     1×                 1×                                               1×               1×                                     1×                                                                                                                                         1×                 1×   1×    
define(["require", "exports"], function (require, exports) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    var RedactionOverlayText = (function () {
        function RedactionOverlayText(pdfViewer, pdfViewerBase) {
            this.pdfViewer = pdfViewer;
            this.pdfViewerBase = pdfViewerBase;
        }
        RedactionOverlayText.prototype.renderRedactionOverlayText = function (annotation, currentAnnot) {
            if (annotation && annotation.id !== 'diagram_helper' && annotation.overlayText !== '') {
                if (!annotation || !annotation.wrapper) {
                    return;
                }
                var text = annotation.overlayText || 'REDACTED';
                var textAlign = annotation.textAlign || 'center';
                var isRepeat = annotation.isRepeat || false;
                var fontFamily = annotation.fontFamily || 'Helvetica';
                var fontSize = annotation.fontSize || 12;
                var fontColor = annotation.fontColor || '#rgba(255, 0, 0, 1)';
                this.removeRedactionOverlayText(annotation);
                var textLayerId = this.pdfViewer.element.id + '_textLayer_' + annotation.pageIndex;
                var textLayer = document.getElementById(textLayerId);
                if (!textLayer) {
                    return;
                }
                var bounds = annotation.bounds || { x: 0, y: 0, width: 0, height: 0 };
                var zoomFactor = this.pdfViewer.viewerBase.getZoomFactor();
                if (currentAnnot && currentAnnot.bounds.length) {
                    var combineBounds = this.pdfViewer.annotation.redactionAnnotationModule.combineBounds(currentAnnot);
                    for (var z = 0; z < combineBounds.length; z++) {
                        var overlayDiv = document.createElement('div');
                        overlayDiv.id = this.pdfViewer.element.id + '_redactionOverlay_' + (z + 1) + '_' + annotation.id;
                        overlayDiv.className = 'e-pv-redaction-overlay-text';
                        overlayDiv.style.position = 'absolute';
                        overlayDiv.style.left = (combineBounds[z].x || 0) + 'px';
                        overlayDiv.style.top = ((combineBounds[z].y || 0)) + 'px';
                        overlayDiv.style.width = (combineBounds[z].width || 0) + 'px';
                        overlayDiv.style.height = ((combineBounds[z].height || 0)) + 'px';
                        overlayDiv.style.boxSizing = 'border-box';
                        overlayDiv.style.overflow = 'hidden';
                        overlayDiv.style.pointerEvents = 'none';
                        overlayDiv.style.fontFamily = fontFamily;
                        overlayDiv.style.fontSize = (fontSize * zoomFactor) + 'px';
                        overlayDiv.style.color = fontColor;
                        overlayDiv.style.textAlign = textAlign;
                        textLayer.appendChild(overlayDiv);
                        annotation.overlayElement = overlayDiv;
                        if (isRepeat) {
                            this.renderRepeatedTextHtml(overlayDiv, text, fontSize * zoomFactor, textAlign);
                        }
                        else {
                            var contentDiv = document.createElement('div');
                            contentDiv.textContent = text;
                            contentDiv.style.setProperty('overflow-wrap', 'break-word');
                            contentDiv.style.wordBreak = 'break-word';
                            contentDiv.style.width = '100%';
                            contentDiv.style.height = '100%';
                            contentDiv.style.display = 'flex';
                            contentDiv.style.flexDirection = 'column';
                            contentDiv.style.fontWeight = '550';
                            overlayDiv.appendChild(contentDiv);
                            if ((combineBounds[z].height || 0) > (combineBounds[z].width || 0) * 1.5) {
                                this.applyVerticalTextLayout(contentDiv, text, textAlign);
                            }
                            else {
                                contentDiv.style.justifyContent = 'flex-start';
                                contentDiv.style.alignItems = this.getAlignStyle(textAlign);
                                contentDiv.textContent = text;
                            }
                        }
                    }
                }
                else {
                    var overlayDiv = document.createElement('div');
                    overlayDiv.id = this.pdfViewer.element.id + '_redactionOverlay_' + annotation.id;
                    overlayDiv.className = 'e-pv-redaction-overlay-text';
                    overlayDiv.style.position = 'absolute';
                    overlayDiv.style.left = ((bounds.x || 0) * zoomFactor) + 'px';
                    overlayDiv.style.top = ((bounds.y || 0) * zoomFactor) + 'px';
                    overlayDiv.style.width = ((bounds.width || 0) * zoomFactor) + 'px';
                    overlayDiv.style.height = ((bounds.height || 0) * zoomFactor) + 'px';
                    overlayDiv.style.boxSizing = 'border-box';
                    overlayDiv.style.overflow = 'hidden';
                    overlayDiv.style.pointerEvents = 'none';
                    overlayDiv.style.fontFamily = fontFamily;
                    overlayDiv.style.fontSize = (fontSize * zoomFactor) + 'px';
                    overlayDiv.style.color = fontColor;
                    overlayDiv.style.textAlign = textAlign;
                    textLayer.appendChild(overlayDiv);
                    annotation.overlayElement = overlayDiv;
                    if (isRepeat) {
                        this.renderRepeatedTextHtml(overlayDiv, text, fontSize * zoomFactor, textAlign);
                    }
                    else {
                        var contentDiv = document.createElement('div');
                        contentDiv.textContent = text;
                        contentDiv.style.setProperty('overflow-wrap', 'break-word');
                        contentDiv.style.wordBreak = 'break-word';
                        contentDiv.style.width = '100%';
                        contentDiv.style.height = '100%';
                        contentDiv.style.display = 'flex';
                        contentDiv.style.flexDirection = 'column';
                        contentDiv.style.fontWeight = '550';
                        overlayDiv.appendChild(contentDiv);
                        if ((bounds.height || 0) > (bounds.width || 0) * 1.5) {
                            this.applyVerticalTextLayout(contentDiv, text, textAlign);
                        }
                        else {
                            contentDiv.style.justifyContent = 'flex-start';
                            contentDiv.style.alignItems = this.getAlignStyle(textAlign);
                            contentDiv.textContent = text;
                        }
                    }
                }
            }
        };
        RedactionOverlayText.prototype.setAttributeHtml = function (element, attributes) {
            var keys = Object.keys(attributes);
            for (var i = 0; i < keys.length; i++) {
                if (keys[parseInt(i.toString(), 10)] !== 'style') {
                    element.setAttribute(keys[parseInt(i.toString(), 10)], attributes[keys[parseInt(i.toString(), 10)]]);
                }
                else {
                    this.applyStyleAgainstCsp(element, attributes[keys[parseInt(i.toString(), 10)]]);
                }
            }
        };
        RedactionOverlayText.prototype.applyStyleAgainstCsp = function (svg, attributes) {
            var keys = attributes.split(';');
            for (var i = 0; i < keys.length; i++) {
                var attribute = keys[parseInt(i.toString(), 10)].split(':');
                if (attribute.length === 2) {
                    svg.style[attribute[0].trim()] = attribute[1].trim();
                }
            }
        };
        RedactionOverlayText.prototype.removeRedactionOverlayText = function (annotation) {
            if (!annotation) {
                return;
            }
            if (annotation.annotationAddMode === 'TextRedaction' || annotation.annotType === 'TextRedaction') {
                for (var z = 0; z < 5000; z++) {
                    var overlayId = this.pdfViewer.element.id + '_redactionOverlay_' + (z + 1) + '_' + annotation.id;
                    var overlayElement = document.getElementById(overlayId);
                    if (overlayElement && overlayElement.parentNode) {
                        overlayElement.parentNode.removeChild(overlayElement);
                    }
                    else {
                        break;
                    }
                }
            }
            else {
                var overlayId = this.pdfViewer.element.id + '_redactionOverlay_' + annotation.id;
                var overlayElement = document.getElementById(overlayId);
                if (overlayElement && overlayElement.parentNode) {
                    overlayElement.parentNode.removeChild(overlayElement);
                }
            }
        };
        RedactionOverlayText.prototype.getAlignStyle = function (textAlign) {
            switch (textAlign.toLowerCase()) {
                case 'left': return 'flex-start';
                case 'right': return 'flex-end';
                case 'center': return 'center';
                default: return 'flex-start';
            }
        };
        RedactionOverlayText.prototype.applyVerticalTextLayout = function (container, text, textAlign) {
            container.innerHTML = '';
            container.style.alignItems = this.getAlignStyle(textAlign);
            container.style.fontWeight = '550';
            var containerWidth = container.parentElement ? parseFloat(container.parentElement.style.width) : 0;
            var words = text.split(' ');
            if (containerWidth < 30) {
                for (var i = 0; i < text.length; i++) {
                    var charSpan = document.createElement('div');
                    charSpan.textContent = text[i];
                    charSpan.style.textAlign = textAlign;
                    container.appendChild(charSpan);
                }
            }
            else {
                container.textContent = text;
                container.style.wordBreak = 'break-all';
            }
        };
        RedactionOverlayText.prototype.renderRepeatedTextHtml = function (container, text, fontSize, textAlign) {
            container.innerHTML = '';
            var wrapperDiv = document.createElement('div');
            wrapperDiv.style.display = 'flex';
            wrapperDiv.style.flexDirection = 'column';
            wrapperDiv.style.width = '100%';
            wrapperDiv.style.height = '100%';
            wrapperDiv.style.justifyContent = 'space-between';
            var lineHeight = fontSize;
            var containerHeight = parseFloat(container.style.height);
            var containerWidth = parseFloat(container.style.width);
            var isVertical = containerHeight > containerWidth * 1.5;
            var availableHeight = containerHeight - 10;
            var lines = Math.max(1, Math.floor(availableHeight / lineHeight));
            var availableWidth = containerWidth - 10;
            var measureElement = document.createElement('span');
            measureElement.style.visibility = 'hidden';
            measureElement.style.position = 'absolute';
            measureElement.style.whiteSpace = 'nowrap';
            measureElement.style.fontFamily = container.style.fontFamily;
            measureElement.style.fontSize = fontSize + 'px';
            measureElement.textContent = text;
            document.body.appendChild(measureElement);
            var textWidth = measureElement.getBoundingClientRect().width;
            document.body.removeChild(measureElement);
            var repetitionsPerLine = Math.floor(availableWidth / textWidth);
            if (repetitionsPerLine === 0) {
                repetitionsPerLine = 1;
            }
            if (isVertical) {
                var verticalDiv = document.createElement('div');
                verticalDiv.style.width = '100%';
                verticalDiv.style.height = '100%';
                verticalDiv.style.display = 'flex';
                verticalDiv.style.flexDirection = 'column';
                verticalDiv.style.justifyContent = 'flex-start';
                verticalDiv.style.textAlign = textAlign;
                var verticalText = '';
                for (var i = 0; i < lines * 2; i++) {
                    verticalText += text + ' ';
                }
                verticalDiv.textContent = verticalText;
                wrapperDiv.appendChild(verticalDiv);
            }
            else {
                for (var i = 0; i < lines; i++) {
                    var lineDiv = document.createElement('div');
                    var requiredAttributes = {
                        style: "width:100%; text-align:" + textAlign + "; white-space:nowrap; line-height:" + lineHeight + "px; overflow:hidden;"
                    };
                    this.setAttributeHtml(lineDiv, requiredAttributes);
                    var totalTextWidth = repetitionsPerLine * textWidth;
                    var lineText = '';
                    if (textAlign.toLowerCase() === 'right') {
                    }
                    else if (textAlign.toLowerCase() === 'center') {
                    }
                    for (var j = 0; j < repetitionsPerLine; j++) {
                        lineText += text;
                    }
                    if (lineText.length > 0) {
                        lineText = lineText.substring(0, lineText.length - 1);
                    }
                    lineDiv.textContent = lineText;
                    wrapperDiv.appendChild(lineDiv);
                }
            }
            container.appendChild(wrapperDiv);
        };
        RedactionOverlayText.prototype.updateRedactionAfterEdit = function (annotation) {
            if (annotation && annotation.shapeAnnotationType === 'Redaction') {
                this.removeRedactionOverlayText(annotation);
                this.renderRedactionOverlayText(annotation);
                if (this.pdfViewer) {
                    this.pdfViewer.renderDrawing(null, annotation.pageIndex);
                }
            }
        };
        return RedactionOverlayText;
    }());
    exports.RedactionOverlayText = RedactionOverlayText;
});