designtopack/src/components/project/brief/PdfViewer.vue

225 lines
5.7 KiB
Vue
Raw Normal View History

<template>
<Dialog
id="add-pdf"
v-model:visible="isOpen"
modal
:draggable="false"
header="Titre du PDF"
class="dialog"
2024-10-23 16:29:41 +02:00
:class="{ 'with-comments': isCommentsOpen }"
:closeOnEscape="true"
>
<template #header>
<button
v-if="
dialog.content.id === 'clientBrief' &&
dialog.content.isValidated !== true
"
class="btn"
@click="validate()"
>
Valider et envoyer le brief
</button>
<h2
class="font-serif text-lg"
:title="openedFile?.label.length ? openedFile.label : openedFile.name"
>
2024-11-19 17:13:18 +01:00
{{ openedFile?.label.length ? openedFile.label : openedFile.name }}
</h2>
</template>
<div id="vpv-container">
<VPdfViewer
2024-10-16 11:19:49 +02:00
:darkMode="true"
:initialThumbnailsVisible="true"
2024-11-16 12:16:03 +01:00
:src="openedFile.url"
local="fr_FR"
@loaded="onPdfLoaded"
/>
2024-10-23 09:48:27 +02:00
<button
id="toggle-comments"
:aria-pressed="isCommentsOpen"
class="btn btn--transparent btn--outline"
2024-10-23 09:48:27 +02:00
data-icon="comment"
@click="isCommentsOpen = !isCommentsOpen"
>
<span class="sr-only">Afficher les commentaires</span>
</button>
2024-11-19 17:01:58 +01:00
<Comments v-if="isCommentsOpen" @show-draft-marker="showDraftMarker" />
</div>
</Dialog>
</template>
<script setup>
import Dialog from "primevue/dialog";
2024-10-23 09:48:27 +02:00
import Comments from "../../comments/Comments.vue";
import { VPdfViewer, useLicense } from "@vue-pdf-viewer/viewer";
import { ref, watch } from "vue";
import { useDialogStore } from "../../../stores/dialog";
import { useRoute, useRouter } from "vue-router";
2024-11-16 12:16:03 +01:00
import { storeToRefs } from "pinia";
import { useApiStore } from "../../../stores/api";
2024-11-18 16:21:07 +01:00
const router = useRouter();
const route = useRoute();
2024-11-16 12:05:37 +01:00
const dialog = useDialogStore();
const api = useApiStore();
2024-11-16 12:16:03 +01:00
const { openedFile, comments } = storeToRefs(useDialogStore());
2024-11-18 16:21:07 +01:00
openedFile.value = route.query.fileIndex
? dialog.content.files[route.query.fileIndex]
: dialog.content.files[0];
const licenseKey = import.meta.env.VITE_VPV_LICENSE;
useLicense({ licenseKey });
2024-11-19 16:53:31 +01:00
const draftComment = ref(null);
const isOpen = ref(true);
watch(isOpen, (newValue) => {
router.push({ name: route.name });
});
console.log(route.query);
const isCommentsOpen = ref(
route.query.hasOwnProperty("comments") ? true : false
);
2024-10-23 11:32:51 +02:00
const currentPageIndex = ref(1);
watch(isCommentsOpen, (newVal) => {
if (newVal) {
2024-11-19 16:59:20 +01:00
setCommentMarkers();
2024-11-11 17:12:26 +01:00
} else {
2024-11-19 16:59:20 +01:00
removeCommentMarkers();
}
});
2024-11-16 12:16:03 +01:00
watch(openedFile, (newVal) => {
2024-11-19 16:59:20 +01:00
removeCommentMarkers();
2024-11-16 12:16:03 +01:00
if (newVal.comments) {
2024-11-19 16:59:20 +01:00
setCommentMarkers();
2024-11-11 17:12:26 +01:00
}
2024-11-16 12:16:03 +01:00
});
2024-11-11 17:12:26 +01:00
2024-10-23 09:48:27 +02:00
// Functions
const onPdfLoaded = () => {
const wrapper = document.querySelector(".vpv-pages-inner-container");
const observer = new IntersectionObserver(
(entries) => {
entries.forEach((entry, index) => {
if (entry.intersectionRatio > 0.5) {
2024-10-23 11:32:51 +02:00
currentPageIndex.value = parseInt(
2024-10-23 09:48:27 +02:00
entry.target.getAttribute("aria-label").split(" ")[1]
);
}
});
},
{
root: wrapper,
threshold: [0.5],
}
);
const observePages = () => {
const pages = document.querySelectorAll(".vpv-page-inner-container");
pages.forEach((page) => {
if (!page.__observed__) {
observer.observe(page);
page.__observed__ = true;
}
});
};
const mutationObserver = new MutationObserver(() => {
observePages();
});
if (wrapper) {
mutationObserver.observe(wrapper, { childList: true, subtree: true });
}
observePages();
};
2024-11-19 16:59:20 +01:00
function setCommentMarkers() {
2024-11-16 12:16:03 +01:00
if (!comments.value) return;
comments.value.forEach((comment) => {
2024-11-11 17:12:26 +01:00
const bubble = document.createElement("a");
2024-11-19 16:31:38 +01:00
bubble.classList.add("comment-marker");
2024-11-11 17:12:26 +01:00
bubble.style.left = comment.position.x + "%";
bubble.style.top = comment.position.y + "%";
bubble.href = "#comment-" + comment.id;
const container = document.querySelector(
`.vpv-page-inner-container[aria-label="page ${comment.position.pageIndex}"] .page-inner-container`
);
container.appendChild(bubble);
2024-11-11 17:12:26 +01:00
setTimeout(() => {
bubble.addEventListener("mouseenter", () => highlight(comment));
bubble.addEventListener("mouseleave", () => unhighlight(comment));
}, 100);
});
}
function highlight(comment) {
const target = document.querySelector("#comment-" + comment.id);
target.classList.add("highlight");
}
function unhighlight(comment) {
const target = document.querySelector("#comment-" + comment.id);
target.classList.remove("highlight");
}
2024-11-19 16:59:20 +01:00
function removeCommentMarkers() {
2024-11-19 16:31:38 +01:00
document.querySelectorAll(".comment-marker").forEach((bubble) => {
2024-11-11 17:12:26 +01:00
bubble.parentNode.removeChild(bubble);
});
}
2024-11-19 16:53:31 +01:00
2024-11-19 17:01:58 +01:00
function showDraftMarker(draftComment) {
2024-11-19 16:53:31 +01:00
const bubble = document.createElement("a");
2024-11-19 16:59:20 +01:00
bubble.classList.add("comment-marker");
bubble.classList.add("comment-marker--draft");
2024-11-19 16:53:31 +01:00
bubble.style.left = draftComment.position.x + "%";
bubble.style.top = draftComment.position.y + "%";
bubble.href = "#comment-" + draftComment.id;
const container = document.querySelector(
2024-11-19 16:59:20 +01:00
`.vpv-page-inner-container[aria-label="page ${draftComment.position.pageIndex}"] .page-inner-container`
2024-11-19 16:53:31 +01:00
);
container.appendChild(bubble);
}
async function validate() {
const response = await api.validateBrief(
route.path + "/client-brief",
route.fullPath
);
if (response.success) {
dialog.content.isValidated = true;
} else {
console.alert(response);
}
}
</script>
<style>
#vpv-container {
width: var(--dialog-max-w);
height: calc(var(--dialog-max-h) - var(--dialog-header-h));
background: black;
position: relative;
}
.with-comments .vpv-pages-container-wrapper {
margin-right: var(--dialog-comments-w, 20rem);
}
</style>
<style scoped>
@import "../../../assets/css/src/2.blocks.pdf-viewer.css";
</style>