index.vue 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. <template>
  2. <div class="main-configurator" ref='configurator'>
  3. <action-panel
  4. :selectOptions="selectOptions"
  5. :userConfigHistory="userConfigHistory"
  6. :userConfigRedoHistory="userConfigRedoHistory"
  7. :onUndo="undo"
  8. :onRedo="redo"
  9. :isOfficial="isOfficial"
  10. @select="onSelectChange"
  11. ></action-panel>
  12. <main-panel
  13. v-if="defaultConfig"
  14. :currentConfig="currentConfig"
  15. :defaultConfig="defaultConfig"
  16. :userConfig="userConfig"
  17. :globalValue="globalValue"
  18. @onChange="userConfigChange"
  19. ></main-panel>
  20. </div>
  21. </template>
  22. <style lang="scss">
  23. .main-configurator {
  24. height: 100%;
  25. display: flex;
  26. flex-direction: column;
  27. }
  28. </style>
  29. <script>
  30. import bus from '../../bus.js';
  31. import { getVars } from '../theme/loader/api.js';
  32. import mainPanel from './main';
  33. import actionPanel from './action';
  34. import {
  35. filterConfigType,
  36. filterGlobalValue,
  37. getActionDisplayName
  38. } from './utils/utils.js';
  39. import Shortcut from './shortcut';
  40. import {
  41. ACTION_APPLY_THEME,
  42. ACTION_DOWNLOAD_THEME,
  43. ACTION_COMPONECT_SELECT
  44. } from '../theme/constant.js';
  45. export default {
  46. props: {
  47. themeConfig: Object,
  48. isOfficial: Boolean,
  49. onUserConfigUpdate: Function
  50. },
  51. components: {
  52. mainPanel,
  53. actionPanel
  54. },
  55. data() {
  56. return {
  57. init: false,
  58. defaultConfig: null,
  59. currentConfig: null,
  60. userConfig: {
  61. global: {},
  62. local: {}
  63. },
  64. userConfigHistory: [],
  65. userConfigRedoHistory: [],
  66. hasLocalConfig: false,
  67. selectOptions: [],
  68. selectedComponent: 'color'
  69. };
  70. },
  71. mixins: [Shortcut],
  72. computed: {
  73. globalValue() {
  74. return filterGlobalValue(this.defaultConfig, this.userConfig);
  75. }
  76. },
  77. mounted() {
  78. ga('send', 'event', 'ThemeConfigurator', 'Init');
  79. this.showConfigurator();
  80. this.enableShortcut();
  81. },
  82. beforeDestroy() {
  83. this.disableShortcut();
  84. },
  85. methods: {
  86. getActionDisplayName(key) {
  87. return getActionDisplayName(key);
  88. },
  89. showConfigurator() {
  90. if (this.init) return;
  91. this.$nextTick(() => {
  92. const loading = this.$loading({
  93. target: this.$refs.configurator
  94. });
  95. let defaultConfig;
  96. getVars()
  97. .then(res => {
  98. defaultConfig = res;
  99. })
  100. .catch(err => {
  101. this.onError(err);
  102. })
  103. .then(() => {
  104. setTimeout(() => {
  105. if (defaultConfig) {
  106. this.defaultConfig = defaultConfig;
  107. this.setSelectOption();
  108. this.filterCurrentConfig();
  109. this.init = true;
  110. }
  111. loading.close();
  112. }, 300); // action after transition
  113. });
  114. });
  115. },
  116. setSelectOption() {
  117. this.selectOptions = this.defaultConfig.map((config) => ({
  118. label: config.name.charAt(0).toUpperCase() + config.name.slice(1),
  119. value: config.name
  120. })).sort((a, b) => {
  121. const A = a.label;
  122. const B = b.label;
  123. if (A < B) return -1;
  124. if (A > B) return 1;
  125. return 0;
  126. });
  127. },
  128. filterCurrentConfig() {
  129. this.currentConfig = this.defaultConfig.find(config => {
  130. return (
  131. config.name === this.selectedComponent
  132. );
  133. });
  134. },
  135. userConfigChange(e) {
  136. this.userConfigHistory.push(JSON.stringify(this.userConfig));
  137. this.userConfigRedoHistory = [];
  138. this.$set(
  139. this.userConfig[filterConfigType(this.currentConfig.name)],
  140. e.key,
  141. e.value
  142. );
  143. this.onAction();
  144. },
  145. onReset() {
  146. this.userConfigRedoHistory = [];
  147. this.userConfigHistory = [];
  148. this.userConfig = {
  149. global: {},
  150. local: {}
  151. };
  152. this.onAction();
  153. },
  154. onDownload() {
  155. bus.$emit(ACTION_DOWNLOAD_THEME, this.userConfig);
  156. },
  157. onAction() {
  158. this.onUserConfigUpdate(this.userConfig);
  159. bus.$emit(ACTION_APPLY_THEME, this.userConfig);
  160. },
  161. undo() {
  162. if (this.userConfigHistory.length > 0) {
  163. this.userConfigRedoHistory.push(JSON.stringify(this.userConfig));
  164. this.userConfig = JSON.parse(this.userConfigHistory.pop());
  165. this.onAction();
  166. }
  167. },
  168. redo() {
  169. if (this.userConfigRedoHistory.length > 0) {
  170. this.userConfigHistory.push(JSON.stringify(this.userConfig));
  171. this.userConfig = JSON.parse(this.userConfigRedoHistory.shift());
  172. this.onAction();
  173. }
  174. },
  175. onSelectChange(val) {
  176. bus.$emit(ACTION_COMPONECT_SELECT, val);
  177. this.selectedComponent = val;
  178. this.filterCurrentConfig();
  179. }
  180. },
  181. watch: {
  182. themeConfig: {
  183. handler(val, oldVal) {
  184. if (!oldVal.globnal) {
  185. this.userConfig = val;
  186. }
  187. }
  188. }
  189. }
  190. };
  191. </script>