This repository has been archived by the owner on Jun 15, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathpdf-view.ios.ts
89 lines (70 loc) · 2.17 KB
/
pdf-view.ios.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import { screen } from 'tns-core-modules/platform';
import { PDFViewCommon, srcProperty } from './pdf-view.common';
class PDFViewDelegate extends NSObject implements WKNavigationDelegate {
public static ObjCProtocols = [WKNavigationDelegate];
private owner: WeakRef<PDFView>;
public static initWithOwner(owner: WeakRef<PDFView>): PDFViewDelegate {
const delegate = PDFViewDelegate.new() as PDFViewDelegate;
delegate.owner = owner;
return delegate;
}
public webViewDidFinishNavigation(webView: WKWebView) {
PDFViewCommon.notifyOfEvent(PDFViewCommon.loadEvent, this.owner);
}
}
export class PDFView extends PDFViewCommon {
private delegate: PDFViewDelegate;
public constructor() {
super();
this.init();
}
public get ios() {
return this.nativeView as WKWebView;
}
public set ios(value: WKWebView) {
this.nativeView = value;
}
public [srcProperty.setNative](value: string) {
this.loadPDF(value);
}
public onLoaded() {
super.onLoaded();
this.ios.navigationDelegate = this.delegate;
}
public onUnloaded() {
this.ios.navigationDelegate = void 0;
super.onUnloaded();
}
public loadPDF(src: string) {
if (!src) {
return;
}
let url: NSURL;
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);
}
}
private init() {
this.delegate = PDFViewDelegate.initWithOwner(new WeakRef(this));
this.ios = new WKWebView({
configuration: WKWebViewConfiguration.new(),
frame: this.mainScreen.bounds,
});
this.ios.opaque = false;
this.ios.autoresizingMask =
// tslint:disable-next-line:no-bitwise
UIViewAutoresizing.FlexibleWidth |
UIViewAutoresizing.FlexibleHeight;
}
private get mainScreen(): UIScreen {
// tslint:disable-next-line:strict-type-predicates
return typeof UIScreen.mainScreen === 'function' ?
UIScreen.mainScreen() as UIScreen : // xCode 7 and below
UIScreen.mainScreen; // xCode 8+
}
}