|
|
@@ -4,6 +4,7 @@ import { assign } from 'lodash-es'
|
|
|
import { computed, ref, watch } from 'vue'
|
|
|
import { generateCssGradient } from '@/utils'
|
|
|
import { DEFAULT_THEME_KEY } from '@/constants'
|
|
|
+import { findVariableById, getAllVariables, isVariableBound } from '@/utils/variableBinding'
|
|
|
|
|
|
import type { IStyleConfig, PartItem } from '../type'
|
|
|
import type { CSSProperties } from 'vue'
|
|
|
@@ -246,6 +247,27 @@ export const useWidgetStyle = (param: StyleParam) => {
|
|
|
const { part, state, styles = [], props } = param.props
|
|
|
const currentTheme = projectStore.project?.currentTheme || DEFAULT_THEME_KEY
|
|
|
const getStyleTheme = (styleItem: any) => styleItem?.theme || DEFAULT_THEME_KEY
|
|
|
+ const allVariables = getAllVariables(
|
|
|
+ projectStore.project?.variables || [],
|
|
|
+ projectStore.activePage?.variables
|
|
|
+ )
|
|
|
+ const resolveVariableValue = (val: any): any => {
|
|
|
+ if (isVariableBound(val)) {
|
|
|
+ const variable = findVariableById(val.varId, allVariables)
|
|
|
+ return variable ? variable.value : val.originValue
|
|
|
+ }
|
|
|
+ if (Array.isArray(val)) {
|
|
|
+ return val.map((item) => resolveVariableValue(item))
|
|
|
+ }
|
|
|
+ if (val && typeof val === 'object') {
|
|
|
+ const result = {}
|
|
|
+ Object.keys(val).forEach((k) => {
|
|
|
+ result[k] = resolveVariableValue(val[k])
|
|
|
+ })
|
|
|
+ return result
|
|
|
+ }
|
|
|
+ return val
|
|
|
+ }
|
|
|
parts.forEach((partItem) => {
|
|
|
styleMap.value[`${partItem.name}Style`] = {}
|
|
|
// 从控件配置的样式列表查找对应样式
|
|
|
@@ -282,13 +304,15 @@ export const useWidgetStyle = (param: StyleParam) => {
|
|
|
|
|
|
// 合并到预设样式
|
|
|
style = assign({}, presetStyle || {}, defaultStyle || {}, style || {})
|
|
|
+ const resolvedStyle = resolveVariableValue(style)
|
|
|
|
|
|
// 遍历style 获取样式
|
|
|
- Object.keys(style || {}).forEach((key) => {
|
|
|
+ Object.keys(resolvedStyle || {}).forEach((key) => {
|
|
|
+ const resolvedValue = resolvedStyle?.[key]
|
|
|
// 合并样式
|
|
|
assign(
|
|
|
styleMap.value[`${partItem.name}Style`],
|
|
|
- getStyle(key, style?.[key], {
|
|
|
+ getStyle(key, resolvedValue, {
|
|
|
width: props?.width,
|
|
|
height: props?.height,
|
|
|
fontNameMap: projectStore.fontNameMap
|
|
|
@@ -296,23 +320,23 @@ export const useWidgetStyle = (param: StyleParam) => {
|
|
|
)
|
|
|
|
|
|
// 获取背景图片src及颜色
|
|
|
- if (key === 'background' && style?.[key]?.image?.imgId) {
|
|
|
- styleMap.value[`${partItem.name}Style`].imageSrc = getImageSrc(style?.[key]?.image?.imgId)
|
|
|
+ if (key === 'background' && resolvedValue?.image?.imgId) {
|
|
|
+ styleMap.value[`${partItem.name}Style`].imageSrc = getImageSrc(resolvedValue?.image?.imgId)
|
|
|
styleMap.value[`${partItem.name}Style`].imageStyle = {
|
|
|
- backgroundColor: style?.[key]?.image?.recolor,
|
|
|
- opacity: (style?.[key]?.image?.alpha || 255) / 255
|
|
|
+ backgroundColor: resolvedValue?.image?.recolor,
|
|
|
+ opacity: (resolvedValue?.image?.alpha || 255) / 255
|
|
|
}
|
|
|
}
|
|
|
// 图片样式
|
|
|
if (key === 'imageStyle') {
|
|
|
styleMap.value[`${partItem.name}Style`].image = {
|
|
|
- backgroundColor: style?.[key]?.recolor,
|
|
|
- opacity: (style?.[key]?.alpha || 255) / 255
|
|
|
+ backgroundColor: resolvedValue?.recolor,
|
|
|
+ opacity: (resolvedValue?.alpha || 255) / 255
|
|
|
}
|
|
|
}
|
|
|
// 线段返回原本值,并解析 image 为 imageSrc/imageAlpha
|
|
|
if (key === 'line') {
|
|
|
- const lineData = style?.line
|
|
|
+ const lineData = resolvedValue
|
|
|
styleMap.value[`${partItem.name}Style`].line = {
|
|
|
color: lineData?.color ?? '',
|
|
|
width: lineData?.width ?? 0,
|
|
|
@@ -322,7 +346,7 @@ export const useWidgetStyle = (param: StyleParam) => {
|
|
|
}
|
|
|
}
|
|
|
if (key === 'curve') {
|
|
|
- const curveData = style?.curve
|
|
|
+ const curveData = resolvedValue
|
|
|
styleMap.value[`${partItem.name}Style`].curve = {
|
|
|
color: curveData?.color ?? '',
|
|
|
width: curveData?.width ?? 0,
|
|
|
@@ -333,10 +357,12 @@ export const useWidgetStyle = (param: StyleParam) => {
|
|
|
}
|
|
|
})
|
|
|
// 处理行高 默认行高为1.2倍
|
|
|
- if (style?.spacer?.lineHeight && style?.text?.size) {
|
|
|
- const baseHeight = style.text.size * 1.2
|
|
|
+ const spacerValue = resolvedStyle?.spacer
|
|
|
+ const textValue = resolvedStyle?.text
|
|
|
+ if (spacerValue?.lineHeight && textValue?.size) {
|
|
|
+ const baseHeight = textValue.size * 1.2
|
|
|
styleMap.value[`${partItem.name}Style`].lineHeight =
|
|
|
- `${baseHeight + style.spacer.lineHeight}px`
|
|
|
+ `${baseHeight + spacerValue.lineHeight}px`
|
|
|
}
|
|
|
})
|
|
|
}
|