index.vue 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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. previewConfig: Object,
  49. isOfficial: Boolean,
  50. onUserConfigUpdate: Function
  51. },
  52. components: {
  53. mainPanel,
  54. actionPanel
  55. },
  56. data() {
  57. return {
  58. init: false,
  59. defaultConfig: null,
  60. currentConfig: null,
  61. userConfig: {
  62. global: {},
  63. local: {}
  64. },
  65. userConfigHistory: [],
  66. userConfigRedoHistory: [],
  67. hasLocalConfig: false,
  68. selectOptions: [],
  69. selectedComponent: 'color'
  70. };
  71. },
  72. mixins: [Shortcut],
  73. computed: {
  74. globalValue() {
  75. return filterGlobalValue(this.defaultConfig, this.userConfig);
  76. }
  77. },
  78. mounted() {
  79. ga('send', 'event', 'ThemeConfigurator', 'Init');
  80. this.showConfigurator();
  81. this.enableShortcut();
  82. },
  83. beforeDestroy() {
  84. this.disableShortcut();
  85. },
  86. methods: {
  87. getActionDisplayName(key) {
  88. return getActionDisplayName(key);
  89. },
  90. showConfigurator() {
  91. if (this.init) return;
  92. this.$nextTick(() => {
  93. const loading = this.$loading({
  94. target: this.$refs.configurator
  95. });
  96. let defaultConfig;
  97. getVars()
  98. .then(res => {
  99. defaultConfig = res;
  100. })
  101. .catch(err => {
  102. this.onError(err);
  103. })
  104. .then(() => {
  105. setTimeout(() => {
  106. if (defaultConfig) {
  107. this.defaultConfig = defaultConfig;
  108. this.setSelectOption();
  109. this.filterCurrentConfig();
  110. this.init = true;
  111. }
  112. loading.close();
  113. }, 300); // action after transition
  114. });
  115. });
  116. },
  117. setSelectOption() {
  118. this.selectOptions = this.defaultConfig.map((config) => ({
  119. label: config.name.charAt(0).toUpperCase() + config.name.slice(1),
  120. value: config.name
  121. })).sort((a, b) => {
  122. const A = a.label;
  123. const B = b.label;
  124. if (A < B) return -1;
  125. if (A > B) return 1;
  126. return 0;
  127. });
  128. },
  129. filterCurrentConfig() {
  130. this.currentConfig = this.defaultConfig.find(config => {
  131. return (
  132. config.name === this.selectedComponent
  133. );
  134. });
  135. },
  136. userConfigChange(e) {
  137. this.userConfigHistory.push(JSON.stringify(this.userConfig));
  138. this.userConfigRedoHistory = [];
  139. this.$set(
  140. this.userConfig[filterConfigType(this.currentConfig.name)],
  141. e.key,
  142. e.value
  143. );
  144. this.onAction();
  145. },
  146. onReset() {
  147. this.userConfigRedoHistory = [];
  148. this.userConfigHistory = [];
  149. this.userConfig = {
  150. global: {},
  151. local: {}
  152. };
  153. this.onAction();
  154. },
  155. onDownload() {
  156. bus.$emit(ACTION_DOWNLOAD_THEME, this.userConfig, this.previewConfig.name);
  157. },
  158. onAction() {
  159. this.onUserConfigUpdate(this.userConfig);
  160. bus.$emit(ACTION_APPLY_THEME, this.userConfig);
  161. },
  162. undo() {
  163. if (this.userConfigHistory.length > 0) {
  164. this.userConfigRedoHistory.push(JSON.stringify(this.userConfig));
  165. this.userConfig = JSON.parse(this.userConfigHistory.pop());
  166. this.onAction();
  167. }
  168. },
  169. redo() {
  170. if (this.userConfigRedoHistory.length > 0) {
  171. this.userConfigHistory.push(JSON.stringify(this.userConfig));
  172. this.userConfig = JSON.parse(this.userConfigRedoHistory.shift());
  173. this.onAction();
  174. }
  175. },
  176. onSelectChange(val) {
  177. bus.$emit(ACTION_COMPONECT_SELECT, val);
  178. this.selectedComponent = val;
  179. this.filterCurrentConfig();
  180. }
  181. },
  182. watch: {
  183. themeConfig: {
  184. handler(val, oldVal) {
  185. if (!oldVal.globnal) {
  186. this.userConfig = val;
  187. }
  188. }
  189. }
  190. }
  191. };
  192. </script>