Align BIM comments with OPS detail card

This commit is contained in:
CODEX
2026-06-20 17:21:07 +03:00
parent 684f85b382
commit d70f6e0886
4 changed files with 800 additions and 192 deletions
+196 -24
View File
@@ -57,11 +57,16 @@ const commentModal = document.getElementById("commentModal");
const commentModalHeader = document.getElementById("commentModalHeader");
const commentModalClose = document.getElementById("commentModalClose");
const commentModalSubtitle = document.getElementById("commentModalSubtitle");
const commentModalEditedMeta = document.getElementById("commentModalEditedMeta");
const commentTitleInput = document.getElementById("commentTitleInput");
const commentBodyInput = document.getElementById("commentBodyInput");
const commentDropzone = document.getElementById("commentDropzone");
const commentAttachButton = document.getElementById("commentAttachButton");
const commentAttachmentInput = document.getElementById("commentAttachmentInput");
const commentAttachmentGrid = document.getElementById("commentAttachmentGrid");
const commentSubitemsSection = document.getElementById("commentSubitemsSection");
const commentSubitemsList = document.getElementById("commentSubitemsList");
const commentAddSubitemButton = document.getElementById("commentAddSubitemButton");
const commentThreadList = document.getElementById("commentThreadList");
const commentReplyInput = document.getElementById("commentReplyInput");
const commentReplyAttachButton = document.getElementById("commentReplyAttachButton");
@@ -70,6 +75,7 @@ const commentReplyAttachmentGrid = document.getElementById("commentReplyAttachme
const commentModalError = document.getElementById("commentModalError");
const commentCancelButton = document.getElementById("commentCancelButton");
const commentApplyButton = document.getElementById("commentApplyButton");
const commentApplyInlineButton = document.getElementById("commentApplyInlineButton");
const treeContainer = document.getElementById("treeContainer");
const displayModeControl = document.getElementById("displayModeControl");
const displayModeSelectHost = document.getElementById("displayModeSelect");
@@ -297,6 +303,7 @@ let activeCommentId = null;
let hoverCommentId = null;
let commentDraftAttachments = [];
let commentReplyAttachments = [];
let commentDraftSubitems = [];
let commentTargetRaf = 0;
let commentLongPressTimer = null;
let commentLongPressStart = null;
@@ -890,8 +897,16 @@ const openCommentContextMenuFromEvent = (event) => {
return true;
};
const readImageFiles = async (fileList) => {
const files = Array.from(fileList || []).filter((file) => file?.type?.startsWith("image/")).slice(0, 8);
const COMMENT_FILE_LIMIT_BYTES = 50 * 1024 * 1024;
const readAttachmentFiles = async (fileList) => {
const files = Array.from(fileList || [])
.filter((file) => file && file.size <= COMMENT_FILE_LIMIT_BYTES)
.slice(0, 8);
const skipped = Array.from(fileList || []).filter((file) => file && file.size > COMMENT_FILE_LIMIT_BYTES).length;
if (skipped && commentModalError) {
commentModalError.textContent = `Файлы больше 50 МБ не добавлены: ${skipped}`;
}
const readOne = (file) => new Promise((resolve) => {
const reader = new FileReader();
reader.onload = () => resolve({
@@ -908,21 +923,51 @@ const readImageFiles = async (fileList) => {
return items.filter(Boolean);
};
const formatFileSize = (value) => {
const size = Number(value);
if (!Number.isFinite(size) || size <= 0) return "";
if (size >= 1024 * 1024) return `${(size / (1024 * 1024)).toFixed(size >= 10 * 1024 * 1024 ? 0 : 1)} МБ`;
if (size >= 1024) return `${Math.round(size / 1024)} КБ`;
return `${Math.round(size)} Б`;
};
const renderAttachmentGrid = (grid, attachments, onRemove) => {
if (!grid) return;
grid.replaceChildren();
(attachments || []).forEach((attachment, index) => {
const thumb = document.createElement("div");
thumb.className = "comment-attachment-thumb";
const img = document.createElement("img");
img.src = attachment.dataUrl;
img.alt = attachment.name || "attachment";
const isImage = String(attachment.type || "").startsWith("image/") || /^data:image\//i.test(String(attachment.dataUrl || ""));
if (isImage) {
const img = document.createElement("img");
img.src = attachment.dataUrl;
img.alt = attachment.name || "attachment";
thumb.appendChild(img);
} else {
const file = document.createElement("div");
file.className = "comment-attachment-file";
file.innerHTML = `
<svg viewBox="0 0 24 24" aria-hidden="true">
<path d="M14 2H7a2 2 0 0 0-2 2v16a2 2 0 0 0 2 2h10a2 2 0 0 0 2-2V7Z"></path>
<path d="M14 2v5h5"></path>
</svg>
`;
const meta = document.createElement("div");
meta.className = "comment-attachment-file__meta";
const name = document.createElement("strong");
name.textContent = attachment.name || "Файл";
const size = document.createElement("span");
size.textContent = formatFileSize(attachment.size) || attachment.type || "Файл";
meta.append(name, size);
file.appendChild(meta);
thumb.appendChild(file);
}
const remove = document.createElement("button");
remove.type = "button";
remove.setAttribute("aria-label", "Удалить картинку");
remove.setAttribute("aria-label", "Удалить файл");
remove.textContent = "×";
remove.addEventListener("click", () => onRemove?.(index));
thumb.append(img, remove);
thumb.appendChild(remove);
grid.appendChild(thumb);
});
};
@@ -934,10 +979,31 @@ const renderThreadAttachments = (container, attachments) => {
attachments.forEach((attachment) => {
const thumb = document.createElement("div");
thumb.className = "comment-attachment-thumb";
const img = document.createElement("img");
img.src = attachment.dataUrl;
img.alt = attachment.name || "attachment";
thumb.appendChild(img);
const isImage = String(attachment.type || "").startsWith("image/") || /^data:image\//i.test(String(attachment.dataUrl || ""));
if (isImage) {
const img = document.createElement("img");
img.src = attachment.dataUrl;
img.alt = attachment.name || "attachment";
thumb.appendChild(img);
} else {
const file = document.createElement("div");
file.className = "comment-attachment-file";
file.innerHTML = `
<svg viewBox="0 0 24 24" aria-hidden="true">
<path d="M14 2H7a2 2 0 0 0-2 2v16a2 2 0 0 0 2 2h10a2 2 0 0 0 2-2V7Z"></path>
<path d="M14 2v5h5"></path>
</svg>
`;
const meta = document.createElement("div");
meta.className = "comment-attachment-file__meta";
const name = document.createElement("strong");
name.textContent = attachment.name || "Файл";
const size = document.createElement("span");
size.textContent = formatFileSize(attachment.size) || attachment.type || "Файл";
meta.append(name, size);
file.appendChild(meta);
thumb.appendChild(file);
}
grid.appendChild(thumb);
});
container.appendChild(grid);
@@ -976,7 +1042,9 @@ const renderCommentThread = (comment) => {
const resetCommentModalAttachments = () => {
commentDraftAttachments = [];
commentReplyAttachments = [];
commentDraftSubitems = [];
renderModalAttachmentInputs();
renderCommentSubitems();
};
const renderModalAttachmentInputs = () => {
@@ -990,6 +1058,40 @@ const renderModalAttachmentInputs = () => {
});
};
const renderCommentSubitems = () => {
if (!commentSubitemsList || !commentSubitemsSection) return;
commentSubitemsList.replaceChildren();
const subitems = Array.isArray(commentDraftSubitems) ? commentDraftSubitems : [];
commentSubitemsSection.classList.toggle("hidden", !subitems.length);
subitems.forEach((subitem, index) => {
const row = document.createElement("label");
row.className = "ops-comment-subitem";
const checkbox = document.createElement("input");
checkbox.type = "checkbox";
checkbox.checked = !!subitem.done;
checkbox.addEventListener("change", () => {
commentDraftSubitems[index] = { ...commentDraftSubitems[index], done: checkbox.checked };
});
const input = document.createElement("input");
input.type = "text";
input.value = subitem.title || "";
input.placeholder = "Новый подэлемент";
input.addEventListener("input", () => {
commentDraftSubitems[index] = { ...commentDraftSubitems[index], title: input.value };
});
const remove = document.createElement("button");
remove.type = "button";
remove.setAttribute("aria-label", "Удалить подэлемент");
remove.textContent = "×";
remove.addEventListener("click", () => {
commentDraftSubitems.splice(index, 1);
renderCommentSubitems();
});
row.append(checkbox, input, remove);
commentSubitemsList.appendChild(row);
});
};
const openCommentModalForTarget = () => {
if (!commentContextTarget || !commentModal) return;
commentsModeActive = true;
@@ -1001,8 +1103,13 @@ const openCommentModalForTarget = () => {
commentBodyInput && (commentBodyInput.value = "");
commentReplyInput && (commentReplyInput.value = "");
if (commentModalSubtitle) {
commentModalSubtitle.textContent = "BIM-COMMENT";
}
if (commentModalEditedMeta) {
const [x, y, z] = commentContextTarget.worldPos || [];
commentModalSubtitle.textContent = Number.isFinite(x) ? `Точка: ${x.toFixed(2)}, ${y.toFixed(2)}, ${z.toFixed(2)}` : "Новый таргет";
commentModalEditedMeta.textContent = Number.isFinite(x)
? `Последнее редактирование: новая точка ${x.toFixed(2)}, ${y.toFixed(2)}, ${z.toFixed(2)}`
: "Последнее редактирование: новый комментарий";
}
if (commentModalError) commentModalError.textContent = "";
resetCommentModalAttachments();
@@ -1024,10 +1131,17 @@ const openCommentModalForComment = (commentId) => {
commentBodyInput && (commentBodyInput.value = comment.body || "");
commentReplyInput && (commentReplyInput.value = "");
if (commentModalSubtitle) {
commentModalSubtitle.textContent = `Создан: ${formatDateTime(comment.createdAt)} · Изменён: ${formatDateTime(comment.updatedAt)}`;
commentModalSubtitle.textContent = comment.id || "BIM-COMMENT";
}
if (commentModalEditedMeta) {
commentModalEditedMeta.textContent = `Последнее редактирование: ${formatDateTime(comment.updatedAt || comment.createdAt)}`;
}
if (commentModalError) commentModalError.textContent = "";
resetCommentModalAttachments();
commentDraftSubitems = Array.isArray(comment.subitems)
? comment.subitems.map((item) => ({ ...item }))
: [];
renderCommentSubitems();
renderCommentThread(comment);
commentModalBackdrop?.classList.remove("hidden");
commentModal.classList.remove("hidden");
@@ -1049,39 +1163,58 @@ const closeCommentModal = () => {
renderCommentTargets();
};
const buildCommentPayload = () => ({
...(commentContextTarget || {}),
title: commentTitleInput?.value?.trim() || "",
body: commentBodyInput?.value?.trim() || "",
attachments: commentDraftAttachments,
});
const normalizedSubitemsDraft = () => (
(commentDraftSubitems || [])
.map((item) => ({
id: item.id || `sub_${Date.now()}_${Math.random().toString(36).slice(2)}`,
title: String(item.title || "").trim(),
done: !!item.done,
}))
.filter((item) => item.title)
);
const buildCommentPayload = ({ title, body, replyBody } = {}) => {
const finalBody = body || replyBody || "";
return {
...(commentContextTarget || {}),
title: title || (finalBody ? finalBody.slice(0, 80) : "Комментарий"),
body: finalBody,
attachments: [...commentDraftAttachments, ...commentReplyAttachments].slice(0, 8),
subitems: normalizedSubitemsDraft(),
};
};
const applyCommentModal = async () => {
if (isGuestMode()) return;
const title = commentTitleInput?.value?.trim() || "";
const body = commentBodyInput?.value?.trim() || "";
const replyBody = commentReplyInput?.value?.trim() || "";
if (!title && !body && commentModalMode === "create") {
if (commentModalError) commentModalError.textContent = "Заполните тему или описание";
if (!title && !body && !replyBody && !commentDraftAttachments.length && !commentReplyAttachments.length && !commentDraftSubitems.length && commentModalMode === "create") {
if (commentModalError) commentModalError.textContent = "Добавьте заголовок, описание, комментарий или файл";
return;
}
if (commentApplyButton) {
commentApplyButton.disabled = true;
commentApplyButton.textContent = "Сохранение...";
}
if (commentApplyInlineButton) {
commentApplyInlineButton.disabled = true;
}
try {
let payload = null;
if (commentModalMode === "create") {
payload = await fetchJSON(COMMENTS_API, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(buildCommentPayload()),
body: JSON.stringify(buildCommentPayload({ title, body, replyBody })),
});
} else if (commentModalCommentId) {
const original = commentRecords.find((item) => item.id === commentModalCommentId);
const subitems = normalizedSubitemsDraft();
const patchPayload = {};
if (!original || title !== (original.title || "")) patchPayload.title = title;
if (!original || body !== (original.body || "")) patchPayload.body = body;
if (JSON.stringify(subitems) !== JSON.stringify(original?.subitems || [])) patchPayload.subitems = subitems;
if (replyBody || commentReplyAttachments.length) {
patchPayload.message = {
body: replyBody,
@@ -1117,6 +1250,9 @@ const applyCommentModal = async () => {
commentApplyButton.disabled = false;
commentApplyButton.textContent = "Применить";
}
if (commentApplyInlineButton) {
commentApplyInlineButton.disabled = false;
}
}
};
@@ -4941,23 +5077,59 @@ const loadProjectSnapshot = async (project) => {
commentModalClose?.addEventListener("click", closeCommentModal);
commentCancelButton?.addEventListener("click", closeCommentModal);
commentApplyButton?.addEventListener("click", applyCommentModal);
commentApplyInlineButton?.addEventListener("click", applyCommentModal);
commentModalBackdrop?.addEventListener("click", (event) => {
if (event.target === commentModalBackdrop) closeCommentModal();
});
commentAttachButton?.addEventListener("click", () => commentAttachmentInput?.click());
commentReplyAttachButton?.addEventListener("click", () => commentReplyAttachmentInput?.click());
commentAddSubitemButton?.addEventListener("click", () => {
commentDraftSubitems.push({
id: `sub_${Date.now()}_${Math.random().toString(36).slice(2)}`,
title: "",
done: false,
});
renderCommentSubitems();
const inputs = commentSubitemsList?.querySelectorAll?.(".ops-comment-subitem input[type='text']");
inputs?.[inputs.length - 1]?.focus?.();
});
commentAttachmentInput?.addEventListener("change", async (event) => {
const files = await readImageFiles(event.target.files);
const files = await readAttachmentFiles(event.target.files);
commentDraftAttachments = [...commentDraftAttachments, ...files].slice(0, 8);
event.target.value = "";
renderModalAttachmentInputs();
});
commentReplyAttachmentInput?.addEventListener("change", async (event) => {
const files = await readImageFiles(event.target.files);
const files = await readAttachmentFiles(event.target.files);
commentReplyAttachments = [...commentReplyAttachments, ...files].slice(0, 8);
event.target.value = "";
renderModalAttachmentInputs();
});
if (commentDropzone) {
commentDropzone.addEventListener("click", (event) => {
if (event.target?.closest?.("button,input,.comment-attachment-thumb")) return;
commentAttachmentInput?.click();
});
["dragenter", "dragover"].forEach((type) => {
commentDropzone.addEventListener(type, (event) => {
event.preventDefault();
event.stopPropagation();
commentDropzone.classList.add("drag-over");
});
});
["dragleave", "drop"].forEach((type) => {
commentDropzone.addEventListener(type, (event) => {
event.preventDefault();
event.stopPropagation();
commentDropzone.classList.remove("drag-over");
});
});
commentDropzone.addEventListener("drop", async (event) => {
const files = await readAttachmentFiles(event.dataTransfer?.files);
commentDraftAttachments = [...commentDraftAttachments, ...files].slice(0, 8);
renderModalAttachmentInputs();
});
}
if (!isGuestMode()) {
loadProjectsList().catch(() => {
templatesMenu?.setData({ templates: templateFallbacks, projects: [], error: "API недоступно" });