-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathentry-editable.ts
More file actions
181 lines (173 loc) · 9.25 KB
/
entry-editable.ts
File metadata and controls
181 lines (173 loc) · 9.25 KB
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
import { EntryModel } from "."
interface AppliedVariants {
_applied_variants: { [key: string]: any }
shouldApplyVariant: boolean
metaKey: string
}
export function addTags(entry: EntryModel, contentTypeUid: string, tagsAsObject: boolean, locale: string = 'en-us'): void {
if (entry) {
// handle case senstivity for contentTypeUid and locale
contentTypeUid = contentTypeUid.toLowerCase();
locale = locale.toLowerCase();
const appliedVariants = entry._applied_variants || entry?.system?.applied_variants || null;
entry.$ = getTag(entry, `${contentTypeUid}.${entry.uid}.${locale}`, tagsAsObject, locale, { _applied_variants: appliedVariants, shouldApplyVariant: !!appliedVariants, metaKey: '' })
}
}
function getTag(content: object, prefix: string, tagsAsObject: boolean, locale: string, appliedVariants: AppliedVariants): object {
const tags: any = {}
const { metaKey, shouldApplyVariant, _applied_variants } = appliedVariants
Object.entries(content).forEach(([key, value]) => {
if (key === '$') return
let metaUID = value && typeof value === 'object' && value !== null && value._metadata && value._metadata.uid ? value._metadata.uid : '';
let updatedMetakey = appliedVariants.shouldApplyVariant ? `${appliedVariants.metaKey ? appliedVariants.metaKey + '.' : ''}${key}` : '';
if (metaUID && updatedMetakey) updatedMetakey = updatedMetakey + '.' + metaUID;
switch (typeof value) {
case "object":
if (Array.isArray(value)) {
value.forEach((obj, index) => {
if (obj === null || obj === undefined) {
return;
}
const childKey = `${key}__${index}`
const parentKey = `${key}__parent`
metaUID = value && typeof value === 'object' && obj !== null && obj._metadata && obj._metadata.uid ? obj._metadata.uid : '';
updatedMetakey = appliedVariants.shouldApplyVariant ? `${appliedVariants.metaKey ? appliedVariants.metaKey + '.' : ''}${key}` : '';
if (metaUID && updatedMetakey) updatedMetakey = updatedMetakey + '.' + metaUID;
/**
* Indexes of array are handled here
* {
* "array": ["hello", "world"],
* "$": {
* "array": {"data-cslp": "content_type_uid.entry_uid.locale.array"}
* "array__0": {"data-cslp": "content_type_uid.entry_uid.locale.array.0"}
* "array__1": {"data-cslp": "content_type_uid.entry_uid.locale.array.1"}
* }
* }
*/
tags[childKey] = getTagsValue(`${prefix}.${key}.${index}`, tagsAsObject, { _applied_variants, shouldApplyVariant, metaKey: updatedMetakey })
tags[parentKey] = getParentTagsValue(`${prefix}.${key}`, tagsAsObject)
if (typeof obj !== 'undefined' && obj !== null && obj._content_type_uid !== undefined && obj.uid !== undefined) {
/**
* References are handled here
* {
* "reference": [{
* "title": "title",
* "uid": "ref_uid",
* "_content_type_uid": "ref_content_type_uid",
* "$": {"title": {"data-cslp": "ref_content_type_uid.ref_uid.locale.title"}}
* }]
* }
*/
const newAppliedVariants = obj._applied_variants || obj?.system?.applied_variants || null //check for _applied_variants in the reference object only return null if not present , do not check in the parent object;
const newShouldApplyVariant = !!newAppliedVariants
value[index].$ = getTag(obj, `${obj._content_type_uid}.${obj.uid}.${obj.locale || locale}`, tagsAsObject, locale, { _applied_variants: newAppliedVariants, shouldApplyVariant: newShouldApplyVariant, metaKey: "" })
} else if (typeof obj === "object") {
/**
* Objects inside Array like modular blocks are handled here
* {
* "array": [{
* "title": "title",
* "$": {"title": {"data-cslp": "content_type_uid.entry_uid.locale.array.0.title"}}
* }]
* }
*/
obj.$ = getTag(obj, `${prefix}.${key}.${index}`, tagsAsObject, locale, { _applied_variants, shouldApplyVariant, metaKey: updatedMetakey })
}
})
} else {
if (value) {
/**
* Objects are handled here
* {
* "object": {
* "title": "title",
* "$": {
* "title": {"data-cslp": "content_type_uid.entry_uid.locale.object.title"}
* }
* },
* }
*/
value.$ = getTag(value, `${prefix}.${key}`, tagsAsObject, locale, { _applied_variants, shouldApplyVariant, metaKey: updatedMetakey })
}
}
/**
* {
* "object": {
* "title": "title",
* },
* "array": ["hello", "world"]
* "$": {
* "object": {"data-cslp": "content_type_uid.entry_uid.locale.object"},
* "array": {"data-cslp": "content_type_uid.entry_uid.locale.array"}
* }
* }
*/
tags[key] = getTagsValue(`${prefix}.${key}`, tagsAsObject, { _applied_variants, shouldApplyVariant, metaKey: updatedMetakey })
break;
default:
/**
* All primitive values are handled here
* {
* "title": "title",
* "$": {title: {"data-cslp": "content_type_uid.entry_uid.locale.title"}}
* }
*/
tags[key] = getTagsValue(`${prefix}.${key}`, tagsAsObject, { _applied_variants, shouldApplyVariant, metaKey: updatedMetakey })
}
})
return tags
}
function getTagsValue(dataValue: string, tagsAsObject: boolean, appliedVariants: { _applied_variants: { [key: string]: any }, shouldApplyVariant: boolean, metaKey: string }): any {
if (appliedVariants.shouldApplyVariant && appliedVariants._applied_variants) {
const isFieldVariantised = appliedVariants._applied_variants[appliedVariants.metaKey];
if(isFieldVariantised) {
const variant = appliedVariants._applied_variants[appliedVariants.metaKey]
// Adding v2 prefix to the cslp tag. New cslp tags are in v2 format. ex: v2:content_type_uid.entry_uid.locale.title
const newDataValueArray = ('v2:' + dataValue).split('.');
newDataValueArray[1] = newDataValueArray[1] + '_' + variant;
dataValue = newDataValueArray.join('.');
}
else {
const parentVariantisedPath = getParentVariantisedPath(appliedVariants);
if(parentVariantisedPath) {
const variant = appliedVariants._applied_variants[parentVariantisedPath];
const newDataValueArray = ('v2:' + dataValue).split('.');
newDataValueArray[1] = newDataValueArray[1] + '_' + variant;
dataValue = newDataValueArray.join('.');
}
}
}
if (tagsAsObject) {
return { "data-cslp": dataValue };
} else {
return `data-cslp=${dataValue}`;
}
}
function getParentTagsValue(dataValue: string, tagsAsObject: boolean): any {
if (tagsAsObject) {
return { "data-cslp-parent-field": dataValue };
} else {
return `data-cslp-parent-field=${dataValue}`;
}
}
function getParentVariantisedPath(appliedVariants: AppliedVariants) {
try {
// Safety fallback
if(!appliedVariants._applied_variants) return '';
const variantisedFieldPaths = Object.keys(appliedVariants._applied_variants).sort((a, b) => {
return b.length - a.length;
});
const childPathFragments = appliedVariants.metaKey.split('.');
// Safety fallback
if(childPathFragments.length === 0 || variantisedFieldPaths.length === 0) return '';
const parentVariantisedPath = variantisedFieldPaths.find(path => {
const parentFragments = path.split('.');
if(parentFragments.length > childPathFragments.length) return false;
return parentFragments.every((fragment, index) => childPathFragments[index] === fragment);
}) ?? '';
return parentVariantisedPath;
}
catch(e) {
return '';
}
}