85 lines
7.9 KiB
JavaScript
85 lines
7.9 KiB
JavaScript
// Explicit presentation adapters. None of these actions writes narrative focus.
|
||
(() => {
|
||
'use strict';
|
||
const registry=window.PlotDirectorFocusWorkspace,shell=document.querySelector('[data-workspace-controller]');if(!registry||!shell)return;
|
||
const controller=shell.dataset.workspaceController,action=shell.dataset.workspaceAction;
|
||
function offer(message,label,perform){let output=document.querySelector('[data-focus-workspace-status]');if(!output){output=document.createElement('p');output.dataset.focusWorkspaceStatus='';output.setAttribute('role','status');shell.after(output);}output.replaceChildren(document.createTextNode(message+' '));if(perform){const button=document.createElement('button');button.type='button';button.className='btn btn-sm btn-outline-secondary';button.textContent=label;button.addEventListener('click',perform);output.append(button);}}
|
||
const sceneURL=focus=>{const url=new URL('/Timeline/Index',location.origin);url.search=new URLSearchParams({projectId:focus.projectID,bookId:focus.bookID,selectedSceneId:focus.sceneID,retainNarrativeFocus:true});return url;};
|
||
const scenePage=document.querySelector('[data-focus-scene]');
|
||
if(scenePage&&Number(scenePage.dataset.focusScene)>0){
|
||
const scene=Number(scenePage.dataset.focusScene),editor=scenePage.querySelector('[data-scene-workspace]');
|
||
let approved=false,leaving=false,priorInert=false,locked=false;
|
||
const dirty=()=>!!scenePage.querySelector('[data-scene-editor-overlay][data-dirty=true]');
|
||
const saving=()=>!!scenePage.querySelector('[data-scene-editor-save]:disabled');
|
||
const scope=f=>f?.projectID===Number(scenePage.dataset.project)&&f.bookID===Number(scenePage.dataset.book)&&!!f.chapterID;
|
||
function permit(){
|
||
if(saving()){offer('Wait for the scene editor to finish saving before navigating.');return false;}
|
||
return !dirty()||window.confirm('Discard unsaved scene edits and navigate to Current Focus?');
|
||
}
|
||
function align(focus,explicit=false){
|
||
if(!scope(focus)){offer('Current Focus belongs to another book. This scene’s editing target is unchanged.');return;}
|
||
if(focus.sceneID===scene)return;
|
||
if(!approved&&(dirty()||saving())&&(!explicit||!permit())){
|
||
offer('Current Focus changed elsewhere. Your unsaved scene edits are retained.','Go to Focus',()=>align(window.PlotDirectorNarrativeFocus.state,true));return;
|
||
}
|
||
leaving=true;
|
||
const path=focus.sceneID?'/Scenes/Edit/'+focus.sceneID:'/Chapters/Details/'+focus.chapterID;
|
||
location.assign(path+'?retainNarrativeFocus=true');
|
||
}
|
||
registry.register({canGo:scope,unavailable:'Choose a chapter or scene in this scene’s book',go:f=>align(f,true),
|
||
beforeChange:target=>{
|
||
if(target.sceneID===scene&&!target.direction)return true;
|
||
if(!permit())return false;
|
||
approved=true;
|
||
// Keep the guarded draft stable until the authoritative request succeeds or fails.
|
||
if(editor){priorInert=editor.inert;editor.inert=true;locked=true;}
|
||
return true;
|
||
},changeFinished:()=>{approved=false;if(locked){editor.inert=priorInert;locked=false;}}});
|
||
window.addEventListener('plotdirector:narrative-focus-changed',e=>align(e.detail));
|
||
window.addEventListener('beforeunload',e=>{if(!leaving&&dirty()){e.preventDefault();e.returnValue='';}});
|
||
window.addEventListener('pageshow',()=>{leaving=false;});
|
||
const initial=window.PlotDirectorNarrativeFocus?.state;if(initial&&window.PlotDirectorSharedFocus)align(initial);
|
||
}
|
||
const chapterPage=document.querySelector('[data-focus-chapter]');
|
||
if(chapterPage){
|
||
let submitting=false;
|
||
chapterPage.addEventListener('input',e=>{const form=e.target.closest('form');if(form)form.dataset.focusDirty='true';});
|
||
chapterPage.addEventListener('change',e=>{const form=e.target.closest('form');if(form)form.dataset.focusDirty='true';});
|
||
chapterPage.addEventListener('reset',e=>delete e.target.dataset.focusDirty);
|
||
chapterPage.addEventListener('submit',()=>{submitting=true;});
|
||
const blocked=()=>submitting||chapterPage.querySelector('form[data-focus-dirty=true],[data-busy=true],form[aria-busy=true]')||document.querySelector('dialog[open],.modal.show');
|
||
const scope=focus=>focus?.projectID===Number(chapterPage.dataset.project)&&focus.bookID===Number(chapterPage.dataset.book)&&!!focus.chapterID;
|
||
function align(focus){
|
||
if(!scope(focus))return;
|
||
if(blocked()){offer('Current Focus changed. Save or cancel the current chapter edits before following it.','Go to Focus',()=>align(window.PlotDirectorNarrativeFocus.state));return;}
|
||
if(focus.chapterID!==Number(chapterPage.dataset.chapter)){
|
||
const path=controller==='Writer'?'/Writer/Chapter/':'/Chapters/Details/';location.assign(path+focus.chapterID+'?retainNarrativeFocus=true');return;
|
||
}
|
||
chapterPage.querySelectorAll('[aria-current=location]').forEach(n=>n.removeAttribute('aria-current'));
|
||
const scene=focus.sceneID?chapterPage.querySelector(`[data-pd-context="scene"][data-pd-context-id="${focus.sceneID}"]`):null;
|
||
if(focus.sceneID&&!scene){offer('The focused scene is not present in this chapter view.','Refresh chapter',()=>{const url=new URL(location.href);url.searchParams.set('retainNarrativeFocus','true');location.assign(url);});return;}
|
||
document.querySelector('[data-focus-workspace-status]')?.remove();
|
||
const target=scene||chapterPage.querySelector('h1');if(scene)scene.setAttribute('aria-current','location');target?.scrollIntoView({block:'center',behavior:'instant'});
|
||
}
|
||
registry.register({canGo:scope,unavailable:'Choose a chapter in this chapter page’s book',go:align});
|
||
window.addEventListener('plotdirector:narrative-focus-changed',e=>align(e.detail));
|
||
const initial=window.PlotDirectorNarrativeFocus?.state;if(initial&&window.PlotDirectorSharedFocus)align(initial);
|
||
window.addEventListener('beforeunload',e=>{if(chapterPage.querySelector('form[data-focus-dirty=true]')&&!submitting){e.preventDefault();e.returnValue='';}});
|
||
}
|
||
if(controller==='Books'&&action==='Details')registry.register({canGo:focus=>!!focus.chapterID,go:focus=>location.assign(`/Chapters/Details/${focus.chapterID}?retainNarrativeFocus=true`)});
|
||
if(controller==='Timeline'&&action==='Index')registry.register({canGo:focus=>!!focus.sceneID,go:focus=>{
|
||
const card=document.querySelector(`.timeline-scene-card[data-scene-id="${focus.sceneID}"]`);
|
||
if(!card||card.hidden||!card.getClientRects().length){offer('Current Focus is hidden by this Timeline view. Your filters are retained.','Reveal focused scene',()=>location.assign(sceneURL(focus)));return;}
|
||
card.scrollIntoView({block:'center',inline:'center'});
|
||
const url=new URL(location.href);for(const key of [...url.searchParams.keys()])if(['selectedsceneid','navigatefocus','retainnarrativefocus'].includes(key.toLowerCase()))url.searchParams.delete(key);
|
||
url.searchParams.set('selectedSceneId',focus.sceneID);url.searchParams.set('retainNarrativeFocus','true');location.assign(url);
|
||
}});
|
||
const plotForm=document.getElementById('plot-workspace-form');
|
||
if(plotForm)registry.register({canGo:focus=>!!focus.sceneID&&Number(plotForm.querySelector('[name=projectId]')?.value)===focus.projectID,go:focus=>{
|
||
const book=Number(plotForm.querySelector('[name=bookId]')?.value);if(book&&book!==focus.bookID){offer('Current Focus belongs to another book. Your weave scope is retained.','Reveal in Timeline',()=>location.assign(sceneURL(focus)));return;}
|
||
const card=[...document.querySelectorAll('[data-plot-scene]')].find(n=>n.querySelector(`[data-scene-inspector="${focus.sceneID}"]`));
|
||
if(!card||card.hidden){offer('Current Focus is outside the visible weave or hidden by its filters. Your selected plot lines and search are retained.','Inspect focused scene',()=>{const trigger=document.createElement('button');trigger.type='button';trigger.dataset.sceneInspector=focus.sceneID;trigger.hidden=true;document.body.append(trigger);trigger.click();trigger.remove();});return;}
|
||
document.querySelectorAll('.plot-weave-focus').forEach(n=>n.classList.remove('plot-weave-focus'));card.classList.add('plot-weave-focus');card.scrollIntoView({block:'nearest',inline:'center'});card.querySelector('[data-scene-inspector]')?.focus({preventScroll:true});
|
||
}});
|
||
})();
|