Blame view

src/components/color-picker/saturation.vue 3.05 KB
e7893a68   梁灏   update ColorPicker
1
  <template>
f2bcd4ad   Graham Fairweather   Color keyboard co...
2
3
4
5
6
7
8
9
10
11
      <div
          :class="[prefixCls + '-saturation-wrapper']"
          tabindex="0"
          @keydown.esc="handleEscape"
          @click="$el.focus()"
          @keydown.left="handleLeft"
          @keydown.right="handleRight"
          @keydown.up="handleUp"
          @keydown.down="handleDown"
      >
e7893a68   梁灏   update ColorPicker
12
          <div
e7893a68   梁灏   update ColorPicker
13
              ref="container"
f2bcd4ad   Graham Fairweather   Color keyboard co...
14
15
              :style="bgColorStyle"
              :class="[prefixCls + '-saturation']"
e7893a68   梁灏   update ColorPicker
16
              @mousedown="handleMouseDown">
f2bcd4ad   Graham Fairweather   Color keyboard co...
17
18
19
20
21
22
              <div :class="[prefixCls + '-saturation--white']"></div>
              <div :class="[prefixCls + '-saturation--black']"></div>
              <div
                  :style="pointerStyle"
                  :class="[prefixCls + '-saturation-pointer']">
                  <div :class="[prefixCls + '-saturation-circle']"></div>
e7893a68   梁灏   update ColorPicker
23
24
25
26
              </div>
          </div>
      </div>
  </template>
f2bcd4ad   Graham Fairweather   Color keyboard co...
27
  
e7893a68   梁灏   update ColorPicker
28
  <script>
f2bcd4ad   Graham Fairweather   Color keyboard co...
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
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
  import HSAMixin from './hsaMixin';
  import Prefixes from './prefixMixin';
  import {clamp, getIncrement} from './utils';
  
  export default {
      name: 'Saturation',
  
      mixins: [HSAMixin, Prefixes],
  
      data() {
          const normalStep = 0.01;
  
          return {
              left: -normalStep,
              right: normalStep,
              up: normalStep,
              down: -normalStep,
              multiplier: 10,
              powerKey: 'shiftKey',
          };
      },
  
      computed: {
          bgColorStyle() {
              return {background: `hsl(${this.value.hsv.h}, 100%, 50%)`};
          },
          pointerStyle() {
              return {top: `${-(this.value.hsv.v * 100) + 1 + 100}%`, left: `${this.value.hsv.s * 100}%`};
          },
      },
  
      methods: {
          change(h, s, v, a) {
              this.$emit('change', {h, s, v, a, source: 'hsva'});
          },
          handleSlide(e, direction, key) {
              e.preventDefault();
              e.stopPropagation();
  
              const isPowerKey = e[this.powerKey];
              const increment = isPowerKey ? direction * this.multiplier : direction;
              const {h, s, v, a} = this.value.hsv;
              const saturation = clamp(s + getIncrement(key, ['left', 'right'], increment), 0, 1);
              const bright = clamp(v + getIncrement(key, ['up', 'down'], increment), 0, 1);
  
              this.change(h, saturation, bright, a);
          },
          handleChange(e) {
              e.preventDefault();
              e.stopPropagation();
  
              const {clientWidth, clientHeight} = this.$refs.container;
              const left = clamp(this.getLeft(e), 0, clientWidth);
              const top = clamp(this.getTop(e), 0, clientHeight);
              const saturation = left / clientWidth;
              const bright = clamp(1 - top / clientHeight, 0, 1);
e7893a68   梁灏   update ColorPicker
85
  
f2bcd4ad   Graham Fairweather   Color keyboard co...
86
              this.change(this.value.hsv.h, saturation, bright, this.value.hsv.a);
e7893a68   梁灏   update ColorPicker
87
          },
f2bcd4ad   Graham Fairweather   Color keyboard co...
88
89
90
          handleMouseDown(e) {
              HSAMixin.methods.handleMouseDown.call(this, e);
              window.addEventListener('mouseup', this.handleChange, false);
e7893a68   梁灏   update ColorPicker
91
          },
f2bcd4ad   Graham Fairweather   Color keyboard co...
92
93
94
          unbindEventListeners(e) {
              HSAMixin.methods.unbindEventListeners.call(this, e);
              window.removeEventListener('mouseup', this.handleChange);
e7893a68   梁灏   update ColorPicker
95
          },
f2bcd4ad   Graham Fairweather   Color keyboard co...
96
97
98
      },
  };
  </script>