Skip to content

Commit

Permalink
Merge pull request #17 from privy-open-source/feat/expose-pdf-context
Browse files Browse the repository at this point in the history
Feat/expose pdf context
  • Loading branch information
radyakaze authored Jun 20, 2023
2 parents a96b0e0 + f18029f commit a247af0
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 10 deletions.
11 changes: 8 additions & 3 deletions demo/src/App.vue
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<script setup lang="ts">
import { reactive } from 'vue'
import { reactive, ref } from 'vue'
import PdfViewer from '@/components/pdf-viewers/PdfViewer.vue'
import PdfObject from '@/components/pdf-object/PdfObject.vue'
import type { PdfObject as IPdfObject } from '@/types/ref'
import type { PdfObject as IPdfObject, PdfViewerContext } from '@/types/ref'
const object = reactive<IPdfObject>({
page: undefined,
Expand All @@ -11,11 +11,15 @@ const object = reactive<IPdfObject>({
width: undefined,
height: undefined
})
const pdf = ref<PdfViewerContext>()
</script>

<template>
<div class="pdf-example">
{{ pdf }}
<pdf-viewer
ref="pdf"
src="https://privy-open-source.github.io/design-system/assets/Calibrator-v3.e869f66c.pdf"
>
<pdf-object
Expand All @@ -32,12 +36,13 @@ const object = reactive<IPdfObject>({
/>
</pdf-object>

<!-- <template #navigation="{ zoomIn, zoomOut, page, totalPage, next, prev }">
<!-- <template #navigation="{ zoomIn, zoomOut, page, totalPage, next, prev, scale }">
<button @click="zoomOut">Zoom Out</button>
<button @click="zoomIn">Zoom In</button>
<div>
Page <span>{{ page }}</span> / {{ totalPage }}
</div>
<div>Scale {{ scale }}</div>
<button @click="prev">Prev</button>
<button @click="next">Next</button>
</template> -->
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@privyid/pdf-ajaib",
"version": "0.1.7",
"version": "0.1.8",
"private": false,
"license": "MIT",
"repository": {
Expand Down
36 changes: 31 additions & 5 deletions src/components/pdf-viewers/PdfViewer.vue
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import type { PropType } from 'vue-demi'
import { computed, defineComponent, onMounted, provide, toRef, watch } from 'vue-demi'
import { pAspectRatio } from '@/directives/aspect-ratio'
import { templateRef, useToNumber, useVModel, watchDebounced, syncRef } from '@vueuse/core'
import { templateRef, useToNumber, useVModel, watchDebounced, syncRef, useToggle } from '@vueuse/core'
import type { LayoutVariant } from './main'
import { PDF_VIEWER_CONTEXT } from './main'
import { useSticky } from './utils/use-sticky'
Expand Down Expand Up @@ -49,17 +49,23 @@ export default defineComponent({
offsetTop: {
type: [Number, String],
default: 0
},
noNavigation: {
type: Boolean,
default: false,
}
},
emits: ['ready', 'loaded', 'error', 'error-password', 'update:page', 'update:scale'],
setup(props, { emit }) {
setup(props, { emit, expose }) {
const root = templateRef<HTMLDivElement>('root')
const container = templateRef<HTMLDivElement>('container')
const viewer = templateRef<HTMLDivElement>('viewer')
const idle = useIdle(container)
const vPage = useVModel(props, 'page')
const vScale = useVModel(props, 'scale')
const [isReady, toggleReady] = useToggle()
const [isLoaded, toggleLoaded] = useToggle()
const offsetTop = useToNumber(toRef(props, 'offsetTop'), {
nanToZero: true
Expand Down Expand Up @@ -112,6 +118,7 @@ export default defineComponent({
})
onLoaded((doc) => {
toggleLoaded(true)
emit('loaded', doc)
})
Expand All @@ -121,6 +128,7 @@ export default defineComponent({
})
onReady((pdfViewer) => {
toggleReady(true)
emit('ready', pdfViewer)
})
Expand Down Expand Up @@ -149,7 +157,25 @@ export default defineComponent({
pdfPage.value--
}
const calculatedScale = computed<number>(() => {
return Number((pdfScale.value * 100).toFixed(0))
})
expose({
idle,
page: pdfPage,
scale: calculatedScale,
totalPage,
zoomIn: pdfZoomIn,
zoomOut: pdfZoomOut,
next: pdfNextPage,
prev: pdfPrevPage,
isReady,
isLoaded,
})
return {
calculatedScale,
pdfPage,
pdfScale,
classNames,
Expand All @@ -164,7 +190,7 @@ export default defineComponent({
pdfZoomIn,
pdfZoomOut,
pdfNextPage,
pdfPrevPage
pdfPrevPage,
}
}
})
Expand Down Expand Up @@ -201,12 +227,12 @@ export default defineComponent({
/>

<transition name="slide-up">
<div class="pdf__navigation">
<div v-if="!noNavigation" class="pdf__navigation">
<slot
name="navigation"
:idle="idle"
:page="pdfPage"
:scale="scale"
:scale="calculatedScale"
:totalPage="totalPage"
:zoom-in="pdfZoomIn"
:zoom-out="pdfZoomOut"
Expand Down
2 changes: 1 addition & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export { default as PdfViewer } from './components/pdf-viewers/PdfViewer.vue'
export { default as PdfObject } from './components/pdf-object/PdfObject.vue'
export { default as PdfObjectAddon } from './components/pdf-object/PdfObjectAddon.vue'
export type { PdfObject as PdfObjectRef } from './types/ref'
export type { PdfObject as PdfObjectRef, PdfViewerContext } from './types/ref'
15 changes: 15 additions & 0 deletions src/types/ref.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,22 @@
import type { Ref } from 'vue'

export interface PdfObject {
page?: number
x?: number
y?: number
width?: number
height?: number
}

export interface PdfViewerContext {
idle: Ref<boolean>
page: Ref<number>
scale: Ref<number>
totalPage: Ref<number>
zoomIn: () => void
zoomOut: () => void
next: () => void
prev: () => void
isReady: Ref<boolean>
isLoaded: Ref<boolean>
}

0 comments on commit a247af0

Please sign in to comment.