|
1 | | -import EmbedPDF from './embedpdf.js'; |
| 1 | +import Data from '../BootstrapBlazor/modules/data.js'; |
| 2 | +import EventHandler from "../BootstrapBlazor/modules/event-handler.js" |
| 3 | +import { default as EmbedPDF, UIPlugin } from './embedpdf.js'; |
| 4 | +import { getTheme, registerBootstrapBlazorModule } from '../BootstrapBlazor/modules/utility.js' |
2 | 5 |
|
3 | | -export function init(id, invoke) { |
| 6 | +export function init(id, invoke, options) { |
4 | 7 | const el = document.getElementById(id); |
5 | 8 | if (el === null) { |
6 | 9 | return null; |
7 | 10 | } |
8 | 11 |
|
9 | 12 | const target = el.querySelector('.pdf-viewer'); |
10 | | - const src = './samples/sample.pdf'; |
| 13 | + const { src, tabBar, theme } = options; |
11 | 14 | const wasmUrl = `${location.origin}/_content/BootstrapBlazor.EmbedPDF/pdfium.wasm`; |
12 | | - EmbedPDF.init({ |
| 15 | + |
| 16 | + let preference = theme; |
| 17 | + if (preference === 'system') { |
| 18 | + preference = getTheme(); |
| 19 | + } |
| 20 | + |
| 21 | + const viewer = EmbedPDF.init({ |
13 | 22 | type: 'container', |
14 | 23 | target: el, |
15 | 24 | src, |
16 | 25 | worker: true, |
17 | | - tabBar: 'always', |
| 26 | + tabBar, |
18 | 27 | theme: { |
19 | | - preference: 'system' |
| 28 | + preference: preference |
20 | 29 | }, |
21 | 30 | wasmUrl |
22 | 31 | }); |
| 32 | + |
| 33 | + registerBootstrapBlazorModule('EmbedPDF', id, () => { |
| 34 | + EventHandler.on(document, 'changed.bb.theme', updateTheme); |
| 35 | + }); |
| 36 | + |
| 37 | + Data.set(id, { el, invoke, options, viewer }); |
| 38 | +} |
| 39 | + |
| 40 | +export function setUrl(id, url) { |
| 41 | + |
| 42 | +} |
| 43 | + |
| 44 | +export function setTheme(id, theme) { |
| 45 | + const pdf = Data.get(id); |
| 46 | + const { viewer } = pdf; |
| 47 | + if (viewer) { |
| 48 | + viewer.setTheme(theme); |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +export function dispose(id) { |
| 53 | + const pdf = Data.get(id); |
| 54 | + Data.remove(id); |
| 55 | + |
| 56 | + const { EmbedPDF } = window.BootstrapBlazor; |
| 57 | + if (EmbedPDF) { |
| 58 | + EmbedPDF.dispose(id, () => { |
| 59 | + EventHandler.off(document, 'changed.bb.theme', updateTheme); |
| 60 | + }); |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +const updateTheme = e => { |
| 65 | + const theme = e.theme; |
| 66 | + |
| 67 | + [...document.querySelectorAll('.bb-embed-pdf')].forEach(s => { |
| 68 | + const id = s.getAttribute('id'); |
| 69 | + if (id) { |
| 70 | + const pdf = Data.get(id); |
| 71 | + if (pdf) { |
| 72 | + const { viewer } = pdf; |
| 73 | + if (viewer) { |
| 74 | + viewer.setTheme(theme); |
| 75 | + } |
| 76 | + } |
| 77 | + } |
| 78 | + }); |
23 | 79 | } |
0 commit comments