Blame view

src/components/color-picker/alpha.vue 2.73 KB
e7893a68   梁灏   update ColorPicker
1
  <template>
f2bcd4ad   Graham Fairweather   Color keyboard co...
2
3
4
5
6
7
8
9
10
11
12
13
      <div
          :class="[prefixCls + '-alpha']"
          tabindex="0"
          @click="$el.focus()"
          @keydown.esc="handleEscape"
          @keydown.left="handleLeft"
          @keydown.right="handleRight"
          @keydown.up="handleUp"
          @keydown.down="handleDown"
      >
          <div :class="[prefixCls + '-alpha-checkboard-wrap']">
              <div :class="[prefixCls + '-alpha-checkerboard']"></div>
e7893a68   梁灏   update ColorPicker
14
          </div>
f2bcd4ad   Graham Fairweather   Color keyboard co...
15
16
17
18
19
20
21
22
23
24
25
26
27
          <div
              :style="gradientStyle"
              :class="[prefixCls + '-alpha-gradient']"></div>
          <div
              ref="container"
              :class="[prefixCls + '-alpha-container']"
              @mousedown="handleMouseDown"
              @touchmove="handleChange"
              @touchstart="handleChange">
              <div
                  :style="{top: 0, left: `${value.a * 100}%`}"
                  :class="[prefixCls + '-alpha-pointer']">
                  <div :class="[prefixCls + '-alpha-picker']"></div>
e7893a68   梁灏   update ColorPicker
28
29
30
31
              </div>
          </div>
      </div>
  </template>
f2bcd4ad   Graham Fairweather   Color keyboard co...
32
  
e7893a68   梁灏   update ColorPicker
33
  <script>
f2bcd4ad   Graham Fairweather   Color keyboard co...
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
  import HSAMixin from './hsaMixin';
  import Prefixes from './prefixMixin';
  import {clamp, toRGBAString} from './utils';
  
  export default {
      name: 'Alpha',
  
      mixins: [HSAMixin, Prefixes],
  
      data() {
          const normalStep = 1;
          const jumpStep = 10;
  
          return {
              left: -normalStep,
              right: normalStep,
              up: jumpStep,
              down: -jumpStep,
              powerKey: 'shiftKey',
          };
      },
  
      computed: {
          gradientStyle() {
              const {r, g, b} = this.value.rgba;
              const start = toRGBAString({r, g, b, a: 0});
              const finish = toRGBAString({r, g, b, a: 1});
  
              return {background: `linear-gradient(to right, ${start} 0%, ${finish} 100%)`};
e7893a68   梁灏   update ColorPicker
63
          },
f2bcd4ad   Graham Fairweather   Color keyboard co...
64
65
66
67
68
69
70
71
72
      },
  
      methods: {
          change(newAlpha) {
              const {h, s, l} = this.value.hsl;
              const {a} = this.value;
  
              if (a !== newAlpha) {
                  this.$emit('change', {h, s, l, a: newAlpha, source: 'rgba'});
e7893a68   梁灏   update ColorPicker
73
74
              }
          },
f2bcd4ad   Graham Fairweather   Color keyboard co...
75
76
77
          handleSlide(e, direction) {
              e.preventDefault();
              e.stopPropagation();
e7893a68   梁灏   update ColorPicker
78
  
f2bcd4ad   Graham Fairweather   Color keyboard co...
79
80
81
82
83
              this.change(clamp(e[this.powerKey] ? direction : Math.round(this.value.hsl.a * 100 + direction) / 100, 0, 1));
          },
          handleChange(e) {
              e.preventDefault();
              e.stopPropagation();
e7893a68   梁灏   update ColorPicker
84
  
f2bcd4ad   Graham Fairweather   Color keyboard co...
85
              const left = this.getLeft(e);
e7893a68   梁灏   update ColorPicker
86
  
f2bcd4ad   Graham Fairweather   Color keyboard co...
87
88
89
              if (left < 0) {
                  this.change(0);
                  return;
e7893a68   梁灏   update ColorPicker
90
              }
f2bcd4ad   Graham Fairweather   Color keyboard co...
91
92
93
94
95
96
97
98
99
100
101
102
103
  
              const {clientWidth} = this.$refs.container;
  
              if (left > clientWidth) {
                  this.change(1);
                  return;
              }
  
              this.change(Math.round(left * 100 / clientWidth) / 100);
          },
      },
  };
  </script>