aboutsummaryrefslogtreecommitdiff
path: root/src/components/settings_modal/helpers/unit_setting.js
blob: daeddd81321e41dda8c8a474f07768f96c68cc66 (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
import Select from 'src/components/select/select.vue'
import Setting from './setting.js'

export const allCssUnits = ['cm', 'mm', 'in', 'px', 'pt', 'pc', 'em', 'ex', 'ch', 'rem', 'vw', 'vh', 'vmin', 'vmax', '%']
export const defaultHorizontalUnits = ['px', 'rem', 'vw']
export const defaultVerticalUnits = ['px', 'rem', 'vh']

export default {
  ...Setting,
  components: {
    ...Setting.components,
    Select
  },
  props: {
    ...Setting.props,
    min: Number,
    units: {
      type: Array,
      default: () => allCssUnits
    },
    unitSet: {
      type: String,
      default: 'none'
    },
    step: {
      type: Number,
      default: 1
    },
    resetDefault: {
      type: Object,
      default: null
    }
  },
  computed: {
    ...Setting.computed,
    stateUnit () {
      return typeof this.state === 'string' ? this.state.replace(/[0-9,.]+/, '') : ''
    },
    stateValue () {
      return typeof this.state === 'string' ? this.state.replace(/[^0-9,.]+/, '') : ''
    }
  },
  methods: {
    ...Setting.methods,
    getUnitString (value) {
      if (this.unitSet === 'none') return value
      return this.$t(['settings', 'units', this.unitSet, value].join('.'))
    },
    updateValue (e) {
      this.configSink(this.path, parseFloat(e.target.value) + this.stateUnit)
    },
    updateUnit (e) {
      let value = this.stateValue
      const newUnit = e.target.value
      if (this.resetDefault) {
        const replaceValue = this.resetDefault[newUnit]
        if (replaceValue != null) {
          value = replaceValue
        }
      }
      this.configSink(this.path, value + newUnit)
    }
  }
}