PlotDirector/PlotLine/wwwroot/js/context-map-renderer.js

27 lines
5.0 KiB
JavaScript

/* Read-only Context adapter around the canonical Maps workspace renderer. No SignalR or geometry saves. */
(() => {
if(!document.body.classList.contains('spatial-renderer-page'))return;
const api=window.PlotDirectorMapWorkspace,canvas=document.querySelector('#mapsCanvas'),app=document.querySelector('#mapsApp');if(!api||!canvas)return;
const origin=location.origin;let channel=null,highlights=[],placement=false,pointer=null;
const send=(action,extra={})=>{if(channel)parent.postMessage({type:'plotdirector.spatial.renderer',channel,action,...extra},origin);};
const ns='http://www.w3.org/2000/svg',layer=document.createElementNS(ns,'g');layer.dataset.spatialOverlay='';layer.setAttribute('pointer-events','none');canvas.append(layer);
function paint(){layer.replaceChildren();const scale=api.view.w/Math.max(canvas.clientWidth,1);for(const item of highlights){const f=api.document.features.find(f=>f.featureID===item.featureID);if(!f||!f.isPlaced)continue;const g=document.createElementNS(ns,'g');g.setAttribute('transform',`translate(${f.x} ${f.y})`);g.setAttribute('role','img');g.setAttribute('aria-label',item.label);const roles=[...new Set(item.roles)].sort((a,b)=>a-b);for(let i=0;i<roles.length;i++){const c=document.createElementNS(ns,'circle');c.setAttribute('r',String((15+i*5)*scale));c.setAttribute('class',`spatial-map-ring spatial-position-${roles[i]+1}`);c.setAttribute('fill','none');c.setAttribute('stroke-width',String(3*scale));g.append(c);}const text=document.createElementNS(ns,'text');text.textContent=roles.map(r=>['P','C','N'][r+1]).join(' · ');text.setAttribute('x',String(24*scale));text.setAttribute('y',String(-18*scale));text.setAttribute('font-size',String(13*scale));text.setAttribute('class','spatial-map-badge');g.append(text);const title=document.createElementNS(ns,'title');title.textContent=item.label;g.append(title);layer.append(g);}}
app.addEventListener('map-rendered',()=>{paint();send('viewport',{view:api.view});});
window.addEventListener('message',async e=>{if(e.origin!==origin||e.source!==parent||e.data?.type!=='plotdirector.spatial.command')return;const m=e.data;if(channel&&m.channel!==channel)return;channel=m.channel;
if(m.action==='init'){if(m.view)api.setView(m.view);send('ready',{map:api.document.map,features:api.document.features,view:api.view});}
if(m.action==='highlight'){highlights=m.highlights||[];paint();}
if(m.action==='zoom')api.zoom(1/m.factor);
if(m.action==='fit')api.fit(m.ids?.length?api.document.features.filter(f=>m.ids.includes(f.featureID)):api.document.features);
if(m.action==='centre'){const f=api.document.features.find(f=>f.featureID===m.id);if(f&&(m.force||f.x<api.view.x||f.x>api.view.x+api.view.w||f.y<api.view.y||f.y>api.view.y+api.view.h))api.focus(f.featureID);if(f)api.select(f.featureID);}
if(m.action==='placement'){placement=!!m.active;canvas.classList.toggle('spatial-placing',placement);}
if(m.action==='reload'){try{const r=await fetch(`/Maps/State?projectId=${api.document.map.projectID}&id=${api.document.map.mapID}`,{cache:'no-store'});if(!r.ok||r.redirected)throw Error('Map unavailable');api.replaceDocument(await r.json());send('ready',{map:api.document.map,features:api.document.features,view:api.view});}catch(error){send('error',{message:error.message});}}
});
canvas.addEventListener('pointerdown',e=>{pointer={x:e.clientX,y:e.clientY,id:e.target.closest('[data-feature-id],[data-boundary-feature-id]')?.dataset.featureId||e.target.closest('[data-boundary-feature-id]')?.dataset.boundaryFeatureId};if(placement){e.preventDefault();e.stopImmediatePropagation();canvas.setPointerCapture(e.pointerId);}},true);
canvas.addEventListener('pointerup',e=>{if(!pointer)return;const start=pointer;pointer=null;if(Math.hypot(start.x-e.clientX,start.y-e.clientY)>5)return;if(placement){e.preventDefault();e.stopImmediatePropagation();send('place',{point:{x:api.world(e)[0],y:api.world(e)[1]}});if(canvas.hasPointerCapture(e.pointerId))canvas.releasePointerCapture(e.pointerId);}else if(start.id){const f=api.document.features.find(f=>f.featureID===start.id);send('select',{feature:f});}},true);
canvas.addEventListener('dblclick',e=>{e.preventDefault();e.stopImmediatePropagation();},true);
canvas.addEventListener('dragover',e=>{if(e.dataTransfer.types.includes('application/x-plotdirector-spatial-location')){e.preventDefault();e.stopImmediatePropagation();e.dataTransfer.dropEffect='copy';}},true);
canvas.addEventListener('drop',e=>{e.preventDefault();e.stopImmediatePropagation();try{const payload=JSON.parse(e.dataTransfer.getData('application/x-plotdirector-spatial-location'));send('drop',{payload,point:{x:api.world(e)[0],y:api.world(e)[1]}});}catch{}},true);
window.addEventListener('keydown',e=>{if((e.ctrlKey||e.metaKey)&&e.key.toLowerCase()==='s'){e.preventDefault();e.stopImmediatePropagation();}if(e.key==='Escape'&&placement){e.preventDefault();e.stopImmediatePropagation();send('cancel');}},true);
canvas.addEventListener('keyup',e=>{if(['Enter',' '].includes(e.key)){const id=e.target.closest('[data-feature-id]')?.dataset.featureId;if(id)send('select',{feature:api.document.features.find(f=>f.featureID===id)});}});
})();