designtopack/src/components/project/PdfViewer.vue

152 lines
3.9 KiB
Vue
Raw Normal View History

<template>
2024-11-27 17:51:49 +01:00
<VPdfViewer
:darkMode="true"
:initialThumbnailsVisible="true"
:src="openedFile.url"
local="fr_FR"
@loaded="onPdfLoaded"
/>
<button
id="toggle-comments"
:aria-pressed="isCommentsOpen"
class="btn btn--transparent btn--outline"
data-icon="comment"
@click="isCommentsOpen = !isCommentsOpen"
>
2024-11-27 17:51:49 +01:00
<span class="sr-only">Afficher les commentaires</span>
</button>
<Comments v-if="isCommentsOpen" @show-draft-marker="showDraftMarker" />
</template>
<script setup>
2024-11-27 17:51:49 +01:00
import Comments from "../comments/Comments.vue";
import { ref, watch } from "vue";
2024-11-27 17:51:49 +01:00
import { useDialogStore } from "../../stores/dialog";
2024-11-16 12:16:03 +01:00
import { storeToRefs } from "pinia";
2024-11-27 17:51:49 +01:00
import { VPdfViewer, useLicense } from "@vue-pdf-viewer/viewer";
import { useRoute, useRouter } from "vue-router";
const licenseKey = import.meta.env.VITE_VPV_LICENSE;
useLicense({ licenseKey });
2024-11-27 17:51:49 +01:00
const { openedFile, isCommentsOpen, comments } = storeToRefs(useDialogStore());
2024-11-19 16:53:31 +01:00
const draftComment = ref(null);
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-11-27 17:51:49 +01:00
const currentPageIndex = ref(1);
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);
}
</script>
<style scoped>
2024-11-27 17:51:49 +01:00
@import "../../assets/css/src/2.blocks.pdf-viewer.css";
</style>