// ============================================================
// 訪問者新增註解時跳出 prompt (取代 Potree 內建的 "Annotation Title" 預設)
// ============================================================
(function installAnnotationPrompt(){
if (!window.Potree || !Potree.AnnotationTool) {
console.warn("[annotation-prompt] Potree.AnnotationTool not available yet, retrying…");
return setTimeout(installAnnotationPrompt, 200);
}
const proto = Potree.AnnotationTool.prototype;
if (proto.__patchedForPrompt) return;
proto.__patchedForPrompt = true;
const origStartInsertion = proto.startInsertion;
proto.startInsertion = function(args){
const annotation = origStartInsertion.call(this, args);
const viewer = this.viewer;
const renderArea = viewer.renderer.domElement;
let done = false;
const onClick = (e) => {
if (done || e.button !== 0) return;
done = true;
renderArea.removeEventListener("mouseup", onClick, true);
setTimeout(() => showAnnotationPrompt(annotation, viewer), 0);
};
renderArea.addEventListener("mouseup", onClick, true);
return annotation;
};
function showAnnotationPrompt(annotation, viewer){
const defaultTitle = "新標記 " + new Date().toISOString().slice(0,16).replace("T"," ");
const backdrop = document.createElement("div");
backdrop.style.cssText = "position:fixed;inset:0;background:rgba(0,0,0,0.55);z-index:999999;display:flex;align-items:center;justify-content:center;font-family:sans-serif;";
const box = document.createElement("div");
box.style.cssText = "background:#fff;color:#222;padding:20px 24px;border-radius:8px;min-width:320px;max-width:480px;box-shadow:0 8px 24px rgba(0,0,0,0.4);";
box.innerHTML = `
新增註解
`;
backdrop.appendChild(box);
document.body.appendChild(backdrop);
const $title = box.querySelector("#ap_title");
const $desc = box.querySelector("#ap_desc");
$title.value = defaultTitle;
setTimeout(() => { $title.focus(); $title.select(); }, 50);
const cleanup = () => {
if (backdrop.parentElement) backdrop.parentElement.removeChild(backdrop);
};
const onOk = () => {
const t = $title.value.trim() || defaultTitle;
const d = $desc.value;
annotation.title = t;
annotation.description = d;
cleanup();
console.log("[annotation-prompt] saved:", t);
};
const onCancel = () => {
viewer.scene.annotations.remove(annotation);
if (annotation.domElement && annotation.domElement.parentElement) {
annotation.domElement.parentElement.removeChild(annotation.domElement[0]);
}
cleanup();
console.log("[annotation-prompt] cancelled, annotation removed");
};
box.querySelector("#ap_ok").addEventListener("click", onOk);
box.querySelector("#ap_cancel").addEventListener("click", onCancel);
backdrop.addEventListener("click", (e) => { if (e.target === backdrop) onCancel(); });
document.addEventListener("keydown", function onKey(e){
if (e.key === "Escape") { onCancel(); document.removeEventListener("keydown", onKey); }
else if (e.key === "Enter" && (e.ctrlKey || e.metaKey)) { onOk(); document.removeEventListener("keydown", onKey); }
});
}
console.log("[annotation-prompt] installed — visitors can now label their annotations");
})();