Skip to content

Commit 379295e

Browse files
committed
doc site add theme switch feature
1 parent 199e06f commit 379295e

File tree

7 files changed

+127
-23
lines changed

7 files changed

+127
-23
lines changed

examples/src/comp/app.less

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,11 +80,13 @@
8080
font-size: 16px;
8181

8282
.switch-lang-container,
83+
.switch-theme-container,
8384
.switch-version-container {
8485
position: relative;
8586
line-height: 1;
8687

8788
.switch-lang-dropdown-pannel,
89+
.switch-theme-dropdown-pannel,
8890
.switch-version-dropdown-pannel {
8991
position: absolute;
9092
display: none;
@@ -130,6 +132,7 @@
130132
}
131133

132134
.switch-lang,
135+
.switch-theme,
133136
.switch-version {
134137
color: #666;
135138
cursor: pointer;

examples/src/comp/app.vue

Lines changed: 84 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
<div class="main-banner-menus-container">
1515
<div class="main-banner-menus">
1616
<span
17-
v-for="item in menus"
17+
v-for="item in currentLocal.menus"
1818
:key="item.name"
1919
:class="[
2020
'main-banner-menu-item',
@@ -119,6 +119,50 @@
119119
</div>
120120
</span>
121121

122+
<!-- switch theme -->
123+
<span class="main-banner-menu-item">
124+
<div
125+
v-click-outside="
126+
() => (showThemeDropdown = false)
127+
"
128+
class="switch-theme-container"
129+
>
130+
<span
131+
class="switch-theme"
132+
@click="
133+
showThemeDropdown = !showThemeDropdown
134+
"
135+
>
136+
{{ currentLocal.docTheme }}
137+
<i class="icon iconfont icon-dropdown" />
138+
</span>
139+
<div
140+
class="switch-theme-dropdown-pannel"
141+
:class="
142+
showThemeDropdown
143+
? 'dropdown-pannel-show'
144+
: ''
145+
"
146+
>
147+
<span
148+
v-for="item in currentLocal.switchDocThemeOptions"
149+
:key="item.value"
150+
:class="[
151+
'dropdown-item',
152+
{
153+
active:
154+
item.value ===
155+
currentDocTheme,
156+
},
157+
]"
158+
@click.stop="themeChange(item)"
159+
>
160+
{{ item.label }}
161+
</span>
162+
</div>
163+
</div>
164+
</span>
165+
122166
<span class="main-banner-menu-item">
123167
<a
124168
class="main-banner-menu-link"
@@ -139,6 +183,7 @@
139183
</template>
140184

141185
<script>
186+
import { getDocTheme, setDocTheme } from "@/utils/cookies";
142187
import locale from "./locale";
143188
import I18nMixins from "./mixins/i18n-mixins";
144189
import ThemeSwitchMixins from "./mixins/theme-switch-mixins.js";
@@ -159,16 +204,23 @@ export default {
159204
{ value: "en", label: "English" },
160205
{ value: "zh", label: "简体中文" },
161206
],
207+
// show lang dropdown
162208
showLangDropdown: false,
163-
//switch version option
209+
//switch version option,
164210
switchVersionOptions: [],
165211
showVersionDropdown: false,
212+
// show theme dropdown
213+
showThemeDropdown: false,
214+
// default doc theme
215+
defalutDocTheme: "default",
216+
// current doc theme
217+
currentDocTheme: "",
166218
};
167219
},
168220
computed: {
169-
// menus
170-
menus() {
171-
return locale[this.currentDocLang]["menus"];
221+
// current local
222+
currentLocal() {
223+
return locale[this.currentDocLang];
172224
},
173225
174226
// show logo
@@ -213,6 +265,21 @@ export default {
213265
this.showLangDropdown = false;
214266
}, 150);
215267
},
268+
// theme change
269+
themeChange({ value }) {
270+
setDocTheme(value);
271+
this.showThemeDropdown = false;
272+
273+
if (window.env === "dev") {
274+
setTimeout(() => {
275+
window.location.reload();
276+
});
277+
} else {
278+
this.switchThemeMix(value).finally(() => {
279+
//this.loadingInstance.close();
280+
});
281+
}
282+
},
216283
// version change
217284
versionChange(item) {
218285
const { protocol, host, pathname, hash } = window.location;
@@ -250,11 +317,23 @@ export default {
250317
}
251318
return result;
252319
},
320+
// init theme
321+
initDocTheme() {
322+
this.currentDocTheme = getDocTheme() ?? this.defalutDocTheme;
323+
// 初始化
324+
if (window.env != "dev") {
325+
this.switchThemeMix(this.currentDocTheme);
326+
}
327+
console.log("current doc theme ::", this.currentDocTheme);
328+
},
253329
},
254330
created() {
255331
getVersions().then(({ data }) => {
256332
this.switchVersionOptions = data;
257333
});
258334
},
335+
mounted() {
336+
this.initDocTheme();
337+
},
259338
};
260339
</script>

examples/src/comp/locale/index.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,12 @@ export default {
2020
isRouter: false,
2121
},
2222
],
23+
docTheme: "主题",
24+
// 主题切换配置
25+
switchDocThemeOptions: [
26+
{ value: "default", label: "默认" },
27+
{ value: "dark", label: "暗黑" },
28+
],
2329
anchorCatalogTitle: "目录",
2430
// 组件配置多语言跟着文档语言环境走
2531
compLang: zhCN,
@@ -79,6 +85,12 @@ export default {
7985
isRouter: false,
8086
},
8187
],
88+
docTheme: "Theme",
89+
// 主题切换配置
90+
switchDocThemeOptions: [
91+
{ value: "default", label: "default" },
92+
{ value: "dark", label: "dark" },
93+
],
8294
anchorCatalogTitle: "Contents",
8395
compLang: enUS,
8496
eidtDemoTitle: "Edit this on Github",

examples/src/comp/mixins/theme-switch-mixins.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@ const themeSwitcherTool = ThemeSwitcherTool({
66
// Your theme list
77
themeList: [
88
{
9-
themeName: "theme-dark",
9+
themeName: "dark",
1010
themePath:
1111
"https://unpkg.com/vue-easytable/libs/theme-dark/index.css",
1212
},
1313
{
14-
themeName: "theme-default",
14+
themeName: "default",
1515
themePath:
1616
"https://unpkg.com/vue-easytable/libs/theme-default/index.css",
1717
},

examples/src/main.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import Vue from "vue";
66
import APP from "@/comp/app.vue";
77
import router from "@/router/index";
88

9+
import { getDocTheme } from "@/utils/cookies";
10+
911
import NProgress from "nprogress"; // Progress 进度条
1012

1113
import "@/css/index.less";
@@ -82,7 +84,12 @@ dev mode
8284
*/
8385

8486
if (window.env === "dev") {
85-
require("../../packages/theme-default/index.less");
87+
const docTheme = getDocTheme();
88+
if (docTheme && docTheme === "dark") {
89+
require("../../packages/theme-dark/index.less");
90+
} else {
91+
require("../../packages/theme-default/index.less");
92+
}
8693
}
8794

8895
import {

examples/src/utils/constant.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,14 @@ export const DOC_LANG = {
44
ZH: "zh",
55
};
66

7+
// doc theme
8+
// export const DOC_THEME = {
9+
// DEFAULT: "default",
10+
// DARK: "dark",
11+
// };
12+
713
// doc lang cookie
814
export const DOC_LANG_COOKIE = "doc_lang_cookie";
15+
16+
// doc theme cookie
17+
export const DOC_THEME_COOKIE = "doc_lang_cookie";

examples/src/utils/cookies.js

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,14 @@
1-
/* import Cookies from "js-cookie";
2-
import { DOC_LANG_COOKIE, DOC_LANG } from "./constant";
1+
import Cookies from "js-cookie";
32

4-
// set doc lang cookie
5-
export function setDocLang(value) {
6-
return Cookies.set(DOC_LANG_COOKIE, value);
7-
}
8-
9-
// get doc lang cookie
10-
export function getDocLang() {
11-
let result = Cookies.get(DOC_LANG_COOKIE);
3+
import { DOC_THEME_COOKIE } from "./constant";
124

13-
console.log("doc lang::", result);
5+
// set doc theme cookie
6+
export function setDocTheme(value) {
7+
return Cookies.set(DOC_THEME_COOKIE, value);
8+
}
149

15-
if (!result) {
16-
result = DOC_LANG.EN;
17-
}
10+
// get doc theme cookie
11+
export function getDocTheme() {
12+
let result = Cookies.get(DOC_THEME_COOKIE);
1813
return result;
1914
}
20-
*/

0 commit comments

Comments
 (0)