PlotDirector/PlotLine/wwwroot/js/native-context-launch.js

29 lines
1.8 KiB
JavaScript

// Reuse My Windows in this authenticated browser environment. Opening never publishes focus.
(() => {
'use strict';
const root=document.querySelector('[data-native-context-launch]');if(!root)return;
const message=root.querySelector('[data-context-launch-status]'),retry=root.querySelector('[data-context-launch-retry]');
const windowID=crypto.randomUUID(),bookID=Number(root.dataset.book);let running=false,finished=false;
async function open(){
const connection=window.PlotDirectorPresenceConnection,environmentID=window.PlotDirectorContextEnvironment;
if(running||finished||connection?.state!=='Connected')return;
if(!environmentID){message.textContent='Context window discovery requires browser storage. No additional window was opened.';return;}
running=true;retry.hidden=true;
try{
await connection.invoke('RegisterContextWindow',{version:1,bookID,windowID,environmentID,panes:[]});
const result=await connection.invoke('OpenContextWindow',bookID);
if(result==='open'){
const url=new URL('/Writing/Context',location.origin);
for(const key of ['book','project','instance','session'])if(root.dataset[key])url.searchParams.set(key+'Id',root.dataset[key]);
finished=true;location.replace(url.href);
}else if(result==='reused'||result==='pending'){
finished=true;message.textContent=result==='reused'?'Context is already open. Its layout, tools, Hold and position were retained.':'A Context window is already opening for this book.';
window.close();
}
}catch{message.textContent='Context window discovery is unavailable. Reconnect and retry; no other window was changed.';retry.hidden=false;}
finally{running=false;}
}
document.addEventListener('pd-presence-ready',()=>void open());
retry.addEventListener('click',()=>void open());void open();
})();