Blame view

src/components/color-picker/recommend-colors.vue 4.32 KB
9af2f01c   梁灏   update ColorPicker
1
  <template>
f2bcd4ad   Graham Fairweather   Color keyboard co...
2
3
4
5
6
7
8
9
10
11
12
13
14
      <div
          ref="reference"
          tabindex="0"
          @click="handleClick"
          @keydown.esc="handleEscape"
          @keydown.enter="handleEnter"
          @keydown.left="handleArrow($event, 'x', left)"
          @keydown.right="handleArrow($event, 'x', right)"
          @keydown.up="handleArrow($event, 'y', up)"
          @keydown.down="handleArrow($event, 'y', down)"
          @blur="blurColor"
          @focus="focusColor"
      >
9af2f01c   梁灏   update ColorPicker
15
          <template v-for="(item, index) in list">
f2bcd4ad   Graham Fairweather   Color keyboard co...
16
17
18
19
20
21
22
23
24
25
26
27
28
29
              <div
                  :key="item + ':' + index"
                  :class="[prefixCls + '-picker-colors-wrapper']">
                  <div :data-color-id="index">
                      <div
                          :style="{background: item}"
                          :class="[prefixCls + '-picker-colors-wrapper-color']"
                      ></div>
                      <div
                          :ref="'color-circle-' + index"
                          :class="[prefixCls + '-picker-colors-wrapper-circle', hideClass]"></div>
                  </div>
              </div>
              <br v-if="lineBreak(list, index)">
9af2f01c   梁灏   update ColorPicker
30
31
32
          </template>
      </div>
  </template>
f2bcd4ad   Graham Fairweather   Color keyboard co...
33
  
9af2f01c   梁灏   update ColorPicker
34
  <script>
f2bcd4ad   Graham Fairweather   Color keyboard co...
35
36
37
38
39
40
41
42
43
44
45
46
47
48
  import Emitter from '../../mixins/emitter';
  import HandleEscapeMixin from './handleEscapeMixin';
  import Prefixes from './prefixMixin';
  import {clamp} from './utils';
  
  export default {
      name: 'RecommendedColors',
  
      mixins: [Emitter, HandleEscapeMixin, Prefixes],
  
      props: {
          list: {
              type: Array,
              default: undefined,
9af2f01c   梁灏   update ColorPicker
49
          },
f2bcd4ad   Graham Fairweather   Color keyboard co...
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
      },
  
      data() {
          const columns = 12;
          const rows = Math.ceil(this.list.length / columns);
          const normalStep = 1;
  
          return {
              left: -normalStep,
              right: normalStep,
              up: -normalStep,
              down: normalStep,
              powerKey: 'shiftKey',
              grid: {x: 1, y: 1},
              rows,
              columns,
          };
      },
  
      computed: {
          hideClass() {
              return `${this.prefixCls}-hide`;
          },
          linearIndex() {
              return this.getLinearIndex(this.grid);
          },
          currentCircle() {
              return this.$refs[`color-circle-${this.linearIndex}`][0];
          },
      },
  
      methods: {
          getLinearIndex(grid) {
              return this.columns * (grid.y - 1) + grid.x - 1;
          },
          getMaxLimit(axis) {
              return axis === 'x' ? this.columns : this.rows;
          },
          handleArrow(e, axis, direction) {
              e.preventDefault();
              e.stopPropagation();
  
              this.blurColor();
  
              const grid = {...this.grid};
  
              if (e[this.powerKey]) {
                  if (direction < 0) {
                      grid[axis] = 1;
                  } else {
                      grid[axis] = this.getMaxLimit(axis);
                  }
              } else {
                  grid[axis] += direction;
              }
  
              const index = this.getLinearIndex(grid);
  
              if (index >= 0 && index < this.list.length) {
                  this.grid[axis] = clamp(grid[axis], 1, this.getMaxLimit(axis));
              }
  
              this.focusColor();
          },
          blurColor() {
              this.currentCircle.classList.add(this.hideClass);
          },
          focusColor() {
              this.currentCircle.classList.remove(this.hideClass);
          },
          handleEnter(e) {
              this.handleClick(e, this.currentCircle);
          },
          handleClick(e, circle) {
              e.preventDefault();
              e.stopPropagation();
  
              this.$refs.reference.focus();
  
              const target = circle || e.target;
              const colorId = target.dataset.colorId || target.parentElement.dataset.colorId;
  
              if (colorId) {
                  this.blurColor();
                  const id = Number(colorId) + 1;
                  this.grid.x = id % this.columns || this.columns;
                  this.grid.y = Math.ceil(id / this.columns);
                  this.focusColor();
                  this.$emit('picker-color', this.list[colorId]);
                  this.$emit('change', {hex: this.list[colorId], source: 'hex'});
1f61a559   梁灏   update ColorPicker
140
              }
f2bcd4ad   Graham Fairweather   Color keyboard co...
141
142
143
144
145
146
147
148
149
150
151
152
153
          },
          lineBreak(list, index) {
              if (!index) {
                  return false;
              }
  
              const nextIndex = index + 1;
  
              return nextIndex < list.length && nextIndex % this.columns === 0;
          },
      },
  };
  </script>