Add BIM model comments

This commit is contained in:
CODEX
2026-06-20 16:58:24 +03:00
parent c248217976
commit 684f85b382
6 changed files with 1849 additions and 6 deletions
+847 -2
View File
@@ -4,7 +4,7 @@ const SDK_URL = "./lib/dc-camera-context.es.js?v=3";
import { createLogo } from "./src/ui/logo.js";
import { toolbarConfig, applyToolbarVars } from "./src/ui/toolbar.js";
import { createTemplatesMenu } from "./src/ui/templatesMenu.js?v=2";
import { createSettingsMenu } from "./src/ui/settingsMenu.js?v=8";
import { createSettingsMenu } from "./src/ui/settingsMenu.js?v=9";
import { createMeasurementModal } from "./src/ui/measurementModal.js";
import { createGlassSelect } from "./src/ui/glassSelect.js";
import { createSliderControl } from "./src/ui/controls/slider.js";
@@ -40,9 +40,36 @@ const settingsButton = document.querySelector('[data-action="settings"]');
const templatesButton = document.querySelector('[data-action="templates"]');
const shareButton = document.querySelector('[data-action="share"]');
const sectionButton = document.querySelector('[data-action="section"]');
const commentsButton = document.querySelector('[data-action="comments"]');
const sectionToolbar = document.getElementById("sectionToolbar");
const sectionOverviewCanvas = document.getElementById("sectionPlanesOverview");
const projectionToggle = document.querySelector('[data-action="projection"]');
const commentsPanel = document.getElementById("commentsPanel");
const commentsPanelHeader = document.getElementById("commentsPanelHeader");
const commentsCollapseButton = document.getElementById("commentsCollapseButton");
const commentsRestoreTray = document.getElementById("commentsRestoreTray");
const commentsRestoreButton = document.getElementById("commentsRestoreButton");
const commentsList = document.getElementById("commentsList");
const commentTargetsLayer = document.getElementById("commentTargetsLayer");
const commentContextMenu = document.getElementById("commentContextMenu");
const commentModalBackdrop = document.getElementById("commentModalBackdrop");
const commentModal = document.getElementById("commentModal");
const commentModalHeader = document.getElementById("commentModalHeader");
const commentModalClose = document.getElementById("commentModalClose");
const commentModalSubtitle = document.getElementById("commentModalSubtitle");
const commentTitleInput = document.getElementById("commentTitleInput");
const commentBodyInput = document.getElementById("commentBodyInput");
const commentAttachButton = document.getElementById("commentAttachButton");
const commentAttachmentInput = document.getElementById("commentAttachmentInput");
const commentAttachmentGrid = document.getElementById("commentAttachmentGrid");
const commentThreadList = document.getElementById("commentThreadList");
const commentReplyInput = document.getElementById("commentReplyInput");
const commentReplyAttachButton = document.getElementById("commentReplyAttachButton");
const commentReplyAttachmentInput = document.getElementById("commentReplyAttachmentInput");
const commentReplyAttachmentGrid = document.getElementById("commentReplyAttachmentGrid");
const commentModalError = document.getElementById("commentModalError");
const commentCancelButton = document.getElementById("commentCancelButton");
const commentApplyButton = document.getElementById("commentApplyButton");
const treeContainer = document.getElementById("treeContainer");
const displayModeControl = document.getElementById("displayModeControl");
const displayModeSelectHost = document.getElementById("displayModeSelect");
@@ -123,6 +150,7 @@ const API_BASE = "/api/projects";
const MODEL_SETTINGS_API = "/api/model-settings";
const AUTH_SESSION_API = "/api/auth/session";
const SHARES_API = "/api/shares";
const COMMENTS_API = "/api/comments";
const acceptByType = {
xkt: ".xkt",
@@ -258,6 +286,23 @@ let activeSectionPlaneId = null;
let sectionPlaneCounter = 0;
let sectionModeActive = false;
let activeSectionAxis = null;
let commentsModeActive = false;
let commentsCollapsed = false;
let commentRecords = [];
let commentSourcesLoaded = new Set();
let commentContextTarget = null;
let commentModalMode = "create";
let commentModalCommentId = null;
let activeCommentId = null;
let hoverCommentId = null;
let commentDraftAttachments = [];
let commentReplyAttachments = [];
let commentTargetRaf = 0;
let commentLongPressTimer = null;
let commentLongPressStart = null;
let suppressNextCanvasClick = false;
let floatingWindowOrder = [];
const floatingWindowElements = new Map();
let objectContextModal = null;
let mathUtil = null;
let selectionState = null; // { ids, modelId, pivotCenter, baseTransforms: Map, transform }
@@ -449,11 +494,690 @@ const getShareTokenFromPath = () => {
return match ? decodeURIComponent(match[1]) : null;
};
const COMMENT_TARGET_ICON = `
<svg viewBox="0 0 24 24" aria-hidden="true">
<path d="M6.5 5.5h11a2.5 2.5 0 0 1 2.5 2.5v6.25a2.5 2.5 0 0 1-2.5 2.5H12l-4.3 3.1v-3.1H6.5A2.5 2.5 0 0 1 4 14.25V8a2.5 2.5 0 0 1 2.5-2.5Z"></path>
</svg>
`;
const formatDateTime = (value) => {
if (!value) return "";
try {
return new Intl.DateTimeFormat("ru-RU", {
day: "2-digit",
month: "2-digit",
year: "2-digit",
hour: "2-digit",
minute: "2-digit",
}).format(new Date(value));
} catch (_) {
return String(value);
}
};
const bringFloatingWindowToFront = (id) => {
if (!id) return;
floatingWindowOrder = [...floatingWindowOrder.filter((item) => item !== id), id];
const zBase = 21000;
floatingWindowOrder.forEach((item, index) => {
const element = floatingWindowElements.get(item);
if (element) element.style.zIndex = `${zBase + index * 10}`;
});
};
const registerFloatingWindowElement = (id, element) => {
if (!id || !element) return;
floatingWindowElements.set(id, element);
if (!floatingWindowOrder.includes(id)) floatingWindowOrder.push(id);
element.addEventListener("pointerdown", (event) => {
if (event.button !== 0) return;
bringFloatingWindowToFront(id);
}, true);
bringFloatingWindowToFront(id);
};
const extractUploadIdentityFromSrc = (src) => {
if (!src || typeof src !== "string") return {};
const match = src.match(/(?:^|\/)uploads\/([^/]+)\/([^/?#]+)/);
return match ? { projectId: match[1], assetId: match[2] } : {};
};
const getCommentSourceForEntry = (entry = getPrimaryModelEntry()) => {
if (!entry) return null;
const meta = entry.meta || {};
const src = getModelSettingsSrc(entry) ||
normalizeModelSettingsSrc(meta.sourceSrc) ||
normalizeModelSettingsSrc(meta.artifactSrc) ||
normalizeModelSettingsSrc(entry.src) ||
"";
const uploadIdentity = extractUploadIdentityFromSrc(src);
return {
src,
sourceSrc: src,
settingsSrc: normalizeModelSettingsSrc(meta.settingsSrc) || src,
projectId: meta.projectId || uploadIdentity.projectId || currentProjectMeta?.id || null,
assetId: meta.assetId || uploadIdentity.assetId || null,
versionId: meta.versionId || null,
};
};
const sourceParamsForCommentSource = (source) => {
const params = new URLSearchParams();
if (!source) return params;
if (source.projectId) params.set("projectId", source.projectId);
if (source.assetId) params.set("assetId", source.assetId);
if (source.src) params.set("src", source.src);
if (source.sourceSrc) params.set("sourceSrc", source.sourceSrc);
if (source.settingsSrc) params.set("settingsSrc", source.settingsSrc);
return params;
};
const sourceCacheKey = (source) => {
const params = sourceParamsForCommentSource(source);
return params.toString();
};
const mergeCommentRecords = (items) => {
const byId = new Map(commentRecords.map((comment) => [comment.id, comment]));
(items || []).forEach((comment) => {
if (comment?.id) byId.set(comment.id, comment);
});
commentRecords = [...byId.values()]
.filter((comment) => !comment.deletedAt)
.sort((first, second) => String(second.updatedAt || "").localeCompare(String(first.updatedAt || "")));
};
const loadCommentsForEntry = async (entry, { force = false } = {}) => {
if (!entry || isGuestMode()) return [];
const source = getCommentSourceForEntry(entry);
const key = sourceCacheKey(source);
if (!key) return [];
if (!force && commentSourcesLoaded.has(key)) return commentRecords;
const params = sourceParamsForCommentSource(source);
const payload = await fetchJSON(`${COMMENTS_API}?${params.toString()}`);
commentSourcesLoaded.add(key);
mergeCommentRecords(payload?.comments || []);
renderCommentsPanel();
renderCommentTargets();
return payload?.comments || [];
};
const loadCommentsForActiveModels = async ({ force = false } = {}) => {
if (isGuestMode()) return;
const entries = activeModels.length ? [...activeModels] : [getPrimaryModelEntry()].filter(Boolean);
for (const entry of entries) {
await loadCommentsForEntry(entry, { force }).catch((err) => {
console.warn("comments load failed", err);
});
}
};
const getCommentDisplayBody = (comment) => {
if (comment?.body) return comment.body;
const lastMessage = Array.isArray(comment?.messages) ? comment.messages[comment.messages.length - 1] : null;
return lastMessage?.body || "";
};
const renderCommentsPanel = () => {
if (!commentsList) return;
commentsList.replaceChildren();
if (!commentsModeActive) return;
if (!commentRecords.length) {
const empty = document.createElement("div");
empty.className = "comments-empty";
empty.textContent = "Комментариев пока нет";
commentsList.appendChild(empty);
return;
}
commentRecords.forEach((comment) => {
const item = document.createElement("button");
item.type = "button";
item.className = "comment-list-item";
item.classList.toggle("active", activeCommentId === comment.id || hoverCommentId === comment.id);
item.dataset.commentId = comment.id;
const title = document.createElement("div");
title.className = "comment-list-title";
title.textContent = comment.title || "Комментарий";
const body = document.createElement("div");
body.className = "comment-list-body";
body.textContent = getCommentDisplayBody(comment) || "Без описания";
const meta = document.createElement("div");
meta.className = "comment-list-meta";
const created = formatDateTime(comment.createdAt);
const updated = formatDateTime(comment.updatedAt);
meta.textContent = updated && updated !== created ? `Создан: ${created} · Изменён: ${updated}` : `Создан: ${created}`;
item.append(title, body, meta);
item.addEventListener("mouseenter", () => {
hoverCommentId = comment.id;
renderCommentsPanel();
renderCommentTargets();
});
item.addEventListener("mouseleave", () => {
if (hoverCommentId === comment.id) hoverCommentId = null;
renderCommentsPanel();
renderCommentTargets();
});
item.addEventListener("click", () => openCommentModalForComment(comment.id));
commentsList.appendChild(item);
});
};
const commentTargetsForRender = () => {
const targets = [...commentRecords];
if (commentModalMode === "create" && commentContextTarget?.worldPos) {
targets.push({
id: "__draft_comment__",
title: "Новый комментарий",
worldPos: commentContextTarget.worldPos,
draft: true,
});
}
return targets;
};
const updateCommentTargetPositions = () => {
if (!viewer?.scene?.camera || !commentTargetsLayer || !commentsModeActive) return;
const canvas = viewer.scene?.canvas?.canvas || mainCanvas;
const rect = canvas?.getBoundingClientRect?.();
if (!rect) return;
commentTargetsLayer.querySelectorAll(".comment-target").forEach((targetEl) => {
const id = targetEl.dataset.commentId;
const record = commentTargetsForRender().find((comment) => comment.id === id);
const pos = record?.worldPos;
if (!Array.isArray(pos) || pos.length !== 3) {
targetEl.hidden = true;
return;
}
let canvasPos = null;
try {
canvasPos = viewer.scene.camera.projectWorldPos(pos);
} catch (_) {
canvasPos = null;
}
if (!canvasPos || !Number.isFinite(canvasPos[0]) || !Number.isFinite(canvasPos[1])) {
targetEl.hidden = true;
return;
}
targetEl.hidden = false;
targetEl.style.left = `${Math.round(rect.left + canvasPos[0])}px`;
targetEl.style.top = `${Math.round(rect.top + canvasPos[1])}px`;
});
};
const startCommentTargetLoop = () => {
if (commentTargetRaf) return;
const tick = () => {
commentTargetRaf = 0;
if (!commentsModeActive) return;
updateCommentTargetPositions();
commentTargetRaf = window.requestAnimationFrame(tick);
};
commentTargetRaf = window.requestAnimationFrame(tick);
};
const stopCommentTargetLoop = () => {
if (commentTargetRaf) {
window.cancelAnimationFrame(commentTargetRaf);
commentTargetRaf = 0;
}
};
const renderCommentTargets = () => {
if (!commentTargetsLayer) return;
commentTargetsLayer.replaceChildren();
if (!commentsModeActive || isGuestMode()) {
commentTargetsLayer.classList.add("hidden");
stopCommentTargetLoop();
return;
}
const targets = commentTargetsForRender();
targets.forEach((comment) => {
const btn = document.createElement("button");
btn.type = "button";
btn.className = "comment-target";
btn.classList.toggle("active", comment.draft || activeCommentId === comment.id || hoverCommentId === comment.id);
btn.dataset.commentId = comment.id;
btn.title = comment.title || "Комментарий";
btn.setAttribute("aria-label", comment.title || "Комментарий");
btn.innerHTML = COMMENT_TARGET_ICON;
btn.addEventListener("mouseenter", () => {
if (!comment.draft) hoverCommentId = comment.id;
renderCommentTargets();
renderCommentsPanel();
});
btn.addEventListener("mouseleave", () => {
if (hoverCommentId === comment.id) hoverCommentId = null;
renderCommentTargets();
renderCommentsPanel();
});
btn.addEventListener("click", (event) => {
event.stopPropagation();
if (!comment.draft) openCommentModalForComment(comment.id);
});
commentTargetsLayer.appendChild(btn);
});
commentTargetsLayer.classList.toggle("hidden", targets.length === 0);
updateCommentTargetPositions();
startCommentTargetLoop();
};
const updateCommentsVisibility = () => {
const visible = commentsModeActive && !isGuestMode() && activeModels.length > 0;
setToolbarButtonActive(commentsButton, visible);
if (commentsPanel) {
commentsPanel.classList.toggle("hidden", !visible || commentsCollapsed);
}
if (commentsRestoreTray) {
commentsRestoreTray.hidden = !visible || !commentsCollapsed;
}
if (visible) {
renderCommentsPanel();
renderCommentTargets();
loadCommentsForActiveModels().catch(() => {});
} else {
commentsPanel?.classList.add("hidden");
if (commentsRestoreTray) commentsRestoreTray.hidden = true;
commentTargetsLayer?.classList.add("hidden");
hideCommentContextMenu();
stopCommentTargetLoop();
}
};
const setCommentsCollapsed = (collapsed) => {
commentsCollapsed = !!collapsed;
updateCommentsVisibility();
};
const toggleCommentsMode = async () => {
if (isGuestMode()) return;
if (!activeModels.length) {
setStatus("Сначала загрузите модель");
return;
}
commentsModeActive = !commentsModeActive;
if (commentsModeActive) {
commentsCollapsed = false;
bringFloatingWindowToFront("comments");
await loadCommentsForActiveModels({ force: true }).catch(() => {});
} else {
activeCommentId = null;
hoverCommentId = null;
}
updateCommentsVisibility();
};
const hideCommentContextMenu = () => {
commentContextMenu?.classList.add("hidden");
};
const clampMenuPosition = (x, y, menu) => {
const width = menu?.offsetWidth || 220;
const height = menu?.offsetHeight || 54;
return {
x: Math.max(8, Math.min(window.innerWidth - width - 8, x)),
y: Math.max(8, Math.min(window.innerHeight - height - 8, y)),
};
};
const getCanvasPointFromEvent = (event) => {
const canvas = viewer?.scene?.canvas?.canvas || mainCanvas;
const rect = canvas?.getBoundingClientRect?.();
if (!rect) return null;
return {
canvasPos: [event.clientX - rect.left, event.clientY - rect.top],
pagePos: [event.clientX, event.clientY],
};
};
const getWorldPosFromPick = (hit) => {
const direct = hit?.worldPos || hit?.worldPosition || hit?.position || hit?.origin;
if (direct && direct.length >= 3) {
return [Number(direct[0]), Number(direct[1]), Number(direct[2])].every(Number.isFinite)
? [Number(direct[0]), Number(direct[1]), Number(direct[2])]
: null;
}
const entity = hit?.entity;
const aabb = entity?.aabb || entity?.model?.aabb || null;
const aabbArray = Array.from(aabb || []);
return aabbArray.length === 6 ? centerFromAABB(aabbArray) : null;
};
const buildCommentTargetFromEvent = (event) => {
if (isGuestMode() || !viewer?.scene) return null;
const point = getCanvasPointFromEvent(event);
if (!point) return null;
const hit = viewer.scene.pick({
canvasPos: point.canvasPos,
pickSurface: true,
});
if (!hit) return null;
const entity = hit.entity || null;
const modelId = entity?.model?.id || entity?.modelId || null;
const objectId = entity?.id || entity?.objectId || null;
const entry = activeModels.find((item) => item?.model?.id === modelId) || getPrimaryModelEntry();
const source = getCommentSourceForEntry(entry);
const worldPos = getWorldPosFromPick(hit);
if (!source || !worldPos) return null;
return {
...source,
modelId,
objectId,
worldPos,
canvasPos: point.canvasPos,
pagePos: point.pagePos,
};
};
const showCommentContextMenu = (target, x, y) => {
if (!commentContextMenu || !target) return;
commentContextTarget = target;
commentContextMenu.classList.remove("hidden");
const pos = clampMenuPosition(x, y, commentContextMenu);
commentContextMenu.style.left = `${pos.x}px`;
commentContextMenu.style.top = `${pos.y}px`;
};
const openCommentContextMenuFromEvent = (event) => {
if (isGuestMode()) return false;
const target = buildCommentTargetFromEvent(event);
if (!target) {
hideCommentContextMenu();
return false;
}
event.preventDefault();
event.stopPropagation();
showCommentContextMenu(target, event.clientX, event.clientY);
return true;
};
const readImageFiles = async (fileList) => {
const files = Array.from(fileList || []).filter((file) => file?.type?.startsWith("image/")).slice(0, 8);
const readOne = (file) => new Promise((resolve) => {
const reader = new FileReader();
reader.onload = () => resolve({
id: `att_${Date.now()}_${Math.random().toString(36).slice(2)}`,
name: file.name,
type: file.type,
size: file.size,
dataUrl: String(reader.result || ""),
});
reader.onerror = () => resolve(null);
reader.readAsDataURL(file);
});
const items = await Promise.all(files.map(readOne));
return items.filter(Boolean);
};
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 remove = document.createElement("button");
remove.type = "button";
remove.setAttribute("aria-label", "Удалить картинку");
remove.textContent = "×";
remove.addEventListener("click", () => onRemove?.(index));
thumb.append(img, remove);
grid.appendChild(thumb);
});
};
const renderThreadAttachments = (container, attachments) => {
if (!attachments?.length) return;
const grid = document.createElement("div");
grid.className = "comment-attachment-grid";
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);
grid.appendChild(thumb);
});
container.appendChild(grid);
};
const renderCommentThread = (comment) => {
if (!commentThreadList) return;
commentThreadList.replaceChildren();
const messages = Array.isArray(comment?.messages) ? comment.messages : [];
if (!messages.length) {
const empty = document.createElement("div");
empty.className = "comment-thread-empty";
empty.textContent = "Комментариев пока нет";
commentThreadList.appendChild(empty);
return;
}
messages.forEach((message) => {
const item = document.createElement("div");
item.className = "comment-thread-message";
const head = document.createElement("div");
head.className = "comment-thread-message__head";
const author = document.createElement("strong");
author.textContent = message?.createdBy?.name || message?.createdBy?.email || "Пользователь";
const date = document.createElement("span");
date.textContent = formatDateTime(message?.updatedAt || message?.createdAt);
head.append(author, date);
const body = document.createElement("div");
body.className = "comment-thread-message__body";
body.textContent = message?.body || "";
item.append(head, body);
renderThreadAttachments(item, message?.attachments || []);
commentThreadList.appendChild(item);
});
};
const resetCommentModalAttachments = () => {
commentDraftAttachments = [];
commentReplyAttachments = [];
renderModalAttachmentInputs();
};
const renderModalAttachmentInputs = () => {
renderAttachmentGrid(commentAttachmentGrid, commentDraftAttachments, (index) => {
commentDraftAttachments.splice(index, 1);
renderModalAttachmentInputs();
});
renderAttachmentGrid(commentReplyAttachmentGrid, commentReplyAttachments, (index) => {
commentReplyAttachments.splice(index, 1);
renderModalAttachmentInputs();
});
};
const openCommentModalForTarget = () => {
if (!commentContextTarget || !commentModal) return;
commentsModeActive = true;
commentsCollapsed = false;
commentModalMode = "create";
commentModalCommentId = null;
activeCommentId = "__draft_comment__";
commentTitleInput && (commentTitleInput.value = "");
commentBodyInput && (commentBodyInput.value = "");
commentReplyInput && (commentReplyInput.value = "");
if (commentModalSubtitle) {
const [x, y, z] = commentContextTarget.worldPos || [];
commentModalSubtitle.textContent = Number.isFinite(x) ? `Точка: ${x.toFixed(2)}, ${y.toFixed(2)}, ${z.toFixed(2)}` : "Новый таргет";
}
if (commentModalError) commentModalError.textContent = "";
resetCommentModalAttachments();
renderCommentThread(null);
commentModalBackdrop?.classList.remove("hidden");
commentModal.classList.remove("hidden");
bringFloatingWindowToFront("comments");
updateCommentsVisibility();
commentTitleInput?.focus();
};
const openCommentModalForComment = (commentId) => {
const comment = commentRecords.find((item) => item.id === commentId);
if (!comment || !commentModal) return;
commentModalMode = "edit";
commentModalCommentId = comment.id;
activeCommentId = comment.id;
commentTitleInput && (commentTitleInput.value = comment.title || "");
commentBodyInput && (commentBodyInput.value = comment.body || "");
commentReplyInput && (commentReplyInput.value = "");
if (commentModalSubtitle) {
commentModalSubtitle.textContent = `Создан: ${formatDateTime(comment.createdAt)} · Изменён: ${formatDateTime(comment.updatedAt)}`;
}
if (commentModalError) commentModalError.textContent = "";
resetCommentModalAttachments();
renderCommentThread(comment);
commentModalBackdrop?.classList.remove("hidden");
commentModal.classList.remove("hidden");
renderCommentsPanel();
renderCommentTargets();
};
const closeCommentModal = () => {
commentModalBackdrop?.classList.add("hidden");
commentModal?.classList.add("hidden");
if (commentModalMode === "create") {
commentContextTarget = null;
}
activeCommentId = null;
commentModalMode = "create";
commentModalCommentId = null;
if (commentModalError) commentModalError.textContent = "";
renderCommentsPanel();
renderCommentTargets();
};
const buildCommentPayload = () => ({
...(commentContextTarget || {}),
title: commentTitleInput?.value?.trim() || "",
body: commentBodyInput?.value?.trim() || "",
attachments: commentDraftAttachments,
});
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 = "Заполните тему или описание";
return;
}
if (commentApplyButton) {
commentApplyButton.disabled = true;
commentApplyButton.textContent = "Сохранение...";
}
try {
let payload = null;
if (commentModalMode === "create") {
payload = await fetchJSON(COMMENTS_API, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(buildCommentPayload()),
});
} else if (commentModalCommentId) {
const original = commentRecords.find((item) => item.id === commentModalCommentId);
const patchPayload = {};
if (!original || title !== (original.title || "")) patchPayload.title = title;
if (!original || body !== (original.body || "")) patchPayload.body = body;
if (replyBody || commentReplyAttachments.length) {
patchPayload.message = {
body: replyBody,
attachments: commentReplyAttachments,
};
}
if (!Object.keys(patchPayload).length) {
closeCommentModal();
return;
}
payload = await fetchJSON(`${COMMENTS_API}/${encodeURIComponent(commentModalCommentId)}`, {
method: "PATCH",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(patchPayload),
});
}
const saved = payload?.comment;
if (saved) {
mergeCommentRecords([saved]);
activeCommentId = saved.id;
commentContextTarget = null;
commentsModeActive = true;
commentsCollapsed = false;
closeCommentModal();
updateCommentsVisibility();
setStatus("Комментарий сохранён");
}
} catch (err) {
console.error(err);
if (commentModalError) commentModalError.textContent = err.message || "Не удалось сохранить комментарий";
} finally {
if (commentApplyButton) {
commentApplyButton.disabled = false;
commentApplyButton.textContent = "Применить";
}
}
};
const setupDraggableFixedWindow = (element, handle) => {
if (!element || !handle) return;
let drag = null;
handle.addEventListener("pointerdown", (event) => {
if (event.button !== 0 || event.target.closest("button,input,textarea")) return;
const rect = element.getBoundingClientRect();
drag = {
pointerId: event.pointerId,
dx: event.clientX - rect.left,
dy: event.clientY - rect.top,
};
element.style.left = `${rect.left}px`;
element.style.top = `${rect.top}px`;
element.style.right = "auto";
element.style.bottom = "auto";
element.style.transform = "none";
handle.setPointerCapture?.(event.pointerId);
event.preventDefault();
});
handle.addEventListener("pointermove", (event) => {
if (!drag || drag.pointerId !== event.pointerId) return;
const width = element.offsetWidth || 320;
const height = element.offsetHeight || 320;
const left = Math.max(8, Math.min(window.innerWidth - width - 8, event.clientX - drag.dx));
const top = Math.max(8, Math.min(window.innerHeight - Math.min(height, window.innerHeight - 16) - 8, event.clientY - drag.dy));
element.style.left = `${left}px`;
element.style.top = `${top}px`;
});
const endDrag = (event) => {
if (!drag || drag.pointerId !== event.pointerId) return;
handle.releasePointerCapture?.(event.pointerId);
drag = null;
};
handle.addEventListener("pointerup", endDrag);
handle.addEventListener("pointercancel", endDrag);
};
const applyRuntimeMode = () => {
document.body.dataset.viewerMode = viewerMode;
if (bottomToolbar) {
bottomToolbar.classList.toggle("hidden", isGuestMode());
}
if (commentsButton) {
commentsButton.classList.toggle("hidden", isGuestMode());
commentsButton.disabled = isGuestMode() || activeModels.length === 0;
}
if (isGuestMode()) {
commentsModeActive = false;
commentsCollapsed = false;
commentContextTarget = null;
activeCommentId = null;
hoverCommentId = null;
commentsPanel?.classList.add("hidden");
if (commentsRestoreTray) commentsRestoreTray.hidden = true;
commentTargetsLayer?.classList.add("hidden");
hideCommentContextMenu();
stopCommentTargetLoop();
}
if (shareButton) {
const canShowShare = !isGuestMode() && runtimeSessionInfo.canShare && activeModels.length > 0;
shareButton.classList.toggle("hidden", !canShowShare);
@@ -1488,7 +2212,18 @@ const updatePanelsVisibility = () => {
projectNameInput.value = "";
}
}
if (!hasModels) {
commentsModeActive = false;
commentsCollapsed = false;
commentContextTarget = null;
activeCommentId = null;
hoverCommentId = null;
commentRecords = [];
commentSourcesLoaded.clear();
closeCommentModal();
}
updateSectionControls();
updateCommentsVisibility();
applyRuntimeMode();
};
@@ -3298,20 +4033,67 @@ const initViewer = async () => {
const canvasEl = viewer.scene?.canvas?.canvas;
if (canvasEl) {
let downPos = null;
const clearCommentLongPress = () => {
if (commentLongPressTimer) {
window.clearTimeout(commentLongPressTimer);
commentLongPressTimer = null;
}
commentLongPressStart = null;
};
canvasEl.addEventListener("contextmenu", (event) => {
event.preventDefault();
const opened = openCommentContextMenuFromEvent(event);
if (!opened) event.preventDefault();
});
canvasEl.addEventListener("pointerdown", (event) => {
if (measureActive) {
downPos = null;
clearCommentLongPress();
return;
}
downPos = { x: event.clientX, y: event.clientY };
if (!isGuestMode() && activeModels.length && (event.pointerType === "touch" || event.pointerType === "pen")) {
clearCommentLongPress();
commentLongPressStart = {
x: event.clientX,
y: event.clientY,
};
commentLongPressTimer = window.setTimeout(() => {
if (!commentLongPressStart) return;
const syntheticEvent = {
clientX: commentLongPressStart.x,
clientY: commentLongPressStart.y,
preventDefault: () => {},
stopPropagation: () => {},
};
if (openCommentContextMenuFromEvent(syntheticEvent)) {
suppressNextCanvasClick = true;
}
clearCommentLongPress();
}, 620);
}
});
canvasEl.addEventListener("pointermove", (event) => {
if (!commentLongPressStart) return;
const dx = Math.abs(event.clientX - commentLongPressStart.x);
const dy = Math.abs(event.clientY - commentLongPressStart.y);
if (dx > 10 || dy > 10) clearCommentLongPress();
});
canvasEl.addEventListener("pointerup", clearCommentLongPress);
canvasEl.addEventListener("pointercancel", clearCommentLongPress);
canvasEl.addEventListener("pointerleave", clearCommentLongPress);
canvasEl.addEventListener("wheel", () => {
hideCommentContextMenu();
clearCommentLongPress();
});
canvasEl.addEventListener("click", (event) => {
if (measureActive) {
return;
}
if (suppressNextCanvasClick) {
suppressNextCanvasClick = false;
downPos = null;
return;
}
if (downPos) {
const dx = Math.abs(event.clientX - downPos.x);
const dy = Math.abs(event.clientY - downPos.y);
@@ -4051,6 +4833,9 @@ const loadProjectSnapshot = async (project) => {
if (key && key.startsWith("section")) {
applySectionPlaneTheme();
}
if (key && key.startsWith("commentTarget")) {
renderCommentTargets();
}
focusCanvasAndControls();
},
onShow: () => {
@@ -4127,6 +4912,52 @@ const loadProjectSnapshot = async (project) => {
}
inspectorCollapseButton?.addEventListener("click", () => setInspectorCollapsed(true));
inspectorRestoreButton?.addEventListener("click", () => setInspectorCollapsed(false));
registerFloatingWindowElement("inspector", sidePanelEl);
registerFloatingWindowElement("comments", commentsPanel);
setupDraggableFixedWindow(commentsPanel, commentsPanelHeader);
setupDraggableFixedWindow(commentModal, commentModalHeader);
commentsCollapseButton?.addEventListener("click", () => setCommentsCollapsed(true));
commentsRestoreButton?.addEventListener("click", () => {
commentsModeActive = true;
commentsCollapsed = false;
bringFloatingWindowToFront("comments");
updateCommentsVisibility();
});
commentContextMenu?.addEventListener("click", (event) => {
const button = event.target?.closest?.("[data-comment-menu-action]");
if (!button) return;
event.preventDefault();
event.stopPropagation();
if (button.dataset.commentMenuAction === "create") {
hideCommentContextMenu();
openCommentModalForTarget();
}
});
document.addEventListener("click", (event) => {
if (!commentContextMenu || commentContextMenu.classList.contains("hidden")) return;
if (commentContextMenu.contains(event.target)) return;
hideCommentContextMenu();
});
commentModalClose?.addEventListener("click", closeCommentModal);
commentCancelButton?.addEventListener("click", closeCommentModal);
commentApplyButton?.addEventListener("click", applyCommentModal);
commentModalBackdrop?.addEventListener("click", (event) => {
if (event.target === commentModalBackdrop) closeCommentModal();
});
commentAttachButton?.addEventListener("click", () => commentAttachmentInput?.click());
commentReplyAttachButton?.addEventListener("click", () => commentReplyAttachmentInput?.click());
commentAttachmentInput?.addEventListener("change", async (event) => {
const files = await readImageFiles(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);
commentReplyAttachments = [...commentReplyAttachments, ...files].slice(0, 8);
event.target.value = "";
renderModalAttachmentInputs();
});
if (!isGuestMode()) {
loadProjectsList().catch(() => {
templatesMenu?.setData({ templates: templateFallbacks, projects: [], error: "API недоступно" });
@@ -4303,6 +5134,18 @@ const loadProjectSnapshot = async (project) => {
document.addEventListener("keydown", (e) => {
if (e.key !== "Escape") return;
if (commentModal && !commentModal.classList.contains("hidden")) {
closeCommentModal();
e.preventDefault();
e.stopPropagation();
return;
}
if (commentContextMenu && !commentContextMenu.classList.contains("hidden")) {
hideCommentContextMenu();
e.preventDefault();
e.stopPropagation();
return;
}
if (e.target && ["INPUT", "TEXTAREA", "SELECT"].includes(e.target.tagName)) return;
if (shareModal && !shareModal.classList.contains("hidden")) {
closeShareModal();
@@ -4569,6 +5412,8 @@ const loadProjectSnapshot = async (project) => {
setMeasureActive(!measureActive);
} else if (action === "section") {
toggleSectionPlanes();
} else if (action === "comments") {
toggleCommentsMode();
} else if (action === "projection") {
toggleProjection();
} else if (action === "share") {