2019-02-28 08:00:59 +00:00
|
|
|
<template>
|
|
|
|
<div>
|
|
|
|
<el-button
|
|
|
|
round
|
|
|
|
type="primary"
|
|
|
|
size="mini"
|
2019-03-14 09:55:28 +00:00
|
|
|
style="background: #66b1ff;border-color: #66b1ff"
|
2019-02-28 08:00:59 +00:00
|
|
|
@click.stop="showConfigurator"
|
|
|
|
>{{getActionDisplayName("theme-editor")}}</el-button>
|
|
|
|
<transition name="fade">
|
|
|
|
<div v-show="visible" class="configurator" ref="configurator">
|
|
|
|
<div
|
|
|
|
class="main-configurator"
|
|
|
|
ref="mainConfigurator"
|
|
|
|
>
|
|
|
|
<div v-if="currentConfig">
|
|
|
|
<main-panel
|
|
|
|
:currentConfig="currentConfig"
|
|
|
|
:defaultConfig="defaultConfig"
|
|
|
|
:userConfig="userConfig"
|
|
|
|
:globalValue="globalValue"
|
|
|
|
@onChange="userConfigChange"
|
|
|
|
></main-panel>
|
|
|
|
</div>
|
|
|
|
<div v-if="init && !currentConfig" class="no-config">
|
|
|
|
<img src="../../assets/images/theme-no-config.png" alt>
|
2019-03-14 09:55:28 +00:00
|
|
|
<span v-if="pageCouldEdit">{{getActionDisplayName("no-config")}}</span>
|
|
|
|
<span v-else>{{getActionDisplayName("no-need-config")}}</span>
|
2019-02-28 08:00:59 +00:00
|
|
|
</div>
|
|
|
|
<download-area></download-area>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</transition>
|
|
|
|
</div>
|
|
|
|
</template>
|
|
|
|
|
|
|
|
<style>
|
|
|
|
.configurator {
|
|
|
|
height: 100%;
|
|
|
|
width: 24%;
|
|
|
|
max-width: 400px;
|
|
|
|
position: fixed;
|
|
|
|
overflow-y: auto;
|
|
|
|
top: 79px;
|
|
|
|
right: 0;
|
|
|
|
bottom: 0;
|
|
|
|
background: #fff;
|
|
|
|
color: #666;
|
|
|
|
line-height: 24px;
|
|
|
|
padding-right: 1%;
|
|
|
|
}
|
|
|
|
.configurator .main-configurator {
|
|
|
|
height: 100%;
|
|
|
|
overflow: auto;
|
|
|
|
background: #F5F7FA;
|
|
|
|
border: 1px solid #EBEEF5;
|
|
|
|
box-shadow: 0 2px 10px 0 rgba(0,0,0,0.06);
|
|
|
|
border-radius: 8px;
|
|
|
|
width: 97%;
|
|
|
|
box-sizing: border-box;
|
|
|
|
margin: 0 .5% 0 5%;
|
|
|
|
}
|
|
|
|
.no-config {
|
|
|
|
margin-top: 130px;
|
|
|
|
text-align: center;
|
|
|
|
img {
|
|
|
|
width: 50%;
|
|
|
|
display: block;
|
|
|
|
margin: 0 auto;
|
|
|
|
}
|
|
|
|
span {
|
|
|
|
margin-top: 10px;
|
|
|
|
display: block;
|
|
|
|
color: #909399;
|
|
|
|
font-size: 14px;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
.fade-enter,.fade-leave-to {
|
|
|
|
transform:translateX(100%);
|
|
|
|
}
|
|
|
|
.fade-enter-active ,.fade-leave-active {
|
|
|
|
transition:all 0.3s ease;
|
|
|
|
}
|
|
|
|
|
|
|
|
@media (min-width: 1600px) {
|
|
|
|
.configurator {
|
|
|
|
padding-right: calc((100% - 1600px) / 2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
</style>
|
|
|
|
|
|
|
|
<script>
|
|
|
|
import bus from '../../bus';
|
|
|
|
import { getVars, updateVars } from './utils/api.js';
|
|
|
|
import mainPanel from './main';
|
|
|
|
import {
|
|
|
|
filterConfigType,
|
|
|
|
filterGlobalValue,
|
|
|
|
updateDomHeadStyle,
|
|
|
|
getActionDisplayName
|
|
|
|
} from './utils/utils.js';
|
|
|
|
import DocStyle from './docStyle';
|
|
|
|
import Loading from './loading';
|
2019-03-14 09:55:28 +00:00
|
|
|
import Shortcut from './shortcut';
|
2019-02-28 08:00:59 +00:00
|
|
|
import DownloadArea from './download';
|
|
|
|
|
2019-03-14 09:55:28 +00:00
|
|
|
const ELEMENT_THEME_USER_CONFIG = 'ELEMENT_THEME_USER_CONFIG';
|
|
|
|
|
|
|
|
const DEFAULT_USER_CONFIG = {
|
|
|
|
global: {},
|
|
|
|
local: {}
|
|
|
|
};
|
|
|
|
|
2019-02-28 08:00:59 +00:00
|
|
|
export default {
|
|
|
|
components: {
|
|
|
|
mainPanel,
|
|
|
|
DownloadArea
|
|
|
|
},
|
|
|
|
data() {
|
|
|
|
return {
|
|
|
|
init: false,
|
|
|
|
visible: false,
|
|
|
|
defaultConfig: null,
|
|
|
|
currentConfig: null,
|
|
|
|
userConfig: {
|
|
|
|
global: {},
|
|
|
|
local: {}
|
|
|
|
},
|
2019-03-14 09:55:28 +00:00
|
|
|
lastApply: 0,
|
|
|
|
userConfigHistory: [],
|
|
|
|
userConfigRedoHistory: [],
|
|
|
|
hasLocalConfig: false
|
2019-02-28 08:00:59 +00:00
|
|
|
};
|
|
|
|
},
|
2019-03-14 09:55:28 +00:00
|
|
|
mixins: [DocStyle, Loading, Shortcut],
|
2019-02-28 08:00:59 +00:00
|
|
|
computed: {
|
|
|
|
globalValue() {
|
|
|
|
return filterGlobalValue(this.defaultConfig, this.userConfig);
|
2019-03-14 09:55:28 +00:00
|
|
|
},
|
|
|
|
pageCouldEdit() {
|
|
|
|
const noNeedEdit = ['installation', 'quickstart', 'i18n', 'custom-theme', 'transition'];
|
|
|
|
const lastPath = this.$route.path.split('/').slice(-1).pop();
|
|
|
|
return noNeedEdit.indexOf(lastPath) < 0;
|
2019-02-28 08:00:59 +00:00
|
|
|
}
|
|
|
|
},
|
2019-03-14 09:55:28 +00:00
|
|
|
mounted() {
|
|
|
|
this.checkLocalThemeConfig();
|
|
|
|
},
|
2019-02-28 08:00:59 +00:00
|
|
|
methods: {
|
|
|
|
getActionDisplayName(key) {
|
|
|
|
return getActionDisplayName(key);
|
|
|
|
},
|
|
|
|
showConfigurator() {
|
|
|
|
this.visible = !this.visible;
|
2019-03-14 09:55:28 +00:00
|
|
|
this.visible ? this.enableShortcut() : this.disableShortcut();
|
2019-02-28 08:00:59 +00:00
|
|
|
bus.$emit('user-theme-config-visible', this.visible);
|
|
|
|
window.userThemeConfigVisible = Boolean(this.visible);
|
|
|
|
if (this.init) return;
|
|
|
|
this.$nextTick(() => {
|
|
|
|
const loading = this.$loading({
|
|
|
|
target: this.$refs.configurator
|
|
|
|
});
|
|
|
|
let defaultConfig;
|
|
|
|
getVars()
|
|
|
|
.then((res) => {
|
|
|
|
defaultConfig = res;
|
2019-03-04 08:15:48 +00:00
|
|
|
ga('send', 'event', 'ThemeConfigurator', 'Init');
|
2019-02-28 08:00:59 +00:00
|
|
|
})
|
|
|
|
.catch((err) => {
|
|
|
|
this.onError(err);
|
|
|
|
})
|
|
|
|
.then(() => {
|
|
|
|
setTimeout(() => {
|
|
|
|
if (defaultConfig) {
|
|
|
|
this.defaultConfig = defaultConfig;
|
|
|
|
this.filterCurrentConfig();
|
|
|
|
this.init = true;
|
2019-03-14 09:55:28 +00:00
|
|
|
this.checkLocalThemeConfig();
|
2019-02-28 08:00:59 +00:00
|
|
|
}
|
|
|
|
loading.close();
|
|
|
|
}, 300); // action after transition
|
|
|
|
});
|
|
|
|
});
|
|
|
|
},
|
2019-03-14 09:55:28 +00:00
|
|
|
checkLocalThemeConfig() {
|
|
|
|
try {
|
|
|
|
if (this.hasLocalConfig) {
|
|
|
|
this.$message(getActionDisplayName('load-local-theme-config'));
|
|
|
|
this.onAction();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const config = JSON.parse(localStorage.getItem(ELEMENT_THEME_USER_CONFIG));
|
|
|
|
if (config && config.global) {
|
|
|
|
this.userConfig = config;
|
|
|
|
this.hasLocalConfig = true;
|
|
|
|
this.showConfigurator();
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
// bad local config
|
|
|
|
}
|
|
|
|
},
|
2019-02-28 08:00:59 +00:00
|
|
|
filterCurrentConfig() {
|
|
|
|
this.currentConfig = this.defaultConfig.find((config) => {
|
2019-03-14 09:55:28 +00:00
|
|
|
return config.name === this.$route.path.split('/').pop().toLowerCase().replace('-', '');
|
2019-02-28 08:00:59 +00:00
|
|
|
});
|
|
|
|
},
|
|
|
|
userConfigChange(e) {
|
2019-03-14 09:55:28 +00:00
|
|
|
this.userConfigHistory.push(JSON.stringify(this.userConfig));
|
|
|
|
this.userConfigRedoHistory = [];
|
2019-02-28 08:00:59 +00:00
|
|
|
this.$set(this.userConfig[filterConfigType(this.currentConfig.name)], e.key, e.value);
|
|
|
|
this.onAction();
|
|
|
|
},
|
|
|
|
applyStyle(res, time) {
|
|
|
|
if (time < this.lastApply) return;
|
2019-03-14 09:55:28 +00:00
|
|
|
this.updateDocs(() => {
|
|
|
|
updateDomHeadStyle('chalk-style', res);
|
|
|
|
});
|
2019-02-28 08:00:59 +00:00
|
|
|
this.lastApply = time;
|
|
|
|
},
|
|
|
|
onDownload() {
|
|
|
|
return updateVars(Object.assign({}, this.userConfig, {download: true}), (xhr) => {
|
|
|
|
xhr.responseType = 'blob';
|
|
|
|
});
|
|
|
|
},
|
|
|
|
onReset() {
|
|
|
|
this.userConfig = {
|
|
|
|
global: {},
|
|
|
|
local: {}
|
|
|
|
};
|
|
|
|
this.onAction();
|
|
|
|
},
|
|
|
|
onAction() {
|
|
|
|
this.triggerComponentLoading(true);
|
|
|
|
const time = +new Date();
|
2019-03-14 09:55:28 +00:00
|
|
|
const currentConfigString = JSON.stringify(this.userConfig);
|
|
|
|
if (JSON.stringify(DEFAULT_USER_CONFIG) === currentConfigString) {
|
|
|
|
localStorage.removeItem(ELEMENT_THEME_USER_CONFIG);
|
|
|
|
} else {
|
|
|
|
localStorage.setItem(ELEMENT_THEME_USER_CONFIG, currentConfigString);
|
|
|
|
}
|
2019-02-28 08:00:59 +00:00
|
|
|
updateVars(this.userConfig)
|
|
|
|
.then((res) => {
|
|
|
|
this.applyStyle(res, time);
|
|
|
|
})
|
|
|
|
.catch((err) => {
|
|
|
|
this.onError(err);
|
|
|
|
})
|
|
|
|
.then(() => {
|
|
|
|
this.triggerComponentLoading(false);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
onError(err) {
|
|
|
|
let message;
|
|
|
|
try {
|
|
|
|
message = JSON.parse(err).message;
|
|
|
|
} catch (e) {
|
|
|
|
message = err;
|
|
|
|
}
|
|
|
|
this.$message.error(message);
|
|
|
|
},
|
|
|
|
triggerComponentLoading(val) {
|
|
|
|
bus.$emit('user-theme-config-loading', val);
|
|
|
|
},
|
2019-03-14 09:55:28 +00:00
|
|
|
updateDocs(cb) {
|
2019-02-28 08:00:59 +00:00
|
|
|
window.userThemeConfig = JSON.parse(JSON.stringify(this.userConfig));
|
|
|
|
bus.$emit('user-theme-config-update', this.userConfig);
|
2019-03-14 09:55:28 +00:00
|
|
|
this.updateDocStyle(this.userConfig, cb);
|
|
|
|
},
|
|
|
|
undo() {
|
|
|
|
if (this.userConfigHistory.length > 0) {
|
|
|
|
this.userConfigRedoHistory.push(JSON.stringify(this.userConfig));
|
|
|
|
this.userConfig = JSON.parse(this.userConfigHistory.pop());
|
|
|
|
this.onAction();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
redo() {
|
|
|
|
if (this.userConfigRedoHistory.length > 0) {
|
|
|
|
this.userConfigHistory.push(JSON.stringify(this.userConfig));
|
|
|
|
this.userConfig = JSON.parse(this.userConfigRedoHistory.shift());
|
|
|
|
this.onAction();
|
|
|
|
}
|
2019-02-28 08:00:59 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
watch: {
|
|
|
|
'$route.path'() {
|
|
|
|
this.defaultConfig && this.filterCurrentConfig();
|
|
|
|
if (!this.$refs.mainConfigurator) return;
|
|
|
|
this.$nextTick(() => {
|
|
|
|
this.$refs.mainConfigurator.scrollTop = 0;
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
</script>
|