Files
ReelShortWeb/src/views/systemManagement/composables/useTranscodeSettings.ts
T

72 lines
2.1 KiB
TypeScript

// Author: fruit.com.cn
import { computed, nextTick, onMounted, reactive, ref } from 'vue'
import type { FormInstance } from 'element-plus'
import type { MediaTranscodeProfile } from '@/api/admin/types'
import { useI18n } from '@/hooks/web/useI18n'
import { useRbac } from '@/stores/rbac'
import { defaultProfile, defaultRung } from './transcodeProfileDefaults'
import {
createEnabledRules,
createProfileRules,
createQualityRules,
positiveEvenRule,
positiveIntegerRule
} from './transcodeProfileRules'
import {
loadTranscodeProfile,
runTranscodeBackfill,
saveTranscodeProfile
} from './transcodeSettingsActions'
/** 转码设置页的响应式状态与动作装配。 */
export function useTranscodeSettings() {
const { t } = useI18n()
const { can } = useRbac()
const formRef = ref<FormInstance>()
const loading = ref(false)
const saving = ref(false)
const backfilling = ref(false)
const loadError = ref('')
const form = reactive<MediaTranscodeProfile>(defaultProfile())
const canManage = computed(() => can('episode:upload'))
const context = { form, formRef, loading, saving, backfilling, loadError, canManage, t }
const rules = computed(() => createProfileRules(t))
const enabledRules = computed(() => createEnabledRules(form, t))
const qualityRules = (index: number) => createQualityRules(form, index, t)
const addRung = (): void => {
form.ladder.push(defaultRung())
nextTick(() => formRef.value?.clearValidate())
}
const deleteRung = (index: number): void => {
form.ladder.splice(index, 1)
nextTick(() => formRef.value?.clearValidate())
}
const loadProfile = () => loadTranscodeProfile(context)
const saveProfile = () => saveTranscodeProfile(context)
onMounted(loadProfile)
return {
form,
formRef,
rules,
loading,
saving,
backfilling,
loadError,
canManage,
qualityRules,
positiveEvenRule: () => positiveEvenRule(t),
positiveIntegerRule: () => positiveIntegerRule(t),
enabledRules,
loadProfile,
addRung,
deleteRung,
saveProfile,
runBackfill: () => runTranscodeBackfill(context)
}
}