Skip to content

Commit 3f88d2a

Browse files
committed
feat: support for multiple field idx
1 parent c2754cd commit 3f88d2a

1 file changed

Lines changed: 67 additions & 8 deletions

File tree

src/entry-editable.ts

Lines changed: 67 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,30 +8,89 @@ export function addTags(entry: EntryModel, contentTypeUid: string, tagsAsObject:
88
function getTag(content: object, prefix: string, tagsAsObject: boolean, locale: string): object {
99
const tags: any = {}
1010
Object.entries(content).forEach(([key, value]) => {
11+
if (key === '$') return
12+
1113
switch (typeof value) {
1214
case "object":
1315
if (Array.isArray(value)) {
1416
value.forEach((obj, index) => {
17+
const childKey = `${key}__${index}`
18+
/**
19+
* Indexes of array are handled here
20+
* {
21+
* "array": ["hello", "world"],
22+
* "$": {
23+
* "array": {"data-cslp": "content_type_uid.entry_uid.locale.array"}
24+
* "array__0": {"data-cslp": "content_type_uid.entry_uid.locale.array.0"}
25+
* "array__1": {"data-cslp": "content_type_uid.entry_uid.locale.array.1"}
26+
* }
27+
* }
28+
*/
29+
tags[childKey] = getTagsValue(`${prefix}.${key}.${index}`, tagsAsObject)
1530
if (typeof obj !== 'undefined' && obj !== null && obj._content_type_uid !== undefined && obj.uid !== undefined) {
31+
/**
32+
* References are handled here
33+
* {
34+
* "reference": [{
35+
* "title": "title",
36+
* "uid": "ref_uid",
37+
* "_content_type_uid": "ref_content_type_uid",
38+
* "$": {"title": {"data-cslp": "ref_content_type_uid.ref_uid.locale.title"}}
39+
* }]
40+
* }
41+
*/
1642
value[index].$ = getTag(obj, `${obj._content_type_uid}.${obj.uid}.${obj.locale || locale}`, tagsAsObject, locale)
17-
}else {
18-
if (typeof obj === "object") {
19-
obj.$ = getTag(obj, `${prefix}.${key}.${index}`, tagsAsObject, locale)
20-
tags[key] = getTagsValue(`${prefix}.${key}.${index}`, tagsAsObject)
21-
22-
} else {
23-
tags[key] = getTagsValue(`${prefix}.${key}`, tagsAsObject)
24-
}
43+
}else if (typeof obj === "object") {
44+
/**
45+
* Objects inside Array like modular blocks are handled here
46+
* {
47+
* "array": [{
48+
* "title": "title",
49+
* "$": {"title": {"data-cslp": "content_type_uid.entry_uid.locale.array.0.title"}}
50+
* }]
51+
* }
52+
*/
53+
obj.$ = getTag(obj,`${prefix}.${key}.${index}`, tagsAsObject, locale)
2554
}
2655
})
2756
}else {
2857
if (value) {
58+
/**
59+
* Objects are handled here
60+
* {
61+
* "object": {
62+
* "title": "title",
63+
* "$": {
64+
* "title": {"data-cslp": "content_type_uid.entry_uid.locale.object.title"}
65+
* }
66+
* },
67+
* }
68+
*/
2969
value.$ = getTag(value, `${prefix}.${key}`, tagsAsObject, locale)
3070
}
3171
}
72+
/**
73+
* {
74+
* "object": {
75+
* "title": "title",
76+
* },
77+
* "array": ["hello", "world"]
78+
* "$": {
79+
* "object": {"data-cslp": "content_type_uid.entry_uid.locale.object"},
80+
* "array": {"data-cslp": "content_type_uid.entry_uid.locale.array"}
81+
* }
82+
* }
83+
*/
3284
tags[key] = getTagsValue(`${prefix}.${key}`, tagsAsObject)
3385
break;
3486
default:
87+
/**
88+
* All primitive values are handled here
89+
* {
90+
* "title": "title",
91+
* "$": {title: {"data-cslp": "content_type_uid.entry_uid.locale.title"}}
92+
* }
93+
*/
3594
tags[key] = getTagsValue(`${prefix}.${key}`, tagsAsObject)
3695
}
3796
})

0 commit comments

Comments
 (0)