Vue
The viewer and the editor each fill an element you give them. In Vue, that's an element in your component's template, and the viewer is created in it once the component has mounted.
Install
sh
npm install @docmentis/udoc-viewersh
npm install @docmentis/udoc-editorThe component
vue
<script setup lang="ts">
import { onBeforeUnmount, onMounted, useTemplateRef, watch } from "vue";
import { UDocViewer } from "@docmentis/udoc-viewer";
/** A viewer filling its element, opening `src`. */
const props = defineProps<{ src: string }>();
const container = useTemplateRef<HTMLDivElement>("container");
let viewer: UDocViewer | undefined;
// Unmounted before `create` resolves, the component destroys the viewer it made.
let gone = false;
onMounted(async () => {
try {
const created = await UDocViewer.create(container.value!);
if (gone) return created.destroy();
viewer = created;
await viewer.open(props.src);
} catch (error) {
console.error("The viewer didn't open the document:", error);
}
});
watch(
() => props.src,
(src) => viewer?.open(src),
);
onBeforeUnmount(() => {
gone = true;
viewer?.destroy();
});
</script>
<template>
<div ref="container" class="pane"></div>
</template>vue
<script setup lang="ts">
import { onBeforeUnmount, onMounted, useTemplateRef, watch } from "vue";
import { UDocEditor } from "@docmentis/udoc-editor";
/** An editor filling its element, opening `src`. */
const props = defineProps<{ src: string }>();
const container = useTemplateRef<HTMLDivElement>("container");
let editor: UDocEditor | undefined;
// Unmounted before `create` resolves, the component destroys the editor it made.
let gone = false;
onMounted(async () => {
try {
const created = await UDocEditor.create(container.value!);
if (gone) return created.destroy();
editor = created;
await editor.open(props.src);
} catch (error) {
console.error("The editor didn't open the document:", error);
}
});
watch(
() => props.src,
(src) => editor?.open(src),
);
onBeforeUnmount(() => {
gone = true;
editor?.destroy();
});
</script>
<template>
<div ref="container" class="pane"></div>
</template>onMountedcreates the viewer once its element is on the page, andonBeforeUnmountdestroys it.createis asynchronous, so the component can unmount before it resolves.gonerecords that, and the viewer created after it is destroyed at once.- The watcher opens each new
srcin the same viewer.
Use it
The viewer fills its element, so give the element a height, here through its pane class:
vue
<DocumentViewer src="/report.pdf" />css
.pane {
height: 100vh;
}The engine's files
Vite puts the engine's binary and fonts beside your app as it builds it, so there's nothing to set up. Quick start says where else they can come from.
The example
examples/vue-vite shows the viewer and the editor side by side in a Vue app built with Vite.