refactor: integrate isLayoutExists and isPageExists

This commit is contained in:
Shigma
2019-03-06 19:23:52 +08:00
parent 4641f35ee9
commit 65a27410d5
7 changed files with 17 additions and 35 deletions
@@ -8,7 +8,7 @@ export default {
},
render (h) {
const pageKey = this.pageKey || this.$parent.$page.key
if (this.$vuepress.isPageExists(pageKey)) {
if (this.$hasComponent(pageKey)) {
return h(pageKey)
}
return h('')
@@ -7,7 +7,7 @@ export default {
computed: {
layout () {
if (this.$page.path) {
if (this.$vuepress.isLayoutExists(this.$page.frontmatter.layout)) {
if (this.$hasComponent(this.$page.frontmatter.layout)) {
return this.$page.frontmatter.layout
}
return 'Layout'
+5 -7
View File
@@ -1,15 +1,13 @@
import Vue from 'Vue'
export declare class Store {
store: Vue;
store: Vue
$get(key: string): any;
$get(key: string): any
$set(key: string, value: any): void;
$set(key: string, value: any): void
$emit: typeof Vue.prototype.$emit;
$emit: typeof Vue.prototype.$emit
$on: typeof Vue.prototype.$on;
$on: typeof Vue.prototype.$on
}
+4 -10
View File
@@ -1,14 +1,8 @@
import { Store } from './Store';
import { AsyncComponent } from 'vue'
import { Store } from './Store'
declare class VuePress extends Store {
isPageExists (pageKey: string): boolean;
isLayoutExists (pageKey: string): boolean;
}
declare module "vue/types/vue" {
declare module 'vue/types/vue' {
export interface Vue {
$vuepress: VuePress;
$vuepress: Store
$hasComponent (key: string): boolean
}
}
@@ -1,4 +1,3 @@
import Vue from 'vue'
import Store from './Store'
// TODO: reuse this function in shared-utils
@@ -14,20 +13,11 @@ function cached (fn) {
const pascalize = cached((str = '') => str.replace(/(^|-)\w/g, s => s.slice(-1).toUpperCase()))
class VuePress extends Store {
isPageExists (pageKey) {
return Boolean(Vue.component(pascalize(pageKey)))
}
isLayoutExists (layout) {
return Boolean(Vue.component(pascalize(layout)))
}
}
export default {
install (Vue) {
const ins = new VuePress()
Vue.$vuepress = ins
Vue.prototype.$vuepress = ins
const vuepress = new Store()
Vue.$vuepress = vuepress
Vue.prototype.$vuepress = vuepress
Vue.prototype.$hasComponent = key => Boolean(Vue.component(pascalize(key)))
}
}
+1 -1
View File
@@ -111,7 +111,7 @@ export default {
computed: {
layout () {
if (this.$page.path) {
if (this.$vuepress.isLayoutExists(this.$frontmatter.layout)) {
if (this.$hasComponent(this.$frontmatter.layout)) {
return this.$frontmatter.layout
}
return 'Layout'
+1 -1
View File
@@ -111,7 +111,7 @@ export default {
computed: {
layout () {
if (this.$page.path) {
if (this.$vuepress.isLayoutExists(this.$frontmatter.layout)) {
if (this.$hasComponent(this.$frontmatter.layout)) {
return this.$frontmatter.layout
}
return 'Layout'