Skip to content

Commit b4cb0fb

Browse files
committed
fix(pdf-viewer): change CDN url
1 parent 26bf929 commit b4cb0fb

4 files changed

Lines changed: 42 additions & 5 deletions

File tree

src/components/global/store.spec.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import {
66
createStore,
77
initStore,
88
setStore,
9+
getCDN,
10+
setCDN,
911
} from './store'
1012

1113
beforeEach(() => {
@@ -33,7 +35,7 @@ describe('createStore', () => {
3335
})
3436
})
3537

36-
describe('setStore', () => {
38+
describe('useStore', () => {
3739
it('should create new store instance', () => {
3840
const a = useStore()
3941
const b = createStore()
@@ -56,3 +58,17 @@ describe('setStore', () => {
5658
expect(c).toBe(b)
5759
})
5860
})
61+
62+
describe('setCDN', () => {
63+
it('should able to set global cdnURL', () => {
64+
const context = useStore()
65+
66+
expect(context.value.cdnURL).toBe('https://unpkg.com/')
67+
expect(getCDN()).toBe('https://unpkg.com/')
68+
69+
setCDN('https://www.jsdelivr.com/package/npm/')
70+
71+
expect(context.value.cdnURL).toBe('https://www.jsdelivr.com/package/npm/')
72+
expect(getCDN()).toBe('https://www.jsdelivr.com/package/npm/')
73+
})
74+
})

src/components/global/store.ts

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,24 @@ type Lang = LiteralUnion<'id' | 'en', string>
1212
* Simple vuex-like-store for global configuration
1313
*/
1414
export interface State {
15-
/* Language setting */
15+
/**
16+
* Language
17+
*/
1618
lang: Lang,
19+
/**
20+
* PDFJS CDN base url
21+
*/
22+
cdnURL: string,
1723
}
1824

1925
let globalState: Ref<State>
2026

2127
export function createStore (): Ref<State> {
2228
const scope = effectScope(true)
23-
const state = scope.run(() => ref<State>({ lang: 'en' }))
29+
const state = scope.run(() => ref<State>({
30+
lang : 'en',
31+
cdnURL: 'https://unpkg.com/',
32+
}))
2433

2534
return state
2635
}
@@ -49,6 +58,14 @@ export function getLang () {
4958
return globalState?.value.lang ?? 'en'
5059
}
5160

61+
export function getCDN () {
62+
return globalState?.value.cdnURL
63+
}
64+
65+
export function setCDN (url: string) {
66+
globalState.value.cdnURL = url
67+
}
68+
5269
/**
5370
* Return reactive global lang setting
5471
* @example

src/components/pdf-viewer/utils/pdfjs.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import type * as PDFJS from 'pdfjs-dist'
22
import type * as PDFJSViewer from 'pdfjs-dist/web/pdf_viewer'
3+
import { withBase } from 'ufo'
4+
import { getCDN } from '../../global/store'
35

46
let pdfjsLib: typeof PDFJS
57

@@ -10,7 +12,7 @@ async function importPdfJS () {
1012
const pdfjs = await import('pdfjs-dist')
1113

1214
if (typeof window !== 'undefined' && 'Worker' in window)
13-
pdfjs.GlobalWorkerOptions.workerSrc = `https://cdn.jsdelivr.net/npm/pdfjs-dist@${pdfjs.version}/build/pdf.worker.min.js`
15+
pdfjs.GlobalWorkerOptions.workerSrc = withBase(`pdfjs-dist@${pdfjs.version}/build/pdf.worker.min.js`, getCDN())
1416

1517
pdfjsLib = pdfjs
1618
}
@@ -34,7 +36,7 @@ export async function getDocument (...params: Parameters<typeof PDFJS['getDocume
3436
}
3537

3638
export async function getCMAPUri () {
37-
return `https://cdn.jsdelivr.net/npm/pdfjs-dist@${await getVersion()}/cmaps/`
39+
return withBase(`pdfjs-dist@${await getVersion()}/cmaps/`, getCDN())
3840
}
3941

4042
export async function getVersion () {

src/core/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,8 @@ export {
102102
setLang,
103103
getLang,
104104
useLang,
105+
getCDN,
106+
setCDN,
105107
type State,
106108
} from '../components/global/store'
107109

0 commit comments

Comments
 (0)