gaoluyang
2026-06-24 712aa51536236d43e87273e4ce45ac5691dffad8
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
<script setup lang="ts">
import type { Ref } from 'vue';
 
import type { ComponentStyle } from '../util';
 
import { useVModel } from '@vueuse/core';
import {
  Col,
  Form,
  FormItem,
  InputNumber,
  Radio,
  RadioGroup,
  Row,
  Slider,
  TabPane,
  Tabs,
  Tree,
} from 'ant-design-vue';
 
import UploadImg from '#/components/upload/image-upload.vue';
import { ColorInput } from '#/views/mall/promotion/components';
 
/**
 * 组件容器属性:目前右边部分
 * 用于包裹组件,为组件提供 背景、外边距、内边距、边框等样式
 */
defineOptions({ name: 'ComponentContainer' });
 
const props = defineProps<{ modelValue: ComponentStyle }>();
const emit = defineEmits(['update:modelValue']);
const formData = useVModel(props, 'modelValue', emit);
const formStyleValues = formData as unknown as Ref<Record<string, number>>;
 
const treeData: any[] = [
  {
    label: '外部边距',
    prop: 'margin',
    children: [
      {
        label: '上',
        prop: 'marginTop',
      },
      {
        label: '右',
        prop: 'marginRight',
      },
      {
        label: '下',
        prop: 'marginBottom',
      },
      {
        label: '左',
        prop: 'marginLeft',
      },
    ],
  },
  {
    label: '内部边距',
    prop: 'padding',
    children: [
      {
        label: '上',
        prop: 'paddingTop',
      },
      {
        label: '右',
        prop: 'paddingRight',
      },
      {
        label: '下',
        prop: 'paddingBottom',
      },
      {
        label: '左',
        prop: 'paddingLeft',
      },
    ],
  },
  {
    label: '边框圆角',
    prop: 'borderRadius',
    children: [
      {
        label: '上左',
        prop: 'borderTopLeftRadius',
      },
      {
        label: '上右',
        prop: 'borderTopRightRadius',
      },
      {
        label: '下右',
        prop: 'borderBottomRightRadius',
      },
      {
        label: '下左',
        prop: 'borderBottomLeftRadius',
      },
    ],
  },
];
 
function handleSliderChange(prop: string) {
  switch (prop) {
    case 'borderRadius': {
      formData.value.borderTopLeftRadius = formData.value.borderRadius;
      formData.value.borderTopRightRadius = formData.value.borderRadius;
      formData.value.borderBottomRightRadius = formData.value.borderRadius;
      formData.value.borderBottomLeftRadius = formData.value.borderRadius;
      break;
    }
    case 'margin': {
      formData.value.marginTop = formData.value.margin;
      formData.value.marginRight = formData.value.margin;
      formData.value.marginBottom = formData.value.margin;
      formData.value.marginLeft = formData.value.margin;
      break;
    }
    case 'padding': {
      formData.value.paddingTop = formData.value.padding;
      formData.value.paddingRight = formData.value.padding;
      formData.value.paddingBottom = formData.value.padding;
      formData.value.paddingLeft = formData.value.padding;
      break;
    }
  }
}
</script>
 
<template>
  <Tabs>
    <!-- 每个组件的自定义内容 -->
    <TabPane tab="内容" key="content" v-if="$slots.default">
      <slot></slot>
    </TabPane>
 
    <!-- 每个组件的通用内容 -->
    <TabPane tab="样式" key="style" force-render>
      <div class="mb-2 bg-gray-100 p-2 text-sm">组件样式:</div>
      <div class="flex flex-col gap-2 rounded-md p-4 shadow-lg">
        <Form :model="formData">
          <FormItem
            label="组件背景"
            name="bgType"
            :label-col="{ style: { width: '109px' } }"
          >
            <RadioGroup v-model:value="formData.bgType">
              <Radio value="color">纯色</Radio>
              <Radio value="img">图片</Radio>
            </RadioGroup>
          </FormItem>
          <FormItem
            label="选择颜色"
            name="bgColor"
            :label-col="{ style: { width: '109px' } }"
            v-if="formData.bgType === 'color'"
          >
            <ColorInput v-model="formData.bgColor" />
          </FormItem>
          <FormItem
            label="上传图片"
            name="bgImg"
            :label-col="{ style: { width: '109px' } }"
            v-else
          >
            <UploadImg
              v-model="formData.bgImg"
              :limit="1"
              :show-description="false"
            >
              <template #tip>建议宽度 750px</template>
            </UploadImg>
          </FormItem>
          <Tree :tree-data="treeData" default-expand-all :block-node="true">
            <template #title="{ dataRef }">
              <FormItem
                :label="dataRef.label"
                :name="dataRef.prop"
                :label-col="{
                  style: { width: dataRef.children ? '80px' : '58px' },
                }"
                class="mb-0 w-full"
              >
                <Row>
                  <Col :span="19">
                    <Slider
                      v-model:value="formStyleValues[dataRef.prop]"
                      :max="100"
                      :min="0"
                      @change="handleSliderChange(dataRef.prop)"
                      class="mr-4"
                    />
                  </Col>
                  <Col :span="4">
                    <InputNumber
                      class="w-[50px]"
                      :max="100"
                      :min="0"
                      v-model:value="formStyleValues[dataRef.prop]"
                    />
                  </Col>
                </Row>
              </FormItem>
            </template>
          </Tree>
          <slot name="style" :style="formData"></slot>
        </Form>
      </div>
    </TabPane>
  </Tabs>
</template>