Skip to content

Commit 84389a7

Browse files
committed
Refactor initPdfViewer using the new registerGlobalInitFunc
1 parent 43c8d85 commit 84389a7

File tree

3 files changed

+6
-8
lines changed

3 files changed

+6
-8
lines changed

templates/repo/settings/lfs_file.tmpl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
<strong>{{ctx.Locale.Tr "repo.audio_not_supported_in_browser"}}</strong>
3232
</audio>
3333
{{else if .IsPDFFile}}
34-
<div class="pdf-content is-loading" data-src="{{$.RawFileLink}}" data-fallback-button-text="{{ctx.Locale.Tr "diff.view_file"}}"></div>
34+
<div class="pdf-content is-loading" data-global-init="initPdfViewer" data-src="{{$.RawFileLink}}" data-fallback-button-text="{{ctx.Locale.Tr "diff.view_file"}}"></div>
3535
{{else}}
3636
<a href="{{$.RawFileLink}}" rel="nofollow" class="tw-p-4">{{ctx.Locale.Tr "repo.file_view_raw"}}</a>
3737
{{end}}

templates/repo/view_file.tmpl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@
108108
<strong>{{ctx.Locale.Tr "repo.audio_not_supported_in_browser"}}</strong>
109109
</audio>
110110
{{else if .IsPDFFile}}
111-
<div class="pdf-content is-loading" data-src="{{$.RawFileLink}}" data-fallback-button-text="{{ctx.Locale.Tr "repo.diff.view_file"}}"></div>
111+
<div class="pdf-content is-loading" data-global-init="initPdfViewer" data-src="{{$.RawFileLink}}" data-fallback-button-text="{{ctx.Locale.Tr "repo.diff.view_file"}}"></div>
112112
{{else}}
113113
<a href="{{$.RawFileLink}}" rel="nofollow" class="tw-p-4">{{ctx.Locale.Tr "repo.file_view_raw"}}</a>
114114
{{end}}

web_src/js/render/pdf.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
import {htmlEscape} from 'escape-goat';
2+
import {registerGlobalInitFunc} from '../modules/observer.ts';
23

34
export async function initPdfViewer() {
4-
const els = document.querySelectorAll('.pdf-content');
5-
if (!els.length) return;
5+
registerGlobalInitFunc('initPdfViewer', async (el: HTMLInputElement) => {
6+
const pdfobject = await import(/* webpackChunkName: "pdfobject" */'pdfobject');
67

7-
const pdfobject = await import(/* webpackChunkName: "pdfobject" */'pdfobject');
8-
9-
for (const el of els) {
108
const src = el.getAttribute('data-src');
119
const fallbackText = el.getAttribute('data-fallback-button-text');
1210
pdfobject.embed(src, el, {
@@ -15,5 +13,5 @@ export async function initPdfViewer() {
1513
`,
1614
});
1715
el.classList.remove('is-loading');
18-
}
16+
});
1917
}

0 commit comments

Comments
 (0)