pleroma-fe/src/components/font_control/font_control.js

120 lines
2.8 KiB
JavaScript
Raw Normal View History

import { set, clone } from 'lodash'
import Select from '../select/select.vue'
2024-06-26 14:17:22 +03:00
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,
faKeyboard,
faFont
} from '@fortawesome/free-solid-svg-icons'
2024-06-26 14:17:22 +03:00
library.add(
faExclamationTriangle,
faKeyboard,
faFont
2024-06-26 14:17:22 +03:00
)
const PRESET_FONTS = new Set(['serif', 'sans-serif', 'monospace', 'inherit'])
2018-12-11 00:56:15 +03:00
export default {
components: {
2024-06-26 14:17:22 +03:00
Select,
Checkbox,
Popover
},
2018-12-11 00:56:15 +03:00
props: [
2022-03-27 14:16:23 +03:00
'name', 'label', 'modelValue', 'fallback', 'options', 'no-inherit'
2018-12-11 00:56:15 +03:00
],
2022-03-27 14:16:23 +03:00
emits: ['update:modelValue'],
2018-12-11 00:56:15 +03:00
data () {
return {
manualEntry: true,
localValue: clone(this.modelValue),
familyCustomLocal: null,
2018-12-11 00:56:15 +03:00
availableOptions: [
this.noInherit ? '' : 'inherit',
'serif',
'sans-serif',
2018-12-11 00:56:15 +03:00
'monospace',
'local',
...(this.options || [])
2018-12-11 00:56:15 +03:00
].filter(_ => _)
}
},
beforeUpdate () {
this.localValue = clone(this.modelValue)
if (this.familyCustomLocal === null && !this.isInvalidFamily(this.modelValue?.family)) {
this.familyCustomLocal = this.modelValue?.family
}
},
methods: {
lookupLocalFonts () {
if (!this.fontsList) {
this.$store.dispatch('queryLocalFonts')
}
this.toggleManualEntry()
},
isInvalidFamily (value) {
return PRESET_FONTS.has(value) || (value === '')
},
toggleManualEntry () {
this.manualEntry = !this.manualEntry
}
2018-12-11 00:56:15 +03:00
},
computed: {
present () {
2024-06-26 14:17:22 +03:00
return typeof this.localValue !== 'undefined'
2018-12-11 00:56:15 +03:00
},
2024-06-26 14:17:22 +03:00
defaultValue () {
return this.localValue || this.fallback || {}
2018-12-11 00:56:15 +03:00
},
fontsListCapable () {
return this.$store.state.interface.browserSupport.localFonts
},
fontsList () {
return this.$store.state.interface.localFonts
},
2018-12-11 00:56:15 +03:00
family: {
get () {
2024-06-26 14:17:22 +03:00
return this.defaultValue.family
},
set (v) {
this.familyCustomLocal = ''
2024-06-26 14:17:22 +03:00
set(this.localValue, 'family', v)
this.$emit('update:modelValue', this.localValue)
}
},
familyCustom: {
get () {
return this.familyCustomLocal
2018-12-11 00:56:15 +03:00
},
set (v) {
this.familyCustomLocal = v
if (!this.isInvalidFamily(v)) {
2024-06-26 14:17:22 +03:00
set(this.localValue, 'family', v)
this.$emit('update:modelValue', this.localValue)
2024-06-26 14:17:22 +03:00
}
2018-12-11 00:56:15 +03:00
}
},
2024-06-26 14:17:22 +03:00
invalidCustom () {
return this.isInvalidFamily(this.familyCustomLocal)
2024-06-26 14:17:22 +03:00
},
2018-12-11 00:56:15 +03:00
isCustom () {
return !PRESET_FONTS.has(this.defaultValue.family)
2018-12-11 00:56:15 +03:00
},
preset: {
get () {
2024-06-26 14:17:22 +03:00
if (PRESET_FONTS.has(this.family)) {
2018-12-11 00:56:15 +03:00
return this.family
} else {
2024-06-26 14:17:22 +03:00
return 'local'
2018-12-11 00:56:15 +03:00
}
},
set (v) {
2024-06-26 14:17:22 +03:00
this.family = v === 'local' ? '' : v
2018-12-11 00:56:15 +03:00
}
}
}
}