designtopack/src/stores/dialog.js

159 lines
4.6 KiB
JavaScript
Raw Normal View History

import { defineStore } from 'pinia';
import { ref, computed, watch } from 'vue';
import { useRoute } from 'vue-router';
export const useDialogStore = defineStore('dialog', () => {
const content = ref(null);
2024-11-16 12:05:37 +01:00
const openedFile = ref(null);
2024-12-20 12:36:21 +01:00
const activeTracks = ref(null);
2024-12-19 16:03:20 +01:00
2024-11-16 12:05:37 +01:00
const comments = computed(() => {
2024-12-20 12:36:21 +01:00
if (activeTracks.value?.length > 0) {
return activeTracks.value[0].files.flatMap((file) =>
file.comments ? file.comments : []
);
}
2024-11-16 12:05:37 +01:00
return openedFile.value.comments;
});
2024-11-16 12:05:37 +01:00
function updateFile(newFile) {
2024-11-28 14:45:45 +01:00
openedFile.value = newFile;
2024-11-16 12:05:37 +01:00
}
2024-11-27 17:51:49 +01:00
const route = useRoute();
const isCommentPanelEnabled = ref(true);
2024-11-27 17:51:49 +01:00
const isCommentsOpen = ref(
route.query.hasOwnProperty('comments') ? true : false
2024-11-27 17:51:49 +01:00
);
2025-01-08 12:12:10 +01:00
const openedComment = ref(null);
2024-11-27 17:51:49 +01:00
2024-12-19 16:03:20 +01:00
const draftComment = ref({});
watch(
draftComment,
(newVal) => {
if (draftComment.value.position) showDraftMarker(newVal);
},
{ deep: true }
);
function showDraftMarker(draftComment) {
const bubble = document.createElement('a');
bubble.classList.add('comment-marker');
bubble.classList.add('comment-marker--draft');
bubble.style.left = draftComment.position.x + '%';
bubble.style.top = draftComment.position.y + '%';
bubble.href = '#comment-' + draftComment.id;
const container = draftComment.position.pageIndex
? document.querySelector(
`.vpv-page-inner-container[aria-label="page ${draftComment.position.pageIndex}"] .page-inner-container`
)
: document.querySelector('.track');
container.appendChild(bubble);
}
2024-12-19 16:03:20 +01:00
2024-12-19 17:11:46 +01:00
const isViewerDisabled = ref(false);
watch(isCommentsOpen, (newVal) => {
if (newVal) {
setCommentMarkers();
} else {
removeCommentMarkers();
}
});
watch(openedFile, () => {
removeCommentMarkers();
if (isCommentsOpen.value) {
setCommentMarkers();
}
});
watch(openedFile, (newVal, oldVal) => {
if (!isCommentsOpen.value || !oldVal || newVal.url == oldVal.url) return;
isViewerDisabled.value = true;
setTimeout(() => {
isViewerDisabled.value = false;
removeCommentMarkers();
if (newVal.comments) {
setCommentMarkers();
}
}, 100);
});
2025-01-08 12:12:10 +01:00
2024-12-19 17:11:46 +01:00
function setCommentMarkers() {
if (!comments.value) return;
comments.value.forEach((comment) => {
const correspondingMarker = document.querySelector(
2025-01-28 09:37:33 +01:00
`.comment-marker[href="#comment-${comment.id}"]`
);
console.log(openedFile.value);
if (comment.location.file.uuid !== openedFile.value.uuid) return;
if (comment.type === 'comment-reply') return;
2025-01-08 12:12:10 +01:00
if (correspondingMarker) return;
const bubble = document.createElement('a');
2024-12-19 17:11:46 +01:00
bubble.classList.add('comment-marker');
bubble.style.left = comment.position.x + '%';
bubble.style.top = comment.position.y + '%';
bubble.href = '#comment-' + comment.id;
2024-12-19 17:11:46 +01:00
let container = comment.position?.pageIndex
? document.querySelector(
`.vpv-page-inner-container[aria-label="page ${comment.position.pageIndex}"] .page-inner-container`
)
: document.querySelector('.track');
2024-12-19 17:11:46 +01:00
const timeOut = container ? 0 : 500;
setTimeout(() => {
container = comment.position?.pageIndex
2024-12-19 17:11:46 +01:00
? document.querySelector(
`.vpv-page-inner-container[aria-label="page ${comment.position.pageIndex}"] .page-inner-container`
)
: document.querySelector('.track');
2024-12-19 17:11:46 +01:00
2025-01-08 12:12:10 +01:00
try {
container.appendChild(bubble);
setTimeout(() => {
bubble.addEventListener('mouseenter', () => highlight(comment));
bubble.addEventListener('mouseleave', () => unhighlight(comment));
2025-01-08 12:12:10 +01:00
}, 100);
} catch (error) {
console.error(
2025-01-08 12:12:10 +01:00
`Impossible de monter la bulle du commentaire ${comment.id}. La page n°${comment.position.pageIndex} n'existe pas dans le DOM.`
);
}
2024-12-19 17:11:46 +01:00
}, timeOut);
});
}
function removeCommentMarkers() {
console.log('remove comment markers');
document.querySelectorAll('.comment-marker').forEach((bubble) => {
2024-12-19 17:11:46 +01:00
bubble.parentNode.removeChild(bubble);
});
}
function highlight(comment) {
const target = document.querySelector('#comment-' + comment.id);
target.classList.add('highlight');
2024-12-19 17:11:46 +01:00
}
function unhighlight(comment) {
const target = document.querySelector('#comment-' + comment.id);
target.classList.remove('highlight');
2024-12-19 17:11:46 +01:00
}
2024-12-19 16:03:20 +01:00
return {
content,
2024-12-20 12:36:21 +01:00
activeTracks,
2024-12-19 16:03:20 +01:00
openedFile,
comments,
draftComment,
isCommentsOpen,
isCommentPanelEnabled,
2024-12-19 16:03:20 +01:00
updateFile,
2025-01-08 12:12:10 +01:00
openedComment,
2024-12-19 16:03:20 +01:00
};
});