@@ -3,7 +3,8 @@ import { computed } from 'vue'
33import type { FormTree } from ' ../../../types'
44import type { PropType } from ' vue'
55import type { Draft07 } from ' @nuxt/content'
6- import { buildFormTreeFromSchema , applyValuesToFormTree } from ' ../../../utils/form'
6+ import { buildFormTreeFromSchema , applyValuesToFormTree , getUpdatedTreeItem } from ' ../../../utils/form'
7+ import { applyValueByPath } from ' ../../../utils/object'
78
89const props = defineProps ({
910 collectionName: {
@@ -16,45 +17,42 @@ const props = defineProps({
1617 },
1718})
1819
19- const data = defineModel <Record <string , unknown >>({ required: true })
20-
21- console .log (' data' , data .value )
20+ const model = defineModel <Record <string , unknown >>({ required: true })
2221
2322const formTree = computed <FormTree >(() => {
2423 return buildFormTreeFromSchema (props .collectionName , props .schema )
2524})
2625
2726const formTreeWithValues = computed ({
2827 get : () => {
29- if (! data .value || ! formTree .value ) {
28+ if (! model .value || ! formTree .value ) {
3029 return null
3130 }
3231
33- return applyValuesToFormTree (formTree .value , { [props .collectionName ]: data .value })
32+ return applyValuesToFormTree (formTree .value , { [props .collectionName ]: model .value })
3433 },
3534 set : (newFormTree ) => {
36- console .log (' newFormTree' , newFormTree )
35+ const updatedItem = getUpdatedTreeItem (formTreeWithValues .value ! , newFormTree ! )
36+ if (! updatedItem ) {
37+ return
38+ }
39+
40+ // Strip the collection name from the id ("#authors/title" → "title"
41+ const pathSegments = updatedItem .id .split (' /' )
42+ pathSegments .shift ()
43+ const jsonContentCopy = JSON .parse (JSON .stringify (model .value ))
44+ model .value = applyValueByPath (jsonContentCopy , pathSegments .join (' /' ), updatedItem .value )
3745 },
3846})
39-
40- // const jsonString = computed({
41- // get: () => JSON.stringify(model.value, null, 2),
42- // set: (value: string) => {
43- // try {
44- // model.value = JSON.parse(value)
45- // }
46- // catch {
47- // // Invalid JSON, don't update
48- // }
49- // },
50- // })
5147 </script >
5248
5349<template >
54- <FormPanelSection
55- v-for =" formItem in formTree[collectionName].children"
56- :key =" formItem.id"
57- v-model =" formTreeWithValues"
58- :form-item =" formItem"
59- />
50+ <template v-if =" formTreeWithValues " >
51+ <FormPanelSection
52+ v-for =" formItem in formTreeWithValues[collectionName].children"
53+ :key =" formItem.id"
54+ v-model =" formTreeWithValues"
55+ :form-item =" formItem"
56+ />
57+ </template >
6058</template >
0 commit comments