Restore Context document selection consistently in controls and reload routes

This commit is contained in:
Nick Beckley 2026-11-21 22:33:29 +00:00
parent cf8a969196
commit 7c3c8f7f09
2 changed files with 9 additions and 4 deletions

View File

@ -58,7 +58,7 @@ const lease=(extra={})=>({instanceId:'11111111-1111-4111-8111-111111111111',sess
step='session expiry and mapping conflict';mode='expired';await page.click('[data-context-refresh]');await page.waitForFunction(()=>document.querySelector('[data-context-pane-status]').textContent.includes('Sign in'));assert.match(await page.locator('[data-context-pane="0"]').innerText(),/revision 3/);mode='mapping';await page.click('[data-context-refresh]');await page.waitForFunction(()=>document.querySelector('[data-context-pane-status]').textContent.includes('mapping'));mode='ok';
step='disconnect and unchanged-position reconnect';revision=4;await page.evaluate(()=>contextConnections[0].reconnecting());assert.match(await page.locator('[data-context-status]').innerText(),/reconnecting/);await page.evaluate(()=>contextConnections[0].reconnected());await page.waitForFunction(()=>document.querySelector('[data-context-pane="0"]').textContent.includes('revision 4'));
step='native ambiguity and missing mapping';leases=[lease(),lease({instanceId:'44444444-4444-4444-8444-444444444444',active:true})];await page.evaluate(()=>window.dispatchEvent(new CustomEvent('plotdirector:refresh-word-status')));await page.waitForFunction(()=>document.querySelector('[data-context-status]').textContent.includes('Choose a Word document'));
await page.click('#native-companion-status');await page.selectOption('#writing-word-document','44444444-4444-4444-8444-444444444444/22222222-2222-4222-8222-222222222222');await page.waitForFunction(()=>document.querySelector('[data-context-workspace]').contextController.position.instanceId==='44444444-4444-4444-8444-444444444444');await page.selectOption('#writing-word-document','');await page.click('[data-close-word-details]');
await page.click('#native-companion-status');await page.selectOption('#writing-word-document','44444444-4444-4444-8444-444444444444/22222222-2222-4222-8222-222222222222');await page.waitForFunction(()=>document.querySelector('[data-context-workspace]').contextController.position.instanceId==='44444444-4444-4444-8444-444444444444');assert.equal(new URL(page.url()).searchParams.get('instanceId'),'44444444-4444-4444-8444-444444444444');await page.reload();await page.waitForFunction(()=>document.querySelector('[data-context-workspace]').contextController.position.instanceId==='44444444-4444-4444-8444-444444444444');await page.click('#native-companion-status');assert.equal(await page.locator('#writing-word-document').inputValue(),'44444444-4444-4444-8444-444444444444/22222222-2222-4222-8222-222222222222');await page.selectOption('#writing-word-document','');await page.click('[data-close-word-details]');
leases=[lease({positionVerified:false,positionMessage:'Mapping pending'})];await page.evaluate(()=>window.dispatchEvent(new CustomEvent('plotdirector:refresh-word-status')));await page.waitForFunction(()=>document.querySelector('[data-context-status]').textContent.includes('Mapping pending'));
step='Office fallback and native precedence';leases=[];office={connected:true,position:{projectId:7,bookId:12,chapterId:11,sceneId:23}};await page.evaluate(()=>window.dispatchEvent(new CustomEvent('plotdirector:refresh-word-status')));await page.waitForFunction(()=>document.querySelector('[data-context-workspace]').contextController.position.sceneId===23);
leases=[lease()];await page.evaluate(()=>window.dispatchEvent(new CustomEvent('plotdirector:refresh-word-status')));await page.waitForFunction(()=>document.querySelector('[data-context-workspace]').contextController.position.sceneId===22);

View File

@ -28,11 +28,16 @@
const stateKey = () => `plotdirector.context.v1.${root.dataset.user}.${windowId}`;
let state;
try { state=JSON.parse(read('localStorage',stateKey())); } catch { }
if (state?.version !== 1 || !Array.isArray(state.panes) || state.panes.length !== 2) state=defaults();
const restoredState=state?.version===1&&Array.isArray(state.panes)&&state.panes.length===2;
if (!restoredState) state=defaults();
if (!['single','columns','rows'].includes(state.layout)) state.layout='single';
state.ratio = Number.isFinite(state.ratio) ? Math.max(.2,Math.min(.8,state.ratio)) : .5;
state.panes=state.panes.map((pane,i)=>({tab:registry.has(pane?.tab)?pane.tab:defaults().panes[i].tab,views:pane?.views && typeof pane.views==='object'?pane.views:{}}));
let selectedDocument = root.dataset.wordInstance && root.dataset.wordSession ? `${root.dataset.wordInstance}/${root.dataset.wordSession}` : state.selectedDocument || '';
let selectedDocument = restoredState ? (typeof state.selectedDocument==='string'?state.selectedDocument:'') : root.dataset.wordInstance && root.dataset.wordSession ? `${root.dataset.wordInstance}/${root.dataset.wordSession}` : '';
function presentSelectedDocument() {
const [instance,session]=selectedDocument.split('/');root.dataset.wordInstance=instance||'';root.dataset.wordSession=session||'';
}
presentSelectedDocument();
let nativeKey = '', unavailable = false;
const save = () => { state.selectedDocument=selectedDocument; write('sessionStorage','plotdirector.context.window.v1',windowId); write('localStorage',stateKey(),JSON.stringify(state)); };
const panes = [...root.querySelectorAll('[data-context-pane]')].map((element,index)=>({ element,index,select:element.querySelector('[data-context-tab]'),host:element.querySelector('[data-context-pane-content]'),status:element.querySelector('[data-context-pane-status]'),records:new Map(),revision:0 }));
@ -219,7 +224,7 @@
root.querySelector('[data-context-new]').addEventListener('click',()=>window.PlotDirectorWritingWorkspace.openContext({...position,pinned}));
root.querySelector('[data-context-refresh]').addEventListener('click',()=>invalidate());
window.addEventListener('native-companion-presence',event=>{if(!pinned)followNative(event.detail||[],!!event.detail?.reconnected);});
window.addEventListener('plotdirector:word-document-selected',event=>{if(pinned)return;selectedDocument=event.detail||'';nativeKey='';save();followNative(nativeLeases,true);});
window.addEventListener('plotdirector:word-document-selected',event=>{if(pinned)return;selectedDocument=event.detail||'';presentSelectedDocument();const url=new URL(location.href);if(selectedDocument){url.searchParams.set('instanceId',root.dataset.wordInstance);url.searchParams.set('sessionId',root.dataset.wordSession);}else{url.searchParams.delete('instanceId');url.searchParams.delete('sessionId');}history.replaceState(null,'',url);nativeKey='';save();followNative(nativeLeases,true);});
window.addEventListener('plotdirector:word-return-to-current',()=>{if(!pinned){nativeKey='';followNative(nativeLeases,true);}});
function wakeStatus(reconnected=false){window.dispatchEvent(new CustomEvent('plotdirector:refresh-word-status',{detail:{reconnected}}));}
async function startConnection(){