Skip to content

Commit

Permalink
feat: Vue.$vuepress & Vue.prototype.$vuepress
Browse files Browse the repository at this point in the history
  • Loading branch information
ulivz committed Nov 4, 2018
1 parent a78a91f commit 9c947b2
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 26 deletions.
19 changes: 0 additions & 19 deletions packages/@vuepress/core/lib/app/Store.js

This file was deleted.

7 changes: 3 additions & 4 deletions packages/@vuepress/core/lib/app/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { siteData } from '@internal/siteData'
import appEnhancers from '@internal/app-enhancers'
import globalUIComponents from '@internal/global-ui'
import ClientComputedMixin from '@transform/ClientComputedMixin'
import Store from './Store'
import Store from './plugins/Store'

// built-in components
import Content from './components/Content'
Expand All @@ -30,9 +30,8 @@ if (module.hot) {

Vue.config.productionTip = false

Vue.$store = new Store()

Vue.use(Router)
Vue.use(Store, '$vuepress')
// mixin for exposing $site and $page
Vue.mixin(dataMixin(ClientComputedMixin, siteData))
// component for rendering markdown content and setting title etc.
Expand Down Expand Up @@ -62,7 +61,7 @@ export function createApp (isServer) {
if (saved) {
return saved
} else if (to.hash) {
if (Vue.$store.get('disableScrollBehavior')) {
if (Vue.$vuepress.$get('disableScrollBehavior')) {
return false
}
return {
Expand Down
6 changes: 3 additions & 3 deletions packages/@vuepress/core/lib/app/dataMixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@ import Vue from 'vue'

export default function dataMixin (I18n, siteData) {
prepare(siteData)
Vue.$store.set('siteData', siteData)
Vue.$vuepress.$set('siteData', siteData)

if (module.hot) {
module.hot.accept(VUEPRESS_TEMP_PATH + '/internal/siteData.js', () => {
prepare(siteData)
Vue.$store.set('siteData', siteData)
Vue.$vuepress.$set('siteData', siteData)
})
}

const I18nConstructor = I18n(Vue.$store.get('siteData'))
const I18nConstructor = I18n(Vue.$vuepress.$get('siteData'))
const i18n = new I18nConstructor()
const descriptors = Object.getOwnPropertyDescriptors(Object.getPrototypeOf(i18n))
const computed = {}
Expand Down
35 changes: 35 additions & 0 deletions packages/@vuepress/core/lib/app/plugins/Store.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import Vue from 'vue'

class Store {
constructor () {
this.store = new Vue({
data: {
state: {}
}
})
}

$get (key) {
return this.store.state[key]
}

$set (key, value) {
Vue.set(this.store.state, key, value)
}

$emit (...args) {
this.store.$emit(...args)
}

$on (...args) {
this.store.$on(...args)
}
}

export default {
install (Vue, options = '$store') {
const store = new Store()
Vue[options] = store
Vue.prototype[options] = store
}
}

0 comments on commit 9c947b2

Please sign in to comment.