(() => { 'use strict'; const instances = new WeakMap(); function mount(root) { if (instances.has(root)) return instances.get(root); const search = root.querySelector('[data-palette-search]'); const scene = root.querySelector('[data-palette-scene]'); const sceneChoice = root.querySelector('[data-palette-scene-choice]'); const wider = root.querySelector('[data-palette-wider]'); const rows = root.querySelector('[data-palette-rows]'); const status = root.querySelector('[data-palette-status]'); const events = new AbortController(); const projectId = Number(root.dataset.projectId) || Number(root.closest('[data-writer-destination]')?.dataset.projectId); const wired = new WeakSet(); let pending; let debounce; function render(frame) { const entries = frame.entries.map(entry => { const row = document.createElement('li'); row.className = 'list-group-item px-0'; row.dataset.entityKey = entry.key; const name = document.createElement('strong'); name.textContent = entry.name; const details = document.createElement('div'); details.className = 'small text-body-secondary'; const provenance = [...new Set(entry.sources.map(source => source.provenance))]; if (entry.isCandidate && !provenance.includes('Candidate')) provenance.push('Candidate'); if (!entry.inScope) provenance.push('Wider story'); if (entry.possiblyAmbiguous) provenance.push('Shared name or alias'); details.textContent = [entry.kind, ...provenance].join(' · '); row.append(name, details); const aliases = entry.aliases.filter(alias => alias !== entry.name); if (aliases.length) { const aliasList = document.createElement('div'); aliasList.className = 'small text-body-secondary'; aliasList.textContent = 'Aliases ' + aliases.join(', '); row.append(aliasList); } row.draggable = true; const selection = { key: entry.key, name: entry.name }; row.addEventListener('dragstart', event => { event.dataTransfer.setData('application/x-plotdirector-intent-element', JSON.stringify(selection)); event.dataTransfer.effectAllowed = 'link'; }); const select = document.createElement('button'); select.type = 'button'; select.className = 'btn btn-sm btn-outline-secondary mt-1'; select.textContent = 'Select to link'; select.setAttribute('aria-label', 'Select ' + entry.name + ' to link'); select.addEventListener('click', () => checklist(root.parentElement)?.selectElement(selection)); row.append(select); return row; }); rows.replaceChildren(...entries); status.textContent = frame.hasMore ? 'Showing the first 200 elements. Refine your search.' : entries.length ? '' : 'No Story Elements found.'; } async function refresh() { clearTimeout(debounce); pending?.abort(); if (!root.open || !root.isConnected) return; const controller = new AbortController(); pending = controller; const url = new URL(root.dataset.url, location.origin); url.searchParams.set('bookID', root.dataset.bookId); url.searchParams.set('chapterID', root.dataset.chapterId); if (scene.checked) url.searchParams.set('sceneID', root.dataset.sceneId); url.searchParams.set('widerStory', String(wider.checked || !!search.value.trim())); url.searchParams.set('search', search.value); status.textContent = 'Loading Story Elements…'; try { const response = await fetch(url, { signal: controller.signal, cache: 'no-store' }); if (!response.ok || response.redirected) throw new Error(); const frame = await response.json(); if (controller.signal.aborted || !root.isConnected) return; if (projectId && frame.scope.projectID !== projectId) throw new Error(); render(frame); } catch { if (controller.signal.aborted || !root.isConnected) return; rows.replaceChildren(); status.textContent = 'Story Elements are unavailable. Reopen the palette to retry.'; } } search.addEventListener('input', () => { pending?.abort(); clearTimeout(debounce); debounce = setTimeout(refresh, 250); }, { signal: events.signal }); root.addEventListener('toggle', refresh, { signal: events.signal }); scene.addEventListener('change', refresh, { signal: events.signal }); wider.addEventListener('change', refresh, { signal: events.signal }); function wireWriter(connection) { if (!connection || wired.has(connection)) return; wired.add(connection); const invalidate = hint => { if (!hint?.projectId || Number(hint.projectId) === projectId) { void refresh(); if (!hint?.chapterId || Number(hint.chapterId) === Number(root.dataset.chapterId)) void checklist(root.parentElement)?.load(); } }; connection.on('ContextInvalidated', invalidate); events.signal.addEventListener('abort', () => connection.off('ContextInvalidated', invalidate), { once: true }); } if (root.closest('[data-writer-destination]')) { document.addEventListener('pd-presence-connection', event => wireWriter(event.detail), { signal: events.signal }); wireWriter(window.PlotDirectorPresenceConnection); } const instance = { refresh, setScene(sceneId) { root.dataset.sceneId = sceneId || ''; sceneChoice.hidden = !sceneId; if (!sceneId) scene.checked = false; void refresh(); }, dispose() { clearTimeout(debounce); pending?.abort(); events.abort(); instances.delete(root); } }; instances.set(root, instance); void refresh(); return instance; } const checklist = node => window.PlotDirectorIntentChecklist?.within(node); const mountChecklist = node => window.PlotDirectorIntentChecklist?.mountWithin(node); const palette = node => node.querySelector('[data-intent-palette]'); window.PlotDirectorIntentPalette = { mountWithin(node) { mountChecklist(node); node.querySelectorAll('[data-intent-palette]').forEach(mount); }, disposeWithin(node) { checklist(node)?.dispose(); node.querySelectorAll('[data-intent-palette]').forEach(root => instances.get(root)?.dispose()); } }; window.PlotDirectorContextLifecycles = { ...window.PlotDirectorContextLifecycles, intent: { mount(node) { mountChecklist(node); mount(palette(node)); }, canRefresh(node) { return !checklist(node)?.guarded(); }, dispose(node) { checklist(node)?.dispose(); instances.get(palette(node))?.dispose(); }, onPosition(node, next, previous) { if (next.chapterId !== previous.chapterId) return false; instances.get(palette(node))?.setScene(next.sceneId); checklist(node)?.setScene(next.sceneId); return true; }, onCacheInvalidation(hint, node) { if (node && checklist(node)?.guarded()) void instances.get(palette(node))?.refresh(); }, onInvalidation(node) { void instances.get(palette(node))?.refresh(); void checklist(node)?.load(); return true; } } }; })();