rl-react-intellix-pdf
Version:
Intelllex React PDF
190 lines (145 loc) • 5.88 kB
JavaScript
function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
function _inheritsLoose(subClass, superClass) { subClass.prototype = Object.create(superClass.prototype); subClass.prototype.constructor = subClass; subClass.__proto__ = superClass; }
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
import React from "react";
import { PDFJS } from "pdfjs-dist/web/pdf_viewer";
import "./index.scss";
if (PDFJS.workerSrc != "undefined") {
window.PDFJS.workerSrc = "https://cdnjs.cloudflare.com/ajax/libs/pdf.js/2.0.338/pdf.worker.js";
} // The workerSrc property shall be specified.
//window.PDFJS.workerSrc = '../../../node_modules/pdfjs-dist/build/pdf.worker.js';
// if (!PDFJS.workerSrc && typeof document !== "undefined") {
// PDFJS.workerSrc = (function () {
// "use strict";
// let scriptTagContainer =
// document.body || document.getElementsByTagName("head")[0];
// let pdfjsSrc = scriptTagContainer.lastChild.src;
// return pdfjsSrc && pdfjsSrc.replace(/\.js$/i, ".worker.js");
// })();
// PDFJS.workerSrc = "/static/js/pdf.worker.js";
// console.log("PDFJS.workerSrc", PDFJS.workerSrc);
// }
var SCROLL_TOP_PADDING = 200;
export var PdfPages = /*#__PURE__*/function (_React$Component) {
_inheritsLoose(PdfPages, _React$Component);
function PdfPages(_props) {
var _this;
_this = _React$Component.call(this, _props) || this;
_defineProperty(_assertThisInitialized(_this), "setupViewer", function () {
PDFJS.disableTextLayer = false;
PDFJS.externalLinkTarget = PDFJS.LinkTarget.PARENT; // PDF Link Service
_this._pdfLinkService = new PDFJS.PDFLinkService({
eventBus: _this._eventBus
}); // PDF Viewer
_this._pdfViewer = new PDFJS.PDFViewer({
container: document.getElementById("pdf-pages"),
removePageBorders: true,
linkService: _this._pdfLinkService
});
_this._pdfLinkService.setViewer(_this._pdfViewer); // PDF Find Controller
_this._pdfFindController = new PDFJS.PDFFindController({
pdfViewer: _this._pdfViewer
});
_this._pdfViewer.setFindController(_this._pdfFindController); // Set external Refs
_this.props.setPdfViewer(_this._pdfViewer);
_this.props.setFindController(_this._pdfFindController);
});
_defineProperty(_assertThisInitialized(_this), "onScrollCheck", function () {
if (_this._pdfViewer) {
var _this$docViewer = _this.docViewer,
scrollTop = _this$docViewer.scrollTop,
scrollHeight = _this$docViewer.scrollHeight;
var currentPage = Math.ceil((scrollTop + SCROLL_TOP_PADDING) / (scrollHeight / _this._pdf.numPages));
_this.props.setCurrentPage(currentPage);
}
});
_defineProperty(_assertThisInitialized(_this), "stream", function (props) {
var url = props.url;
if (url) {
var xhr = new XMLHttpRequest();
xhr.open("GET", url);
xhr.responseType = "arraybuffer";
xhr.onprogress = function (e) {
if (e.lengthComputable) {
var percent = Math.round(e.loaded / e.total * 100);
_this.props.updateProgressBar(percent);
}
};
xhr.onload = function () {
if (xhr.status === 200) {
var _ab = xhr.response;
_this.pdf = _ab;
_this.loadPDF(_ab);
} else {
console.error("Error while requesting", url);
}
};
xhr.onerror = function () {
console.error("Error while requesting", url);
};
xhr.send();
}
});
_defineProperty(_assertThisInitialized(_this), "loadPDF", function () {
var src = _this.pdf;
if (!src) {
return;
}
PDFJS.getDocument(src).then(function (pdf) {
console.log("pdf", pdf);
_this._pdf = pdf;
_this.props.setPdf(_this._pdf);
_this.update();
}, function (err) {
console.log("errr", err);
var error = err.name || err.toString(); // this.pdfLoadError.emit(error);
});
});
_defineProperty(_assertThisInitialized(_this), "zoom", function (scale) {
_this._zoom = scale;
_this._pdfViewer.currentScale = _this._zoom;
});
_this.state = {
pdf: ""
};
return _this;
}
var _proto = PdfPages.prototype;
_proto.componentDidMount = function componentDidMount() {
this.setupViewer();
this.stream(this.props);
this.docViewer = document.getElementById("viewer-container");
this.docViewer.addEventListener("scroll", this.onScrollCheck);
};
_proto.componentWillReceiveProps = function componentWillReceiveProps(newProps) {
if (newProps.scale && newProps.scale !== this.props.scale) {
this.zoom(newProps.scale);
}
if (newProps.url !== this.props.url) {
this.setupViewer();
this.stream(newProps);
}
};
_proto.componentWillUnmount = function componentWillUnmount() {
if (this.state.pdf != null) {
var pdf = this.state.pdf;
pdf && pdf.destroy();
this.documentPromise && this.documentPromise.cancel();
this.docViewer.removeEventListener("scroll", this.onScrollCheck);
}
};
_proto.update = function update() {
if (this._pdfViewer) {
this._pdfViewer.setDocument(this._pdf);
}
if (this._pdfLinkService) {
this._pdfLinkService.setDocument(this._pdf, null);
}
this.render();
};
_proto.render = function render() {
return /*#__PURE__*/React.createElement("div", null);
};
return PdfPages;
}(React.Component);
export default PdfPages;