aboutsummaryrefslogtreecommitdiff
path: root/src/components/shadow_control/shadow_control.js
blob: 4521305ea954543e091e3b34e3ef7fabf6359cea (plain)
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
import ColorInput from 'src/components/color_input/color_input.vue'
import OpacityInput from 'src/components/opacity_input/opacity_input.vue'
import Select from 'src/components/select/select.vue'
import Checkbox from 'src/components/checkbox/checkbox.vue'
import Popover from 'src/components/popover/popover.vue'
import ComponentPreview from 'src/components/component_preview/component_preview.vue'
import { getCssShadow, getCssShadowFilter } from '../../services/theme_data/theme_data.service.js'
import { library } from '@fortawesome/fontawesome-svg-core'
import { throttle } from 'lodash'
import {
  faTimes,
  faChevronDown,
  faChevronUp,
  faPlus
} from '@fortawesome/free-solid-svg-icons'

library.add(
  faChevronDown,
  faChevronUp,
  faTimes,
  faPlus
)

const toModel = (object = {}) => ({
  x: 0,
  y: 0,
  blur: 0,
  spread: 0,
  inset: false,
  color: '#000000',
  alpha: 1,
  ...object
})

export default {
  props: [
    'modelValue', 'fallback', 'separateInset', 'noPreview', 'disabled'
  ],
  emits: ['update:modelValue', 'subShadowSelected'],
  data () {
    return {
      selectedId: 0,
      // TODO there are some bugs regarding display of array (it's not getting updated when deleting for some reason)
      cValue: (this.modelValue ?? this.fallback ?? []).map(toModel)
    }
  },
  components: {
    ColorInput,
    OpacityInput,
    Select,
    Checkbox,
    Popover,
    ComponentPreview
  },
  beforeUpdate () {
    this.cValue = (this.modelValue ?? this.fallback ?? []).map(toModel)
  },
  computed: {
    selected () {
      const selected = this.cValue[this.selectedId]
      if (selected) {
        return { ...selected }
      }
      return null
    },
    present () {
      return this.selected != null && !this.usingFallback
    },
    shadowsAreNull () {
      return this.modelValue == null
    },
    currentFallback () {
      return this.fallback?.[this.selectedId]
    },
    moveUpValid () {
      return this.selectedId > 0
    },
    moveDnValid () {
      return this.selectedId < this.cValue.length - 1
    },
    usingFallback () {
      return this.modelValue == null
    },
    style () {
      if (this.separateInset) {
        return {
          filter: getCssShadowFilter(this.cValue),
          boxShadow: getCssShadow(this.cValue, true)
        }
      }
      return {
        boxShadow: getCssShadow(this.cValue)
      }
    }
  },
  watch: {
    selected (value) {
      this.$emit('subShadowSelected', this.selectedId)
    }
  },
  methods: {
    updateProperty: throttle(function (prop, value) {
      this.cValue[this.selectedId][prop] = value
      if (prop === 'inset' && value === false && this.separateInset) {
        this.cValue[this.selectedId].spread = 0
      }
      this.$emit('update:modelValue', this.cValue)
    }, 100),
    add () {
      this.cValue.push(toModel(this.selected))
      this.selectedId = Math.max(this.cValue.length - 1, 0)
      this.$emit('update:modelValue', this.cValue)
    },
    del () {
      this.cValue.splice(this.selectedId, 1)
      this.selectedId = this.cValue.length === 0 ? undefined : Math.max(this.selectedId - 1, 0)
      this.$emit('update:modelValue', this.cValue)
    },
    moveUp () {
      const movable = this.cValue.splice(this.selectedId, 1)[0]
      this.cValue.splice(this.selectedId - 1, 0, movable)
      this.selectedId -= 1
      this.$emit('update:modelValue', this.cValue)
    },
    moveDn () {
      const movable = this.cValue.splice(this.selectedId, 1)[0]
      this.cValue.splice(this.selectedId + 1, 0, movable)
      this.selectedId += 1
      this.$emit('update:modelValue', this.cValue)
    }
  }
}