aboutsummaryrefslogtreecommitdiff
path: root/src/components/font_control/font_control.js
blob: 52c3e70a6e725a101c9b348b7f09c31d5e35f6bb (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
import { set } from 'lodash'
import Select from '../select/select.vue'
import Checkbox from 'src/components/checkbox/checkbox.vue'
import Popover from 'src/components/popover/popover.vue'

import { library } from '@fortawesome/fontawesome-svg-core'
import { faExclamationTriangle } from '@fortawesome/free-solid-svg-icons'

library.add(
  faExclamationTriangle
)

const PRESET_FONTS = new Set(['serif', 'sans-serif', 'monospace', 'inherit'])

export default {
  components: {
    Select,
    Checkbox,
    Popover
  },
  props: [
    'name', 'label', 'modelValue', 'fallback', 'options', 'no-inherit'
  ],
  emits: ['update:modelValue'],
  data () {
    return {
      localValue: this.modelValue,
      customFamily: '',
      availableOptions: [
        this.noInherit ? '' : 'inherit',
        'local',
        ...(this.options || []),
        'serif',
        'monospace',
        'sans-serif'
      ].filter(_ => _)
    }
  },
  beforeUpdate () {
    this.localValue = this.modelValue
  },
  computed: {
    present () {
      return typeof this.localValue !== 'undefined'
    },
    defaultValue () {
      return this.localValue || this.fallback || {}
    },
    family: {
      get () {
        return this.defaultValue.family
      },
      set (v) {
        set(this.localValue, 'family', v)
        this.$emit('update:modelValue', this.localValue)
      }
    },
    familyCustom: {
      get () {
        return this.customFamily
      },
      set (v) {
        this.customFamily = v
        if (!PRESET_FONTS.has(this.customFamily)) {
          set(this.localValue, 'family', v)
          this.$emit('update:modelValue', this.customFamily)
        }
      }
    },
    invalidCustom () {
      return PRESET_FONTS.has(this.customFamily)
    },
    isCustom () {
      return !PRESET_FONTS.has(this.family)
    },
    preset: {
      get () {
        if (PRESET_FONTS.has(this.family)) {
          return this.family
        } else {
          return 'local'
        }
      },
      set (v) {
        this.family = v === 'local' ? '' : v
      }
    }
  }
}