PlotDirector/PlotLine/wwwroot/js/scene-explorer.js

59 lines
5.8 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.

// Local presentation only. The optional promotion callback is the sole explicit global write path.
(() => {
'use strict';
let adapter=null;
window.PlotDirectorFocusWorkspace={register(value){adapter=value;window.dispatchEvent(new Event('plotdirector:focus-adapter-ready'));},get adapter(){return adapter;}};
window.PlotDirectorSceneExplorer = {
create({ host, positions, selected, change, select = null, range = null, previous = null, next = null,
output = null, enabled = () => true, focusAdapter = true, reveal = () => {}, listenSelect = true, listenRange = true, listenButtons = true }) {
const controls = document.createElement('div'); controls.className = 'pd-scene-explorer';
controls.setAttribute('role', 'group'); controls.setAttribute('aria-label', 'Local Scene Explorer');
const make = (tag, text) => { const node = document.createElement(tag); if (text) node.textContent = text; controls.append(node); return node; };
const ownedSelect = !select;
select ||= make('select'); select.setAttribute('aria-label', 'Scene to visualise');
if (!previous) { previous = make('button', '‹ Previous Scene'); previous.type = 'button'; }
if (!next) { next = make('button', 'Next Scene ›'); next.type = 'button'; }
previous.dataset.ribbonIcon = 'chevron-left'; next.dataset.ribbonIcon = 'chevron-right';
for(const [button,key]of [[previous,'chevron-left'],[next,'chevron-right']]){const svg=document.querySelector('[data-pwa-icon-catalog]')?.content.querySelector(`[data-icon-key="${key}"]`)?.cloneNode(true);if(svg&&!button.querySelector('svg')){svg.removeAttribute('data-icon-key');button.textContent=key==='chevron-left'?'Previous Scene':'Next Scene';button.prepend(svg);}}
previous.title = 'Visualise the previous scene'; next.title = 'Visualise the next scene';
if (!range) { range = make('input'); range.type = 'range'; }
range.setAttribute('aria-label', 'Scene position in manuscript order'); range.step = '1';
output ||= make('output'); output.setAttribute('aria-live', 'polite');
const hint = make('small', 'Scene Explorer changes this visualisation only.');
const promote=make('button','Set Current Focus Here');promote.type='button';promote.hidden=true;
controls.append(hint);
promote.addEventListener('click',async()=>{const item=items[index];const focus=window.PlotDirectorNarrativeFocus;if(!item||!focus)return;const target=focus.state.positions.find(p=>String(p.sceneID)===String(item.id));if(target)await focus.set(target);});
function promotion(){const focus=window.PlotDirectorNarrativeFocus;promote.hidden=!focus;promote.disabled=!focus||!items[index]||!focus.state.positions.some(p=>String(p.sceneID)===String(items[index].id));promote.title=promote.disabled?'Choose a scene from the current book to set global focus':'Set current narrative focus without changing this visualisation';}
const events=new AbortController();window.addEventListener('plotdirector:narrative-focus-changed',promotion,{signal:events.signal});
host.append(controls);
let items = [], index = -1, signature = '';
function sync() {
if (!enabled()) return;
items = positions(); index = items.findIndex(p => String(p.id) === String(selected()));
const nextSignature = items.map(p => `${p.id}:${p.label}`).join('|');
if (ownedSelect && signature !== nextSignature) {
select.replaceChildren(...items.map(p => new Option(p.label, String(p.id)))); signature = nextSignature;
}
select.value = index >= 0 ? String(items[index].id) : '';
range.min = '0'; range.max = String(Math.max(0, items.length - 1)); range.value = String(Math.max(0, index));
range.disabled = !items.length; previous.disabled = !items.some((p,i)=>p.isScene!==false&&i<index); next.disabled = !items.some((p,i)=>p.isScene!==false&&i>index);
output.textContent = index >= 0 ? items[index].label : 'No scene selected'; range.setAttribute('aria-valuetext', output.textContent);
select.disabled = !items.length; promotion();
}
function choose(id) {
if (!enabled()) return false;
items = positions(); const found = items.find(p => String(p.id) === String(id));
if (!found) return false;
change(found); sync();return true;
}
function step(direction) { sync();const candidates=items.map((p,i)=>({p,i})).filter(({p,i})=>p.isScene!==false&&(direction<0?i<index:i>index));const target=direction<0?candidates.at(-1):candidates[0];if(target)choose(target.p.id); }
if (listenSelect) select.addEventListener('change', () => choose(select.value));
if (listenRange) range.addEventListener('input', () => { const item = positions()[Number(range.value)]; if (item) choose(item.id); });
if (listenButtons) { previous.addEventListener('click', () => step(-1)); next.addEventListener('click', () => step(1)); }
if(focusAdapter)window.PlotDirectorFocusWorkspace.register({canGo:focus=>host.isConnected&&enabled()&&positions().some(p=>p.isScene!==false&&String(p.id)===String(focus.sceneID)),unavailable:'The focused scene is not available in this local Scene Explorer',go:focus=>{if(!enabled())throw Error('This Scene Explorer is unavailable during the current workspace operation.');reveal();if(!choose(focus.sceneID))throw Error('The focused scene is unavailable in this Scene Explorer.');}});
sync();
return { sync, choose, step, destroy:()=>events.abort(), get position() { return items[index] || null; }, controls, hint };
}
};
})();