theme-preview.tpl 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. <style lang="scss">
  2. .page-container.page-theme-preview {
  3. padding-top: 30px;
  4. .display {
  5. width: 75%;
  6. display: inline-block;
  7. vertical-align: top;
  8. h3 {
  9. font-size: 28px;
  10. margin: 30px 0 0 0;
  11. }
  12. }
  13. .side {
  14. display: inline-block;
  15. width: 25%;
  16. .editor {
  17. overflow: hidden;
  18. background: #f5f7fa;
  19. border: 1px solid #ebeef5;
  20. border-radius: 5px;
  21. margin-bottom: 20px;
  22. &.fixed {
  23. position: fixed;
  24. width: 285px;
  25. box-sizing: border-box;
  26. }
  27. }
  28. }
  29. }
  30. </style>
  31. <template>
  32. <div class="page-container page-theme-preview" ref="themePreview">
  33. <section class="display">
  34. <el-button type="text" icon="el-icon-back" @click="navBack">
  35. <%= 1 >
  36. </el-button>
  37. <h3>{{previewConfig.name}}</h3>
  38. <basic-tokens-preview>
  39. </basic-tokens-preview>
  40. <components-preview>
  41. </components-preview>
  42. </section>
  43. <aside class="side">
  44. <section class="editor" :style="{top: `${editorTop}px`, height: `${editorHeight}px`}" :class="{'fixed': isFixed}">
  45. <theme-configurator
  46. :isOfficial="isOfficial"
  47. :themeConfig="themeConfig"
  48. :previewConfig="previewConfig"
  49. :onUserConfigUpdate="onUserConfigUpdate"
  50. >
  51. </theme-configurator>
  52. </section>
  53. </aside>
  54. </div>
  55. </template>
  56. <script>
  57. import bus from '../../bus.js';
  58. import ThemeConfigurator from '../../components/theme-configurator';
  59. import ComponentsPreview from '../../components/theme/components-preview';
  60. import BasicTokensPreview from '../../components/theme/basic-tokens-preview';
  61. import {
  62. loadPreviewFromLocal,
  63. loadUserThemeFromLocal,
  64. saveUserThemeToLocal
  65. } from '../../components/theme/localstorage';
  66. import {
  67. getThemeConfigObject
  68. } from '../../components/theme/utils';
  69. import {
  70. ACTION_APPLY_THEME
  71. } from '../../components/theme/constant.js';
  72. import throttle from 'throttle-debounce/throttle';
  73. import { getActionDisplayName } from '../../components/theme-configurator/utils/utils';
  74. const maxUserTheme = 8;
  75. export default {
  76. components: {
  77. ThemeConfigurator,
  78. BasicTokensPreview,
  79. ComponentsPreview
  80. },
  81. data() {
  82. return {
  83. previewConfig: {},
  84. themeConfig: {},
  85. userTheme: [],
  86. editorTop: 0,
  87. editorHeight: 1000,
  88. isFixed: false
  89. };
  90. },
  91. computed: {
  92. isOfficial() {
  93. return this.previewConfig.type === 'official';
  94. }
  95. },
  96. created() {
  97. this.throttledHandleScroll = throttle(10, true, index => {
  98. this.handleScroll(index);
  99. });
  100. },
  101. methods: {
  102. navBack() {
  103. this.$router.go(-1);
  104. this.$nextTick(() => {
  105. window.scrollTo(0, 0);
  106. });
  107. },
  108. getNewUserThemeName(originName) {
  109. let n = 1;
  110. let name;
  111. while (true) {
  112. name = `${originName}-${n}`;
  113. if (this.userTheme.filter(theme => (theme.name === name)).length === 0) {
  114. break;
  115. }
  116. n += 1;
  117. }
  118. return name;
  119. },
  120. onUserConfigUpdate(userConfig) {
  121. const themeConfig = JSON.stringify(userConfig);
  122. const { type, name } = this.previewConfig;
  123. if (this.isOfficial) {
  124. if (this.userTheme.length >= maxUserTheme) {
  125. this.$message.error(getActionDisplayName('max-user-theme'));
  126. return;
  127. }
  128. const autoUserName = this.getNewUserThemeName(name);
  129. this.previewConfig.name = autoUserName;
  130. this.previewConfig.type = 'user';
  131. this.userTheme.push({
  132. update: Date.now(),
  133. name: autoUserName,
  134. theme: themeConfig
  135. });
  136. saveUserThemeToLocal(this.userTheme);
  137. return;
  138. }
  139. if (type === 'user') {
  140. this.userTheme.forEach((config) => {
  141. if (config.name === name) {
  142. config.update = Date.now();
  143. config.theme = themeConfig;
  144. }
  145. });
  146. saveUserThemeToLocal(this.userTheme);
  147. }
  148. },
  149. handleScroll() {
  150. const rect = this.$refs.themePreview.getBoundingClientRect();
  151. let offsetTop = rect.top;
  152. let offsetBottom = rect.bottom;
  153. const calHeight = this.editorHeight + 30 + 20;
  154. if (offsetTop < 0) {
  155. this.isFixed = true;
  156. if (offsetBottom < calHeight) {
  157. this.editorTop = 30 - calHeight + offsetBottom;
  158. } else {
  159. this.editorTop = 30;
  160. }
  161. } else {
  162. this.isFixed = false;
  163. this.editorTop = 0;
  164. }
  165. }
  166. },
  167. beforeDestroy() {
  168. window.removeEventListener('scroll', this.throttledHandleScroll);
  169. },
  170. mounted() {
  171. this.editorHeight = window.innerHeight - 40 - 5;
  172. window.addEventListener('scroll', this.throttledHandleScroll);
  173. this.userTheme = loadUserThemeFromLocal();
  174. const previewConfig = loadPreviewFromLocal();
  175. const pageRefer = this.$route.params.refer;
  176. if (!previewConfig || !pageRefer) {
  177. this.$alert(getActionDisplayName('no-preview-config'), getActionDisplayName('notice'), {
  178. confirmButtonText: getActionDisplayName('confirm'),
  179. callback: action => {
  180. const newPath = this.$route.path.replace('/preview', '');
  181. this.$router.replace(newPath);
  182. }
  183. });
  184. return;
  185. }
  186. this.previewConfig = previewConfig;
  187. const themeConfig = getThemeConfigObject(previewConfig.theme);
  188. if (themeConfig) {
  189. this.themeConfig = themeConfig;
  190. bus.$emit(ACTION_APPLY_THEME, themeConfig);
  191. }
  192. }
  193. };
  194. </script>