-
Notifications
You must be signed in to change notification settings - Fork 4.7k
/
Copy pathutil.js
131 lines (118 loc) Β· 2.95 KB
/
util.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import Vue from 'vue'
import layoutComponents from '@internal/layout-components'
import pageComponents from '@internal/page-components'
const asyncComponents = Object.assign({}, layoutComponents, pageComponents)
export function isPageExists (pageKey) {
return Boolean(pageComponents[pageKey])
}
export function isPageLoaded (pageKey) {
return Boolean(Vue.component(pageKey))
}
export function getPageAsyncComponent (pageKey) {
return pageComponents[pageKey]
}
export function isLayoutExists (layout) {
return Boolean(layoutComponents[layout])
}
export function isLayoutLoaded (layout) {
return Boolean(Vue.component(layout))
}
export function getLayoutAsyncComponent (pageKey) {
return layoutComponents[pageKey]
}
export function ensureAsyncComponentsLoaded (...names) {
return Promise.all(names.filter(v => v).map(async (name) => {
if (!Vue.component(name) && asyncComponents[name]) {
const comp = await asyncComponents[name]()
Vue.component(name, comp.default)
}
}))
}
/**
* Inject option to Vue SFC
* @param {object} options
* @param {string} key
* @param {any} value
*/
export function injectComponentOption (options, key, value) {
const arrayInject = () => {
if (!options[key]) options[key] = []
options[key].push(...value)
}
const objectInject = () => {
if (!options[key]) options[key] = {}
Object.assign(options[key], value)
}
// const primitiveInject = () => options[key] = value
switch (key) {
case 'components': objectInject(); break
case 'mixins': arrayInject(); break
default: throw new Error('Unknown option name.')
}
}
export function findPageForPath (pages, path) {
for (let i = 0; i < pages.length; i++) {
const page = pages[i]
if (page.path === path) {
return page
}
}
return {
path: '',
frontmatter: {}
}
}
export function findPageByKey (pages, key) {
for (let i = 0; i < pages.length; i++) {
const page = pages[i]
if (page.key === key) {
return page
}
}
return {
path: '',
frontmatter: {}
}
}
/**
* Normalize config.
* This utility is mainly for plugin developers. For some
* plugins that need internationalize the text. but it's
* not recommenbded to let plugin care about to the internal
* i18n implementation, so this utility was born.
*
*
* Usage:
*
* import { normalizeConfig } from '@app/util'
* export default {
* data () {
* return { config }
* }
* computed: {
* normalizedConfig() {
* return normalizeConfig(this, config)
* }
* }
* }
*
*
* e.g.
*
* Config: : 'Text'
* Normalized Config: 'Text'
*
* Config: : { '/': 'Text', '/zh/': 'ζζ¬' }
* Normalized Config: 'Text' or 'ζζ¬'
*
* @param {Vue} component
* @param {any} rawConfig
* @returns {any}
*/
export function normalizeConfig (component, rawConfig) {
const { $localePath } = component
if (typeof rawConfig === 'object' && rawConfig[$localePath]) {
return rawConfig[$localePath]
}
return rawConfig
}