Blame view

src/components/color-picker/hue.vue 2.34 KB
e7893a68   梁灏   update ColorPicker
1
  <template>
f2bcd4ad   Graham Fairweather   Color keyboard co...
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
      <div
          :class="[prefixCls + '-hue']"
          tabindex="0"
          @click="$el.focus()"
          @keydown.esc="handleEscape"
          @keydown.left="handleLeft"
          @keydown.right="handleRight"
          @keydown.up="handleUp"
          @keydown.down="handleDown"
      >
          <div
              ref="container"
              :class="[prefixCls + '-hue-container']"
              @mousedown="handleMouseDown"
              @touchmove="handleChange"
              @touchstart="handleChange">
              <div
                  :style="{top: 0, left: `${percent}%`}"
                  :class="[prefixCls + '-hue-pointer']">
                  <div :class="[prefixCls + '-hue-picker']"></div>
e7893a68   梁灏   update ColorPicker
22
23
24
25
              </div>
          </div>
      </div>
  </template>
f2bcd4ad   Graham Fairweather   Color keyboard co...
26
  
e7893a68   梁灏   update ColorPicker
27
  <script>
f2bcd4ad   Graham Fairweather   Color keyboard co...
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
  import HASMixin from './hsaMixin';
  import Prefixes from './prefixMixin';
  import {clamp} from './utils';
  
  export default {
      name: 'Hue',
  
      mixins: [HASMixin, Prefixes],
  
      data() {
          const normalStep = 1 / 360 * 25;
          const jumpStep = 20 * normalStep;
  
          return {
              left: -normalStep,
              right: normalStep,
              up: jumpStep,
              down: -jumpStep,
              powerKey: 'shiftKey',
              percent: clamp(this.value.hsl.h * 100 / 360, 0, 100),
          };
      },
  
      methods: {
          change(percent) {
              this.percent = clamp(percent, 0, 100);
e7893a68   梁灏   update ColorPicker
54
  
f2bcd4ad   Graham Fairweather   Color keyboard co...
55
56
57
58
59
              const {h, s, l, a} = this.value.hsl;
              const newHue = clamp(percent / 100 * 360, 0, 360);
  
              if (h !== newHue) {
                  this.$emit('change', {h: newHue, s, l, a, source: 'hsl'});
e7893a68   梁灏   update ColorPicker
60
61
              }
          },
f2bcd4ad   Graham Fairweather   Color keyboard co...
62
63
64
          handleSlide(e, direction) {
              e.preventDefault();
              e.stopPropagation();
e7893a68   梁灏   update ColorPicker
65
  
f2bcd4ad   Graham Fairweather   Color keyboard co...
66
67
68
69
              if (e[this.powerKey]) {
                  this.change(direction < 0 ? 0 : 100);
                  return;
              }
e7893a68   梁灏   update ColorPicker
70
  
f2bcd4ad   Graham Fairweather   Color keyboard co...
71
72
73
74
75
              this.change(this.percent + direction);
          },
          handleChange(e) {
              e.preventDefault();
              e.stopPropagation();
e7893a68   梁灏   update ColorPicker
76
  
f2bcd4ad   Graham Fairweather   Color keyboard co...
77
              const left = this.getLeft(e);
e7893a68   梁灏   update ColorPicker
78
  
f2bcd4ad   Graham Fairweather   Color keyboard co...
79
80
81
82
              if (left < 0) {
                  this.change(0);
                  return;
              }
e7893a68   梁灏   update ColorPicker
83
  
f2bcd4ad   Graham Fairweather   Color keyboard co...
84
85
86
87
88
              const {clientWidth} = this.$refs.container;
  
              if (left > clientWidth) {
                  this.change(100);
                  return;
e7893a68   梁灏   update ColorPicker
89
              }
f2bcd4ad   Graham Fairweather   Color keyboard co...
90
91
92
93
94
95
  
              this.change(left * 100 / clientWidth);
          },
      },
  };
  </script>