theme-card.vue 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  1. <style lang="scss">
  2. .theme-card-item {
  3. user-select: none;
  4. border-radius: 4px;
  5. overflow: hidden;
  6. background: #fff;
  7. height: 90%;
  8. margin: 25px 0;
  9. box-shadow: 0 0 1px 0 #666;
  10. &.is-hidden {
  11. opacity: 0;
  12. height: 0;
  13. }
  14. .upload {
  15. cursor: pointer;
  16. background: #f5f7fa;
  17. height: 100%;
  18. width: 100%;
  19. display: flex;
  20. justify-content: center;
  21. align-items:center;
  22. .upload-action {
  23. width: 40%;
  24. margin: 0 auto;
  25. text-align: center;
  26. color: #606266;
  27. img {
  28. display: block;
  29. margin: 0 auto;
  30. }
  31. span {
  32. display: block;
  33. font-size: 14px;
  34. margin-top: 8px;
  35. }
  36. }
  37. }
  38. .preview {
  39. position: relative;
  40. height: 70%;
  41. width: 100%;
  42. .line {
  43. height: 50%;
  44. }
  45. .line-2 {
  46. width: 50%;
  47. height: 100%;
  48. display: inline-block;
  49. }
  50. .line-4 {
  51. width: 25%;
  52. height: 100%;
  53. display: inline-block;
  54. }
  55. .action {
  56. transition: all .3s;
  57. position: absolute;
  58. opacity: 0;
  59. top: 0;
  60. left: 0;
  61. right: 0;
  62. bottom: 0;
  63. .action-mask {
  64. position: absolute;
  65. top: 0;
  66. left: 0;
  67. right: 0;
  68. bottom: 0;
  69. background: #000;
  70. opacity: 0.4;
  71. }
  72. .action-block {
  73. position: absolute;
  74. width: 50%;
  75. height: 50%;
  76. left: 25%;
  77. top: 25%;
  78. }
  79. .action-item {
  80. cursor: pointer;
  81. display: inline-block;
  82. height: 100%;
  83. width: 30%;
  84. color: #eee;
  85. &:hover {
  86. color: #fff;
  87. .circle {
  88. border-color: #fff;
  89. }
  90. }
  91. .icon {
  92. height: 50%;
  93. font-size: 22px;
  94. text-align: center;
  95. display: flex;
  96. justify-content:center;
  97. align-items:center;
  98. img {
  99. width: 130%;
  100. }
  101. }
  102. .name {
  103. font-size: 12px;
  104. height: 50%;
  105. text-align: center;
  106. display: flex;
  107. justify-content:center;
  108. align-items:center;
  109. margin-top: 4px;
  110. }
  111. }
  112. .action-item-right {
  113. margin-left: 40%;
  114. }
  115. }
  116. }
  117. .info {
  118. height: 30%;
  119. line-height: 16px;
  120. display: flex;
  121. align-items: center;
  122. .info-center {
  123. width: 100%;
  124. }
  125. .title {
  126. font-weight: bold;
  127. font-size: 16px;
  128. color: #303133;
  129. padding: 0 12px;
  130. justify-content: space-between;
  131. }
  132. .right {
  133. float: right;
  134. font-weight: normal;
  135. font-size: 14px;
  136. color: #909399;
  137. }
  138. .more {
  139. font-size: 16px;
  140. cursor: pointer;
  141. }
  142. .description {
  143. padding: 0 12px;
  144. font-size: 14px;
  145. color: #606266;
  146. margin-top: 10px;
  147. }
  148. }
  149. &.is-upload {
  150. box-shadow: none;
  151. border: 1px dashed #DCDFE6;
  152. }
  153. &.is-upload:hover {
  154. box-shadow: none;
  155. }
  156. &:hover {
  157. box-shadow: 0 0 10px 0 #999;
  158. .action {
  159. opacity: 1;
  160. }
  161. }
  162. }
  163. </style>
  164. <template>
  165. <section class="theme-card-item" :class="{'is-hidden': !config || !config.name, 'is-upload': isUpload}">
  166. <template v-if="isUpload">
  167. <div class="upload" @click="uploadClick">
  168. <div class="upload-action">
  169. <img src="../../assets/images/icon-upload.svg"/>
  170. <span>{{getActionDisplayName('upload-theme')}}</span>
  171. </div>
  172. </div>
  173. <input
  174. class="el-upload__input"
  175. type="file"
  176. ref="input"
  177. @change="uploadAction"
  178. accept="application/json"
  179. />
  180. </template>
  181. <template v-else>
  182. <div class="preview">
  183. <div class="line">
  184. <span class="line-2" :style="{background: mainColor}"></span>
  185. <span class="line-2" :style="{background: textPrimaryColor}"></span>
  186. </div>
  187. <div class="line">
  188. <span class="line-4" :style="{background: mainColor50}"></span>
  189. <span class="line-4" :style="{background: mainColor80}"></span>
  190. <span class="line-4" :style="{background: borderBaseColor}"></span>
  191. <span class="line-4" :style="{background: textSecondaryColor}"></span>
  192. </div>
  193. <div class="action">
  194. <div class="action-mask"></div>
  195. <div class="action-block">
  196. <div
  197. class="action-item"
  198. :class="index && 'action-item-right'"
  199. v-for="(item, index) in actionArray"
  200. :key="index"
  201. @click="iconClick(item.action)"
  202. >
  203. <div class="icon">
  204. <img :src="item.icon"/>
  205. <span class="circle"></span>
  206. </div>
  207. <div class="name">
  208. <span>{{item.name}}</span>
  209. </div>
  210. </div>
  211. </div>
  212. </div>
  213. </div>
  214. <div class="info">
  215. <div class="info-center">
  216. <div class="title">
  217. <span>{{config.name}}</span>
  218. <span class="right" v-if="isOfficial">by {{config.author}}</span>
  219. <span class="right more" v-else>
  220. <el-dropdown @command="actionClick">
  221. <i class="el-icon-more"></i>
  222. <el-dropdown-menu slot="dropdown">
  223. <el-dropdown-item command="rename">{{getActionDisplayName('rename-theme')}}</el-dropdown-item>
  224. <el-dropdown-item command="copy">{{getActionDisplayName('copy-theme')}}</el-dropdown-item>
  225. <el-dropdown-item
  226. command="delete"
  227. style="color: #F56C6C;"
  228. >
  229. {{getActionDisplayName('delete-theme')}}
  230. </el-dropdown-item>
  231. </el-dropdown-menu>
  232. </el-dropdown>
  233. </span>
  234. </div>
  235. <div class="description" v-if="isOfficial">{{getActionDisplayName(getDescriptionKey(config.name))}} </div>
  236. <div class="description" v-else>{{getActionDisplayName('last-modified')}} {{formatDate(config.update)}}</div>
  237. </div>
  238. </div>
  239. </template>
  240. </section>
  241. </template>
  242. <script>
  243. import bus from '../../bus';
  244. import {
  245. DEFAULT_THEME_CONFIG,
  246. ACTION_DOWNLOAD_THEME
  247. } from './constant.js';
  248. import { savePreviewToLocal } from './localstorage';
  249. import { tintColor } from '../../color.js';
  250. import dateUtil from 'element-ui/src/utils/date';
  251. import { getActionDisplayName } from '../theme-configurator/utils/utils';
  252. export default {
  253. props: {
  254. config: Object,
  255. type: String,
  256. base: {
  257. type: String,
  258. default: ''
  259. }
  260. },
  261. data() {
  262. return {
  263. deleteVisible: false
  264. };
  265. },
  266. methods: {
  267. getActionDisplayName(key) {
  268. return getActionDisplayName(key);
  269. },
  270. getDescriptionKey(name) {
  271. return name ? `description-${name.toLowerCase()}` : '';
  272. },
  273. formatDate(timestamp) {
  274. if (!timestamp) return '';
  275. return dateUtil.format(new Date(timestamp), 'yyyy-MM-dd HH:mm');
  276. },
  277. uploadClick() {
  278. this.$refs.input.value = null;
  279. this.$refs.input.click();
  280. },
  281. uploadAction(ev) {
  282. const files = ev.target.files;
  283. if (!files) return;
  284. var reader = new FileReader();
  285. reader.onload = (e) => {
  286. try {
  287. const jsonString = e.target.result;
  288. const jsonObject = JSON.parse(jsonString);
  289. if (!jsonObject.global || !jsonObject.local) {
  290. return this.$message.error('JSON format error');
  291. }
  292. this.$emit('action', 'upload', jsonString);
  293. } catch (e) {
  294. this.$message.error('Upload error');
  295. console.error(e);
  296. }
  297. };
  298. reader.readAsText(files[0]);
  299. },
  300. actionClick(e) {
  301. this.$emit('action', e, this.config);
  302. },
  303. iconClick(e) {
  304. switch (e) {
  305. case 'preview':
  306. case 'edit':
  307. const { name, theme } = this.config;
  308. savePreviewToLocal({
  309. type: this.type,
  310. name,
  311. theme
  312. });
  313. this.$router.push({
  314. name: `theme-preview-${this.$route.meta.lang}`,
  315. params: {
  316. refer: 'theme'
  317. }
  318. });
  319. this.$nextTick(() => {
  320. window.scrollTo(0, 0);
  321. });
  322. break;
  323. case 'download':
  324. bus.$emit(ACTION_DOWNLOAD_THEME, this.theme, this.config.name);
  325. break;
  326. default:
  327. this.$emit('action', e, this.config);
  328. return;
  329. }
  330. },
  331. deleteUserTheme() {
  332. this.deleteVisible = false;
  333. this.$emit('action', 'delete', this.config);
  334. }
  335. },
  336. computed: {
  337. isUpload() {
  338. return this.type === 'upload';
  339. },
  340. theme() {
  341. if (this.config.theme) {
  342. return JSON.parse(this.config.theme);
  343. }
  344. return DEFAULT_THEME_CONFIG;
  345. },
  346. mainColor() {
  347. return this.theme.global['$--color-primary'] || '#1989FA';
  348. },
  349. mainColor50() {
  350. return tintColor(this.mainColor, 0.5);
  351. },
  352. mainColor80() {
  353. return tintColor(this.mainColor, 0.8);
  354. },
  355. textPrimaryColor() {
  356. return this.theme.global['$--color-text-primary'] || '#303133';
  357. },
  358. borderBaseColor() {
  359. return this.theme.global['$--border-color-base'] || '#DCDFE6';
  360. },
  361. textSecondaryColor() {
  362. return this.theme.global['$--color-text-secondary'] || '#909399';
  363. },
  364. isOfficial() {
  365. return this.type === 'official';
  366. },
  367. actionArray() {
  368. if (this.isOfficial) {
  369. return [
  370. {
  371. icon: require('../../assets/images/icon-check.png'),
  372. name: getActionDisplayName('theme-check'),
  373. action: 'preview'
  374. },
  375. {
  376. icon: require('../../assets/images/icon-copy.png'),
  377. name: getActionDisplayName('theme-copy'),
  378. action: 'copy'
  379. }
  380. ];
  381. }
  382. return [
  383. {
  384. icon: require('../../assets/images/icon-edit.png'),
  385. name: getActionDisplayName('theme-edit'),
  386. action: 'edit'
  387. },
  388. {
  389. icon: require('../../assets/images/icon-download.png'),
  390. name: getActionDisplayName('download-theme'),
  391. action: 'download'
  392. }
  393. ];
  394. }
  395. }
  396. };
  397. </script>