diff --git a/PlotLine/wwwroot/js/shared-manuscript-focus.js b/PlotLine/wwwroot/js/shared-manuscript-focus.js index df4c68c6..150448ac 100644 --- a/PlotLine/wwwroot/js/shared-manuscript-focus.js +++ b/PlotLine/wwwroot/js/shared-manuscript-focus.js @@ -8,7 +8,7 @@ function refresh(force=false){if(!book||!connected)return Promise.resolve(false) async function navigate(target,forcePublication=false){const priorLocal=local;if(target.localSelection&&Number(target.bookID||book)===book){local=normal({...local,projectID:project,bookID:book,chapterID:target.chapterID,sceneID:target.sceneID||null,paragraphID:target.paragraphID});save();}if(publishing){queuedTarget=target;return true;}const effective=held?priorLocal:latest?.isValid?latest:null;if(!forcePublication&&Number(target.sceneID||target.sceneId)>0&&Number(target.sceneID||target.sceneId)===effective?.sceneID&&!effective.paragraphID&&!target.paragraphID&&!target.paragraphId)return true;if(!forcePublication&&!target.direction&&!target.sceneID&&!target.sceneId&&!target.paragraphID&&!target.paragraphId&&Number(target.chapterID||target.chapterId)>0&&Number(target.chapterID||target.chapterId)===effective?.chapterID&&!effective.sceneID&&!effective.paragraphID)return true;const paragraph=target.paragraphID||target.paragraphId;if(!forcePublication&¶graph&¶graph===effective?.paragraphID&&Number(target.chapterID||target.chapterId)===effective.chapterID)return true;if(publishing)return false;if(target.bookID&&Number(target.bookID)!==book)return false;publishing=true;const localRequest=held;selectionPublishing=target.localSelection===true;try{const normalized={chapterID:target.chapterID||target.chapterId||null,sceneID:target.sceneID||target.sceneId||null,paragraphID:target.paragraphID||target.paragraphId||null,...(target.direction?{direction:target.direction}:{}),...(target.manuscriptID?{manuscriptID:target.manuscriptID}:{})};if(held&&target.direction)Object.assign(normalized,{chapterID:local.chapterID,sceneID:local.sceneID,paragraphID:local.paragraphID});const result=await request(localRequest?'Resolve':'Publish',normalized);if(result.bookID!==book||result.projectID!==project)return false;if(localRequest){if(held)apply(result,selectionPublishing?'selection':'local');else await refresh(true);}else accept(result,true);return true;}catch(error){status(error.message);return false;}finally{publishing=false;selectionPublishing=false;if(queuedTarget){const next=queuedTarget;queuedTarget=null;void navigate(next,true);}}} async function toggle(){if(!connected||!authoritative||disconnected)return;queuedTarget=null;if(held){if(!await refresh()){status('Hold retained: reconnect before catching up to authoritative shared focus.');return;}held=false;save();if(latest?.isValid&&latest.chapterID)apply(latest,'release');else status('Following ยท no valid shared focus; local position retained.');}else{held=true;save();status();}window.dispatchEvent(new CustomEvent('plotdirector:focus-mode',{detail:{held}}));} async function validateLocal(){if(!held||!local?.chapterID)return;try{const result=await request('Validate',local);if(!result.isValid)status('Held reference is no longer valid. Select another local position or Release Hold.');}catch{status('Held reference cannot be resolved. Select another position; data updates remain connected.');}} -function ready(c,reconnected=false){if(!c||c!==connection||c.state!=='Connected')return;connected=true;const key=c.connectionId||'connected';if(!reconnected&&readyKey===key){if(!authoritative)void refresh();return;}readyKey=key;invalidateRead();status();trace('connection',reconnected?'reconnected':'ready');void refresh();} +function ready(c,reconnected=false){if(!c||c!==connection||c.state!=='Connected')return;connected=true;const key=c.connectionId||'connected';if(readyKey===key){if(!authoritative)void refresh();return;}readyKey=key;invalidateRead();status();trace('connection',reconnected?'reconnected':'ready');void refresh();} function wire(c){if(!c||connection===c)return;if(connection)return;connection=c;c.on('ManuscriptFocusChanged',packet=>{trace('notification','received',normal(packet));accept(packet);});c.on('ManuscriptFocusDelivery',packet=>{if(Number(packet.bookID||packet.bookId)===book&&Number(packet.version)===latest?.version)status('Word cannot follow this reference in the background. Save and synchronise Word; other windows remain independent.');});c.on('ContextInvalidated',hint=>{if(hint.bookId&&Number(hint.bookId)!==book||hint.projectId&&Number(hint.projectId)!==project)return;window.dispatchEvent(new CustomEvent('plotdirector:reference-invalidated',{detail:hint}));void validateLocal();});function offline(){connected=false;disconnected=true;readyKey=null;invalidateRead();status('Shared focus disconnected. Your local position is retained.');trace('connection','disconnected');}c.onreconnecting(offline);c.onclose(offline);c.onreconnected(()=>ready(c,true));ready(c);} const channel=!context&&window.BroadcastChannel?new BroadcastChannel('pd.shared-focus.identity.'+user):null;channel?.addEventListener('message',event=>{const m=event.data;if(m.id!==id||m.page===page)return;if(m.kind==='claim')channel.postMessage({kind:'occupied',id,page,target:m.page});if(m.kind==='occupied'&&m.target===page){id=crypto.randomUUID();sessionStorage.setItem('pd.shared-focus.window',id);held=false;sequence=0;save();status();void refresh(true);}});channel?.postMessage({kind:'claim',id,page}); context?.addEventListener('context-capabilities-changed',()=>{const next=context.contextController?.windowId;if(next&&next!==id){id=next;held=false;sequence=0;save();status();void refresh(true);}});