@finanzritter/nativescript-pdf-view
Version:
A basic PDF viewer plugin for NativeScript, to display PDF documents on iOS and Android.
101 lines • 3.64 kB
JavaScript
var pdfviewer = com.github.barteksc.pdfviewer;
import { Http, knownFolders } from '@nativescript/core';
import { PDFViewCommon, srcProperty } from './pdf-view.common';
var PDFViewSubclass = /** @class */ (function (_super) {
__extends(PDFViewSubclass, _super);
function PDFViewSubclass(a, b) {
var _this = _super.call(this, a, b) || this;
PDFViewSubclass.constructorCalled = true;
// necessary when extending TypeScript constructors
return global.__native(_this);
}
PDFViewSubclass.prototype.setUri = function (uri) {
this.uri = uri;
};
PDFViewSubclass.prototype.setEnableAnnotationRendering = function (enable) {
this.enableAnnotationRendering = enable;
};
PDFViewSubclass.prototype.setOnLoadHandler = function (func) {
this.onLoadHandler = func;
};
PDFViewSubclass.prototype.drawPdf = function () {
if (this.uri != null) {
var defaultSpacingDP = 8;
this
.fromUri(this.uri)
.onLoad(this.onLoadHandler)
.spacing(defaultSpacingDP)
.enableAnnotationRendering(this.enableAnnotationRendering)
.fitEachPage(true)
.load();
}
};
// mimics the usage of AndroidPdfView and the onAttachedToWindow method in react-native-pdf at
// https://github.com/wonday/react-native-pdf/blob/master/android/src/main/java/org/wonday/pdf/PdfView.java#L207-L211
PDFViewSubclass.prototype.onAttachedToWindow = function () {
_super.prototype.onAttachedToWindow.call(this);
if (this.isRecycled()) {
this.drawPdf();
}
};
PDFViewSubclass.constructorCalled = false;
return PDFViewSubclass;
}(pdfviewer.PDFView));
export class PDFView extends PDFViewCommon {
constructor() {
super(...arguments);
this.tempFolder = knownFolders.temp().getFolder('PDFViewer.temp/');
this.onLoadHandler = (() => {
const pdfViewRef = new WeakRef(this);
return new pdfviewer.listener.OnLoadCompleteListener({
loadComplete: numPages => {
PDFViewCommon.notifyOfEvent(PDFViewCommon.loadEvent, pdfViewRef);
},
});
})();
}
get android() {
return this.nativeView;
}
set android(value) {
this.nativeView = value;
}
createNativeView() {
return new PDFViewSubclass(this._context, void 0);
}
[srcProperty.setNative](value) {
this.loadPDF(value);
}
loadPDF(src) {
if (!src || !this.android) {
return;
}
this.promise = void 0;
if (src.indexOf('://') === -1) {
src = 'file://' + src;
}
else if (src.indexOf('file://') !== 0) {
this.cacheThenLoad(src);
return;
}
const uri = android.net.Uri.parse(src);
this.android.setUri(uri);
this.android.setEnableAnnotationRendering(this.enableAnnotationRendering);
this.android.setOnLoadHandler(this.onLoadHandler);
this.android.drawPdf();
}
cacheThenLoad(url) {
this.tempFolder.clear().then(() => {
const promise = this.promise = Http
.getFile(url, `${this.tempFolder.path}/${Date.now()}.pdf`)
.then(file => {
if (this.promise === promise) {
this.loadPDF(file.path);
}
}).catch(error => {
console.error(error);
});
});
}
}
//# sourceMappingURL=pdf-view.android.js.map