Skip to content

Commit 707e1c9

Browse files
committed
Add "props" documentation, implement container queries, and update version to 1.0.10
1 parent be3ba98 commit 707e1c9

File tree

7 files changed

+157
-14
lines changed

7 files changed

+157
-14
lines changed

docs/.vitepress/config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ export default defineConfig({
2929
text: "Introduction",
3030
items: [
3131
{ text: "Getting Started", link: "/guide/getting-started" },
32+
{ text: "Props", link: "/guide/props" },
3233
{ text: "Composables", link: "/guide/composables" },
3334
],
3435
},

docs/components/PdfViewer.vue

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ const vuepdfjs = useTemplateRef<typeof VuePDFjs>('vuepdfjs')
1010
const pages = ref(0);
1111
const hideToolbar = ref(false);
1212
const hideSidebar = ref(false);
13+
const useContainerQuery = ref(false)
14+
const playgroundWidth = ref(100)
1315
1416
const options = reactive<NonNullable<VuePDFjsProps['options']>>({
1517
locale: {
@@ -76,24 +78,33 @@ const onPdfAppLoaded = () => {
7678
}
7779
7880
const pdf = 'https://raw.githubusercontent.com/mozilla/pdf.js/v4.10.38/web/compressed.tracemonkey-pldi-09.pdf'
81+
const source = ref(pdf)
7982
</script>
8083

8184
<template>
8285
We have {{ pages }} pages in this document.
8386

84-
<div>
87+
<div style="margin-bottom: 10px;">
8588
<input type="checkbox" v-model="hideToolbar" /> Hide Toolbar
8689
<input type="checkbox" v-model="hideSidebar" /> Hide Sidebar
87-
<button type="button" class="custom-button">
90+
<button type="button" class="custom-button" @click="source = `invalid${new Date().getTime()}.pdf`">
8891
Load Invalid PDF
8992
</button>
93+
<input type="checkbox" v-model="useContainerQuery" /> Use container query
94+
<div>
95+
<label>Playground Width: {{ playgroundWidth }}%</label>
96+
<input type="range" v-model="playgroundWidth" min="0" max="100" />
97+
</div>
98+
</div>
99+
<div id="playground-container" :style="{ width: playgroundWidth + '%' }">
100+
<VuePDFjs ref="vuepdfjs" :source :options="options" :source-options="sourceOptions" :use-container-query
101+
@pdf-app:loaded="onPdfAppLoaded" />
90102
</div>
91-
<VuePDFjs ref="vuepdfjs" :source="pdf" :options="options" :source-options="sourceOptions"
92-
@pdf-app:loaded="onPdfAppLoaded" />
93103
</template>
94104

95105
<style>
96-
.pdf_viewer {
106+
.pdf_viewer,
107+
#playground-container {
97108
height: 650px;
98109
width: 100%;
99110
overflow: hidden;
@@ -109,4 +120,8 @@ const pdf = 'https://raw.githubusercontent.com/mozilla/pdf.js/v4.10.38/web/compr
109120
border-radius: 5px;
110121
cursor: pointer;
111122
}
123+
124+
.custom-button:hover {
125+
background-color: #0056b3;
126+
}
112127
</style>

docs/guide/props.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# Props
2+
3+
## source
4+
5+
- **Type:** `PDFSource | PDFSourceWithOptions | PDFDocumentProxy`
6+
- **Description:** The source of the PDF file.
7+
- **Optional**
8+
9+
## sourceOptions
10+
11+
- **Type:** `PDFSourceOptions`
12+
- **Description:** The options for the PDF source, including error handling.
13+
14+
## useContainerQuery
15+
16+
- **Type:** `boolean`
17+
- **Default:** `true`
18+
- **Description:** If true, the component uses container queries to adjust the layout based on the container size. The pdf.js viewer only use media queries to adjust the layout based on the window size.
19+
20+
## options
21+
22+
- **Type:** `object`
23+
- **Description:** Options to configure the PDF viewer.
24+
- **locale:**
25+
- **code:** `string` (required) - Locale code.
26+
- **ftl:** `string` (required) - Locale FTL text.
27+
- **toolbar:** `ToolbarContainerProps`
28+
- **sidebar:** `SidebarContainerProps`

packages/vue-pdfjs-playground/src/App.vue

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ const vuepdfjs = useTemplateRef<InstanceType<typeof VuePDFjs>>('vuepdfjs')
1111
1212
const hideToolbar = ref(false)
1313
const hideSidebar = ref(false)
14+
const useContainerQuery = ref(false)
15+
const playgroundWidth = ref(100)
1416
1517
const options = reactive<NonNullable<VuePDFjsProps['options']>>({
1618
locale: {
@@ -88,7 +90,6 @@ const source = ref<any>()
8890
watch(document, (value) => {
8991
source.value = value
9092
})
91-
9293
</script>
9394

9495
<template>
@@ -98,12 +99,17 @@ watch(document, (value) => {
9899
<button type="button" class="custom-button" @click="source = `invalid${new Date().getTime()}.pdf`">
99100
Load Invalid PDF
100101
</button>
102+
<input type="checkbox" v-model="useContainerQuery" /> Use container query
103+
<div>
104+
<label>Playground Width: {{ playgroundWidth }}%</label>
105+
<input type="range" v-model="playgroundWidth" min="0" max="100" />
106+
</div>
101107
</div>
102108
<div>
103109
We have {{ vuepdfjs?.pdfPages }} pages in this document.
104110
</div>
105-
<div id="playground">
106-
<VuePDFjs ref="vuepdfjs" :source :options="options" :source-options="sourceOptions"
111+
<div id="playground" :style="{ width: playgroundWidth + '%' }">
112+
<VuePDFjs ref="vuepdfjs" :source :options="options" :source-options="sourceOptions" :use-container-query
107113
@pdf-app:loaded="onPdfAppLoaded" />
108114
</div>
109115
</template>

packages/vue/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@tuttarealstep/vue-pdf.js",
33
"description": "A Vue component for displaying PDF files using the standard `pdf.js` viewer. This package provides a simple and powerful integration to embed PDF viewers in Vue applications.",
4-
"version": "1.0.9",
4+
"version": "1.0.10",
55
"private": false,
66
"type": "module",
77
"author": "Stefano Valenzano (https://github.com/tuttarealstep)",

packages/vue/src/assets/scss/main.scss

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,50 @@
2525
[hidden] {
2626
display: none !important;
2727
}
28+
29+
&--container-query {
30+
31+
.appContainer {
32+
container-name: app-container;
33+
container-type: inline-size;
34+
}
35+
36+
@container app-container (max-width: 840px) {
37+
#sidebarContainer {
38+
background-color: var(--sidebar-narrow-bg-color);
39+
}
40+
41+
#outerContainer.sidebarOpen #viewerContainer {
42+
inset-inline-start: 0 !important;
43+
}
44+
}
45+
46+
@container app-container (max-width: 750px) {
47+
.hiddenMediumView {
48+
display: none !important;
49+
}
50+
51+
.visibleMediumView:not(.hidden, [hidden]) {
52+
display: inline-block !important;
53+
}
54+
}
55+
56+
@container app-container (max-width: 690px) {
57+
58+
.hiddenSmallView,
59+
.hiddenSmallView * {
60+
display: none !important;
61+
}
62+
63+
#toolbarContainer #toolbarViewer .toolbarButtonSpacer {
64+
width: 0;
65+
}
66+
}
67+
68+
@container app-container (max-width: 560px) {
69+
#scaleSelectContainer {
70+
display: none;
71+
}
72+
}
73+
}
2874
}

packages/vue/src/components/VuePDFjs.vue

Lines changed: 52 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<script setup lang="ts">
22
import '../assets/scss/main.scss'
33
4-
import { onMounted, onUnmounted, ref, shallowRef, provide, watch } from 'vue';
4+
import { onMounted, onUnmounted, ref, shallowRef, provide, watch, onUpdated, nextTick } from 'vue';
55
import { initViewer, PDFViewerApplicationOptions } from '../scripts/viewer';
66
77
import OuterContainer from './OuterContainer.vue';
@@ -17,8 +17,21 @@ import { sidebarOptionsKey, toolbarOptionsKey } from '@/keys';
1717
import { SidebarContainerProps } from './SidebarContainer.vue';
1818
1919
export interface VuePDFjsProps {
20+
/**
21+
* The source of the PDF file.
22+
*/
2023
source?: PDFSource | PDFSourceWithOptions | PDFDocumentProxy
24+
/**
25+
* The options for the PDF source.
26+
*/
2127
sourceOptions?: PDFSourceOptions,
28+
/**
29+
* If true, the component will use the container query to adjust the viewer height.
30+
*/
31+
useContainerQuery?: boolean,
32+
/**
33+
* The options for the PDF viewer.
34+
*/
2235
options?: {
2336
locale?: {
2437
code: string,
@@ -30,7 +43,8 @@ export interface VuePDFjsProps {
3043
}
3144
3245
const props = withDefaults(defineProps<VuePDFjsProps>(), {
33-
source: null
46+
source: null,
47+
useContainerQuery: true,
3448
});
3549
3650
const emit = defineEmits<{
@@ -161,6 +175,25 @@ async function openSource(source: PDFSource | PDFSourceWithOptions | PDFDocument
161175
}
162176
}
163177
178+
//When the component is updated, we need to fix the viewer because sometimes the css variables are not applied correctly
179+
const fixViewer = () => {
180+
// We need to mimic the #updateContainerHeightCss because is a private method
181+
if (!pdfApp.value) {
182+
return;
183+
}
184+
185+
try {
186+
const pdfAppViewer = pdfApp.value.pdfViewer;
187+
const containerHeight = pdfAppViewer.container.clientHeight;
188+
189+
if (containerHeight !== pdfAppViewer.previousContainerHeight) {
190+
container.value?.style.setProperty("--viewer-container-height", `${containerHeight}px`);
191+
}
192+
} catch (error) {
193+
console.error(error);
194+
}
195+
}
196+
164197
watch(() => props.source, async (source) => {
165198
await openSource(source);
166199
})
@@ -172,9 +205,23 @@ onMounted(async () => {
172205
await openSource(props.source);
173206
})
174207
208+
onUpdated(async () => {
209+
//Wait for the next tick to fix the viewer
210+
nextTick(() => {
211+
fixViewer();
212+
})
213+
})
214+
175215
onUnmounted(async () => {
176-
if (pdfApp.value)
177-
await pdfApp.value.close();
216+
if (!pdfApp.value) {
217+
return;
218+
}
219+
220+
// Unbind events
221+
pdfApp.value.unbindEvents();
222+
pdfApp.value.unbindWindowEvents();
223+
224+
await pdfApp.value.close();
178225
})
179226
180227
defineExpose({
@@ -187,7 +234,7 @@ defineExpose({
187234
</script>
188235

189236
<template>
190-
<div class="vue-pdfjs">
237+
<div class="vue-pdfjs" :class="{ 'vue-pdfjs--container-query': props.useContainerQuery }">
191238
<div class="appContainer" ref="container" id="appContainer">
192239
<OuterContainer />
193240
<PrintContainer />

0 commit comments

Comments
 (0)