Skip to content

Commit

Permalink
feat: spa navigation
Browse files Browse the repository at this point in the history
  • Loading branch information
yyx990803 committed Apr 27, 2020
1 parent 4353126 commit 21d3cd8
Showing 1 changed file with 29 additions and 15 deletions.
44 changes: 29 additions & 15 deletions lib/app/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,38 @@ import {
inject,
watchEffect,
shallowRef
} from 'vue'
} from '/@modules/vue'
import { Layout } from '/@theme/index.js'

const PathSymbol = Symbol()

const App = {
setup() {
const path = ref(window.location.pathname)
const path = ref(location.pathname)

// window.addEventListener('click', e => {
// if (e.target.tagName === 'A') {
// e.preventDefault()
// if (e.target.href && e.target.href.indexOf(location.host)) {
// history.pushState(null, '', e.target.href)
// }
// }
// })
window.addEventListener(
'click',
(e) => {
if (e.target.tagName === 'A') {
const { href, target } = e.target
if (
target !== `_blank` &&
href.startsWith(`${location.protocol}//${location.host}`)
) {
e.preventDefault()
// TODO save scroll position
history.pushState(null, '', href)
path.value = location.pathname
}
}
},
{ capture: true }
)

window.addEventListener('popstate', (e) => {
// TODO restore scroll position
path.value = location.pathname
})

provide(PathSymbol, path)

Expand All @@ -40,16 +55,15 @@ const Content = {
watchEffect(() => {
let pagePath = path.value.replace(/\.html$/, '')
if (pagePath.endsWith('/')) {
pagePath += 'index.md'
} else {
pagePath += '.md'
pagePath += 'index'
}

import(pagePath)
// awlays force re-fetch content in dev
import(`${pagePath}.md?t=${Date.now()}`)
.then((m) => {
comp.value = m.default
})
.catch(err => {
.catch((err) => {
comp.value = Default404
})
})
Expand Down

0 comments on commit 21d3cd8

Please sign in to comment.