refactor: setting

This commit is contained in:
lrl 2021-11-09 19:24:55 +08:00
parent 782cea9995
commit e98dee9f0a
9 changed files with 85 additions and 70 deletions

View File

@ -7,6 +7,9 @@
"Locale": "zh", "Locale": "zh",
"Layout": "vertical", "Layout": "vertical",
"Theme": "default", "Theme": "default",
"Grey": false,
"Weak": false,
"HideTabs": false,
"MapConfigure": { "MapConfigure": {
"amapKey": "97b3248d1553172e81f168cf94ea667e", "amapKey": "97b3248d1553172e81f168cf94ea667e",
"baiduKey": "wTHbkkEweiFqZLKunMIjcrb2RcqNXkhc", "baiduKey": "wTHbkkEweiFqZLKunMIjcrb2RcqNXkhc",

View File

@ -17,6 +17,8 @@ const props = defineProps({
const keepAlive: Boolean = ref( const keepAlive: Boolean = ref(
getCurrentInstance().appContext.config.globalProperties.$config?.KeepAlive getCurrentInstance().appContext.config.globalProperties.$config?.KeepAlive
); );
const instance =
getCurrentInstance().appContext.app.config.globalProperties.$storage;
const transitions = computed(() => { const transitions = computed(() => {
return route => { return route => {
@ -24,6 +26,13 @@ const transitions = computed(() => {
}; };
}); });
const hideTabs = computed(() => {
return instance?.sets.hideTabs;
});
const layout = computed(() => {
return instance?.layout.layout === "vertical";
});
const transitionMain = defineComponent({ const transitionMain = defineComponent({
render() { render() {
return h( return h(
@ -62,6 +71,12 @@ const transitionMain = defineComponent({
<template> <template>
<section <section
:class="[props.fixedHeader ? 'app-main' : 'app-main-nofixed-header']" :class="[props.fixedHeader ? 'app-main' : 'app-main-nofixed-header']"
:style="[
hideTabs && layout ? 'padding-top: 48px;' : '',
!hideTabs && layout ? 'padding-top: 85px;' : '',
hideTabs && !layout ? 'padding-top: 62px' : '',
!hideTabs && !layout ? 'padding-top: 98px;' : ''
]"
> >
<router-view> <router-view>
<template #default="{ Component, route }"> <template #default="{ Component, route }">

View File

@ -5,6 +5,7 @@ import {
unref, unref,
watch, watch,
computed, computed,
nextTick,
useCssModule, useCssModule,
getCurrentInstance getCurrentInstance
} from "vue"; } from "vue";
@ -72,32 +73,12 @@ const markValue = ref(storageLocal.getItem("showModel") || "smart");
const logoVal = ref(storageLocal.getItem("logoVal") || "1"); const logoVal = ref(storageLocal.getItem("logoVal") || "1");
const localOperate = (key: string, value?: any, model?: string): any => {
model && model === "set"
? storageLocal.setItem(key, value)
: storageLocal.getItem(key);
};
const settings = reactive({ const settings = reactive({
greyVal: storageLocal.getItem("greyVal"), greyVal: instance.sets.grey,
weekVal: storageLocal.getItem("weekVal"), weakVal: instance.sets.weak,
tagsVal: storageLocal.getItem("tagsVal") tabsVal: instance.sets.hideTabs
}); });
settings.greyVal === null
? localOperate("greyVal", false, "set")
: document.querySelector("html")?.setAttribute("class", "html-grey");
settings.weekVal === null
? localOperate("weekVal", false, "set")
: document.querySelector("html")?.setAttribute("class", "html-weakness");
if (settings.tagsVal === null) {
localOperate("tagsVal", false, "set");
settings.tagsVal = false;
}
window.document.body.setAttribute("data-show-tag", settings.tagsVal);
function toggleClass(flag: boolean, clsName: string, target?: HTMLElement) { function toggleClass(flag: boolean, clsName: string, target?: HTMLElement) {
const targetEl = target || document.body; const targetEl = target || document.body;
let { className } = targetEl; let { className } = targetEl;
@ -108,35 +89,52 @@ function toggleClass(flag: boolean, clsName: string, target?: HTMLElement) {
// //
const greyChange = ({ value }): void => { const greyChange = ({ value }): void => {
toggleClass(settings.greyVal, "html-grey", document.querySelector("html")); toggleClass(settings.greyVal, "html-grey", document.querySelector("html"));
value instance.sets = {
? localOperate("greyVal", true, "set") grey: value,
: localOperate("greyVal", false, "set"); weak: instance.sets.weak,
hideTabs: instance.sets.hideTabs
};
}; };
// //
const weekChange = ({ value }): void => { const weekChange = ({ value }): void => {
toggleClass( toggleClass(
settings.weekVal, settings.weakVal,
"html-weakness", "html-weakness",
document.querySelector("html") document.querySelector("html")
); );
value instance.sets = {
? localOperate("weekVal", true, "set") grey: instance.sets.grey,
: localOperate("weekVal", false, "set"); weak: value,
hideTabs: instance.sets.hideTabs
};
}; };
const tagsChange = () => { const tagsChange = () => {
let showVal = settings.tagsVal; let showVal = settings.tabsVal;
showVal instance.sets = {
? storageLocal.setItem("tagsVal", true) grey: instance.sets.grey,
: storageLocal.setItem("tagsVal", false); weak: instance.sets.weak,
hideTabs: showVal
};
emitter.emit("tagViewsChange", showVal); emitter.emit("tagViewsChange", showVal);
}; };
//
nextTick(() => {
settings.greyVal &&
document.querySelector("html")?.setAttribute("class", "html-grey");
settings.weakVal &&
document.querySelector("html")?.setAttribute("class", "html-weakness");
settings.tabsVal && tagsChange();
});
// //
function onReset() { function onReset() {
storageLocal.clear(); storageLocal.clear();
storageSession.clear(); storageSession.clear();
toggleClass(false, "html-grey", document.querySelector("html"));
toggleClass(false, "html-weakness", document.querySelector("html"));
router.push("/login"); router.push("/login");
} }
@ -271,7 +269,7 @@ function setLayoutThemeColor(theme: string) {
<li> <li>
<span>色弱模式</span> <span>色弱模式</span>
<el-switch <el-switch
v-model="settings.weekVal" v-model="settings.weakVal"
inline-prompt inline-prompt
inactive-color="#a6a6a6" inactive-color="#a6a6a6"
active-text="开" active-text="开"
@ -283,7 +281,7 @@ function setLayoutThemeColor(theme: string) {
<li> <li>
<span>隐藏标签页</span> <span>隐藏标签页</span>
<el-switch <el-switch
v-model="settings.tagsVal" v-model="settings.tabsVal"
inline-prompt inline-prompt
inactive-color="#a6a6a6" inactive-color="#a6a6a6"
active-text="开" active-text="开"

View File

@ -447,7 +447,6 @@ onBeforeMount(() => {
emitter.on("tagViewsChange", key => { emitter.on("tagViewsChange", key => {
if (unref(showTags) === key) return; if (unref(showTags) === key) return;
showTags.value = key; showTags.value = key;
window.document.body.setAttribute("data-show-tag", key);
}); });
// //

View File

@ -57,6 +57,15 @@ const layout = computed(() => {
theme: instance.$config?.Theme ?? "default" theme: instance.$config?.Theme ?? "default"
}; };
} }
//
if (!instance.$storage.sets) {
// eslint-disable-next-line
instance.$storage.sets = {
grey: instance.$config?.Grey ?? false,
weak: instance.$config?.Weak ?? false,
hideTabs: instance.$config?.HideTabs ?? false
};
}
return instance.$storage?.layout.layout; return instance.$storage?.layout.layout;
}); });
@ -80,6 +89,10 @@ const set: setType = reactive({
withoutAnimation: set.sidebar.withoutAnimation, withoutAnimation: set.sidebar.withoutAnimation,
mobile: set.device === "mobile" mobile: set.device === "mobile"
}; };
}),
hideTabs: computed(() => {
return instance.$storage?.sets.hideTabs;
}) })
}); });
@ -144,7 +157,14 @@ const layoutHeader = defineComponent({
render() { render() {
return h( return h(
"div", "div",
{ class: { "fixed-header": set.fixedHeader } }, {
class: { "fixed-header": set.fixedHeader },
style: [
set.hideTabs && layout.value.includes("horizontal")
? "box-shadow: 0 1px 4px rgb(0 21 41 / 8%);"
: ""
]
},
{ {
default: () => [ default: () => [
!hiddenSideBar.value && layout.value.includes("vertical") !hiddenSideBar.value && layout.value.includes("vertical")

View File

@ -47,6 +47,7 @@ export interface setType {
withoutAnimation: boolean; withoutAnimation: boolean;
mobile: boolean; mobile: boolean;
}; };
hideTabs: boolean;
} }
export type childrenType = { export type childrenType = {

View File

@ -640,35 +640,3 @@ body[layout="horizontal"] {
transition: none !important; transition: none !important;
} }
} }
// vertical模式下不隐藏标签页
body[layout="vertical"][data-show-tag="false"] {
.fixed-header + .app-main {
padding-top: 85px;
}
}
// vertical模式下隐藏标签页
body[layout="vertical"][data-show-tag="true"] {
.fixed-header + .app-main {
padding-top: 48px;
}
}
// horizontal模式下不隐藏标签页
body[layout="horizontal"][data-show-tag="false"] {
.fixed-header + .app-main {
padding-top: 98px;
}
}
// horizontal模式下隐藏标签页
body[layout="horizontal"][data-show-tag="true"] {
.fixed-header {
box-shadow: 0 1px 4px rgb(0 21 41 / 8%);
}
.fixed-header + .app-main {
padding-top: 62px;
}
}

View File

@ -33,6 +33,14 @@ export const injectResponsiveStorage = (app: App, config: ServerConfigs) => {
layout: config.Layout ?? "vertical", layout: config.Layout ?? "vertical",
theme: config.Theme ?? "default" theme: config.Theme ?? "default"
} }
},
sets: {
type: Object,
default: Storage.getData(undefined, "sets") ?? {
grey: config.Grey,
weak: config.Weak,
hideTabs: config.HideTabs
}
} }
}); });
}; };

3
types/global.d.ts vendored
View File

@ -91,6 +91,9 @@ declare global {
Locale?: string; Locale?: string;
Layout?: string; Layout?: string;
Theme?: string; Theme?: string;
Grey?: boolean;
Weak?: boolean;
HideTabs?: boolean;
MapConfigure?: { MapConfigure?: {
amapKey?: string; amapKey?: string;
baiduKey?: string; baiduKey?: string;