|
| 1 | +<template> |
| 2 | + <table class="comfy-modal-content comfy-table"> |
| 3 | + <tbody> |
| 4 | + <tr v-for="setting in sortedSettings" :key="setting.id"> |
| 5 | + <td> |
| 6 | + <span> |
| 7 | + {{ setting.name }} |
| 8 | + </span> |
| 9 | + <Chip |
| 10 | + v-if="setting.tooltip" |
| 11 | + icon="pi pi-info-circle" |
| 12 | + severity="secondary" |
| 13 | + v-tooltip="setting.tooltip" |
| 14 | + class="info-chip" |
| 15 | + /> |
| 16 | + </td> |
| 17 | + <td> |
| 18 | + <component |
| 19 | + :is="markRaw(getSettingComponent(setting))" |
| 20 | + :id="setting.id" |
| 21 | + :modelValue="settingStore.get(setting.id)" |
| 22 | + @update:modelValue="updateSetting(setting, $event)" |
| 23 | + v-bind="getSettingAttrs(setting)" |
| 24 | + /> |
| 25 | + </td> |
| 26 | + </tr> |
| 27 | + </tbody> |
| 28 | + </table> |
| 29 | +</template> |
| 30 | + |
| 31 | +<script setup lang="ts"> |
| 32 | +import { type Component, computed, markRaw } from 'vue' |
| 33 | +import InputText from 'primevue/inputtext' |
| 34 | +import InputNumber from 'primevue/inputnumber' |
| 35 | +import Select from 'primevue/select' |
| 36 | +import Chip from 'primevue/chip' |
| 37 | +import ToggleSwitch from 'primevue/toggleswitch' |
| 38 | +import { useSettingStore } from '@/stores/settingStore' |
| 39 | +import { SettingParams } from '@/types/settingTypes' |
| 40 | +import CustomSettingValue from '@/components/dialog/content/setting/CustomSettingValue.vue' |
| 41 | +import InputSlider from '@/components/dialog/content/setting/InputSlider.vue' |
| 42 | +
|
| 43 | +const settingStore = useSettingStore() |
| 44 | +const sortedSettings = computed<SettingParams[]>(() => { |
| 45 | + return Object.values(settingStore.settings) |
| 46 | + .filter((setting: SettingParams) => setting.type !== 'hidden') |
| 47 | + .sort((a, b) => a.name.localeCompare(b.name)) |
| 48 | +}) |
| 49 | +
|
| 50 | +function getSettingAttrs(setting: SettingParams) { |
| 51 | + const attrs = { ...(setting.attrs || {}) } |
| 52 | + const settingType = setting.type |
| 53 | + if (typeof settingType === 'function') { |
| 54 | + attrs['renderFunction'] = () => |
| 55 | + settingType( |
| 56 | + setting.name, |
| 57 | + (v) => updateSetting(setting, v), |
| 58 | + settingStore.get(setting.id), |
| 59 | + setting.attrs |
| 60 | + ) |
| 61 | + } |
| 62 | + switch (setting.type) { |
| 63 | + case 'combo': |
| 64 | + attrs['options'] = setting.options |
| 65 | + if (typeof setting.options[0] !== 'string') { |
| 66 | + attrs['optionLabel'] = 'text' |
| 67 | + attrs['optionValue'] = 'value' |
| 68 | + } |
| 69 | + break |
| 70 | + } |
| 71 | +
|
| 72 | + attrs['class'] += ' comfy-vue-setting-input' |
| 73 | + return attrs |
| 74 | +} |
| 75 | +
|
| 76 | +function getSettingComponent(setting: SettingParams): Component { |
| 77 | + if (typeof setting.type === 'function') { |
| 78 | + // return setting.type( |
| 79 | + // setting.name, (v) => updateSetting(setting, v), settingStore.get(setting.id), setting.attrs) |
| 80 | + return CustomSettingValue |
| 81 | + } |
| 82 | + switch (setting.type) { |
| 83 | + case 'boolean': |
| 84 | + return ToggleSwitch |
| 85 | + case 'number': |
| 86 | + return InputNumber |
| 87 | + case 'slider': |
| 88 | + return InputSlider |
| 89 | + case 'combo': |
| 90 | + return Select |
| 91 | + default: |
| 92 | + return InputText |
| 93 | + } |
| 94 | +} |
| 95 | +
|
| 96 | +const updateSetting = (setting: SettingParams, value: any) => { |
| 97 | + if (setting.onChange) setting.onChange(value, settingStore.get(setting.id)) |
| 98 | +
|
| 99 | + settingStore.set(setting.id, value) |
| 100 | +} |
| 101 | +</script> |
| 102 | + |
| 103 | +<style> |
| 104 | +.info-chip { |
| 105 | + background: transparent !important; |
| 106 | +} |
| 107 | +.comfy-vue-setting-input { |
| 108 | + width: 100%; |
| 109 | +} |
| 110 | +</style> |
| 111 | + |
| 112 | +<style scoped> |
| 113 | +.comfy-table { |
| 114 | + width: 100%; |
| 115 | +} |
| 116 | +</style> |
0 commit comments