@nativescript/pdf
Version:
A NativeScript plugin to display PDF files on iOS and Android
62 lines • 2.33 kB
JavaScript
import { PDFViewCommon, srcProperty } from './common';
var PDFWebViewDelegate = /** @class */ (function (_super) {
__extends(PDFWebViewDelegate, _super);
function PDFWebViewDelegate() {
return _super !== null && _super.apply(this, arguments) || this;
}
PDFWebViewDelegate.initWithOwner = function (owner) {
var delegate = PDFWebViewDelegate.new();
delegate.owner = owner;
return delegate;
};
PDFWebViewDelegate.prototype.webViewDidFinishNavigation = function (webView) {
PDFViewCommon.notifyOfEvent(PDFViewCommon.loadEvent, this.owner);
};
PDFWebViewDelegate.ObjCProtocols = [WKNavigationDelegate];
return PDFWebViewDelegate;
}(NSObject));
export class PDFView extends PDFViewCommon {
// @ts-ignore
get ios() {
return this.nativeView;
}
createNativeView() {
this.delegate = PDFWebViewDelegate.initWithOwner(new WeakRef(this));
const webView = new WKWebView({
configuration: WKWebViewConfiguration.new(),
frame: UIScreen.mainScreen.bounds,
});
webView.navigationDelegate = this.delegate;
webView.opaque = false;
webView.autoresizingMask =
// tslint:disable-next-line:no-bitwise
2 /* UIViewAutoresizing.FlexibleWidth */ | 16 /* UIViewAutoresizing.FlexibleHeight */;
return webView;
}
[srcProperty.setNative](value) {
this.loadPDF(value);
}
loadPDF(src) {
if (!src) {
return;
}
let url;
// detect base64 stream
const base64prefix = 'data:application/pdf;base64,';
if (src.indexOf(base64prefix) === 0) {
const base64data = NSData.alloc().initWithBase64EncodedStringOptions(src.substring(base64prefix.length), 1 /* NSDataBase64DecodingOptions.IgnoreUnknownCharacters */);
this.createTempFile(base64data);
return;
}
if (src.indexOf('://') === -1) {
url = NSURL.fileURLWithPath(src);
this.ios.loadFileURLAllowingReadAccessToURL(url, url);
}
else {
url = NSURL.URLWithString(src);
const urlRequest = NSURLRequest.requestWithURL(url);
this.ios.loadRequest(urlRequest);
}
}
}
//# sourceMappingURL=index.ios.js.map