Commit c2ad5966f4050e4df70854fa906d180ea6678bb6
Merge branch 'feat/support-enum' into 'main_dev'
perf(src/packages): 自定义文本,支持枚举值显示name See merge request yunteng/thingskit-view!253
Showing
6 changed files
with
378 additions
and
0 deletions
src/hooks/external/useTsl.ts
0 → 100644
| 1 | +/** | |
| 2 | + * 此hook用于获取物模型 | |
| 3 | + * @param entityId 设备id | |
| 4 | + * @returns | |
| 5 | + */ | |
| 6 | + | |
| 7 | +import { getAttribute, getDeviceDetail } from '@/api/external/common' | |
| 8 | + | |
| 9 | +export function useTsl() { | |
| 10 | + const handleDeviceProfileAttributes = async (entityId: string) => { | |
| 11 | + const deviceDetailRes = await getDeviceDetail(entityId) | |
| 12 | + const { deviceProfileId } = deviceDetailRes | |
| 13 | + if (!deviceProfileId) return | |
| 14 | + const attributeRes = await getAttribute(deviceProfileId) | |
| 15 | + const dataFormat = handleDataFormat(deviceDetailRes, attributeRes) | |
| 16 | + return dataFormat | |
| 17 | + } | |
| 18 | + | |
| 19 | + const handleDataFormat = (deviceDetail: Recordable, attributes: Recordable[]) => { | |
| 20 | + const { name, tbDeviceId } = deviceDetail | |
| 21 | + const attribute = attributes.map((item: Recordable) => ({ | |
| 22 | + identifier: item.identifier, | |
| 23 | + name: item.name, | |
| 24 | + detail: item.detail | |
| 25 | + })) | |
| 26 | + return { | |
| 27 | + name, | |
| 28 | + tbDeviceId, | |
| 29 | + attribute | |
| 30 | + } | |
| 31 | + } | |
| 32 | + | |
| 33 | + return { | |
| 34 | + handleDeviceProfileAttributes | |
| 35 | + } | |
| 36 | +} | ... | ... |
| 1 | +import { PublicConfigClass } from '@/packages/public' | |
| 2 | +import { CreateComponentType } from '@/packages/index.d' | |
| 3 | +import { OverrideTextEnumCommonConfig } from './index' | |
| 4 | +import cloneDeep from 'lodash/cloneDeep' | |
| 5 | +import { chartInitConfig } from '@/settings/designSetting' | |
| 6 | + | |
| 7 | +export enum WritingModeEnum { | |
| 8 | + HORIZONTAL = '水平', | |
| 9 | + VERTICAL = '垂直' | |
| 10 | +} | |
| 11 | + | |
| 12 | +export const WritingModeObject = { | |
| 13 | + [WritingModeEnum.HORIZONTAL]: 'horizontal-tb', | |
| 14 | + [WritingModeEnum.VERTICAL]: 'vertical-rl' | |
| 15 | +} | |
| 16 | + | |
| 17 | +export enum FontWeightEnum { | |
| 18 | + NORMAL = '常规', | |
| 19 | + BOLD = '加粗' | |
| 20 | +} | |
| 21 | + | |
| 22 | +export const FontWeightObject = { | |
| 23 | + [FontWeightEnum.NORMAL]: 'normal', | |
| 24 | + [FontWeightEnum.BOLD]: 'bold' | |
| 25 | +} | |
| 26 | + | |
| 27 | +export const option = { | |
| 28 | + link: '', | |
| 29 | + linkHead: 'http://', | |
| 30 | + dataset: '我是文本', | |
| 31 | + fontSize: 20, | |
| 32 | + fontColor: '#ffffff', | |
| 33 | + paddingX: 10, | |
| 34 | + paddingY: 10, | |
| 35 | + textAlign: 'center', // 水平对齐方式 | |
| 36 | + fontWeight: 'normal', | |
| 37 | + | |
| 38 | + // 边框 | |
| 39 | + borderWidth: 0, | |
| 40 | + borderColor: '#ffffff', | |
| 41 | + borderRadius: 5, | |
| 42 | + | |
| 43 | + // 字间距 | |
| 44 | + letterSpacing: 5, | |
| 45 | + writingMode: 'horizontal-tb', | |
| 46 | + backgroundColor: '#00000000', | |
| 47 | + | |
| 48 | + //跳转方式 | |
| 49 | + linkMethod: 'default' | |
| 50 | +} | |
| 51 | + | |
| 52 | +export default class Config extends PublicConfigClass implements CreateComponentType { | |
| 53 | + public key = OverrideTextEnumCommonConfig.key | |
| 54 | + public attr = { ...chartInitConfig, w: 150, h: 40, zIndex: -1 } | |
| 55 | + public chartConfig = cloneDeep(OverrideTextEnumCommonConfig) | |
| 56 | + public option = cloneDeep(option) | |
| 57 | +} | ... | ... |
| 1 | +<template> | |
| 2 | + <collapse-item name="信息" :expanded="true"> | |
| 3 | + <setting-item-box name="文字" :alone="true"> | |
| 4 | + <setting-item> | |
| 5 | + <n-input v-model:value="optionData.dataset" type="textarea" size="small"></n-input> | |
| 6 | + </setting-item> | |
| 7 | + </setting-item-box> | |
| 8 | + <setting-item-box name="链接" :alone="true"> | |
| 9 | + <setting-item> | |
| 10 | + <n-input-group> | |
| 11 | + <n-select | |
| 12 | + v-model:value="optionData.linkHead" | |
| 13 | + size="small" | |
| 14 | + :style="{ width: '80%' }" | |
| 15 | + :options="linkHeadOptions" | |
| 16 | + /> | |
| 17 | + <n-input v-model:value="optionData.link" size="small"></n-input> | |
| 18 | + </n-input-group> | |
| 19 | + </setting-item> | |
| 20 | + </setting-item-box> | |
| 21 | + <setting-item-box name="跳转方式" :alone="true"> | |
| 22 | + <setting-item> | |
| 23 | + <n-input-group> | |
| 24 | + <n-select | |
| 25 | + v-model:value="optionData.linkMethod" | |
| 26 | + size="small" | |
| 27 | + :style="{ width: '80%' }" | |
| 28 | + :options="linkMethodOptions" | |
| 29 | + /> | |
| 30 | + <n-button | |
| 31 | + :disabled="!optionData.linkMethod" | |
| 32 | + secondary | |
| 33 | + size="small" | |
| 34 | + @click="handleLinkClick(optionData.linkMethod)" | |
| 35 | + >跳转</n-button | |
| 36 | + > | |
| 37 | + </n-input-group> | |
| 38 | + </setting-item> | |
| 39 | + </setting-item-box> | |
| 40 | + </collapse-item> | |
| 41 | + | |
| 42 | + <collapse-item name="样式" :expanded="true"> | |
| 43 | + <setting-item-box name="文字"> | |
| 44 | + <setting-item name="颜色"> | |
| 45 | + <n-color-picker size="small" :modes="['hex']" v-model:value="optionData.fontColor"></n-color-picker> | |
| 46 | + </setting-item> | |
| 47 | + <setting-item name="字体大小"> | |
| 48 | + <n-input-number v-model:value="optionData.fontSize" size="small" placeholder="字体大小"></n-input-number> | |
| 49 | + </setting-item> | |
| 50 | + <setting-item name="字体粗细"> | |
| 51 | + <n-select v-model:value="optionData.fontWeight" size="small" :options="fontWeightOptions" /> | |
| 52 | + </setting-item> | |
| 53 | + <setting-item name="X轴内边距"> | |
| 54 | + <n-input-number v-model:value="optionData.paddingX" size="small" placeholder="输入内边距"></n-input-number> | |
| 55 | + </setting-item> | |
| 56 | + <setting-item name="Y轴内边距"> | |
| 57 | + <n-input-number v-model:value="optionData.paddingY" size="small" placeholder="输入内边距"></n-input-number> | |
| 58 | + </setting-item> | |
| 59 | + | |
| 60 | + <setting-item name="水平对齐"> | |
| 61 | + <n-select v-model:value="optionData.textAlign" size="small" :options="textAlignOptions" /> | |
| 62 | + </setting-item> | |
| 63 | + <setting-item name="文本方向"> | |
| 64 | + <n-select v-model:value="optionData.writingMode" size="small" :options="verticalOptions" /> | |
| 65 | + </setting-item> | |
| 66 | + | |
| 67 | + <setting-item name="字间距"> | |
| 68 | + <n-input-number v-model:value="optionData.letterSpacing" size="small" placeholder="输入字间距"></n-input-number> | |
| 69 | + </setting-item> | |
| 70 | + </setting-item-box> | |
| 71 | + | |
| 72 | + <setting-item-box name="边框"> | |
| 73 | + <setting-item name="宽度"> | |
| 74 | + <n-input-number | |
| 75 | + v-model:value="optionData.borderWidth" | |
| 76 | + size="small" | |
| 77 | + :min="0" | |
| 78 | + placeholder="宽度" | |
| 79 | + ></n-input-number> | |
| 80 | + </setting-item> | |
| 81 | + <setting-item name="颜色"> | |
| 82 | + <n-color-picker size="small" :modes="['hex']" v-model:value="optionData.borderColor"></n-color-picker> | |
| 83 | + </setting-item> | |
| 84 | + <setting-item name="圆角"> | |
| 85 | + <n-input-number | |
| 86 | + v-model:value="optionData.borderRadius" | |
| 87 | + size="small" | |
| 88 | + :min="0" | |
| 89 | + placeholder="圆角" | |
| 90 | + ></n-input-number> | |
| 91 | + </setting-item> | |
| 92 | + </setting-item-box> | |
| 93 | + | |
| 94 | + <setting-item-box name="背景" :alone="true"> | |
| 95 | + <setting-item name="背景颜色"> | |
| 96 | + <n-color-picker size="small" :modes="['hex']" v-model:value="optionData.backgroundColor"></n-color-picker> | |
| 97 | + </setting-item> | |
| 98 | + </setting-item-box> | |
| 99 | + </collapse-item> | |
| 100 | +</template> | |
| 101 | + | |
| 102 | +<script setup lang="ts"> | |
| 103 | +import { PropType } from 'vue' | |
| 104 | +import { option, WritingModeEnum, WritingModeObject, FontWeightEnum, FontWeightObject } from './config' | |
| 105 | +import { CollapseItem, SettingItemBox, SettingItem } from '@/components/Pages/ChartItemSetting' | |
| 106 | +const props = defineProps({ | |
| 107 | + optionData: { | |
| 108 | + type: Object as PropType<typeof option>, | |
| 109 | + required: true | |
| 110 | + } | |
| 111 | +}) | |
| 112 | + | |
| 113 | +const textAlignOptions = [ | |
| 114 | + { label: '左对齐', value: 'start' }, | |
| 115 | + { label: '居中', value: 'center' }, | |
| 116 | + { label: '右对齐', value: 'end' } | |
| 117 | +] | |
| 118 | + | |
| 119 | +const linkMethodOptions = [ | |
| 120 | + { label: '默认', value: 'default' }, | |
| 121 | + { label: '新开窗口', value: 'open' } | |
| 122 | +] | |
| 123 | + | |
| 124 | +const verticalOptions = [ | |
| 125 | + { | |
| 126 | + label: WritingModeEnum.HORIZONTAL, | |
| 127 | + value: WritingModeObject[WritingModeEnum.HORIZONTAL] | |
| 128 | + }, | |
| 129 | + { | |
| 130 | + label: WritingModeEnum.VERTICAL, | |
| 131 | + value: WritingModeObject[WritingModeEnum.VERTICAL] | |
| 132 | + } | |
| 133 | +] | |
| 134 | +const fontWeightOptions = [ | |
| 135 | + { | |
| 136 | + label: FontWeightEnum.NORMAL, | |
| 137 | + value: FontWeightObject[FontWeightEnum.NORMAL] | |
| 138 | + }, | |
| 139 | + { | |
| 140 | + label: FontWeightEnum.BOLD, | |
| 141 | + value: FontWeightObject[FontWeightEnum.BOLD] | |
| 142 | + } | |
| 143 | +] | |
| 144 | +const handleLinkClick = (target: string) => { | |
| 145 | + const hrefStr = props.optionData.linkHead + props.optionData.link | |
| 146 | + if (target === 'open') window.open(hrefStr) | |
| 147 | + else window.location.href = hrefStr | |
| 148 | +} | |
| 149 | +const linkHeadOptions = [ | |
| 150 | + { label: 'http://', value: 'http://' }, | |
| 151 | + { label: 'https://', value: 'https://' } | |
| 152 | +] | |
| 153 | +</script> | ... | ... |
| 1 | +import { ConfigType, PackagesCategoryEnum, ChartFrameEnum } from '@/packages/index.d' | |
| 2 | +import { ChatCategoryEnum, ChatCategoryEnumName } from '@/packages/components/Informations/index.d' | |
| 3 | +import { useWidgetKey } from '@/packages/external/useWidgetKey' | |
| 4 | + | |
| 5 | +const { key, conKey, chartKey } = useWidgetKey('OverrideTextEnumCommon', true) | |
| 6 | + | |
| 7 | +export const OverrideTextEnumCommonConfig: ConfigType = { | |
| 8 | + key, | |
| 9 | + chartKey, | |
| 10 | + conKey, | |
| 11 | + title: '自定义文字(支持枚举)', | |
| 12 | + category: ChatCategoryEnum.TEXT, | |
| 13 | + categoryName: ChatCategoryEnumName.TEXT, | |
| 14 | + package: PackagesCategoryEnum.INFORMATIONS, | |
| 15 | + chartFrame: ChartFrameEnum.COMMON, | |
| 16 | + image: 'text_static.png' | |
| 17 | +} | ... | ... |
| 1 | +<template> | |
| 2 | + <div class="go-text-box"> | |
| 3 | + <div class="content"> | |
| 4 | + <span style="cursor: pointer; white-space: pre-wrap" v-if="link" @click="click">{{ option.dataset }}</span> | |
| 5 | + <span style="white-space: pre-wrap" v-else>{{ option.dataset }}</span> | |
| 6 | + </div> | |
| 7 | + </div> | |
| 8 | +</template> | |
| 9 | + | |
| 10 | +<script setup lang="ts"> | |
| 11 | +import { PropType, toRefs, shallowReactive } from 'vue' | |
| 12 | +import { CreateComponentType } from '@/packages/index.d' | |
| 13 | +import { useChartDataFetch } from '@/hooks' | |
| 14 | +import { useTsl } from '@/hooks/external/useTsl' | |
| 15 | +import { useChartEditStore } from '@/store/modules/chartEditStore/chartEditStore' | |
| 16 | +import { option as configOption } from './config' | |
| 17 | + | |
| 18 | +const props = defineProps({ | |
| 19 | + chartConfig: { | |
| 20 | + type: Object as PropType<CreateComponentType & typeof option>, | |
| 21 | + required: true | |
| 22 | + } | |
| 23 | +}) | |
| 24 | + | |
| 25 | +const { handleDeviceProfileAttributes } = useTsl() | |
| 26 | + | |
| 27 | +const { | |
| 28 | + linkHead, | |
| 29 | + link, | |
| 30 | + fontColor, | |
| 31 | + fontSize, | |
| 32 | + letterSpacing, | |
| 33 | + paddingY, | |
| 34 | + paddingX, | |
| 35 | + textAlign, | |
| 36 | + borderWidth, | |
| 37 | + borderColor, | |
| 38 | + borderRadius, | |
| 39 | + writingMode, | |
| 40 | + backgroundColor, | |
| 41 | + fontWeight, | |
| 42 | + linkMethod | |
| 43 | +} = toRefs(props.chartConfig.option) | |
| 44 | + | |
| 45 | +const option = shallowReactive({ | |
| 46 | + dataset: configOption.dataset | |
| 47 | +}) | |
| 48 | + | |
| 49 | +//动态请求物模型 | |
| 50 | +const fetchTsl = (entityId: string) => { | |
| 51 | + return handleDeviceProfileAttributes(entityId) | |
| 52 | +} | |
| 53 | + | |
| 54 | +//解构设备和属性参数 | |
| 55 | +const deconstructionParams = (chartConfig: CreateComponentType) => { | |
| 56 | + const { request } = chartConfig | |
| 57 | + const { requestParams } = request | |
| 58 | + const { Params } = requestParams | |
| 59 | + const { entityId, keys } = Params | |
| 60 | + return { entityId, keys } | |
| 61 | +} | |
| 62 | + | |
| 63 | +//找到枚举值name,属性暂时只选择一个 | |
| 64 | +const findEnumName = (attribute: Recordable[], keys: string[], newData: Recordable) => { | |
| 65 | + const findCurrentAttr = attribute.find(findAttrItem => findAttrItem.identifier === keys.at(0))?.detail?.dataType | |
| 66 | + ?.specsList | |
| 67 | + return findCurrentAttr.find( | |
| 68 | + (findCurrentItem: Recordable) => findCurrentItem.value === Number(newData.data[keys.at(0)!][0][1]) | |
| 69 | + )?.name | |
| 70 | +} | |
| 71 | + | |
| 72 | +// 预览更新 | |
| 73 | +useChartDataFetch(props.chartConfig, useChartEditStore, async (newData: string) => { | |
| 74 | + const { entityId, keys } = deconstructionParams(props.chartConfig) as Recordable | |
| 75 | + const { attribute } = (await fetchTsl(entityId)) as unknown as Recordable | |
| 76 | + if (Object.prototype.toString.call(newData) !== '[object Object]') { | |
| 77 | + option.dataset = newData | |
| 78 | + } else if ('subscriptionId' in (newData as unknown as Recordable)) { | |
| 79 | + // ws | |
| 80 | + option.dataset = findEnumName(attribute, keys, newData as unknown as Recordable) || '暂无数据' | |
| 81 | + } | |
| 82 | +}) | |
| 83 | + | |
| 84 | +//打开链接 | |
| 85 | +const click = () => { | |
| 86 | + const hrefStr = linkHead.value + link.value | |
| 87 | + if (linkMethod.value === 'open') window.open(hrefStr) | |
| 88 | + else window.location.href = hrefStr | |
| 89 | +} | |
| 90 | +</script> | |
| 91 | + | |
| 92 | +<style lang="scss" scoped> | |
| 93 | +@include go('text-box') { | |
| 94 | + display: flex; | |
| 95 | + align-items: center; | |
| 96 | + justify-content: v-bind('textAlign'); | |
| 97 | + | |
| 98 | + .content { | |
| 99 | + color: v-bind('fontColor'); | |
| 100 | + padding: v-bind('`${paddingY}px ${paddingX}px`'); | |
| 101 | + font-size: v-bind('fontSize + "px"'); | |
| 102 | + letter-spacing: v-bind('letterSpacing + "px"'); | |
| 103 | + writing-mode: v-bind('writingMode'); | |
| 104 | + font-weight: v-bind('fontWeight'); | |
| 105 | + border-style: solid; | |
| 106 | + border-width: v-bind('borderWidth + "px"'); | |
| 107 | + border-radius: v-bind('borderRadius + "px"'); | |
| 108 | + border-color: v-bind('borderColor'); | |
| 109 | + | |
| 110 | + background-color: v-bind('backgroundColor'); | |
| 111 | + } | |
| 112 | +} | |
| 113 | +</style> | ... | ... |
| ... | ... | @@ -9,6 +9,7 @@ import { OverrideInputsDateConfig } from '@/packages/components/external/Informa |
| 9 | 9 | import { OverrideInputsTabConfig } from '@/packages/components/external/Informations/Inputs/OverrideInputsTab' |
| 10 | 10 | import { OverrideIframeConfig } from '@/packages/components/external/Informations/Mores/OverrideIframe' |
| 11 | 11 | import { OverrideTextCommonConfig } from '@/packages/components/external/Informations/Texts/OverrideTextCommon' |
| 12 | +import { OverrideTextEnumCommonConfig } from '@/packages/components/external/Informations/Texts/OverrideTextEnumCommon' | |
| 12 | 13 | import { OverrideTextBarrageConfig } from '@/packages/components/external/Informations/Texts/OverrideTextBarrage' |
| 13 | 14 | import { OverrideTextGradientConfig } from '@/packages/components/external/Informations/Texts/OverrideTextGradient' |
| 14 | 15 | import { OverrideVideoConfig } from '@/packages/components/external/Informations/Mores/OverrideVideo' |
| ... | ... | @@ -136,6 +137,7 @@ export function useInjectLib(packagesList: EPackagesType) { |
| 136 | 137 | addWidgetToCategoryByCategoryName(packagesList, PackagesCategoryEnum.INFORMATIONS, OverrideInputsDateConfig)//重写信息下的日期 |
| 137 | 138 | addWidgetToCategoryByCategoryName(packagesList, PackagesCategoryEnum.INFORMATIONS, OverrideInputsTabConfig)//重写信息下的tab |
| 138 | 139 | addWidgetToCategoryByCategoryName(packagesList, PackagesCategoryEnum.INFORMATIONS, OverrideTextCommonConfig)//重写信息下的文字 |
| 140 | + addWidgetToCategoryByCategoryName(packagesList, PackagesCategoryEnum.INFORMATIONS, OverrideTextEnumCommonConfig)//重写信息下的文字(支持枚举) | |
| 139 | 141 | addWidgetToCategoryByCategoryName(packagesList, PackagesCategoryEnum.INFORMATIONS, OverrideTextBarrageConfig)//重写信息下的弹幕文字 |
| 140 | 142 | addWidgetToCategoryByCategoryName(packagesList, PackagesCategoryEnum.INFORMATIONS, OverrideTextGradientConfig)//重写信息下的渐变文字 |
| 141 | 143 | addWidgetToCategoryByCategoryName(packagesList, PackagesCategoryEnum.INFORMATIONS, OverrideVideoConfig)//重写信息下的视频 | ... | ... |