Skip to content

Commit

Permalink
feat: dynamic routes with json
Browse files Browse the repository at this point in the history
  • Loading branch information
deot committed Sep 7, 2020
1 parent 35a49fa commit b8851dd
Show file tree
Hide file tree
Showing 12 changed files with 263 additions and 531 deletions.
6 changes: 3 additions & 3 deletions docs/doc.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ module.exports = {
},
'/api/:name': {
default: 'default',
sidebar,
sidebar: ENV_IS_DEV ? '/docs/sidebar.json' : '/wya-doc/docs/sidebar.json',
header: './components/header.vue',
footer: './components/footer.vue',
extra: null,
Expand All @@ -48,12 +48,12 @@ module.exports = {
},
devServer: {},
},
runtime: {
runtime: {
define: {
__DOC_SITE_DIR__: ENV_IS_DEV ? '/' : '/wya-doc/site/',
__DOC_MD_DIR__: ENV_IS_DEV ? '/docs/' : '/wya-doc/docs/',
// __DOC_MD_DIR__: (lang, name) => { return lang + name; },
__DOC_VERSION__: "1.0.0",
}
}
};
};
104 changes: 104 additions & 0 deletions docs/sidebar.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
[
{
"path": "/changelog",
"name": {
"zh-CN": "更新日志",
"en-US": "Changelog"
}
},
{
"name": {
"zh-CN": "开发指南",
"en-US": "Development"
},
"children": [
{
"path": "/installation",
"name": {
"zh-CN": "安装",
"en-US": "Installation"
}
},
{
"path": "/quickstart",
"name": {
"zh-CN": "快速上手",
"en-US": "Quick Start"
}
},
{
"path": "/i18n",
"name": {
"zh-CN": "国际化",
"en-US": "Internationalization"
}
}
]
},
{
"name": {
"zh-CN": "组合",
"en-US": "Group"
},
"group": [
{
"name": {
"zh-CN": "组合 1",
"en-US": "Group 1"
},
"list": [
{
"path": "/children-group-11",
"name": {
"zh-CN": "子 1 - 1",
"en-US": "children 1 - 1"
}
},
{
"path": "/children-group-12",
"name": {
"zh-CN": "子 1 - 2",
"en-US": "children 1 - 2"
}
},
{
"path": "/children-group-13",
"name": {
"zh-CN": "子 1 - 3",
"en-US": "children 1 - 3"
}
}
]
},
{
"name": {
"zh-CN": "组合 2",
"en-US": "Group 2"
},
"list": [
{
"path": "/children-group-21",
"name": {
"zh-CN": "子 2 - 1",
"en-US": "children 2 - 1"
}
},
{
"path": "/children-group-22",
"name": {
"zh-CN": "子 2 - 2",
"en-US": "children 2 - 2"
}
},
{
"path": "/children-group-23",
"name": {
"zh-CN": "子 2 - 3",
"en-US": "children 2 - 3"
}
}
]
}
]
}
]
2 changes: 1 addition & 1 deletion packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
"@babel/runtime": "^7.10.2",
"@wya/doc-playground": "^1.0.0",
"@wya/doc-utils": "^1.0.0",
"@wya/http": "^1.4.5",
"@wya/http": "^1.5.0-alpha.2",
"@wya/ps": "^1.0.2",
"@wya/sass": "^1.2.0",
"@wya/socket": "^1.1.0",
Expand Down
5 changes: 2 additions & 3 deletions packages/cli/src/client/app.vue
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export default {
extra,
} = this;
return {
return {
sidebar,
footer,
header,
Expand Down Expand Up @@ -62,7 +62,7 @@ export default {
<style lang="scss">
#pages {
padding: 90px 0 0;
display: flex;
align-content: flex-start;
justify-content: space-between;
Expand All @@ -73,5 +73,4 @@ export default {
width: 100%;
min-height: 100vh;
}
</style>
66 changes: 52 additions & 14 deletions packages/cli/src/client/components/sidebar.vue
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@
<div class="c-layout-sidebar">
<div class="c-layout-sidebar__scroller">
<ul class="">
<li
<li
v-for="(item, index) in navs"
:key="index"
class="c-layout-sidebar__item"
>
>
<div>
<router-link
v-if="item.path"
<router-link
v-if="item.path"
:to="`${rootPath}${item.path}`"
:class="{'is-active': $route.path === `${rootPath}${item.path}`}"
tag="span"
:class="{'is-active': $route.path === `${rootPath}${item.path}`}"
tag="span"
class="c-layout-sidebar__item--link"
>
{{ item.name | i18n(currentLocale) }}
Expand Down Expand Up @@ -40,7 +40,7 @@
{{ group.name | i18n(currentLocale) }}
</div>
<ul>
<router-link
<router-link
v-for="component in group.list"
:key="component.path"
:class="{'is-active': $route.path === `${rootPath}${component.path}`}"
Expand All @@ -60,17 +60,19 @@
</template>

<script>
import { Message } from '@wya/vc';
import { ajax } from '@wya/http';
export default {
name: 'c-layout-sidebar',
data() {
const { sidebar } = this.$route.meta || {};
const locale = this.$route.path.split('/')[1];
const [, locale, route] = this.$route.path.split('/');
return {
currentLocale: locale,
rootPath: locale ? `/${locale}/components` : `/components`,
navs: sidebar
currentLocale: locale,
rootPath: locale ? `/${locale}/${route}` : `/${route}`,
navs: []
};
},
Expand All @@ -80,16 +82,52 @@ export default {
let el = this.$el.querySelector('.c-layout-sidebar__item.is-active');
el && el.scrollIntoView();
this.loadNavs();
},
beforeDestroy() {
this.$vc.emit('layout-sidebar', { status: false });
this.$vc.off('locale-change', this.handleUpdate);
},
methods: {
handleUpdate({ locale }) {
const { sidebar } = this.$route.meta || {};
this.navs = sidebar;
this.currentLocale = locale;
this.loadNavs();
},
async loadNavs() {
const { sidebar } = this.$route.meta || {};
if (typeof sidebar === 'string') {
let url = `${location.origin}${sidebar}`;
let data;
if (__DEV__) {
try {
data = (await this.$global.db.read(url) || {}).data;
} catch (e) {
console.log(e);
}
}
ajax({
url,
onAfter: ({ response }) => {
return {
status: 1,
data: response.data
};
},
localData: data
}).then((res) => {
this.navs = res.data;
__DEV__ && this.$global.db.update({
__id: url,
data: res
});
}).catch(e => {
console.log(e);
});
} else {
this.navs = sidebar;
}
}
}
};
Expand Down
39 changes: 39 additions & 0 deletions packages/cli/src/client/entry-client.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { createApp } from './main';

(async () => {
const { app, router } = await createApp();

router.beforeEach((to, from, next) => {
app.$vc.clear();
next();
});
// 先不考虑服务端渲染情况
router.onReady(() => {
app.$mount();

const { redirect } = sessionStorage;
delete sessionStorage.redirect;

// github pages hack
const curUrl = `${location.pathname}${location.search}${location.hash}`;
if (redirect && redirect.includes(baseSiteDir) && redirect != curUrl) {
try {
router.push(redirect.replace(baseSiteDir, '/'));
} catch (e) {
location.href = redirect;
}
}

let lang = app.$route.path.split('/');
if (app.$global.lang && app.$global.lang != lang[1]) {
lang[1] = app.$global.lang;

let url = `/${lang.slice(1).join('/')}${location.search}${location.hash}`;
app.$router.replace(url);
}
});

window.app = app;
})();


Empty file.
67 changes: 0 additions & 67 deletions packages/cli/src/client/index.js

This file was deleted.

Loading

0 comments on commit b8851dd

Please sign in to comment.