Skip to content

Commit

Permalink
feat(pdf-viewer): add prop config
Browse files Browse the repository at this point in the history
  • Loading branch information
adenvt committed Mar 9, 2024
1 parent 352dc71 commit ba9bbb0
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 10 deletions.
27 changes: 21 additions & 6 deletions src/components/pdf-viewer/PdfViewer.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,7 @@ it('should load document from src url', async () => {
await delay(2)

expect(viewer).toBeInTheDocument()
// eslint-disable-next-line unicorn/no-useless-undefined
expect(useViewer.openDoc).lastCalledWith('http://sample.pdf', undefined)
expect(useViewer.openDoc).lastCalledWith('http://sample.pdf', undefined, {})
})

it('should load document with password if prop password given', async () => {
Expand All @@ -53,7 +52,7 @@ it('should load document with password if prop password given', async () => {
await delay(2)

expect(viewer).toBeInTheDocument()
expect(useViewer.openDoc).lastCalledWith('http://sample.pdf', '123456')
expect(useViewer.openDoc).lastCalledWith('http://sample.pdf', '123456', {})
})

it('should have style layout fit if prop `layout` set to "fit"', () => {
Expand Down Expand Up @@ -86,21 +85,21 @@ it('should reload document if src changed', async () => {
vi.advanceTimersByTime(2)

expect(viewer).toBeInTheDocument()
expect(useViewer.openDoc).lastCalledWith('http://sample.pdf', '')
expect(useViewer.openDoc).lastCalledWith('http://sample.pdf', '', {})

src.value = 'http://sample-3.pdf'
await nextTick()

vi.advanceTimersByTime(502) // skip debounce

expect(useViewer.openDoc).lastCalledWith('http://sample-3.pdf', '')
expect(useViewer.openDoc).lastCalledWith('http://sample-3.pdf', '', {})

password.value = '123456'
await nextTick()

vi.advanceTimersByTime(502) // skip debounce

expect(useViewer.openDoc).lastCalledWith('http://sample-3.pdf', '123456')
expect(useViewer.openDoc).lastCalledWith('http://sample-3.pdf', '123456', {})

vi.useRealTimers()
})
Expand Down Expand Up @@ -146,6 +145,22 @@ it('should emit error-password when document is protected but no password provid
expect(onErrorPassword).toBeCalled()
})

it('should able to pass option when open document using props `config`', async () => {
const screen = render({
components: { PdfViewer },
template : `<pdf-viewer
:config="{ disableStream: true, disableRange: true }"
src="http://sample.pdf" />`,
})

const viewer = screen.queryByTestId('pdf-viewer')

await delay(2)

expect(viewer).toBeInTheDocument()
expect(useViewer.openDoc).lastCalledWith('http://sample.pdf', undefined, { disableStream: true, disableRange: true })
})

it('should expose navigation in slot', async () => {
const page = ref(1)
const scale = ref(1)
Expand Down
9 changes: 7 additions & 2 deletions src/components/pdf-viewer/PdfViewer.vue
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ import PdfObjects from '../pdf-object/PdfObjects.vue'
import { useViewer } from './utils/use-viewer'
import type { PDFViewer } from 'pdfjs-dist/web/pdf_viewer'
import type { PDFDocumentProxy } from 'pdfjs-dist'
import type { OpenDocConfig } from './utils/use-viewer'
const props = defineProps({
src: {
Expand Down Expand Up @@ -147,6 +148,10 @@ const props = defineProps({
type : [Number, String],
default: 0,
},
config: {
type : [Object] as PropType<OpenDocConfig>,
default: () => ({}),
},
})
const emit = defineEmits<{
Expand Down Expand Up @@ -193,7 +198,7 @@ const {
} = useViewer(container, viewer)
watchDebounced(() => [props.src, props.password], ([src, password]) => {
openDoc(src, password)
openDoc(src, password, props.config)
}, { debounce: 500 })
watch(() => props.layout, (layout) => {
Expand All @@ -202,7 +207,7 @@ watch(() => props.layout, (layout) => {
onMounted(async () => {
if (props.src)
openDoc(props.src, props.password)
openDoc(props.src, props.password, props.config)
})
onLoaded((doc) => {
Expand Down
10 changes: 8 additions & 2 deletions src/components/pdf-viewer/utils/use-viewer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ import {
getDocument,
} from './pdfjs'

export interface OpenDocConfig {
disableStream: boolean,
disableRange: boolean,
}

export function useViewer (container: Ref<HTMLDivElement>, viewer: Ref<HTMLDivElement>) {
const pdfDoc = shallowRef<PDFJS.PDFDocumentProxy>()
const pdfEventBus = shallowRef<EventBus>()
Expand All @@ -42,7 +47,7 @@ export function useViewer (container: Ref<HTMLDivElement>, viewer: Ref<HTMLDivEl
const errorEvent: EventHook<Error> = createEventHook<Error>()
const readyEvent: EventHook<PDFViewer> = createEventHook<PDFViewer>()

async function openDoc (url: string, password?: string) {
async function openDoc (url: string, password?: string, config: Partial<OpenDocConfig> = {}) {
loading.value = true
error.value = undefined

Expand All @@ -57,7 +62,8 @@ export function useViewer (container: Ref<HTMLDivElement>, viewer: Ref<HTMLDivEl
password : password,
cMapUrl : await getCMAPUri(),
cMapPacked : true,
disableStream: false,
disableStream: config.disableStream,
disableRange : config.disableRange,
})

pdfDoc.value = await pdfLoadingTask.value.promise
Expand Down

0 comments on commit ba9bbb0

Please sign in to comment.