mirror of
https://github.com/aljazceru/pear-docs.git
synced 2025-12-17 14:34:19 +01:00
navigation refactor
This commit is contained in:
@@ -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 = ''
|
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|||||||
11
pulse.html
11
pulse.html
@@ -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">
|
||||||
|
|||||||
Reference in New Issue
Block a user