navigation refactor

This commit is contained in:
rafapaezbas
2024-01-23 13:44:14 +01:00
parent 3ad2f72142
commit 09d575d90e
2 changed files with 43 additions and 51 deletions

View File

@@ -54,6 +54,8 @@ customElements.define('docs-viewer', class extends HTMLElement {
this.home = document.querySelector('#home') this.home = document.querySelector('#home')
this.status = document.querySelector('#status')
this.page = '/readme.md' this.page = '/readme.md'
this.home.style.display = 'none' this.home.style.display = 'none'
@@ -64,59 +66,60 @@ customElements.define('docs-viewer', class extends HTMLElement {
}) })
observer.observe(document.documentElement, { attributes: true, attributeFilter: ['class'] }) observer.observe(document.documentElement, { attributes: true, attributeFilter: ['class'] })
this.panel.addEventListener('click', (evt) => { this.panel.addEventListener('click', async (evt) => {
evt.preventDefault()
if (evt.target?.tagName !== 'A') return if (evt.target?.tagName !== 'A') return
const targetUrl = new URL(evt.target.href)
if (targetUrl.hash === '#documentation') {
await this.load('/readme.md')
return
}
const href = evt.target.href const href = evt.target.href
const { origin } = new URL(location.href) const { origin } = new URL(location.href)
const url = new URL(href, location.href) const url = new URL(href, location.href)
if (url.origin !== origin) return window.open(href) if (url.origin !== origin) return window.open(href)
evt.preventDefault()
this.load(evt.target.pathname) this.load(evt.target.pathname)
}) })
window.addEventListener('hashchange', () => {
if (location.hash === '#documentation' && this.page === '/readme.md') this.home.style.display = 'none'
else this.home.style.display = ''
})
window.addEventListener('popstate', (evt) => { window.addEventListener('click', async (evt) => {
if (location.hash.startsWith('#documentation')) { evt.preventDefault()
const home = location.hash.endsWith('-home') if (evt.target?.tagName !== 'A') return
if (home) { const targetUrl = new URL(evt.target.href)
this.page = '/readme.md' if (targetUrl.hash === '#documentation') {
history.replaceState(null, null, this.page + location.hash) const docsViewer = document.querySelector('docs-viewer')
location.href = '/pulse.html#documentation' await docsViewer.load('/readme.md')
return
}
if (location.pathname.endsWith('.md') === false) {
history.replaceState(null, null, this.page + location.hash)
return
}
evt.preventDefault()
this.page = location.pathname
this.#load().catch((err) => this.#error(err))
} }
}) })
this.#load().catch((err) => this.#error(err)) window.addEventListener('popstate', async (evt) => {
console.log(evt)
if (evt.state === null) {
this.home.style.display = 'none'
this.panel.style.display = 'none'
this.status.style.display = ''
return
}
const docsViewer = document.querySelector('docs-viewer')
await docsViewer.load(evt.state.page, { back: true })
return
})
const config = global[Symbol.for('pear.config')] || {}
if (config.link && config.link !== 'pear:dev') {
window.addEventListener('load', async () => {
await this.load(config.link.slice(5))
})
}
} }
#error (err) { async load (page, opts = {}) {
try { this.panel.querySelector('slot').innerHTML = err.stack } catch (e) { console.error(err, e) } const html = await fetch(page)
} const text = await marked.parse(await html.text())
this.panel.querySelector('slot').innerHTML = text, { headerIds: false, mangle: false }
load (page) { this.home.style.display = ''
document.documentElement.scrollTo(0, 0) this.panel.style.display = ''
this.page = page this.status.style.display = 'none'
history.pushState(null, null, page + location.hash) if (!opts.back) history.pushState({ page }, null)
return this.#load().catch((err) => this.#error(err))
}
async #load () {
this.panel.querySelector('slot').innerHTML = marked.parse(await (await fetch(this.page)).text(), { headerIds: false, mangle: false })
if (location.hash === '#documentation' && this.page === '/readme.md') this.home.style.display = 'none'
else this.home.style.display = ''
} }
}) })

View File

@@ -64,11 +64,6 @@
} }
section { section {
display: none;
}
section:target {
display: block; display: block;
padding-top: 156px; padding-top: 156px;
} }
@@ -209,12 +204,6 @@
<a href="#status" style="text-indent: -1rem">System Status</a> <a href="#status" style="text-indent: -1rem">System Status</a>
<a href="#documentation">Documentation</a> <a href="#documentation">Documentation</a>
</nav> </nav>
<pre>
__ \ _ \ _` | __|
| | __/ ( | |
.__/ \___| \__,_| _|
_|
</pre>
</div> </div>
</header> </header>
<section id="status"> <section id="status">