// Shared in-place page re-rendering for the editor tabs: fetch a page // without transitions and swap its dynamic regions into the live document. // Used by BannerEditor (banner design changes), SiteEditor (theme changes) // and StructureEditor (tree navigation). // Drop the public page runtime's in-memory prefetch cache. Editors call this // whenever a site-wide or page change invalidates the cached HTML of other // pages (theme, headings, structure, banner, etc.). The cache is rebuilt by // re-preloading visible links once the editor panel closes. export function dropPageCache() { dispatchEvent(new CustomEvent('pagerite:drop-page-cache')) } export function runScripts(root) { // Scripts injected via innerHTML do not execute; re-create them. if (!root) return for (const old of root.querySelectorAll('script')) { const s = document.createElement('script') for (const a of old.attributes) s.setAttribute(a.name, a.value) s.textContent = old.textContent old.replaceWith(s) } } function swapRegions(doc) { for (const id of ['page-banner', 'nav', 'main']) { const fresh = doc.getElementById(id) const el = document.getElementById(id) if (fresh && el) el.replaceWith(document.importNode(fresh, true)) } // #sidebar is omitted entirely when the section has no sub-navigation, // so it may be absent on either side: replace, insert, or remove. const freshSidebar = doc.getElementById('sidebar') const curSidebar = document.getElementById('sidebar') if (freshSidebar && curSidebar) { curSidebar.replaceWith(document.importNode(freshSidebar, true)) } else if (freshSidebar) { document.getElementById('main')?.before(document.importNode(freshSidebar, true)) } else if (curSidebar) { curSidebar.remove() } // The brand lives in the header, outside the swappable regions, and is // absent entirely when neither a brand nor custom brand HTML is set. A // plain link keeps its element (text swap only, preserving the shrink- // to-fit observers); anything else (custom HTML wrapper) is replaced. const freshBrand = doc.getElementById('brand') const curBrand = document.getElementById('brand') if (freshBrand && curBrand && freshBrand.tagName === 'A' && curBrand.tagName === 'A') { curBrand.textContent = freshBrand.textContent } else if (freshBrand && curBrand) { curBrand.replaceWith(document.importNode(freshBrand, true)) runScripts(document.getElementById('brand')) } else if (curBrand) { curBrand.remove() } else if (freshBrand) { document.getElementById('nav')?.before(document.importNode(freshBrand, true)) runScripts(document.getElementById('brand')) } // Site-wide custom CSS is in and must be swapped too. const freshUserStyle = doc.getElementById('pagerite-user') const curUserStyle = document.getElementById('pagerite-user') if (freshUserStyle && curUserStyle) { curUserStyle.textContent = freshUserStyle.textContent } else if (freshUserStyle) { document.head.appendChild(document.importNode(freshUserStyle, true)) } else if (curUserStyle) { curUserStyle.remove() } // Theme and other public stylesheets live in , rendered with stable // ids by the backend (links in dev, inline