PlotDirector/PlotLine/wwwroot/js/narrative-focus-workspaces.js

50 lines
5.6 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// 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 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;}
offer('Displaying Current Focus: '+focus.label);
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)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});
}});
})();