@syncfusion/ej2-pdfviewer
Version:
Essential JS 2 PDF viewer Component
935 lines • 119 kB
JavaScript
import { createElement, Browser, isNullOrUndefined } from '@syncfusion/ej2-base';
/**
* The `TextSelection` module is used to handle the text selection of PDF viewer.
*
* @param {Event} event - event
* @returns {void}
*/
var TextSelection = /** @class */ (function () {
/**
* @param {PdfViewer} pdfViewer - It describes about the pdfviewer
* @param {PdfViewerBase} pdfViewerBase - It describes about the pdfviewer base
* @private
* @returns {void}
*/
function TextSelection(pdfViewer, pdfViewerBase) {
var _this = this;
/**
* @private
*/
this.isTextSelection = false;
/**
* @private
*/
this.selectionStartPage = null;
this.isBackwardPropagatedSelection = false;
this.contextMenuHeight = 144;
this.backwardStart = 0;
/**
* @private
*/
this.selectionRangeArray = [];
this.selectionAnchorTouch = null;
this.selectionFocusTouch = null;
this.scrollMoveTimer = 0;
this.isMouseLeaveSelection = false;
/**
* @private
*/
this.isTouchSelection = false;
this.previousScrollDifference = 0;
this.topStoreLeft = null;
this.topStoreRight = null;
this.isTextSearched = false;
this.isSelectionStartTriggered = false;
this.allTextContent = '';
this.onLeftTouchSelectElementTouchStart = function (event) {
_this.initiateSelectionByTouch();
};
this.onRightTouchSelectElementTouchStart = function (event) {
_this.initiateSelectionByTouch();
};
this.onLeftTouchSelectElementTouchEnd = function (event) {
_this.terminateSelectionByTouch(event);
};
this.onRightTouchSelectElementTouchEnd = function (event) {
_this.terminateSelectionByTouch(event);
};
this.onLeftTouchSelectElementTouchMove = function (event) {
var range;
var nodeElement;
var zoomFactorabovehundard = 15;
var zoomFactorAboveSeventy = 10;
var zoomFactoraboveFifty = 8;
var zoomFactorbelowFifty = 4;
event.preventDefault();
event.target.style.zIndex = '0';
var rightElement = _this.dropDivElementRight;
var isTouchedWithinViewerContainer = _this.isTouchedWithinContainer(event);
if (rightElement && isTouchedWithinViewerContainer) {
var dropBounds = rightElement.getBoundingClientRect();
var xTouch = event.changedTouches[0].clientX;
var yTouch = event.changedTouches[0].clientY;
event.target.style.zIndex = '1000';
nodeElement = _this.getNodeElement(range, xTouch, yTouch, event, nodeElement);
if (nodeElement) {
var currentDifference = Math.sqrt((yTouch - dropBounds.top) * (yTouch - dropBounds.top) +
(xTouch - dropBounds.left) * (xTouch - dropBounds.left));
var isCloserMovement = _this.isCloserTouchScroll(currentDifference);
var isTextSelected = false;
var zoomFactor = _this.pdfViewerBase.getZoomFactor();
var topDifference = Math.abs(yTouch - dropBounds.top);
var textHeight = zoomFactor > 1 ? zoomFactorabovehundard : zoomFactor > 0.7 ?
zoomFactorAboveSeventy : zoomFactor > 0.5 ? zoomFactoraboveFifty : zoomFactorbelowFifty;
if (parseInt(yTouch.toString(), 10) <= parseInt(dropBounds.top.toString(), 10) &&
(parseInt(topDifference.toString(), 10) >= textHeight) ||
parseInt(xTouch.toString(), 10) <= parseInt(dropBounds.left.toString(), 10) &&
(parseInt(topDifference.toString(), 10) <= textHeight)) {
_this.dropElementLeft.style.transform = 'rotate(0deg)';
_this.dropElementRight.style.transform = 'rotate(-90deg)';
isTextSelected = _this.selectTextByTouch(nodeElement.parentElement, xTouch, yTouch, false, 'left', isCloserMovement);
}
else {
_this.dropElementLeft.style.transform = 'rotate(-90deg)';
_this.dropElementRight.style.transform = 'rotate(0deg)';
isTextSelected = _this.selectTextByTouch(nodeElement.parentElement, xTouch, yTouch, true, 'left', isCloserMovement);
}
if (isTextSelected) {
var elementClientRect = _this.dropDivElementLeft.getBoundingClientRect();
var pageTopValue = _this.pdfViewerBase.pageSize[_this.pdfViewerBase.currentPageNumber - 1].top;
var topClientValue = _this.getClientValueTop(yTouch, _this.pdfViewerBase.currentPageNumber - 1);
var currentPageLeft = _this.pdfViewerBase.getElement('_pageDiv_' + (_this.pdfViewerBase.currentPageNumber - 1)).getBoundingClientRect().left;
var currentRangeLeft = xTouch - currentPageLeft;
_this.dropDivElementLeft.style.top = pageTopValue * _this.pdfViewerBase.getZoomFactor() + topClientValue + 'px';
_this.topStoreLeft = { pageTop: pageTopValue, topClientValue: _this.getMagnifiedValue(topClientValue),
pageNumber: _this.pdfViewerBase.currentPageNumber - 1, left: _this.getMagnifiedValue(currentRangeLeft),
isHeightNeeded: false };
_this.dropDivElementLeft.style.left = xTouch - _this.pdfViewerBase.viewerContainer.getBoundingClientRect().left - (elementClientRect.width / 2) + _this.pdfViewerBase.viewerContainer.scrollLeft + 'px';
_this.previousScrollDifference = currentDifference;
}
}
}
};
this.onRightTouchSelectElementTouchMove = function (event) {
var range;
var nodeElement;
var zoomFactorabovehundard = 25;
var zoomFactorAboveSeventy = 15;
var zoomFactoraboveFifty = 8;
var zoomFactorbelowFifty = 7;
event.preventDefault();
event.target.style.zIndex = '0';
var leftElement = _this.dropDivElementLeft;
var isTouchedWithinViewerContainer = _this.isTouchedWithinContainer(event);
if (leftElement && isTouchedWithinViewerContainer) {
var dropPosition = leftElement.getBoundingClientRect();
var touchX = event.changedTouches[0].clientX;
var touchY = event.changedTouches[0].clientY;
event.target.style.zIndex = '1000';
nodeElement = _this.getNodeElement(range, touchX, touchY, event, nodeElement);
if (nodeElement) {
var currentDifference = Math.sqrt((touchY - dropPosition.top) * (touchY - dropPosition.top) +
(touchX - dropPosition.left) * (touchX - dropPosition.left));
var isCloserMovement = _this.isCloserTouchScroll(currentDifference);
var isTextSelected = false;
var zoomFactor = _this.pdfViewerBase.getZoomFactor();
var topDifference = Math.abs(touchY - dropPosition.top);
var textHeight = zoomFactor > 1 ? (zoomFactor * zoomFactorabovehundard) :
(zoomFactor > 0.7 ? zoomFactorAboveSeventy : (zoomFactor > 0.5 ? zoomFactoraboveFifty : zoomFactorbelowFifty));
if ((parseInt(touchY.toString(), 10) >= parseInt(dropPosition.top.toString(), 10) &&
parseInt(topDifference.toString(), 10) >= textHeight) || (parseInt(topDifference.toString(), 10) <= textHeight &&
parseInt(touchX.toString(), 10) >= parseInt(dropPosition.left.toString(), 10))) {
_this.dropElementRight.style.transform = 'rotate(-90deg)';
_this.dropElementLeft.style.transform = 'rotate(0deg)';
isTextSelected = _this.selectTextByTouch(nodeElement.parentElement, touchX, touchY, true, 'right', isCloserMovement);
}
else {
_this.dropElementRight.style.transform = 'rotate(0deg)';
_this.dropElementLeft.style.transform = 'rotate(-90deg)';
isTextSelected = _this.selectTextByTouch(nodeElement.parentElement, touchX, touchY, false, 'right', isCloserMovement);
}
if (isTextSelected) {
var pageTopValue = _this.pdfViewerBase.pageSize[_this.pdfViewerBase.currentPageNumber - 1].top;
var topClientValue = _this.getClientValueTop(touchY, _this.pdfViewerBase.currentPageNumber - 1);
var elementClientRect = _this.dropDivElementRight.getBoundingClientRect();
_this.dropDivElementRight.style.top = pageTopValue * _this.pdfViewerBase.getZoomFactor() + topClientValue + 'px';
var currentPageLeft = _this.pdfViewerBase.getElement('_pageDiv_' + (_this.pdfViewerBase.currentPageNumber - 1)).getBoundingClientRect().left;
var currentRangeLeft = touchX - currentPageLeft;
_this.topStoreRight = { pageTop: pageTopValue, topClientValue: _this.getMagnifiedValue(topClientValue),
pageNumber: _this.pdfViewerBase.currentPageNumber - 1, left: _this.getMagnifiedValue(currentRangeLeft),
isHeightNeeded: false };
_this.dropDivElementRight.style.left = touchX - _this.pdfViewerBase.viewerContainer.getBoundingClientRect().left - (elementClientRect.width / 2) + _this.pdfViewerBase.viewerContainer.scrollLeft + 'px';
_this.previousScrollDifference = currentDifference;
}
}
}
};
this.pdfViewer = pdfViewer;
this.pdfViewerBase = pdfViewerBase;
}
/**
* @param {EventTarget} target - It describes about the target
* @param {number} x - It describes about the X value
* @param {number} y - It describes about the Y value
* @param {boolean} isExtended - It describes about the isExtended boolean value
* @private
* @returns {void}
*/
TextSelection.prototype.textSelectionOnMouseMove = function (target, x, y, isExtended) {
var targetElement = target;
this.isTextSearched = true;
if (targetElement.nodeType === targetElement.TEXT_NODE) {
if (!this.isSelectionStartTriggered && !this.pdfViewerBase.getTextMarkupAnnotationMode()) {
this.pdfViewer.fireTextSelectionStart(this.pdfViewerBase.currentPageNumber);
this.isSelectionStartTriggered = true;
}
this.isBackwardPropagatedSelection = false;
var range = targetElement.ownerDocument.createRange();
var selection = window.getSelection();
if (selection.anchorNode !== null) {
var position = selection.anchorNode.compareDocumentPosition(selection.focusNode);
if (!position && selection.anchorOffset > selection.focusOffset || position === Node.DOCUMENT_POSITION_PRECEDING) {
this.isBackwardPropagatedSelection = true;
}
}
range.selectNodeContents(targetElement);
var currentPosition = 0;
var endPosition = range.endOffset;
while (currentPosition < endPosition) {
range.setStart(targetElement, currentPosition);
range.setEnd(targetElement, currentPosition + 1);
var rangeBounds = range.getBoundingClientRect();
var rightBounds = rangeBounds.right;
if (isExtended) {
rightBounds = parseInt(rangeBounds.right.toString(), 10);
}
if (rangeBounds.left <= x && rightBounds >= x && parseInt(rangeBounds.top.toString(), 10) <= y && rangeBounds.bottom >= y) {
if (selection.anchorNode !== null && selection.anchorNode.parentNode.classList.contains('e-pv-text')) {
if (selection.anchorOffset > currentPosition) {
if (this.backwardStart !== 0) {
range.setStart(selection.anchorNode, this.backwardStart);
}
else {
range.setStart(selection.anchorNode, selection.anchorOffset + 1);
}
}
else {
range.setStart(selection.anchorNode, selection.anchorOffset);
}
}
selection.removeAllRanges();
selection.addRange(range);
if (!this.isTextSelection) {
this.selectionStartPage = this.pdfViewerBase.currentPageNumber - 1;
}
this.isTextSelection = true;
var isIE = !!document.documentMode;
if (!isIE) {
if (this.isBackwardPropagatedSelection || range.endOffset > currentPosition) {
if (this.backwardStart !== range.startOffset && range.startOffset >= currentPosition) {
this.backwardStart = range.endOffset;
}
if (currentPosition === 0 && range.endOffset !== 1) {
selection.extend(targetElement, currentPosition);
}
else {
selection.extend(targetElement, currentPosition + 1);
}
}
else if (isExtended) {
selection.extend(targetElement, currentPosition);
}
else {
selection.extend(targetElement, currentPosition + 1);
}
}
range.detach();
}
currentPosition += 1;
}
var annotationModule = this.pdfViewer.annotationModule;
if (annotationModule && annotationModule.textMarkupAnnotationModule &&
annotationModule.textMarkupAnnotationModule.
isEnableTextMarkupResizer(annotationModule.textMarkupAnnotationModule.currentTextMarkupAddMode)) {
var leftDivElement = document.getElementById(this.pdfViewer.element.id + '_droplet_left');
if (this.pdfViewerBase.isSelection && selection && selection.rangeCount > 0) {
var currentrange = selection.getRangeAt(0);
var rect = currentrange.getBoundingClientRect();
var left = rect.left;
var top_1 = rect.top;
this.pdfViewer.annotation.textMarkupAnnotationModule.updateLeftposition(left, top_1);
this.pdfViewerBase.isSelection = false;
}
else if ((leftDivElement && leftDivElement.style.display === 'none')) {
this.pdfViewer.annotation.textMarkupAnnotationModule.updateLeftposition(x, y);
}
this.pdfViewer.annotation.textMarkupAnnotationModule.updatePosition(x, y);
}
}
else {
for (var i = 0; i < targetElement.childNodes.length; i++) {
if (targetElement.childNodes[parseInt(i.toString(), 10)].nodeType === targetElement.TEXT_NODE) {
var range = this.getSelectionRange(i, targetElement);
var rangeBounds = range.getBoundingClientRect();
if (rangeBounds.left <= x && rangeBounds.right >= parseInt(x.toString(), 10) &&
parseInt(rangeBounds.top.toString(), 10) <= y && rangeBounds.bottom >= y) {
range.detach();
this.textSelectionOnMouseMove(targetElement.childNodes[parseInt(i.toString(), 10)], x, y, isExtended);
}
else {
range.detach();
}
}
}
}
};
/**
* @param {EventTarget} target - It describes about the target
* @param {number} x - It describes about the X value
* @param {number} y - It describes about the Y value
* @param {boolean} isforward - It describes about the isforward boolean value
* @private
* @returns {boolean} - boolean
*/
TextSelection.prototype.textSelectionOnDrag = function (target, x, y, isforward) {
var targetElement = target;
this.isTextSearched = true;
if (targetElement.nodeType === targetElement.TEXT_NODE) {
this.isBackwardPropagatedSelection = false;
var range = targetElement.ownerDocument.createRange();
var selection = window.getSelection();
if (selection.anchorNode !== null) {
var position = selection.anchorNode.compareDocumentPosition(selection.focusNode);
if (!position && selection.anchorOffset > selection.focusOffset || position === Node.DOCUMENT_POSITION_PRECEDING) {
this.isBackwardPropagatedSelection = true;
}
}
range.selectNodeContents(targetElement);
var currentPosition = 0;
var endPosition = range.endOffset;
while (currentPosition < endPosition) {
range.setStart(targetElement, currentPosition);
range.setEnd(targetElement, currentPosition + 1);
var rangeBounds = range.getBoundingClientRect();
if (rangeBounds.left <= x && rangeBounds.right >= x && parseInt(rangeBounds.top.toString(), 10) <= y &&
rangeBounds.bottom >= y) {
if (isforward) {
if (selection.anchorNode !== null && selection.anchorNode.parentNode.classList.contains('e-pv-text')) {
range.setStart(selection.anchorNode, selection.anchorOffset);
}
selection.removeAllRanges();
selection.addRange(range);
selection.extend(targetElement, currentPosition);
}
else if (selection.focusNode) {
range.setEnd(selection.focusNode, selection.focusOffset);
selection.removeAllRanges();
selection.addRange(range);
}
if (!this.isTextSelection) {
this.selectionStartPage = this.pdfViewerBase.currentPageNumber - 1;
}
this.isTextSelection = true;
range.detach();
return true;
}
currentPosition += 1;
}
if (this.pdfViewerBase.isSelection) {
var currentrange = selection.getRangeAt(0);
var rect = currentrange.getBoundingClientRect();
var left = rect.left;
var top_2 = rect.top;
this.pdfViewer.annotation.textMarkupAnnotationModule.updateLeftposition(left, top_2);
this.pdfViewerBase.isSelection = false;
}
this.pdfViewer.annotation.textMarkupAnnotationModule.updatePosition(x, y);
}
else {
for (var i = 0; i < targetElement.childNodes.length; i++) {
if (targetElement.childNodes[parseInt(i.toString(), 10)].nodeType === targetElement.TEXT_NODE) {
var range = this.getSelectionRange(i, targetElement);
var rangeBounds = range.getBoundingClientRect();
if (rangeBounds.left <= x && rangeBounds.right >= x && parseInt(rangeBounds.top.toString(), 10) <= y &&
rangeBounds.bottom >= y) {
range.detach();
this.textSelectionOnDrag(targetElement.childNodes[parseInt(i.toString(), 10)], x, y, isforward);
}
else {
range.detach();
}
}
}
}
return null;
};
/**
* Select the target text region in the PDF document of the given bounds.
*
* @param {number} pageNumbers - Specifies the page number
* @param {IRectangle[]} bounds - Specifies the bounds of the texts.
* @returns {void}
*/
TextSelection.prototype.selectTextRegion = function (pageNumbers, bounds) {
var element = null;
var pageNumber = (pageNumbers - 1);
for (var k = 0; k < bounds.length; k++) {
var bound = bounds[parseInt(k.toString(), 10)];
var x = (bound.left ? bound.left : bound.Left) * this.pdfViewerBase.getZoomFactor();
var y = (bound.top ? bound.top : bound.Top) * this.pdfViewerBase.getZoomFactor();
var width = (bound.width ? bound.width : bound.Width) * this.pdfViewerBase.getZoomFactor();
var textLayer = this.pdfViewerBase.getElement('_textLayer_' + pageNumber);
if (textLayer) {
var textDivs = textLayer.childNodes;
for (var n = 0; n < textDivs.length; n++) {
if (textDivs[parseInt(n.toString(), 10)]) {
var rangebounds = textDivs[parseInt(n.toString(), 10)].getBoundingClientRect();
var top_3 = this.getClientValueTop(rangebounds.top, pageNumber);
var currentLeft = rangebounds.left - this.pdfViewerBase.getElement('_pageDiv_' + pageNumber).getBoundingClientRect().left;
var totalLeft = currentLeft + rangebounds.width;
var textDiVLeft = parseInt(textDivs[parseInt(n.toString(), 10)].style.left, 10);
var currentTop = parseInt(textDivs[parseInt(n.toString(), 10)].style.top, 10);
var isLeftBounds = this.checkLeftBounds(currentLeft, textDiVLeft, totalLeft, x);
var isTopBounds = this.checkTopBounds(top_3, currentTop, y);
if (isLeftBounds && isTopBounds) {
element = textDivs[parseInt(n.toString(), 10)];
break;
}
}
}
if (element != null) {
var boundingRect = this.pdfViewerBase.getElement('_textLayer_' + pageNumber).getBoundingClientRect();
this.textSelectionOnMouseMove(element, x + boundingRect.left, y + boundingRect.top, false);
if ((bounds.length - 1) === k) {
this.textSelectionOnMouseMove(element, x + boundingRect.left + width, y + boundingRect.top, false);
}
}
}
}
};
/**
* @param {number} left - It describes about the left value
* @param {number} textDiVLeft - It describes about the text div left value
* @param {number} totalLeft - It describes about the total left value
* @param {number} x - It describes about the x value
* @private
* @returns {boolean} - boolean
*/
TextSelection.prototype.checkLeftBounds = function (left, textDiVLeft, totalLeft, x) {
var isExists = false;
if (left === parseInt(x.toString(), 10) || parseInt(left.toString(), 10) === parseInt(x.toString(), 10) ||
(left + 1) === parseInt(x.toString(), 10) || (left - 1) === parseInt(x.toString(), 10)
|| textDiVLeft === parseInt(x.toString(), 10) || textDiVLeft === x || (totalLeft >= x && left <= x)) {
isExists = true;
}
return isExists;
};
/**
* @param {number} top - It describes about the top value
* @param {number} currentTop - It describes about the current top value
* @param {number} y - It describes about the Y value
* @private
* @returns {boolean} - boolean
*/
TextSelection.prototype.checkTopBounds = function (top, currentTop, y) {
var isExists = false;
if ((top === parseInt(y.toString(), 10) || parseInt(top.toString(), 10) === parseInt(y.toString(), 10) ||
parseInt((top + 1).toString(), 10) === parseInt(y.toString(), 10) ||
parseInt((top - 1).toString(), 10) === parseInt(y.toString(), 10)
|| currentTop === parseInt(y.toString(), 10) || currentTop === y)) {
isExists = true;
}
return isExists;
};
/**
* @param {MouseEvent} event - It describes about the event
* @private
* @returns {void}
*/
TextSelection.prototype.textSelectionOnMouseLeave = function (event) {
var _this = this;
event.preventDefault();
var viewerTop = this.pdfViewerBase.viewerContainer.offsetTop;
if (this.pdfViewer.magnificationModule) {
if (this.pdfViewer.magnificationModule.fitType === 'fitToPage') {
return;
}
}
if (event.clientY > viewerTop) {
this.scrollMoveTimer = setInterval(function () {
_this.scrollForwardOnSelection();
}, 500);
}
else {
this.scrollMoveTimer = setInterval(function () {
_this.scrollBackwardOnSelection();
}, 500);
}
};
TextSelection.prototype.scrollForwardOnSelection = function () {
if (!this.pdfViewerBase.isSignInitialClick) {
this.isMouseLeaveSelection = true;
this.pdfViewerBase.viewerContainer.scrollTop = this.pdfViewerBase.viewerContainer.scrollTop + 200;
this.stichSelectionOnScroll(this.pdfViewerBase.currentPageNumber - 1);
}
};
TextSelection.prototype.scrollBackwardOnSelection = function () {
this.isMouseLeaveSelection = true;
this.pdfViewerBase.viewerContainer.scrollTop = this.pdfViewerBase.viewerContainer.scrollTop - 200;
this.stichSelectionOnScroll(this.pdfViewerBase.currentPageNumber - 1);
};
/**
* @private
* @returns {void}
*/
TextSelection.prototype.clear = function () {
if (this.scrollMoveTimer) {
this.isMouseLeaveSelection = false;
clearInterval(this.scrollMoveTimer);
}
};
/**
* @param {any} element - It describes about the element
* @param {number} x - It describes about the X value
* @param {number} y - It describes about the Y value
* @param {boolean} isStoreSelection - It describes about the isStoreSelection value
* @private
* @returns {void}
*/
TextSelection.prototype.selectAWord = function (element, x, y, isStoreSelection) {
var padding = 0;
if (Browser.isDevice && !this.pdfViewer.enableDesktopMode) {
padding = 3;
}
if (element.nodeType === element.TEXT_NODE) {
var selection = window.getSelection();
var range = element.ownerDocument.createRange();
range.selectNodeContents(element);
var currentPosition = 0;
var endPosition = range.endOffset;
while (currentPosition < endPosition) {
range.setStart(element, currentPosition);
range.setEnd(element, currentPosition + 1);
var rangeBounds = range.getBoundingClientRect();
if (rangeBounds.left <= x + padding && rangeBounds.right >= x - padding &&
rangeBounds.top <= y + padding && rangeBounds.bottom >= y - padding) {
var textContent = element.textContent;
var indices = [];
var startPosition = void 0;
var endPos = void 0;
for (var i = 0; i < textContent.length; i++) {
if (textContent[parseInt(i.toString(), 10)] === ' ') {
indices.push(i);
}
}
for (var j = 0; j < indices.length; j++) {
if (currentPosition === indices[parseInt(j.toString(), 10)]) {
startPosition = indices[parseInt(j.toString(), 10)];
endPos = indices[parseInt(j.toString(), 10)];
}
if (indices[0] > currentPosition) {
startPosition = 0;
endPos = indices[parseInt(j.toString(), 10)];
break;
}
if (currentPosition > indices[parseInt(j.toString(), 10)] && currentPosition < indices[j + 1]) {
startPosition = indices[parseInt(j.toString(), 10)];
endPos = indices[j + 1];
}
else if (currentPosition > indices[parseInt(j.toString(), 10)]) {
if (!indices[j + 1]) {
startPosition = indices[parseInt(j.toString(), 10)];
}
}
}
if (!endPos) {
endPos = textContent.length;
}
if (startPosition === 0) {
range.setStart(element, startPosition);
}
else {
range.setStart(element, startPosition + 1);
}
range.setEnd(element, endPos);
selection.removeAllRanges();
selection.addRange(range);
this.isTextSelection = true;
var startParent = isNullOrUndefined(range.startContainer.parentElement) ?
range.startContainer.parentNode : range.startContainer.parentElement;
this.selectionStartPage = parseInt(startParent.id.split('_text_')[1], 10);
if (isStoreSelection) {
this.selectionAnchorTouch = { anchorNode: selection.anchorNode.parentElement.id,
anchorOffset: selection.anchorOffset };
this.selectionFocusTouch = { focusNode: selection.focusNode.parentElement.id, focusOffset: selection.focusOffset };
}
if (!Browser.isIE) {
range.detach();
}
break;
}
currentPosition += 1;
}
}
else {
for (var i = 0; i < element.childNodes.length; i++) {
var range = this.getSelectionRange(i, element);
var rangeBounds = range.getBoundingClientRect();
if (rangeBounds.left <= x + padding && rangeBounds.right >= x - padding &&
rangeBounds.top <= y + padding && rangeBounds.bottom >= y - padding) {
range.detach();
this.selectAWord(element.childNodes[parseInt(i.toString(), 10)], x, y, isStoreSelection);
}
else {
range.detach();
}
}
}
};
TextSelection.prototype.getSelectionRange = function (index, element) {
var range = element.childNodes[parseInt(index.toString(), 10)].ownerDocument.createRange();
range.selectNodeContents(element.childNodes[parseInt(index.toString(), 10)]);
return range;
};
/**
* @param {MouseEvent} event - It describes about the event
* @private
* @returns {void}
*/
TextSelection.prototype.selectEntireLine = function (event) {
var textIds = [];
var targetElement = event.target;
var targetRect = targetElement.getBoundingClientRect();
var targetcentre = parseInt((targetRect.top + (targetRect.height / 2)).toString(), 10);
var pageNumber = parseInt(event.target.id.split('_text_')[1], 10);
var textDivs = document.querySelectorAll('div[id*="' + this.pdfViewer.element.id + '_text_' + pageNumber + '"]');
if (targetElement.classList.contains('e-pv-text')) {
this.pdfViewer.fireTextSelectionStart(pageNumber + 1);
for (var i = 0; i < textDivs.length; i++) {
var rect = textDivs[parseInt(i.toString(), 10)].getBoundingClientRect();
var topValue = parseInt(rect.top.toString(), 10);
var bottomValue = parseInt(rect.bottom.toString(), 10);
if ((topValue <= targetcentre && bottomValue > targetcentre) && (targetRect.bottom + 10 > bottomValue)) {
var textId = textDivs[parseInt(i.toString(), 10)].id;
if (textId !== '') {
textIds.push(textId);
}
}
}
var selection = window.getSelection();
selection.removeAllRanges();
var range = document.createRange();
var lengths = (textIds.length - 1);
var d1 = document.getElementById(textIds[0]);
var d2 = document.getElementById(textIds[parseInt(lengths.toString(), 10)]);
var childNodes = d2.childNodes.length;
if (childNodes > 0) {
range.setStart(d1.childNodes[0], 0);
range.setEnd(d2.childNodes[0], d2.textContent.length);
}
else {
range.setStart(d1.childNodes[0], 0);
range.setEnd(d2, 1);
}
this.selectionStartPage = parseInt(range.startContainer.parentElement.id.split('_text_')[1], 10);
selection.addRange(range);
this.isTextSelection = true;
if (selection != null && this.pdfViewer.contextMenuSettings.contextMenuAction === 'MouseUp') {
this.calculateContextMenuPosition(event.clientY, event.clientY);
}
}
};
/**
* @private
* @returns {void}
*/
TextSelection.prototype.enableTextSelectionMode = function () {
this.pdfViewerBase.isTextSelectionDisabled = false;
if (!isNullOrUndefined(this.pdfViewerBase.viewerContainer)) {
this.pdfViewerBase.viewerContainer.classList.remove('e-disable-text-selection');
this.pdfViewerBase.viewerContainer.classList.add('e-enable-text-selection');
this.pdfViewerBase.viewerContainer.addEventListener('selectstart', function (e) {
e.preventDefault();
return true;
});
}
};
TextSelection.prototype.clearTextSelection = function () {
if (this.isTextSelection) {
this.pdfViewerBase.textLayer.clearDivSelection();
if (window.getSelection) {
if (window.getSelection().removeAllRanges) {
window.getSelection().removeAllRanges();
}
}
if (this.pdfViewer.linkAnnotationModule) {
var lowerPageIndex = this.pdfViewerBase.currentPageNumber - 3;
lowerPageIndex = (lowerPageIndex < 0) ? 0 : lowerPageIndex;
var higherPageIndex = this.pdfViewer.currentPageNumber + 1;
higherPageIndex = (higherPageIndex < (this.pdfViewerBase.pageCount - 1)) ? higherPageIndex :
(this.pdfViewerBase.pageCount - 1);
for (var i = lowerPageIndex; i <= higherPageIndex; i++) {
this.pdfViewer.linkAnnotationModule.modifyZindexForTextSelection(i, false);
}
}
if (this.pdfViewer.annotation && this.pdfViewer.annotation.textMarkupAnnotationModule) {
this.pdfViewer.annotation.textMarkupAnnotationModule.showHideDropletDiv(true);
}
this.selectionRangeArray = [];
this.isTextSelection = false;
this.isTouchSelection = false;
if (this.pdfViewer.textSearchModule) {
this.pdfViewer.textSearchModule.searchAfterSelection();
}
this.pdfViewerBase.contextMenuModule.close();
this.removeTouchElements();
}
};
/**
* @private
* @returns {void}
*/
TextSelection.prototype.removeTouchElements = function () {
if (this.dropDivElementLeft) {
this.dropDivElementLeft.parentElement.removeChild(this.dropDivElementLeft);
this.dropDivElementLeft = null;
this.dropElementLeft.style.transform = 'rotate(0deg)';
}
if (this.dropDivElementRight) {
this.dropDivElementRight.parentElement.removeChild(this.dropDivElementRight);
this.dropDivElementRight = null;
this.dropElementRight.style.transform = 'rotate(-90deg)';
}
};
/**
* @private
* @returns {void}
*/
TextSelection.prototype.resizeTouchElements = function () {
var viewerContainerLeft = this.pdfViewerBase.viewerContainer.getBoundingClientRect().left;
if (this.dropDivElementLeft) {
var elementClientRect = this.dropDivElementLeft.getBoundingClientRect();
var dropElementHeight = 0;
var leftCurrentPagePosition = this.pdfViewerBase.getElement('_pageDiv_' + this.topStoreLeft.pageNumber).getBoundingClientRect();
this.dropDivElementLeft.style.left = parseFloat(this.topStoreLeft.left.toString()) * this.pdfViewerBase.getZoomFactor() + leftCurrentPagePosition.left - viewerContainerLeft - (elementClientRect.width / 2) + 'px';
if (this.topStoreLeft.isHeightNeeded) {
dropElementHeight = (elementClientRect.height / 2) * this.pdfViewerBase.getZoomFactor();
}
this.dropDivElementLeft.style.top = parseFloat(this.topStoreLeft.pageTop.toString()) * this.pdfViewerBase.getZoomFactor() + parseFloat(this.topStoreLeft.topClientValue.toString()) * this.pdfViewerBase.getZoomFactor() + dropElementHeight + 'px';
}
if (this.dropDivElementRight) {
var elementClientRect = this.dropDivElementRight.getBoundingClientRect();
var dropElementHeight = 0;
var rightCurrentPagePosition = this.pdfViewerBase.getElement('_pageDiv_' + this.topStoreRight.pageNumber).getBoundingClientRect();
this.dropDivElementRight.style.left = parseFloat(this.topStoreRight.left.toString()) * this.pdfViewerBase.getZoomFactor() + rightCurrentPagePosition.left - viewerContainerLeft - (elementClientRect.width / 2) + 'px';
if (this.topStoreRight.isHeightNeeded) {
dropElementHeight = (elementClientRect.height / 2) * this.pdfViewerBase.getZoomFactor();
}
this.dropDivElementRight.style.top = parseFloat(this.topStoreRight.pageTop.toString()) * this.pdfViewerBase.getZoomFactor() + parseFloat(this.topStoreRight.topClientValue.toString()) * this.pdfViewerBase.getZoomFactor() + dropElementHeight + 'px';
}
};
/**
* @param {MouseEvent} event - It describes about the event
* @private
* @returns {void}
*/
TextSelection.prototype.textSelectionOnMouseup = function (event) {
this.clear();
if (window.getSelection().anchorNode !== null) {
this.isMouseLeaveSelection = false;
this.isSelectionStartTriggered = false;
this.maintainSelectionOnZoom(true, false);
this.fireTextSelectEnd();
var isTextSearch = this.pdfViewerBase.textLayer.getTextSearchStatus();
if (isTextSearch) {
this.pdfViewerBase.textLayer.clearDivSelection();
var indexes = this.pdfViewer.textSearchModule.getIndexes();
var lowerPageValue = parseFloat(indexes.lowerPageValue.toString());
var higherPageValue = parseFloat(indexes.higherPageValue.toString());
for (var i = lowerPageValue; i < higherPageValue; i++) {
this.applySelectionRangeOnScroll(i);
}
this.pdfViewer.textSearchModule.searchAfterSelection();
}
else {
this.applySpanForSelection();
}
if (this.pdfViewer.linkAnnotationModule) {
this.pdfViewer.linkAnnotationModule.modifyZindexForTextSelection(this.pdfViewerBase.currentPageNumber - 1, false);
}
if (this.isTextSearched && this.pdfViewer.contextMenuSettings.contextMenuAction === 'MouseUp') {
this.calculateContextMenuPosition(event.clientY, event.clientX);
this.isTextSearched = false;
}
}
else {
this.pdfViewerBase.textLayer.clearDivSelection();
if (this.pdfViewer.textSearchModule) {
this.pdfViewer.textSearchModule.searchAfterSelection();
}
this.pdfViewerBase.contextMenuModule.close();
this.removeTouchElements();
}
};
/**
* @private
* @returns {void}
*/
TextSelection.prototype.fireTextSelectEnd = function () {
if (this.selectionRangeArray.length !== 0) {
var selectEndPageIndex = 0;
var selectedText = '';
var selectedBounds = [];
for (var k = 0; k < this.selectionRangeArray.length; k++) {
selectedText += this.selectionRangeArray[parseInt(k.toString(), 10)].textContent;
for (var j = 0; j < this.selectionRangeArray[parseInt(k.toString(), 10)].rectangleBounds.length; j++) {
var currentBound = this.selectionRangeArray[parseInt(k.toString(), 10)].rectangleBounds[parseInt(j.toString(), 10)];
selectedBounds.push({ left: currentBound.left, right: currentBound.right, top: currentBound.top,
bottom: currentBound.bottom, width: currentBound.width, height: currentBound.height,
pageIndex: this.selectionRangeArray[parseInt(k.toString(), 10)].pageNumber + 1 });
}
if (this.selectionRangeArray[parseInt(k.toString(), 10)].isBackward && k === 0) {
selectEndPageIndex = this.selectionRangeArray[parseInt(k.toString(), 10)].pageNumber + 1;
}
else if (!this.selectionRangeArray[parseInt(k.toString(), 10)].isBackward && k === this.selectionRangeArray.length - 1) {
selectEndPageIndex = this.selectionRangeArray[parseInt(k.toString(), 10)].pageNumber + 1;
}
}
this.pdfViewer.fireTextSelectionEnd(selectEndPageIndex, selectedText, selectedBounds);
}
};
/**
* @param {boolean} isMaintainSelection - It describes about the isMaintainSelection value
* @param {boolean} isStich - It describes about the isStich value
* @private
* @returns {void}
*/
TextSelection.prototype.maintainSelectionOnZoom = function (isMaintainSelection, isStich) {
var selection = window.getSelection();
if (selection.type === 'Range' || (!selection.type && !selection.isCollapsed)) {
var isBackward = this.pdfViewerBase.textLayer.isBackWardSelection(selection);
if (selection.anchorNode != null) {
var anchorPageId = parseInt(this.getNodeElementFromNode(selection.anchorNode).id.split('_text_')[1], 10);
var focusPageId = parseInt(this.getNodeElementFromNode(selection.focusNode).id.split('_text_')[1], 10);
if (this.isTouchSelection && isNaN(focusPageId)) {
var focusElement = selection.focusNode;
if (focusElement === this.pdfViewerBase.pageContainer) {
var lastChildNode = this.pdfViewerBase.pageContainer.lastChild;
if (lastChildNode.classList.contains('e-pv-touch-select-drop')) {
focusPageId = parseInt(lastChildNode.previousSibling.previousSibling.id.split('_pageDiv_')[1], 10);
}
else if (lastChildNode.classList.contains('e-pv-page-div')) {
focusPageId = parseInt(lastChildNode.id.split('_pageDiv_')[1], 10);
}
}
}
if (!isBackward) {
for (var i = anchorPageId; i <= focusPageId; i++) {
this.maintainSelectionOnScroll(i, isStich);
}
}
else {
for (var i = anchorPageId; i >= focusPageId; i--) {
this.maintainSelectionOnScroll(i, isStich);
}
}
}
if (!isMaintainSelection) {
selection.removeAllRanges();
}
}
};
/**
* @param {number} pageNumber - It describes about the page number value
* @private
* @returns {boolean} - boolean
*/
TextSelection.prototype.isSelectionAvailableOnScroll = function (pageNumber) {
var isSelectionAvailable = false;
var ranges = this.selectionRangeArray;
for (var i = 0; i < ranges.length; i++) {
if (ranges[parseInt(i.toString(), 10)] !== null) {
if (pageNumber === ranges[parseInt(i.toString(), 10)].pageNumber) {
isSelectionAvailable = true;
if (this.isTouchSelection && !this.pdfViewerBase.getMagnified()) {
isSelectionAvailable = false;
}
break;
}
}
}
return isSelectionAvailable;
};
/**
* @param {number} pageNumber - It describes about the page number
* @private
* @returns {void}
*/
TextSelection.prototype.applySelectionRangeOnScroll = function (pageNumber) {
if (this.isMouseLeaveSelection) {
this.applySelectionMouseScroll(pageNumber);
}
else {
this.applySelectionRange(pageNumber);
}
};
TextSelection.prototype.getSelectionRangeFromArray = function (pageNumber) {
var isSelectionAvailable = false;
var selectionRange = null;
var ranges = this.selectionRangeArray;
for (var i = 0; i < ranges.length; i++) {
if (ranges[parseInt(i.toString(), 10)] !== null) {
if (pageNumber === ranges[parseInt(i.toString(), 10)].pageNumber) {
selectionRange = ranges[parseInt(i.toString(), 10)];
isSelectionAvailable = true;
break;
}
}
}
return { isSelectionAvailable: isSelectionAvailable, selectionRange: selectionRange };
};
TextSelection.prototype.applySelectionRange = function (pageNumber) {
var selectionObject = this.getSelectionRangeFromArray(pageNumber);
var isSelectionAvailable = selectionObject.isSelectionAvailable;
var textLayer = document.getElementById(this.pdfViewer.element.id + '_textLayer_' + pageNumber);
if (textLayer) {
if (isSelectionAvailable && textLayer.childNodes.length !== 0) {
var selectionRange = selectionObject.selectionRange;
var anchorOffsetDiv = void 0;
var focusOffsetDiv = void 0;
var anchorOffset = void 0;
var focusOffset = void 0;
if (selectionRange.isBackward) {
var startId = parseInt(selectionRange.endNode.split('_text_')[1].split('_')[1], 10);
var endId = parseInt(selectionRange.startNode.split('_text_')[1].split('_')[1], 10);
if (startId < endId) {
anchorOffsetDiv = startId;
anchorOffset = selectionRange.endOffset;
focusOffset = selectionRange.startOffset;
focusOffsetDiv = endId;
}
else {
anchorOffsetDiv = endId;
anchorOffset = selectionRange.endOffset;
focusOffsetDiv = startId;
focusOffset = selectionRange.startOffset;
}
}
else {
anchorOffsetDiv = parseInt(selectionRange.startNode.split('text_')[1].split('_')[1], 10);
focusOffsetDiv = parseInt(selectionRange.endNode.split('text_')[1].split('_')[1], 10);
anchorOffset = selectionRange.startOffset;
focusOffset = selectionRange.endOffset;
}
window.getSelection().removeAllRanges();
this.pdfViewerBase.textLayer.applySpanForSelection(pageNumber, pageNumber, anchorOffsetDiv, focusOffsetDiv, anchorOffset, focusOffset);
if (this.pdfViewer.textSearchModule) {
this.pdfViewer.textSearchModule.searchAfterSelection();
}
}
}
};
TextSelection.prototype.applySelectionMouseScroll = function (pageNumber) {
var selectionObject = this.getSelectionRangeFromArray(pageNumber);
var isSelectionAvailable = selectionObject.isSelectionAvailable;
if (i