File size: 3,808 Bytes
89ce340
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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
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
140
141
142
143
144
145
146
147
148
149
<template>
  <div class="gradient-bar">
    <div class="bar" ref="barRef" :style="{ backgroundImage: gradientStyle }" @click="$event => addPoint($event)"></div>
    <div class="point" 
      :class="{ 'active': index === i }"
      v-for="(item, i) in points" 
      :key="item.pos + '-' + i" 
      :style="{
        backgroundColor: item.color,
        left: `calc(${item.pos}% - 5px)`,
      }"
      @mousedown.left="movePoint(i)"
      @click.right="removePoint(i)"
    ></div>
  </div>
</template>

<script lang="ts" setup>
import type { GradientColor } from '@/types/slides'
import { ref, computed, watchEffect } from 'vue'

const props = defineProps<{
  value: GradientColor[]
  index: number
}>()

const emit = defineEmits<{
  (event: 'update:value', payload: GradientColor[]): void
  (event: 'update:index', payload: number): void
}>()

const points = ref<GradientColor[]>([])

const barRef = ref<HTMLElement>()

watchEffect(() => {
  points.value = props.value
  if (props.index > props.value.length - 1) emit('update:index', 0)
})

const gradientStyle = computed(() => {
  const list = points.value.map(item => `${item.color} ${item.pos}%`)
  return `linear-gradient(to right, ${list.join(',')})`
})

const removePoint = (index: number) => {
  if (props.value.length <= 2) return

  let targetIndex = 0

  if (index === props.index) {
    targetIndex = (index - 1 < 0) ? 0 : index - 1
  }
  else if (props.index === props.value.length - 1) {
    targetIndex = props.value.length - 2
  }

  const values = props.value.filter((item, _index) => _index !== index)
  emit('update:index', targetIndex)
  emit('update:value', values)
}

const movePoint = (index: number) => {
  let isMouseDown = true

  document.onmousemove = e => {
    if (!isMouseDown) return
    if (!barRef.value) return

    let pos = Math.round((e.clientX - barRef.value.getBoundingClientRect().left) / barRef.value.clientWidth * 100)
    if (pos > 100) pos = 100
    if (pos < 0) pos = 0

    points.value = points.value.map((item, _index) => {
      if (_index === index) return { ...item, pos }
      return item
    })
  }
  document.onmouseup = () => {
    isMouseDown = false

    const point = points.value[index]
    const _points = [...points.value]
    _points.splice(index, 1)

    let targetIndex = 0
    for (let i = 0; i < _points.length; i++) {
      if (point.pos > _points[i].pos) targetIndex = i + 1
    }

    _points.splice(targetIndex, 0, point)

    emit('update:index', targetIndex)
    emit('update:value', _points)
    
    document.onmousemove = null
    document.onmouseup = null
  }
}

const addPoint = (e: MouseEvent) => {
  if (props.value.length >= 6) return
  if (!barRef.value) return
  const pos = Math.round((e.clientX - barRef.value.getBoundingClientRect().left) / barRef.value.clientWidth * 100)

  let targetIndex = 0
  for (let i = 0; i < props.value.length; i++) {
    if (pos > props.value[i].pos) targetIndex = i + 1
  }
  const color = props.value[targetIndex - 1] ? props.value[targetIndex - 1].color : props.value[targetIndex].color
  const values = [...props.value]
  values.splice(targetIndex, 0, { pos, color })
  emit('update:index', targetIndex)
  emit('update:value', values)
}
</script>

<style lang="scss" scoped>
.gradient-bar {
  width: calc(100% - 10px);
  height: 18px;
  padding: 1px 0;
  margin: 3px 0;
  position: relative;
  left: 5px;

  .bar {
    height: 16px;
    border: 1px solid #d9d9d9;
  }
  .point {
    width: 10px;
    height: 18px;
    background-color: #fff;
    position: absolute;
    top: 0;
    border: 2px solid #fff;
    outline: 1px solid #d9d9d9;
    box-shadow: 0 0 2px 2px #d9d9d9;
    border-radius: 1px;
    cursor: pointer;

    &.active {
      outline: 1px solid $themeColor;
      box-shadow: 0 0 2px 2px $themeColor;
    }
  }
}
</style>