* 'main' of https://framagit.org/isUnknown/design-to-pack:
  comment 360 working
  create comment working
  create store for comment draft
This commit is contained in:
Timothée Goguely 2024-12-19 17:16:42 +01:00
commit b4be010af7
12 changed files with 230 additions and 194 deletions

View file

@ -1,63 +1,4 @@
Comments:
-
location:
page:
uri: >
projects/miss-dior-blooming-bouquet/proposal
title: Offre commerciale
project:
title: Miss Dior Blooming Bouquet
uri: projects/miss-dior-blooming-bouquet
dialogUri: '/projects/miss-dior-blooming-bouquet?dialog=proposal&fileIndex=0'
file:
uuid: file://3vTh1tMFeFM2JxaN
url: >
http://localhost:8888/media/pages/projects/miss-dior-blooming-bouquet/proposal/788ddebfe3-1731941917/des-textos-revelent-comment-bfm-sest-mise-au-service-de-sarkozy-le-boss.pdf
position:
pageIndex: 1
x: "57.105131038425"
y: "13.127413127413"
text: un commentaire
author:
name: Adrien Payet
email: adrien.payet@outlook.com
uuid: user://WWjXgPWk
role: admin
date: 2024-12-19T10:26:05+01:00
id: e16a5304-330b-4460-acbd-a6e471f4cda2
type: comment
replies:
-
location:
page:
uri: >
projects/miss-dior-blooming-bouquet/proposal
title: Offre commerciale
project:
title: Miss Dior Blooming Bouquet
uri: projects/miss-dior-blooming-bouquet
dialogUri: '/projects/miss-dior-blooming-bouquet?dialog=proposal&fileIndex=0'
file:
uuid: file://3vTh1tMFeFM2JxaN
url: >
http://localhost:8888/media/pages/projects/miss-dior-blooming-bouquet/proposal/788ddebfe3-1731941917/des-textos-revelent-comment-bfm-sest-mise-au-service-de-sarkozy-le-boss.pdf
parent:
author:
name: Adrien Payet
email: adrien.payet@outlook.com
id: e16a5304-330b-4460-acbd-a6e471f4cda2
position:
pageIndex: 1
text: une réponse
author:
name: Adrien Payet
email: adrien.payet@outlook.com
uuid: user://WWjXgPWk
role: admin
date: 2024-12-19T10:26:09+01:00
id: bc64e066-299d-4d80-a651-2c17482cda2f
type: comment-reply
Comments:
----

View file

@ -1 +1,34 @@
Comments:
-
location:
page:
uri: >
projects/miss-dior-blooming-bouquet/virtual-sample/piste-1
title: Piste 1
project:
title: Miss Dior Blooming Bouquet
uri: projects/miss-dior-blooming-bouquet
dialogUri: >
/projects/miss-dior-blooming-bouquet?dialog=virtual-sample
file:
uuid: file://J9q2ceZL2aW19oEU
url: >
http://localhost:8888/media/pages/projects/miss-dior-blooming-bouquet/virtual-sample/piste-1/f26b2c223b-1733765560/0_6.png
position:
x: '50.119047619048'
y: '42.563482466747'
text: test
author:
name: Adrien Payet
email: adrien.payet@outlook.com
uuid: user://WWjXgPWk
role: admin
date: 2024-12-19T16:49:15+01:00
id: 4b311fe6-a6e4-486f-a9ce-b8820a234e2e
type: comment
replies: [ ]
----
Uuid: J9q2ceZL2aW19oEU

View file

@ -1,5 +1,5 @@
Uuid: WrFkfiMVZuxwdYad
Comments:
----
Comments:
Uuid: WrFkfiMVZuxwdYad

View file

@ -1,7 +1,15 @@
Comments:
----
Cover:
----
Label:
----
Uuid: 7Bqr3iCH8ltDrdGi
----

View file

@ -0,0 +1,3 @@
fields:
comments:
type: hidden

View file

@ -4,13 +4,15 @@ namespace adrienpayet\D2P\data;
class Position
{
public int $pageIndex;
public ?int $pageIndex = null;
public ?float $x = null;
public ?float $y = null;
public function __construct(array $data)
{
$this->pageIndex = $data['pageIndex'];
if (isset($data["pageIndex"])) {
$this->pageIndex = $data['pageIndex'];
}
if (isset($data["x"])) {
$this->x = (float) $data['x'];
$this->y = (float) $data['y'];
@ -18,9 +20,11 @@ class Position
}
public function toArray() {
$array = [
"pageIndex" => $this->pageIndex,
];
$array = [];
if ($this->pageIndex) {
$array["pageIndex"] = $this->pageIndex;
}
if ($this->x) {
$array["x"] = $this->x;

View file

@ -9,17 +9,11 @@ return [
$json = file_get_contents('php://input');
$data = json_decode($json);
$parsedUrl = parse_url($data->dialogUri);
$query = $parsedUrl['query'] ?? null;
parse_str($query, $queryParams);
$stepSlug = $queryParams['dialog'] ?? null;
$targetPageUri = $stepSlug ? $parsedUrl['path'] . '/' . $stepSlug : $parsedUrl['path'];
$project = page($parsedUrl['path']);
$page = page($targetPageUri);
$page = page($data->fileParentUri);
$project = $page->parent()->template() == "project" ? $page->parent() : $page->parent()->parent();
$file = $page->file($data->fileName);
$user = kirby()->user($data->userUuid);
$comments = $file->comments()->isEmpty() == true ? [] : Yaml::decode($file->comments()->value());
@ -31,7 +25,6 @@ return [
"file" => $file
],
"position" => [
"pageIndex" => $data->position->pageIndex,
"x" => $data->position->x,
"y" => $data->position->y
],
@ -42,9 +35,13 @@ return [
"type" => "comment",
];
if (isset($data->position->pageIndex)) {
$commentData["position"]["pageIndex"] = $data->position->pageIndex;
}
$newComment = new Comment($commentData);
$comments[] = $newComment->toArray();
$comments[] = $newComment->toArray();
$newFile = $file->update([
'comments' => $comments
@ -55,9 +52,7 @@ return [
try {
$user->sendNotification($project, $commentData);
} catch (\Throwable $th) {
echo json_encode([
"error" => $th->getMessage() . " in " . $th->getFile() . " line " . $th->getLine(),
]);
throw new Exception($th->getMessage(), 1);
}
exit;

View file

@ -19,7 +19,7 @@ function getFileData($file) {
$data['tags'] = $file->tags()->split();
};
if($file->extension() == "pdf") {
if($file->comments()->exists()) {
$data['comments'] = $file->comments()->exists() && $file->comments() ? Data::decode($file->comments()->value(), 'yaml') : [];
$data['cover'] = $file->cover()->exists() && $file->cover()->isNotEmpty() ? $file->cover()->toFile()->resize(576)->url() : false;
}

View file

@ -118,15 +118,12 @@ import { useRoute } from "vue-router";
dayjs.locale("fr");
const emits = defineEmits(["show-draft-marker"]);
const { user } = useUserStore();
const { page } = usePageStore();
const dialog = useDialogStore();
const { comments, openedFile } = storeToRefs(useDialogStore());
const { comments, openedFile, draftComment } = storeToRefs(useDialogStore());
const api = useApiStore();
const draftComment = ref({});
const openedComment = ref(null);
const isAddOpen = ref(false);
@ -150,17 +147,10 @@ watch(isAddOpen, (newVal) => {
}
});
watch(
draftComment,
(newVal) => {
if (newVal.position) {
emits("show-draft-marker", newVal);
}
},
{ deep: true }
);
const viewContainer = document.querySelector(".vpv-pages-inner-container");
const viewContainer =
openedFile.value.type === "document"
? document.querySelector(".vpv-pages-inner-container")
: document.querySelector(".track");
window.addEventListener("keydown", (event) => {
if (
@ -171,6 +161,7 @@ window.addEventListener("keydown", (event) => {
handleSubmit();
}
});
// Functions
function handleSubmit(event = null) {
if (event) {
@ -211,6 +202,10 @@ async function replyComment(newComment) {
}
async function addComment(newComment) {
const matchFileParentUri = openedFile.value.url.match(
/projects\/.*?(?=\/[^/]+\/[^/]+$)/
);
newComment.fileParentUri = matchFileParentUri ? matchFileParentUri[0] : null;
const newFile = await api.addComment(newComment);
resetDraftComment();
isAddOpen.value = false;
@ -240,8 +235,18 @@ function toggleCommentPositionMode(enable) {
}
function handleCommentPositionClick(event) {
if (openedFile.value.type === "document") {
prepareDraftCommentInPdf(event);
} else {
prepareDraftCommentInImage(event);
}
isAddOpen.value = true;
toggleCommentPositionMode(false);
}
function prepareDraftCommentInPdf(event) {
const pageContainer = event.target.closest(".page-inner-container");
if (!pageContainer) return;
if (!pageContainer || !viewContainer) return;
const pageLabel = pageContainer
.closest(".vpv-page-inner-container")
@ -259,13 +264,30 @@ function handleCommentPositionClick(event) {
const relativeX = (x / pageRect.width) * 100;
const relativeY = (y / pageRect.height) * 100;
console.log(pageIndex);
draftComment.value.position = {
x: relativeX,
y: relativeY,
pageIndex: parseInt(pageIndex),
};
isAddOpen.value = true;
toggleCommentPositionMode(false);
}
function prepareDraftCommentInImage(event) {
if (!viewContainer) return;
const imageRect = viewContainer.getBoundingClientRect();
const mouseTop = event.clientY;
const mouseLeft = event.clientX;
const relativeX = ((mouseLeft - imageRect.left) / imageRect.width) * 100;
const relativeY = ((mouseTop - imageRect.top) / imageRect.height) * 100;
draftComment.value.position = {
x: relativeX,
y: relativeY,
};
}
function openComment(comment) {
@ -366,6 +388,8 @@ function openComment(comment) {
flex-grow: 1;
}
.track.waiting-comment,
.track.waiting-comment .drag-zone,
.vpv-pages-inner-container.waiting-comment .page-inner-container,
.vpv-pages-inner-container.waiting-comment .vpv-text-layer-text,
.vpv-pages-inner-container.waiting-comment .vpv-text-layer-wrapper {

View file

@ -18,7 +18,7 @@
>{{ isCommentsOpen ? "Masquer" : "Afficher" }} les commentaires</span
>
</button>
<Comments v-if="isCommentsOpen" @show-draft-marker="showDraftMarker" />
<Comments v-if="isCommentsOpen" />
</template>
<script setup>
@ -32,38 +32,9 @@ import { useRoute, useRouter } from "vue-router";
const licenseKey = import.meta.env.VITE_VPV_LICENSE ?? 'd0ab730a-ebba-4060-856c-76d322565645';
useLicense({ licenseKey });
const { openedFile, isCommentsOpen, comments } = storeToRefs(useDialogStore());
const draftComment = ref(null);
const isViewerDisabled = ref(false);
watch(openedFile, () => {
removeCommentMarkers();
setCommentMarkers();
});
watch(isCommentsOpen, (newVal) => {
if (newVal) {
setCommentMarkers();
} else {
removeCommentMarkers();
}
});
watch(openedFile, (newVal, oldVal) => {
if (newVal.url === oldVal.url) return;
isViewerDisabled.value = true;
setTimeout(() => {
isViewerDisabled.value = false;
removeCommentMarkers();
if (newVal.comments) {
setCommentMarkers();
}
}, 100);
});
const { openedFile, isCommentsOpen, comments, draftComment } = storeToRefs(
useDialogStore()
);
const currentPageIndex = ref(1);
@ -107,60 +78,6 @@ const onPdfLoaded = () => {
observePages();
};
function setCommentMarkers() {
if (!comments.value) return;
comments.value.forEach((comment) => {
const bubble = document.createElement("a");
bubble.classList.add("comment-marker");
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);
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");
}
function removeCommentMarkers() {
document.querySelectorAll(".comment-marker").forEach((bubble) => {
bubble.parentNode.removeChild(bubble);
});
}
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 = document.querySelector(
`.vpv-page-inner-container[aria-label="page ${draftComment.position.pageIndex}"] .page-inner-container`
);
container.appendChild(bubble);
}
</script>
<style>

View file

@ -24,7 +24,7 @@
<span class="label">Verre parachevé</span>
</button>
</header>
<div class="dialog__inner" id="verre-brut">
<div id="vpv-container" class="dialog__inner">
<PdfViewer />
</div>
</template>

View file

@ -1,21 +1,23 @@
import { defineStore } from "pinia";
import { ref, computed } from "vue";
import { ref, computed, watch } from "vue";
import { useRoute } from "vue-router";
export const useDialogStore = defineStore("dialog", () => {
const content = ref(null);
const openedFile = ref(null);
const comments = computed(() => {
return openedFile.value.comments;
});
function updateFile(newFile) {
console.log(newFile);
openedFile.value = newFile;
if (!content.value.files) return;
content.value.files = content.value.files.map((file) =>
file.id === newFile.id ? newFile : file
);
// if (!content.value.files) return;
// content.value.files = content.value.files.map((file) =>
// file.id === newFile.id ? newFile : file
// );
}
const route = useRoute();
@ -23,5 +25,114 @@ export const useDialogStore = defineStore("dialog", () => {
route.query.hasOwnProperty("comments") ? true : false
);
return { content, openedFile, comments, isCommentsOpen, updateFile };
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);
}
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);
});
function setCommentMarkers() {
if (!comments.value) return;
comments.value.forEach((comment) => {
const bubble = document.createElement("a");
bubble.classList.add("comment-marker");
bubble.style.left = comment.position.x + "%";
bubble.style.top = comment.position.y + "%";
bubble.href = "#comment-" + comment.id;
let container = comment.position?.pageIndex
? document.querySelector(
`.vpv-page-inner-container[aria-label="page ${comment.position.pageIndex}"] .page-inner-container`
)
: document.querySelector(".track");
const timeOut = container ? 0 : 500;
setTimeout(() => {
container = comment.position?.pageIndex
? document.querySelector(
`.vpv-page-inner-container[aria-label="page ${comment.position.pageIndex}"] .page-inner-container`
)
: document.querySelector(".track");
container.appendChild(bubble);
setTimeout(() => {
bubble.addEventListener("mouseenter", () => highlight(comment));
bubble.addEventListener("mouseleave", () => unhighlight(comment));
}, 100);
}, timeOut);
});
}
function removeCommentMarkers() {
document.querySelectorAll(".comment-marker").forEach((bubble) => {
bubble.parentNode.removeChild(bubble);
});
}
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");
}
return {
content,
openedFile,
comments,
draftComment,
isCommentsOpen,
updateFile,
};
});