// Read-only book projection. Exploring changes this view only; Word navigation stays explicit. (() => { 'use strict'; const states=new WeakMap(); const node=(tag,text,cls)=>{const el=document.createElement(tag);if(text!==undefined)el.textContent=text;if(cls)el.className=cls;return el;}; const reference=scene=>`Ch ${scene.chapter} · Sc ${scene.number}`; function nearest(entries,index){ let lo=0,hi=entries.length; while(lo>1;if(entries[mid].index[s.id,i])),byLine=new Map(data.lines.map(l=>[l.id,{line:l,associations:new Set(),entries:[]}])); for(const item of data.memberships){const line=byLine.get(item.line),index=indices.get(item.scene);if(!line||index===undefined)continue;line.associations.add(index);const developments=item.developments.filter(d=>d.title?.trim()||d.detail?.trim());if(developments.length)line.entries.push({index,developments});} for(const line of byLine.values())line.entries.sort((a,b)=>a.index-b.index); const state={fragment,data,indices,lines:[...byLine.values()],selected:null,current:null,filters:new Set(JSON.parse(fragment.closest('[data-writing-component]').dataset.plotCategories||'["active","nearby"]'))};states.set(fragment,state); fragment.addEventListener('click',event=>{ const button=event.target.closest('button');if(!button)return; if(button.hasAttribute('data-plot-category')){const key=button.dataset.plotCategory;if(state.filters.has(key))state.filters.delete(key);else state.filters.add(key);render(state);} if(button.hasAttribute('data-plot-all')){state.filters=new Set(['active','nearby','elsewhere']);render(state);} if(button.hasAttribute('data-plot-scene')){state.selected=Number(button.dataset.plotScene);render(state);} if(button.hasAttribute('data-plot-step')){state.selected=Math.max(0,Math.min(data.scenes.length-1,state.selected+Number(button.dataset.plotStep)));render(state);} if(button.hasAttribute('data-plot-return')){state.selected=state.current;render(state);window.dispatchEvent(new CustomEvent('plotdirector:word-return-to-current'));} }); fragment.querySelector('[data-plot-scrubber]')?.addEventListener('input',event=>{state.selected=Number(event.target.value);render(state);}); return state; } function developmentCard(state,entry,label,cls,associated=false){ const card=node('section',undefined,'writing-plot-development '+cls);card.append(node('h4',label)); if(!entry){card.append(node('strong',label.startsWith('Now')?'No recorded development':label==='Before'?'No earlier recorded development':'No later recorded development')); if(associated)card.append(node('p','Saved association with this scene; no development text is recorded.','small text-muted')); return card; } const scene=state.data.scenes[entry.index];card.append(node('p',`${reference(scene)} — ${scene.title}`,'writing-plot-reference')); if(label==='After')card.append(node('p','Upcoming in manuscript order','small text-muted')); const list=node('ul',undefined,'writing-plot-developments'); for(const development of entry.developments){const item=node('li');if(development.thread)item.append(node('p',development.thread,'small text-muted')); if(development.title)item.append(node('strong',development.title)); if(development.detail){const detail=node('p',development.detail,'writing-plot-detail');if(development.detail.length>240){item.append(node('p',development.detail.slice(0,240)+'…'));const more=node('details');more.append(node('summary','Full saved detail'),detail);item.append(more);}else item.append(detail);} if(!development.title&&!development.detail)item.append(node('p',development.role||'Saved association; no development text recorded.')); list.append(item); } card.append(list);return card; } function render(state){ const {fragment,data,selected,current}=state,scene=data.scenes[selected]; fragment.closest('[data-writing-component]').dataset.plotCategories=JSON.stringify([...state.filters]); const counts={active:0,nearby:0,elsewhere:0}; const rows=state.lines.map(line=>{const context=nearest(line.entries,selected);const category=line.associations.has(selected)?'active':context.before||context.after?'nearby':'elsewhere';counts[category]++;return {...line,context,category};}); fragment.querySelectorAll('[data-plot-category]').forEach(b=>b.setAttribute('aria-pressed',String(state.filters.has(b.dataset.plotCategory)))); fragment.querySelectorAll('[data-plot-count]').forEach(n=>n.textContent=`(${counts[n.dataset.plotCount]})`); if(scene){ const range=fragment.querySelector('[data-plot-scrubber]');range.value=selected;range.setAttribute('aria-valuetext',`${reference(scene)} — ${scene.title}`); const status=fragment.querySelector('[data-plot-position]');status.textContent=`${selected===current?'Current scene':'Exploring'}: ${reference(scene)} — ${scene.title}${selected!==current&&data.scenes[current]?` · Word/current scene: ${reference(data.scenes[current])}`:''}`; const go=fragment.querySelector('[data-context-go-scene]');go.dataset.contextGoScene=scene.id; fragment.querySelectorAll('[data-plot-step]').forEach(b=>b.disabled=Number(b.dataset.plotStep)<0?selected===0:selected===data.scenes.length-1); const strip=fragment.querySelector('[data-plot-scene-strip]'),focused=strip.contains(document.activeElement),buttons=[]; for(let i=Math.max(0,selected-3);i<=Math.min(data.scenes.length-1,selected+3);i++){ const s=data.scenes[i],button=node('button',undefined,'btn btn-outline-secondary');button.type='button';button.dataset.plotScene=i;button.setAttribute('aria-label',`${reference(s)} — ${s.title}`);if(i===selected)button.setAttribute('aria-current','location');button.append(node('span',reference(s)));if(i===selected)button.append(node('strong',s.title));buttons.push(button); } strip.replaceChildren(...buttons);if(focused)strip.querySelector('[aria-current]')?.focus({preventScroll:true}); const active=strip.querySelector('[aria-current]');if(active)strip.scrollLeft=Math.max(0,active.offsetLeft-strip.offsetLeft-(strip.clientWidth-active.clientWidth)/2); } const output=fragment.querySelector('[data-plot-rows]'),rendered=[]; for(const category of ['active','nearby','elsewhere'])for(const row of rows.filter(r=>r.category===category&&state.filters.has(category))){ const article=node('article',undefined,'writing-plot-context-row');article.dataset.plotLine=row.line.id;article.dataset.plotStatus=category; // Validate the saved colour before using it as a CSS value; all prose uses textContent. if(CSS.supports('color',row.line.colour))article.style.setProperty('--line-colour',row.line.colour); const identity=node('div',undefined,'writing-plot-identity');identity.append(node('h3',row.line.name));if(row.line.description)identity.append(node('p',row.line.description)); let status=category==='active'?'Active here':category==='nearby'?'Nearby':'Elsewhere'; if(category==='nearby'){const distances=[];if(row.context.before)distances.push(`previous ${selected-row.context.before.index} scene${selected-row.context.before.index===1?'':'s'} ago`);if(row.context.after)distances.push(`next in ${row.context.after.index-selected} scene${row.context.after.index-selected===1?'':'s'}`);status+=` · ${distances.join(' · ')}`;} identity.append(node('p',status,'writing-plot-status'));article.append(identity); article.append(developmentCard(state,row.context.before,'Before','is-before'),developmentCard(state,row.context.now,'Now — This Scene','is-now',row.associations.has(selected)),developmentCard(state,row.context.after,'After','is-after')); rendered.push(article); } if(!scene)output.replaceChildren(node('p',data.scenes.length?'Select a recognised scene to show plot line context.':'No manuscript scenes are available for this book.')); else if(!rendered.length)output.replaceChildren(node('p',data.lines.length?'No plot lines in the selected categories. Use Show all to include every saved plot line.':'No saved plot lines are available for this book.')); else output.replaceChildren(...rendered); } function refresh(host){const fragment=host.querySelector('[data-context-plot-cards]');if(!fragment)return;const state=states.get(fragment)||mount(fragment);const id=Number(host.closest('[data-writing-context]').querySelector('[data-writing-context-fragment]')?.dataset.sceneId),current=state.indices.get(id)??null; if(current!==state.current||state.selected===null){state.current=current;state.selected=current;render(state);} } window.PlotDirectorContextPlots={refresh}; })();