File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 66 createStore ,
77 initStore ,
88 setStore ,
9+ getCDN ,
10+ setCDN ,
911} from './store'
1012
1113beforeEach ( ( ) => {
@@ -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+ } )
Original file line number Diff line number Diff line change @@ -12,15 +12,24 @@ type Lang = LiteralUnion<'id' | 'en', string>
1212 * Simple vuex-like-store for global configuration
1313 */
1414export interface State {
15- /* Language setting */
15+ /**
16+ * Language
17+ */
1618 lang : Lang ,
19+ /**
20+ * PDFJS CDN base url
21+ */
22+ cdnURL : string ,
1723}
1824
1925let globalState : Ref < State >
2026
2127export 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
Original file line number Diff line number Diff line change 11import type * as PDFJS from 'pdfjs-dist'
22import type * as PDFJSViewer from 'pdfjs-dist/web/pdf_viewer'
3+ import { withBase } from 'ufo'
4+ import { getCDN } from '../../global/store'
35
46let 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
3638export 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
4042export async function getVersion ( ) {
Original file line number Diff line number Diff 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
You can’t perform that action at this time.
0 commit comments