theme-card.vue 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  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. from: String
  261. },
  262. data() {
  263. return {
  264. deleteVisible: false
  265. };
  266. },
  267. methods: {
  268. getActionDisplayName(key) {
  269. return getActionDisplayName(key);
  270. },
  271. getDescriptionKey(name) {
  272. return name ? `description-${name.toLowerCase()}` : '';
  273. },
  274. formatDate(timestamp) {
  275. if (!timestamp) return '';
  276. return dateUtil.format(new Date(timestamp), 'yyyy-MM-dd HH:mm');
  277. },
  278. uploadClick() {
  279. this.$refs.input.value = null;
  280. this.$refs.input.click();
  281. },
  282. uploadAction(ev) {
  283. const files = ev.target.files;
  284. if (!files) return;
  285. var reader = new FileReader();
  286. reader.onload = (e) => {
  287. try {
  288. const jsonString = e.target.result;
  289. const jsonObject = JSON.parse(jsonString);
  290. if (!jsonObject.global || !jsonObject.local) {
  291. return this.$message.error('JSON format error');
  292. }
  293. this.$emit('action', 'upload', jsonString);
  294. } catch (e) {
  295. this.$message.error('Upload error');
  296. console.error(e);
  297. }
  298. };
  299. reader.readAsText(files[0]);
  300. },
  301. actionClick(e) {
  302. this.$emit('action', e, this.config);
  303. },
  304. iconClick(e) {
  305. switch (e) {
  306. case 'preview':
  307. case 'edit':
  308. if (this.from) {
  309. this.$emit('action', e, this.config);
  310. return;
  311. }
  312. const { name, theme } = this.config;
  313. savePreviewToLocal({
  314. type: this.type,
  315. name,
  316. theme
  317. });
  318. this.$router.push({
  319. name: `theme-preview-${this.$route.meta.lang}`,
  320. params: {
  321. refer: 'theme'
  322. }
  323. });
  324. this.$nextTick(() => {
  325. window.scrollTo(0, 0);
  326. });
  327. break;
  328. case 'download':
  329. bus.$emit(ACTION_DOWNLOAD_THEME, this.theme, this.config.name);
  330. break;
  331. default:
  332. this.$emit('action', e, this.config);
  333. return;
  334. }
  335. },
  336. deleteUserTheme() {
  337. this.deleteVisible = false;
  338. this.$emit('action', 'delete', this.config);
  339. }
  340. },
  341. computed: {
  342. isUpload() {
  343. return this.type === 'upload';
  344. },
  345. theme() {
  346. if (this.config.theme) {
  347. return JSON.parse(this.config.theme);
  348. }
  349. return DEFAULT_THEME_CONFIG;
  350. },
  351. mainColor() {
  352. return this.theme.global['$--color-primary'] || '#1989FA';
  353. },
  354. mainColor50() {
  355. return tintColor(this.mainColor, 0.5);
  356. },
  357. mainColor80() {
  358. return tintColor(this.mainColor, 0.8);
  359. },
  360. textPrimaryColor() {
  361. return this.theme.global['$--color-text-primary'] || '#303133';
  362. },
  363. borderBaseColor() {
  364. return this.theme.global['$--border-color-base'] || '#DCDFE6';
  365. },
  366. textSecondaryColor() {
  367. return this.theme.global['$--color-text-secondary'] || '#909399';
  368. },
  369. isOfficial() {
  370. return this.type === 'official';
  371. },
  372. actionArray() {
  373. if (this.isOfficial) {
  374. return [
  375. {
  376. icon: require('../../assets/images/icon-check.png'),
  377. name: getActionDisplayName('theme-check'),
  378. action: 'preview'
  379. },
  380. {
  381. icon: require('../../assets/images/icon-copy.png'),
  382. name: getActionDisplayName('theme-copy'),
  383. action: 'copy'
  384. }
  385. ];
  386. }
  387. return [
  388. {
  389. icon: require('../../assets/images/icon-edit.png'),
  390. name: getActionDisplayName('theme-edit'),
  391. action: 'edit'
  392. },
  393. {
  394. icon: require('../../assets/images/icon-download.png'),
  395. name: getActionDisplayName('download-theme'),
  396. action: 'download'
  397. }
  398. ];
  399. }
  400. }
  401. };
  402. </script>