Skip to content

Commit

Permalink
add vue example
Browse files Browse the repository at this point in the history
  • Loading branch information
mipo1357 committed Jul 23, 2024
1 parent fd0e18c commit 5bb4c13
Show file tree
Hide file tree
Showing 16 changed files with 2,497 additions and 313 deletions.
2,343 changes: 2,343 additions & 0 deletions examples/vue/package-lock.json

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions examples/vue/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
"format": "prettier --write src/"
},
"dependencies": {
"comlink": "^4.4.1",
"mupdf": "^0.2.2",
"vue": "^3.4.29"
},
"devDependencies": {
Expand Down
Binary file added examples/vue/public/test.pdf
Binary file not shown.
58 changes: 16 additions & 42 deletions examples/vue/src/App.vue
Original file line number Diff line number Diff line change
@@ -1,47 +1,21 @@
<script setup lang="ts">
import HelloWorld from './components/HelloWorld.vue'
import TheWelcome from './components/TheWelcome.vue'
import { useMupdf } from '@/composables/useMupdf'
import { watch, ref } from 'vue'
const { workerInitialized, loadDocument, renderPage, currentPage } = useMupdf()
const pdfUrl = ref<string | null>(null)
watch(workerInitialized, async (isInitialized) => {
if (isInitialized) {
const response = await fetch('/test.pdf')
const arrayBuffer = await response.arrayBuffer()
await loadDocument(arrayBuffer)
const pngData = await renderPage(0)
pdfUrl.value = URL.createObjectURL(new Blob([pngData], { type: 'image/png' }))
}
})
</script>

<template>
<header>
<img alt="Vue logo" class="logo" src="./assets/logo.svg" width="125" height="125" />

<div class="wrapper">
<HelloWorld msg="You did it!" />
</div>
</header>

<main>
<TheWelcome />
</main>
<img v-if="pdfUrl" :src="pdfUrl" alt="PDF page" />
</template>

<style scoped>
header {
line-height: 1.5;
}
.logo {
display: block;
margin: 0 auto 2rem;
}
@media (min-width: 1024px) {
header {
display: flex;
place-items: center;
padding-right: calc(var(--section-gap) / 2);
}
.logo {
margin: 0 2rem 0 0;
}
header .wrapper {
display: flex;
place-items: flex-start;
flex-wrap: wrap;
}
}
</style>
41 changes: 0 additions & 41 deletions examples/vue/src/components/HelloWorld.vue

This file was deleted.

88 changes: 0 additions & 88 deletions examples/vue/src/components/TheWelcome.vue

This file was deleted.

87 changes: 0 additions & 87 deletions examples/vue/src/components/WelcomeItem.vue

This file was deleted.

7 changes: 0 additions & 7 deletions examples/vue/src/components/icons/IconCommunity.vue

This file was deleted.

7 changes: 0 additions & 7 deletions examples/vue/src/components/icons/IconDocumentation.vue

This file was deleted.

7 changes: 0 additions & 7 deletions examples/vue/src/components/icons/IconEcosystem.vue

This file was deleted.

7 changes: 0 additions & 7 deletions examples/vue/src/components/icons/IconSupport.vue

This file was deleted.

19 changes: 0 additions & 19 deletions examples/vue/src/components/icons/IconTooling.vue

This file was deleted.

36 changes: 36 additions & 0 deletions examples/vue/src/composables/useMupdf.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import * as Comlink from 'comlink';
import { ref, shallowRef } from 'vue';
import type { MupdfWorker } from '../types/mupdf';

const worker = new Worker(new URL('../workers/mupdf.worker', import.meta.url), { type: 'module' });
const mupdfWorker = Comlink.wrap<MupdfWorker>(worker);
const workerInitialized = ref(false);

worker.addEventListener('message', (event) => {
if (event.data === 'MUPDF_LOADED') {
workerInitialized.value = true;
}
});

export function useMupdf() {
const document = shallowRef<ArrayBuffer | null>(null);
const currentPage = ref(0);

const loadDocument = async (arrayBuffer: ArrayBuffer) => {
document.value = arrayBuffer;
return mupdfWorker.loadDocument(arrayBuffer);
};

const renderPage = async (pageIndex: number) => {
if (!document.value) throw new Error('Document not loaded');
currentPage.value = pageIndex;
return mupdfWorker.renderPageAsImage(pageIndex, (window.devicePixelRatio * 96) / 72);
};

return {
workerInitialized,
loadDocument,
renderPage,
currentPage,
};
}
4 changes: 4 additions & 0 deletions examples/vue/src/types/mupdf.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export interface MupdfWorker {
loadDocument(document: ArrayBuffer): Promise<boolean>;
renderPageAsImage(pageIndex?: number, scale?: number): Promise<Uint8Array>;
}
Loading

0 comments on commit 5bb4c13

Please sign in to comment.