ng-cw-v12
Version:
Angular UI Component Library
129 lines (120 loc) • 7.7 kB
JavaScript
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('@angular/core'), require('@angular/common'), require('rxjs'), require('@angular/platform-browser')) :
typeof define === 'function' && define.amd ? define('ng-cw-v12/pdf-viewer', ['exports', '@angular/core', '@angular/common', 'rxjs', '@angular/platform-browser'], factory) :
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory((global["ng-cw-v12"] = global["ng-cw-v12"] || {}, global["ng-cw-v12"]["pdf-viewer"] = {}), global.ng.core, global.ng.common, global.rxjs, global.ng.platformBrowser));
})(this, (function (exports, i0, i2, rxjs, i1) { 'use strict';
function _interopNamespace(e) {
if (e && e.__esModule) return e;
var n = Object.create(null);
if (e) {
Object.keys(e).forEach(function (k) {
if (k !== 'default') {
var d = Object.getOwnPropertyDescriptor(e, k);
Object.defineProperty(n, k, d.get ? d : {
enumerable: true,
get: function () { return e[k]; }
});
}
});
}
n["default"] = e;
return Object.freeze(n);
}
var i0__namespace = /*#__PURE__*/_interopNamespace(i0);
var i2__namespace = /*#__PURE__*/_interopNamespace(i2);
var i1__namespace = /*#__PURE__*/_interopNamespace(i1);
var PdfViewerComponent = /** @class */ (function () {
function PdfViewerComponent(sanitizer) {
var _this = this;
this.sanitizer = sanitizer;
/** pdf文件地址 */
this.ncUrl = '';
this.useIframe = true;
this.messageListener = null;
this.errorSubject = new rxjs.Subject();
this.messageListener = function (event) {
// 检查消息类型
if (event.data && event.data.type === 'iframe-error') {
var errorData = event.data;
_this.errorSubject.next(errorData);
if (errorData.message.includes('Promise.withResolvers is not a function') ||
errorData.message.includes("Unexpected token '{'") ||
errorData.message.includes("Unexpected token '('")) {
console.log(errorData.message);
//只在第一次错误时切换到 object 模式
if (_this.useIframe) {
_this.switchToObjectMode();
}
}
}
};
window.addEventListener('message', this.messageListener);
}
PdfViewerComponent.prototype.ngOnChanges = function (changes) {
if (changes['ncUrl']) {
this.updateViewerUrls();
}
};
PdfViewerComponent.prototype.ngAfterViewInit = function () { };
PdfViewerComponent.prototype.ngOnDestroy = function () {
if (this.messageListener) {
window.removeEventListener('message', this.messageListener);
}
};
PdfViewerComponent.prototype.updateViewerUrls = function () {
// 组织 pdfjs viewer 方式url
var iframeUrl = "assets/pdfjs-dist-build/web/viewer.html?file=" + encodeURIComponent(this.ncUrl);
this.viewerUrl = this.sanitizer.bypassSecurityTrustResourceUrl(iframeUrl);
// 组织 object 方式url
this.objectUrl = this.sanitizer.bypassSecurityTrustResourceUrl(this.ncUrl);
};
PdfViewerComponent.prototype.switchToObjectMode = function () {
this.useIframe = false;
console.log("%c \u5DF2\u5207\u6362\u5230 object \u6A21\u5F0F\u52A0\u8F7D PDF ", "background:#67C23A;border:1px solid #67C23A; padding: 1px; border-radius: 2px; color: #fff;");
};
return PdfViewerComponent;
}());
PdfViewerComponent.ɵfac = i0__namespace.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "12.1.5", ngImport: i0__namespace, type: PdfViewerComponent, deps: [{ token: i1__namespace.DomSanitizer }], target: i0__namespace.ɵɵFactoryTarget.Component });
PdfViewerComponent.ɵcmp = i0__namespace.ɵɵngDeclareComponent({ minVersion: "12.0.0", version: "12.1.5", type: PdfViewerComponent, selector: "nc-pdf-viewer", inputs: { ncUrl: "ncUrl" }, usesOnChanges: true, ngImport: i0__namespace, template: "<!-- \u4F18\u5148\u4F7F\u7528 iframe \u52A0\u8F7D pdfjs viewer -->\n<iframe *ngIf=\"useIframe\" #pdfIframe [src]=\"viewerUrl\" width=\"100%\" height=\"100%\" [attr.frameBorder]=\"0\"\n style=\"border: none;\">\n</iframe>\n\n<!-- \u5F53 iframe \u5931\u8D25\u65F6\uFF0C\u4F7F\u7528 object \u76F4\u63A5\u52A0\u8F7D PDF -->\n<object *ngIf=\"!useIframe\" [data]=\"objectUrl\" width=\"100%\" height=\"100%\" type=\"application/pdf\">\n <p>\u60A8\u7684\u6D4F\u89C8\u5668\u4E0D\u652F\u6301\u663E\u793APDF\u6587\u4EF6\u3002<a [href]=\"objectUrl\" target=\"_blank\">\u70B9\u51FB\u8FD9\u91CC\u4E0B\u8F7DPDF</a></p>\n</object>", styles: [""], directives: [{ type: i2__namespace.NgIf, selector: "[ngIf]", inputs: ["ngIf", "ngIfThen", "ngIfElse"] }] });
i0__namespace.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "12.1.5", ngImport: i0__namespace, type: PdfViewerComponent, decorators: [{
type: i0.Component,
args: [{
selector: 'nc-pdf-viewer',
templateUrl: './pdf-viewer.component.html',
styleUrls: ['./pdf-viewer.component.less']
}]
}], ctorParameters: function () { return [{ type: i1__namespace.DomSanitizer }]; }, propDecorators: { ncUrl: [{
type: i0.Input
}] } });
var NcPdfViewerModule = /** @class */ (function () {
function NcPdfViewerModule() {
}
return NcPdfViewerModule;
}());
NcPdfViewerModule.ɵfac = i0__namespace.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "12.1.5", ngImport: i0__namespace, type: NcPdfViewerModule, deps: [], target: i0__namespace.ɵɵFactoryTarget.NgModule });
NcPdfViewerModule.ɵmod = i0__namespace.ɵɵngDeclareNgModule({ minVersion: "12.0.0", version: "12.1.5", ngImport: i0__namespace, type: NcPdfViewerModule, declarations: [PdfViewerComponent], imports: [i2.CommonModule], exports: [PdfViewerComponent] });
NcPdfViewerModule.ɵinj = i0__namespace.ɵɵngDeclareInjector({ minVersion: "12.0.0", version: "12.1.5", ngImport: i0__namespace, type: NcPdfViewerModule, imports: [[
i2.CommonModule
]] });
i0__namespace.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "12.1.5", ngImport: i0__namespace, type: NcPdfViewerModule, decorators: [{
type: i0.NgModule,
args: [{
declarations: [
PdfViewerComponent
],
imports: [
i2.CommonModule
],
exports: [
PdfViewerComponent
]
}]
}] });
/**
* Generated bundle index. Do not edit.
*/
exports.NcPdfViewerModule = NcPdfViewerModule;
exports.PdfViewerComponent = PdfViewerComponent;
Object.defineProperty(exports, '__esModule', { value: true });
}));
//# sourceMappingURL=ng-cw-v12-pdf-viewer.umd.js.map