ColorPicker.vue 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. <template>
  2. <el-color-picker
  3. :predefine="groupedPredefineColors"
  4. popper-class="color-picker has-history"
  5. v-bind="$attrs"
  6. v-on="$listeners"
  7. @change="saveColorRecentlyUsed($event)"
  8. />
  9. </template>
  10. <script>
  11. import { GetColorList_Common, GetColorList_RecentlyUsed, SaveColor_RecentlyUsed } from '@/api/project';
  12. export default {
  13. name: 'ColorPicker',
  14. inheritAttrs: false,
  15. props: {
  16. // 常用颜色类型
  17. type: {
  18. type: Number,
  19. default: 0,
  20. },
  21. },
  22. data() {
  23. return {
  24. commonList: [], // 常用颜色
  25. recentlyUsedList: [], // 制作人员历史颜色
  26. };
  27. },
  28. computed: {
  29. groupedPredefineColors() {
  30. return [
  31. ...this.commonList.map(({ color }) => `#${color}`),
  32. ...this.recentlyUsedList.map(({ color }) => `#${color}`),
  33. ];
  34. },
  35. },
  36. created() {
  37. this.getColorList_Common();
  38. this.getColorList_RecentlyUsed();
  39. },
  40. methods: {
  41. getColorList_Common() {
  42. GetColorList_Common({ type: this.type }).then(({ color_list }) => {
  43. this.commonList = color_list;
  44. });
  45. },
  46. getColorList_RecentlyUsed() {
  47. GetColorList_RecentlyUsed({ type: this.type }).then(({ color_list }) => {
  48. this.recentlyUsedList = color_list;
  49. });
  50. },
  51. saveColorRecentlyUsed(color) {
  52. SaveColor_RecentlyUsed({ type: this.type, color: color.replace('#', '') }).then(() => {
  53. this.getColorList_RecentlyUsed();
  54. });
  55. },
  56. },
  57. };
  58. </script>
  59. <style lang="scss">
  60. .color-picker {
  61. .el-color-predefine__colors {
  62. display: flex;
  63. flex-wrap: wrap;
  64. &::before {
  65. flex: 0 0 100%;
  66. order: 0;
  67. margin-bottom: 8px;
  68. font-size: 12px;
  69. color: #666;
  70. content: '常用颜色';
  71. }
  72. }
  73. .el-color-predefine__color-selector:nth-child(-n + 20) {
  74. order: 1;
  75. }
  76. &.has-history .el-color-predefine__colors::after {
  77. flex: 0 0 100%;
  78. order: 2;
  79. margin-top: 10px;
  80. margin-bottom: 8px;
  81. font-size: 12px;
  82. color: #666;
  83. content: '制作人员历史颜色';
  84. }
  85. .el-color-predefine__color-selector:nth-child(n + 21) {
  86. order: 3;
  87. }
  88. }
  89. </style>