Skip to content

Commit 6254265

Browse files
author
puhui999
committed
【代码优化】MALL: 修复模板装修时组件属性响应式丢失的问题
1 parent ae0cc94 commit 6254265

File tree

4 files changed

+54
-52
lines changed

4 files changed

+54
-52
lines changed

src/components/DiyEditor/components/mobile/NavigationBar/index.vue

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<div v-for="(cell, cellIndex) in cellList" :key="cellIndex" :style="getCellStyle(cell)">
55
<span v-if="cell.type === 'text'">{{ cell.text }}</span>
66
<img v-else-if="cell.type === 'image'" :src="cell.imgUrl" alt="" class="h-full w-full" />
7-
<SearchBar v-else :property="getSearchProp" />
7+
<SearchBar v-else :property="getSearchProp(cell)" />
88
</div>
99
</div>
1010
<img
@@ -51,14 +51,14 @@ const getCellStyle = (cell: NavigationBarCellProperty) => {
5151
} as StyleValue
5252
}
5353
// 获得搜索框属性
54-
const getSearchProp = (cell: NavigationBarCellProperty) => {
54+
const getSearchProp = computed(() => (cell: NavigationBarCellProperty) => {
5555
return {
5656
height: 30,
5757
showScan: false,
5858
placeholder: cell.placeholder,
5959
borderRadius: cell.borderRadius
6060
} as SearchProperty
61-
}
61+
})
6262
</script>
6363
<style lang="scss" scoped>
6464
.navigation-bar {

src/components/DiyEditor/index.vue

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@
110110
<el-tag
111111
v-if="showPageConfig"
112112
:effect="selectedComponent?.uid === pageConfigComponent.uid ? 'dark' : 'plain'"
113-
:type="selectedComponent?.uid === pageConfigComponent.uid ? '' : 'info'"
113+
:type="selectedComponent?.uid === pageConfigComponent.uid ? 'primary' : 'info'"
114114
size="large"
115115
@click="handleComponentSelected(pageConfigComponent)"
116116
>
@@ -121,7 +121,7 @@
121121
<el-tag
122122
v-if="component.position === 'fixed'"
123123
:effect="selectedComponent?.uid === component.uid ? 'dark' : 'plain'"
124-
:type="selectedComponent?.uid === component.uid ? '' : 'info'"
124+
:type="selectedComponent?.uid === component.uid ? 'primary' : 'info'"
125125
closable
126126
size="large"
127127
@click="handleComponentSelected(component)"
@@ -191,7 +191,7 @@ import { cloneDeep, includes } from 'lodash-es'
191191
import { component as PAGE_CONFIG_COMPONENT } from '@/components/DiyEditor/components/mobile/PageConfig/config'
192192
import { component as NAVIGATION_BAR_COMPONENT } from './components/mobile/NavigationBar/config'
193193
import { component as TAB_BAR_COMPONENT } from './components/mobile/TabBar/config'
194-
import { isString } from '@/utils/is'
194+
import { isEmpty, isString } from '@/utils/is'
195195
import { DiyComponent, DiyComponentLibrary, PageConfig } from '@/components/DiyEditor/util'
196196
import { componentConfigs } from '@/components/DiyEditor/components/mobile'
197197
import { array, oneOfType } from 'vue-types'
@@ -238,24 +238,42 @@ const props = defineProps({
238238
watch(
239239
() => props.modelValue,
240240
() => {
241-
const modelValue = isString(props.modelValue)
242-
? (JSON.parse(props.modelValue) as PageConfig)
243-
: props.modelValue
244-
pageConfigComponent.value.property = modelValue?.page || PAGE_CONFIG_COMPONENT.property
241+
const modelValue =
242+
isString(props.modelValue) && !isEmpty(props.modelValue)
243+
? (JSON.parse(props.modelValue) as PageConfig)
244+
: props.modelValue
245+
pageConfigComponent.value.property =
246+
(typeof modelValue !== 'string' && modelValue?.page) || PAGE_CONFIG_COMPONENT.property
245247
navigationBarComponent.value.property =
246-
modelValue?.navigationBar || NAVIGATION_BAR_COMPONENT.property
247-
tabBarComponent.value.property = modelValue?.tabBar || TAB_BAR_COMPONENT.property
248+
(typeof modelValue !== 'string' && modelValue?.navigationBar) ||
249+
NAVIGATION_BAR_COMPONENT.property
250+
tabBarComponent.value.property =
251+
(typeof modelValue !== 'string' && modelValue?.tabBar) || TAB_BAR_COMPONENT.property
248252
// 查找对应的页面组件
249-
pageComponents.value = (modelValue?.components || []).map((item) => {
250-
const component = componentConfigs[item.id]
251-
return { ...component, property: item.property }
252-
})
253+
pageComponents.value = ((typeof modelValue !== 'string' && modelValue?.components) || []).map(
254+
(item) => {
255+
const component = componentConfigs[item.id]
256+
return { ...component, property: item.property }
257+
}
258+
)
253259
},
254260
{
255261
immediate: true
256262
}
257263
)
258264
265+
/** 选择组件修改其属性后更新它的配置 */
266+
watch(
267+
selectedComponent,
268+
(val: any) => {
269+
if (!val || selectedComponentIndex.value === -1) {
270+
return
271+
}
272+
pageComponents.value[selectedComponentIndex.value] = selectedComponent.value!
273+
},
274+
{ deep: true }
275+
)
276+
259277
// 保存
260278
const handleSave = () => {
261279
// 发送保存通知

src/components/InputWithColor/index.vue

Lines changed: 4 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
<template>
2-
<el-input v-model="valueRef" v-bind="$attrs">
2+
<el-input v-model="modelValue" v-bind="$attrs">
33
<template #append>
4-
<el-color-picker v-model="colorRef" :predefine="PREDEFINE_COLORS" />
4+
<el-color-picker v-model="color" :predefine="PREDEFINE_COLORS" />
55
</template>
66
</el-input>
77
</template>
88

99
<script lang="ts" setup>
1010
import { propTypes } from '@/utils/propTypes'
1111
import { PREDEFINE_COLORS } from '@/utils/color'
12+
import { useVModels } from '@vueuse/core'
1213
1314
/**
1415
* 带颜色选择器输入框
@@ -19,33 +20,8 @@ const props = defineProps({
1920
modelValue: propTypes.string.def('').isRequired,
2021
color: propTypes.string.def('').isRequired
2122
})
22-
23-
watch(
24-
() => props.modelValue,
25-
(val: string) => {
26-
if (val === unref(valueRef)) return
27-
valueRef.value = val
28-
}
29-
)
30-
3123
const emit = defineEmits(['update:modelValue', 'update:color'])
32-
33-
// 输入框的值
34-
const valueRef = ref(props.modelValue)
35-
watch(
36-
() => valueRef.value,
37-
(val: string) => {
38-
emit('update:modelValue', val)
39-
}
40-
)
41-
// 颜色
42-
const colorRef = ref(props.color)
43-
watch(
44-
() => colorRef.value,
45-
(val: string) => {
46-
emit('update:color', val)
47-
}
48-
)
24+
const { modelValue, color } = useVModels(props, emit)
4925
</script>
5026
<style scoped lang="scss">
5127
:deep(.el-input-group__append) {

src/views/mall/promotion/diy/template/decorate.vue

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,9 @@ const formLoading = ref(false) // 表单的加载中:1)修改时的数据加
5252
const formData = ref<DiyTemplateApi.DiyTemplatePropertyVO>()
5353
const formRef = ref() // 表单 Ref
5454
// 当前编辑的属性
55-
const currentFormData = ref<DiyTemplateApi.DiyTemplatePropertyVO | DiyPageApi.DiyPageVO>()
55+
const currentFormData = ref<DiyTemplateApi.DiyTemplatePropertyVO | DiyPageApi.DiyPageVO>({
56+
property: ''
57+
} as DiyPageApi.DiyPageVO)
5658
// templateItem 对应的缓存
5759
const currentFormDataMap = ref<
5860
Map<string, DiyTemplateApi.DiyTemplatePropertyVO | DiyPageApi.DiyPageVO>
@@ -92,17 +94,21 @@ const handleTemplateItemChange = (val: number) => {
9294
// 编辑模板
9395
if (val === 0) {
9496
libs.value = templateLibs
95-
currentFormData.value = isEmpty(data) ? formData.value : data
97+
currentFormData.value = (isEmpty(data) ? formData.value : data) as
98+
| DiyTemplateApi.DiyTemplatePropertyVO
99+
| DiyPageApi.DiyPageVO
96100
return
97101
}
98102
99103
// 编辑页面
100104
libs.value = PAGE_LIBS
101-
currentFormData.value = isEmpty(data)
102-
? formData.value!.pages.find(
103-
(page: DiyPageApi.DiyPageVO) => page.name === templateItems[val].name
104-
)
105-
: data
105+
currentFormData.value = (
106+
isEmpty(data)
107+
? formData.value!.pages.find(
108+
(page: DiyPageApi.DiyPageVO) => page.name === templateItems[val].name
109+
)
110+
: data
111+
) as DiyTemplateApi.DiyTemplatePropertyVO | DiyPageApi.DiyPageVO
106112
}
107113
108114
// 提交表单
@@ -170,7 +176,9 @@ const recoverPageIndex = () => {
170176
sessionStorage.removeItem(DIY_PAGE_INDEX_KEY)
171177
172178
// 重新初始化数据
173-
currentFormData.value = formData.value
179+
currentFormData.value = formData.value as
180+
| DiyTemplateApi.DiyTemplatePropertyVO
181+
| DiyPageApi.DiyPageVO
174182
currentFormDataMap.value = new Map<
175183
string,
176184
DiyTemplateApi.DiyTemplatePropertyVO | DiyPageApi.DiyPageVO

0 commit comments

Comments
 (0)