-
-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathEmbedPDF.razor.js
More file actions
153 lines (131 loc) · 3.94 KB
/
EmbedPDF.razor.js
File metadata and controls
153 lines (131 loc) · 3.94 KB
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
import Data from '../BootstrapBlazor/modules/data.js'
import EventHandler from "../BootstrapBlazor/modules/event-handler.js"
import EmbedPDF from './embedpdf.js'
import { getTheme, registerBootstrapBlazorModule } from '../BootstrapBlazor/modules/utility.js'
export async function init(id, invoke, options) {
const el = document.getElementById(id);
if (el === null) {
return null;
}
const target = el.querySelector('.pdf-viewer');
const { src, tabBar, theme, lang, currentPage, scrollStrategy, pageGap } = options;
const wasmUrl = `${location.origin}/_content/BootstrapBlazor.EmbedPDF/pdfium.wasm`;
let preference = theme;
if (preference === 'system') {
preference = getTheme();
}
let currentPageGap = pageGap;
if (currentPageGap === 0) {
currentPageGap = 20;
}
const viewer = EmbedPDF.init({
type: 'container',
target: el,
src,
worker: true,
tabBar,
theme: {
preference: preference
},
wasmUrl,
i18n: {
defaultLocale: lang,
fallbackLocale: 'en'
},
scroll: {
defaultStrategy: scrollStrategy,
defaultPageGap: currentPageGap
}
});
registerBootstrapBlazorModule('EmbedPDF', id, () => {
EventHandler.on(document, 'changed.bb.theme', updateTheme);
});
Data.set(id, { el, invoke, options, viewer });
const registry = await viewer.registry;
const scroll = registry.getPlugin('scroll').provides();
if (currentPage !== 0) {
scroll.onLayoutReady((event) => {
scroll.scrollToPage({
pageNumber: currentPage,
behavior: 'instant'
});
});
}
}
export async function setUrl(id, url, keepCurrentDoc) {
const pdf = Data.get(id);
const { viewer } = pdf;
if (viewer) {
const registry = await viewer.registry;
const docManager = registry.getPlugin('document-manager').provides();
if (!keepCurrentDoc) {
const doc = docManager.getActiveDocument();
if (doc) {
docManager.closeDocument(doc.id).toPromise();
}
}
const name = getFileName(url);
docManager.openDocumentUrl({
url,
name,
autoActivate: true
}).toPromise();
}
}
export function setTheme(id, theme) {
const pdf = Data.get(id);
const { viewer } = pdf;
if (viewer) {
viewer.setTheme(theme);
}
}
export async function setLocale(id, locale) {
const pdf = Data.get(id);
const { viewer } = pdf;
if (viewer) {
const registry = await viewer.registry;
const i18n = registry.getPlugin('i18n').provides();
i18n.setLocale(locale);
}
}
export async function setScrollStrategy(id, strategy) {
const pdf = Data.get(id);
const { viewer } = pdf;
if (viewer) {
const registry = await viewer.registry;
const scroll = registry.getPlugin('scroll').provides();
scroll.setScrollStrategy(strategy);
}
}
export function dispose(id) {
const pdf = Data.get(id);
Data.remove(id);
const { EmbedPDF } = window.BootstrapBlazor;
if (EmbedPDF) {
EmbedPDF.dispose(id, () => {
EventHandler.off(document, 'changed.bb.theme', updateTheme);
});
}
}
const updateTheme = e => {
const theme = e.theme;
[...document.querySelectorAll('.bb-embed-pdf')].forEach(s => {
const id = s.getAttribute('id');
if (id) {
const pdf = Data.get(id);
if (pdf) {
const { viewer } = pdf;
if (viewer) {
viewer.setTheme(theme);
}
}
}
});
}
function getFileName(filePath) {
const lastSeparatorIndex = Math.max(
filePath.lastIndexOf('/'),
filePath.lastIndexOf('\\')
);
return filePath.substring(lastSeparatorIndex + 1);
}