-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathentry-editable.ts
More file actions
213 lines (201 loc) · 11 KB
/
entry-editable.ts
File metadata and controls
213 lines (201 loc) · 11 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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
import { EntryModel } from "."
interface AppliedVariants {
_applied_variants: { [key: string]: any }
shouldApplyVariant: boolean
metaKey: string
}
/**
* Adds Contentstack Live Preview (CSLP) data tags to an entry for editable UIs.
* Mutates the entry by attaching a `$` property with tag strings or objects
* (e.g. `data-cslp` / `data-cslp-parent-field`) for each field, including nested
* objects and references. Supports variant-aware tagging when the entry has
* applied variants.
*
* @param entry - The entry (EmbeddedItem) to tag. Must have uid and optional system/applied variants.
* @param contentTypeUid - Content type UID (e.g. `blog_post`). Used as part of the tag path.
* @param tagsAsObject - If true, tags are stored as objects (e.g. `{ "data-cslp": "..." }`); if false, as strings (e.g. `data-cslp=...`).
* @param locale - Locale code for the tag path (default: `'en-us'`).
* @param options.useLowerCaseLocale - Optional boolean to make locale case-insensitive.
*/
export function addTags(entry: EntryModel, contentTypeUid: string, tagsAsObject: boolean, locale: string = 'en-us', options?: {
useLowerCaseLocale?: boolean
}): void {
const { useLowerCaseLocale = true } = options || {};
if (entry) {
// handle case senstivity for contentTypeUid and locale
contentTypeUid = contentTypeUid.toLowerCase();
locale = useLowerCaseLocale ? locale.toLowerCase() : locale;
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: '' })
}
}
/** @internal Exported for testing the null/undefined guard (Issue #193). */
export function getTag(content: object, prefix: string, tagsAsObject: boolean, locale: string, appliedVariants: AppliedVariants): object {
if (content == null) {
return {}
}
const tags: any = {}
const { shouldApplyVariant, _applied_variants } = appliedVariants
Object.entries(content).forEach(([key, value]) => {
if (key === '$') return
let metaUID = value?._metadata?.uid ?? '';
const metaKeyPrefix = appliedVariants.metaKey ? appliedVariants.metaKey + '.' : '';
let updatedMetakey = appliedVariants.shouldApplyVariant ? `${metaKeyPrefix}${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 = obj?._metadata?.uid ?? '';
updatedMetakey = appliedVariants.shouldApplyVariant ? `${metaKeyPrefix}${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] = tagsAsObject
? getTagsValueAsObject(`${prefix}.${key}.${index}`, { _applied_variants, shouldApplyVariant, metaKey: updatedMetakey })
: getTagsValueAsString(`${prefix}.${key}.${index}`, { _applied_variants, shouldApplyVariant, metaKey: updatedMetakey })
tags[parentKey] = tagsAsObject ? getParentTagsValueAsObject(`${prefix}.${key}`) : getParentTagsValueAsString(`${prefix}.${key}`)
if (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] = tagsAsObject
? getTagsValueAsObject(`${prefix}.${key}`, { _applied_variants, shouldApplyVariant, metaKey: updatedMetakey })
: getTagsValueAsString(`${prefix}.${key}`, { _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] = tagsAsObject
? getTagsValueAsObject(`${prefix}.${key}`, { _applied_variants, shouldApplyVariant, metaKey: updatedMetakey })
: getTagsValueAsString(`${prefix}.${key}`, { _applied_variants, shouldApplyVariant, metaKey: updatedMetakey })
}
})
return tags
}
type TagsAppliedVariants = { _applied_variants: { [key: string]: any }; shouldApplyVariant: boolean; metaKey: string };
function applyVariantToDataValue(dataValue: string, appliedVariants: TagsAppliedVariants): string {
if (appliedVariants?.shouldApplyVariant && appliedVariants?._applied_variants) {
const isFieldVariantised = appliedVariants._applied_variants[appliedVariants.metaKey];
if (isFieldVariantised) {
const variant = appliedVariants._applied_variants[appliedVariants.metaKey];
const newDataValueArray = ('v2:' + dataValue).split('.');
newDataValueArray[1] = newDataValueArray[1] + '_' + variant;
return newDataValueArray.join('.');
}
const parentVariantisedPath = getParentVariantisedPath(appliedVariants as AppliedVariants);
if (parentVariantisedPath) {
const variant = appliedVariants._applied_variants[parentVariantisedPath];
const newDataValueArray = ('v2:' + dataValue).split('.');
newDataValueArray[1] = newDataValueArray[1] + '_' + variant;
return newDataValueArray.join('.');
}
}
return dataValue;
}
function getTagsValueAsObject(dataValue: string, appliedVariants: TagsAppliedVariants): { "data-cslp": string } {
const resolved = applyVariantToDataValue(dataValue, appliedVariants);
return { "data-cslp": resolved };
}
function getTagsValueAsString(dataValue: string, appliedVariants: TagsAppliedVariants): string {
const resolved = applyVariantToDataValue(dataValue, appliedVariants);
return `data-cslp=${resolved}`;
}
function getParentTagsValueAsObject(dataValue: string): { "data-cslp-parent-field": string } {
return { "data-cslp-parent-field": dataValue };
}
function getParentTagsValueAsString(dataValue: string): string {
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 '';
}
}