refactor(core): adjust directory structure:
This commit is contained in:
parent
e07d36f44e
commit
aad62b65f5
20 changed files with 381 additions and 93 deletions
24
src/core/route/hash.js
Normal file
24
src/core/route/hash.js
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
import { cleanPath, getLocation } from './util'
|
||||
|
||||
export function ensureSlash () {
|
||||
const path = getHash()
|
||||
if (path.charAt(0) === '/') return
|
||||
replaceHash('/' + path)
|
||||
}
|
||||
|
||||
export function getHash () {
|
||||
// We can't use window.location.hash here because it's not
|
||||
// consistent across browsers - Firefox will pre-decode it!
|
||||
const href = window.location.href
|
||||
const index = href.indexOf('#')
|
||||
return index === -1 ? '' : href.slice(index + 1)
|
||||
}
|
||||
|
||||
function replaceHash (path) {
|
||||
const i = window.location.href.indexOf('#')
|
||||
window.location.replace(
|
||||
window.location.href.slice(0, i >= 0 ? i : 0) + '#' + path
|
||||
)
|
||||
}
|
||||
|
||||
// TODO 把第二个 hash 转成 ?id=
|
||||
17
src/core/route/index.js
Normal file
17
src/core/route/index.js
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
import { ensureSlash } from './hash'
|
||||
|
||||
export function routeMixin (Docsify) {
|
||||
Docsify.prototype.$route = {
|
||||
query: location.query || {},
|
||||
path: location.path || '/',
|
||||
base: ''
|
||||
}
|
||||
}
|
||||
|
||||
export function initRoute (vm) {
|
||||
ensureSlash()
|
||||
window.addEventListener('hashchange', () => {
|
||||
ensureSlash()
|
||||
vm.$fetch()
|
||||
})
|
||||
}
|
||||
11
src/core/route/util.js
Normal file
11
src/core/route/util.js
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
export function cleanPath (path) {
|
||||
return path.replace(/\/+/g, '/')
|
||||
}
|
||||
|
||||
export function getLocation (base) {
|
||||
let path = window.location.pathname
|
||||
if (base && path.indexOf(base) === 0) {
|
||||
path = path.slice(base.length)
|
||||
}
|
||||
return (path || '/') + window.location.search + window.location.hash
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue