pleroma-fe/src/components/settings_modal/helpers/unit_setting.js

84 lines
1.7 KiB
JavaScript
Raw Normal View History

import Select from 'src/components/select/select.vue'
import Setting from './setting.js'
2026-01-06 16:22:52 +02:00
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,
2026-01-06 16:22:52 +02:00
Select,
},
props: {
...Setting.props,
min: Number,
units: {
type: Array,
2026-01-06 16:22:52 +02:00
default: () => allCssUnits,
},
unitSet: {
type: String,
2026-01-06 16:22:52 +02:00
default: 'none',
},
step: {
type: Number,
2026-01-06 16:22:52 +02:00
default: 1,
},
resetDefault: {
type: Object,
2026-01-06 16:22:52 +02:00
default: null,
},
},
computed: {
...Setting.computed,
2026-01-06 16:22:52 +02:00
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,
2026-01-06 16:22:52 +02:00
getUnitString(value) {
if (this.unitSet === 'none') return value
return this.$t(['settings', 'units', this.unitSet, value].join('.'))
},
2026-01-06 16:22:52 +02:00
updateValue(e) {
2026-08-04 19:55:44 +03:00
this.configSink(this.path, Number.parseFloat(e.target.value) + this.stateUnit)
},
2026-01-06 16:22:52 +02:00
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)
2026-01-06 16:22:52 +02:00
},
},
}