feat(cli): set title by lang

This commit is contained in:
陈嘉涵 2019-12-09 17:07:01 +08:00
parent cf172f5cbc
commit 396002495f
10 changed files with 75 additions and 25 deletions

View File

@ -10,18 +10,18 @@ window.syncPath = function () {
const currentDir = router.history.current.path; const currentDir = router.history.current.path;
if (isInIframe) { if (isInIframe) {
window.top.changePath(currentDir); window.top.replacePath(currentDir);
} else if (!isMobile) { } else if (!isMobile) {
const iframe = document.querySelector('iframe'); const iframe = document.querySelector('iframe');
if (iframe) { if (iframe) {
iframeReady(iframe, () => { iframeReady(iframe, () => {
iframe.contentWindow.changePath(currentDir); iframe.contentWindow.replacePath(currentDir);
}); });
} }
} }
}; };
window.changePath = function (path = '') { window.replacePath = function (path = '') {
// should preserve hash for anchor // should preserve hash for anchor
if (window.vueRouter.currentRoute.path !== path) { if (window.vueRouter.currentRoute.path !== path) {
window.vueRouter.replace(path).catch(() => {}); window.vueRouter.replace(path).catch(() => {});

View File

@ -1,7 +1,7 @@
function iframeReady(iframe, callback) { function iframeReady(iframe, callback) {
const doc = iframe.contentDocument || iframe.contentWindow.document; const doc = iframe.contentDocument || iframe.contentWindow.document;
const interval = () => { const interval = () => {
if (iframe.contentWindow.changePath) { if (iframe.contentWindow.replacePath) {
callback(); callback();
} else { } else {
setTimeout(() => { setTimeout(() => {

View File

@ -65,6 +65,23 @@ export default {
watch: { watch: {
lang(val) { lang(val) {
setLang(val); setLang(val);
this.setTitle();
}
},
created() {
this.setTitle();
},
methods: {
setTitle() {
let { title } = this.config;
if (this.config.description) {
title += ` - ${this.config.description}`;
}
document.title = title;
} }
} }
}; };

View File

@ -9,8 +9,7 @@ export default {
name: 'van-doc-simulator', name: 'van-doc-simulator',
props: { props: {
src: String, src: String
lang: String
}, },
data() { data() {
@ -33,12 +32,6 @@ export default {
} }
}, },
watch: {
lang(val) {
location.hash = `#${location.hash.replace(this.lang, val)}`;
}
},
mounted() { mounted() {
window.addEventListener('scroll', () => { window.addEventListener('scroll', () => {
this.scrollTop = window.scrollY; this.scrollTop = window.scrollY;

View File

@ -13,7 +13,7 @@
<slot /> <slot />
</doc-content> </doc-content>
</doc-container> </doc-container>
<doc-simulator v-if="simulator" :src="simulator" :lang="lang" /> <doc-simulator v-if="simulator" :src="simulator" />
</div> </div>
</template> </template>

View File

@ -31,6 +31,17 @@ function parseName(name) {
}; };
} }
function getLangFromRoute(route) {
const lang = route.path.split('/')[1];
const langs = Object.keys(locales);
if (langs.indexOf(lang) !== -1) {
return lang;
}
return getLang();
}
function getRoutes() { function getRoutes() {
const routes = []; const routes = [];
const names = Object.keys(documents); const names = Object.keys(documents);
@ -38,7 +49,7 @@ function getRoutes() {
if (locales) { if (locales) {
routes.push({ routes.push({
path: '*', path: '*',
redirect: `/${getLang()}/` redirect: route => `/${getLangFromRoute(route)}/`
}); });
} else { } else {
routes.push({ routes.push({

View File

@ -10,6 +10,17 @@ const { locales, defaultLang } = config.site;
setDefaultLang(defaultLang); setDefaultLang(defaultLang);
function getLangFromRoute(route) {
const lang = route.path.split('/')[1];
const langs = Object.keys(locales);
if (langs.indexOf(lang) !== -1) {
return lang;
}
return getLang();
}
function getRoutes() { function getRoutes() {
const routes = []; const routes = [];
const names = Object.keys(demos); const names = Object.keys(demos);
@ -18,7 +29,7 @@ function getRoutes() {
if (langs.length) { if (langs.length) {
routes.push({ routes.push({
path: '*', path: '*',
redirect: () => `/${getLang()}/` redirect: route => `/${getLangFromRoute(route)}/`
}); });
langs.forEach(lang => { langs.forEach(lang => {

View File

@ -1,11 +1,10 @@
import glob from 'fast-glob'; import glob from 'fast-glob';
import { join, parse } from 'path'; import { join, parse } from 'path';
import { existsSync } from 'fs-extra'; import { existsSync, readdirSync } from 'fs-extra';
import { import {
pascalize, pascalize,
removeExt, removeExt,
getVantConfig, getVantConfig,
getComponents,
smartOutputFile smartOutputFile
} from '../common'; } from '../common';
import { import {
@ -96,8 +95,8 @@ function genExportConfig() {
} }
export function genSiteDesktopShared() { export function genSiteDesktopShared() {
const components = getComponents(); const dirs = readdirSync(SRC_DIR);
const documents = resolveDocuments(components); const documents = resolveDocuments(dirs);
const code = `${genImportConfig()} const code = `${genImportConfig()}
${genImportDocuments(documents)} ${genImportDocuments(documents)}

View File

@ -1,12 +1,11 @@
import { join } from 'path'; import { join } from 'path';
import { existsSync } from 'fs-extra'; import { existsSync, readdirSync } from 'fs-extra';
import { SRC_DIR, SITE_MODILE_SHARED_FILE } from '../common/constant'; import { SRC_DIR, SITE_MODILE_SHARED_FILE } from '../common/constant';
import { import {
pascalize, pascalize,
removeExt, removeExt,
decamelize, decamelize,
getVantConfig, getVantConfig,
getComponents,
smartOutputFile smartOutputFile
} from '../common'; } from '../common';
@ -90,8 +89,8 @@ ${genConfig(demos)}
} }
export function genSiteMobileShared() { export function genSiteMobileShared() {
const components = getComponents(); const dirs = readdirSync(SRC_DIR);
const code = genCode(components); const code = genCode(dirs);
smartOutputFile(SITE_MODILE_SHARED_FILE, code); smartOutputFile(SITE_MODILE_SHARED_FILE, code);
} }

View File

@ -9,8 +9,28 @@ import {
SITE_DESKTOP_SHARED_FILE SITE_DESKTOP_SHARED_FILE
} from '../common/constant'; } from '../common/constant';
const siteConfig = getVantConfig().site; function getSiteConfig() {
const title = `${siteConfig.title} - ${siteConfig.description}`; const siteConfig = getVantConfig().site;
if (siteConfig.locales) {
return siteConfig.locales[siteConfig.defaultLang || 'en-US'];
}
return siteConfig;
}
function getTitle(config: { title: string, description?: string }) {
let { title } = config;
if (config.description) {
title += ` - ${config.description}`;
}
return title;
}
const siteConfig = getSiteConfig();
const title = getTitle(siteConfig);
export const siteDevBaseConfig = merge(baseConfig as any, { export const siteDevBaseConfig = merge(baseConfig as any, {
entry: { entry: {