23 lines
2.2 KiB
JavaScript
23 lines
2.2 KiB
JavaScript
/* Reuse the main window's established presence connection. Never create a connection or overwrite drafts. */
|
|
(() => {
|
|
if(window.PlotDirectorSpatialLiveUpdates||document.body.classList.contains('spatial-renderer-page'))return;
|
|
const wired=new WeakSet();let pending=null,flight=false,warned=false;
|
|
async function refresh(hint={}){
|
|
const map=document.querySelector('#mapsApp')&&window.PlotDirectorMapWorkspace,floor=document.querySelector('[data-floor-plan-workspace]');if(!map&&!floor)return;
|
|
const project=Number(map?.document?.map.projectID||floor?.dataset.spatialProjectId);
|
|
if(hint.projectId&&Number(hint.projectId)!==project)return;
|
|
if(hint.kind&&!(map?['maps','locations','spatial']:['floorplans','locations','spatial']).includes(hint.kind))return;
|
|
pending=hint;if(flight)return;flight=true;
|
|
try{
|
|
let done=false;
|
|
if(map){if(!map.isEditing&&!map.dirty&&!map.busy&&!window.PlotDirectorMapAppearance?.adjusting){const response=await fetch(`/Maps/State?projectId=${project}&id=${map.document.map.mapID}`,{cache:'no-store'});if(!response.ok||response.redirected)return;const next=await response.json();if(window.PlotDirectorMapWorkspace===map&&!map.isEditing&&!map.dirty&&!map.busy&&!window.PlotDirectorMapAppearance?.adjusting&&next.map.mapID===map.document.map.mapID){if(next.map.revision>map.document.map.revision)map.replaceDocument(next);done=true;}}}
|
|
else done=await floor.refreshSpatialReference?.()===true;
|
|
if(done){pending=null;warned=false;}else if(!warned){warned=true;window.PlotDirector?.toast?.info('Saved spatial information changed elsewhere. Your active edits remain here; finish them before refreshing.');}
|
|
}catch{/* Persisted changes remain authoritative; normal navigation can retry. */}
|
|
finally{flight=false;}
|
|
}
|
|
function wire(connection){if(!connection||wired.has(connection))return;wired.add(connection);connection.on('ContextInvalidated',hint=>void refresh(hint));}
|
|
document.addEventListener('pd-presence-connection',e=>wire(e.detail));document.addEventListener('pd-presence-ready',()=>wire(window.PlotDirectorPresenceConnection));wire(window.PlotDirectorPresenceConnection);
|
|
window.addEventListener('focus',()=>{if(pending)void refresh(pending);});window.PlotDirectorSpatialLiveUpdates=Object.freeze({refresh});
|
|
})();
|