Skip to content

Commit 329b092

Browse files
bzp2010bzp2010
andauthored
feature: add object property editor (#170)
* feat: add object property editor * fix some problem Co-authored-by: bzp2010 <[email protected]>
1 parent fbb6315 commit 329b092

File tree

1 file changed

+100
-0
lines changed

1 file changed

+100
-0
lines changed

src/components/PluginDialog/index.vue

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,39 @@
156156
{{ $t('button.addValue') }}
157157
</el-button>
158158
</div>
159+
160+
<!-- object property -->
161+
<div
162+
v-if="schema.properties[key].type === 'object'"
163+
class="object-input-container"
164+
>
165+
<div
166+
v-for="(objectItem, objectIndex) in objectPropertiesArray[key]"
167+
:key="objectIndex"
168+
class="object-input-item"
169+
>
170+
<el-input
171+
v-model="objectPropertiesArray[key][objectIndex].key"
172+
:placeholder="`${key} [key ${objectIndex}]`"
173+
@input="onObjectPropertyChange(key, $event, true)"
174+
/>
175+
<el-input
176+
v-model="objectPropertiesArray[key][objectIndex].value"
177+
:placeholder="`${key} [value ${objectIndex}]`"
178+
@input="onObjectPropertyChange(key, $event, false)"
179+
/>
180+
<el-button
181+
@click="deleteObjectItem(key, objectIndex)"
182+
>
183+
{{ $t('button.delete') }}
184+
</el-button>
185+
</div>
186+
<el-button
187+
@click="addObjectItem(key)"
188+
>
189+
{{ $t('button.addValue') }}
190+
</el-button>
191+
</div>
159192
</el-form-item>
160193
</el-form>
161194
<span
@@ -202,6 +235,7 @@ export default class extends Vue {
202235
private isDataChanged: boolean = false
203236
private showDialog: boolean = false
204237
private arrayPropertiesLength = {}
238+
private objectPropertiesArray = {}
205239
206240
@Watch('show')
207241
private onShowChange(value: boolean) {
@@ -282,6 +316,15 @@ export default class extends Vue {
282316
break
283317
case 'object':
284318
schemaKeys[key] = {}
319+
this.objectPropertiesArray[key] = []
320+
if (this.pluginData[key]) {
321+
Object.keys(this.pluginData[key]).map(item => {
322+
this.objectPropertiesArray[key].push({
323+
key: item,
324+
value: this.pluginData[key][item]
325+
})
326+
})
327+
}
285328
break
286329
case 'boolean':
287330
schemaKeys[key] = false
@@ -333,6 +376,8 @@ export default class extends Vue {
333376
(this.$refs.form as any).validate((valid: boolean) => {
334377
// 标记该插件数据是否通过校验
335378
if (valid) {
379+
// Reorganize an array of object properties into objects
380+
this.data = Object.assign({}, this.data, this.reorganizeObjectProperty())
336381
this.data = this.processOneOfProp(this.data)
337382
this.$emit('save', this.name, this.data)
338383
this.$message.warning(`${this.$t('message.clickSaveButton')}`)
@@ -355,6 +400,53 @@ export default class extends Vue {
355400
}
356401
}
357402
403+
/**
404+
* Add item to object property
405+
* @param key
406+
*/
407+
private addObjectItem(key: any) {
408+
this.objectPropertiesArray[key].push({
409+
key: '',
410+
value: ''
411+
})
412+
this.isDataChanged = true
413+
this.$forceUpdate()
414+
}
415+
416+
/**
417+
* Delete item form object property
418+
* @param key
419+
* @param index
420+
*/
421+
private deleteObjectItem(key: any, index: number) {
422+
this.objectPropertiesArray[key].splice(index, 1)
423+
this.isDataChanged = true
424+
this.$forceUpdate()
425+
}
426+
427+
/**
428+
* Reorganize an array of object properties into objects
429+
*/
430+
private reorganizeObjectProperty() {
431+
let data = {}
432+
for (let i in this.objectPropertiesArray) {
433+
let objectItem = {}
434+
this.objectPropertiesArray[i].map((item: any) => {
435+
objectItem[item.key] = item.value
436+
})
437+
data[i] = objectItem
438+
}
439+
return data
440+
}
441+
442+
/**
443+
* Force rerender on object property content changed
444+
*/
445+
private onObjectPropertyChange() {
446+
this.isDataChanged = true
447+
this.$forceUpdate()
448+
}
449+
358450
private onPropertyChange(key: any, value: any) {
359451
this.data[key] = value
360452
this.isDataChanged = true
@@ -419,5 +511,13 @@ export default class extends Vue {
419511
display: flex;
420512
margin-top: 5px;
421513
}
514+
515+
.object-input-container > * {
516+
display: flex;
517+
margin-top: 5px;
518+
:not(:first-child){
519+
margin-left: 5px;
520+
}
521+
}
422522
}
423523
</style>

0 commit comments

Comments
 (0)