@contentstack/utils
Version:
Contentstack utilities for Javascript
207 lines • 12.3 kB
JavaScript
/**
* 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, contentTypeUid, tagsAsObject, locale, options) {
var _a;
if (locale === void 0) { locale = 'en-us'; }
var _b = (options || {}).useLowerCaseLocale, useLowerCaseLocale = _b === void 0 ? true : _b;
if (entry) {
// handle case senstivity for contentTypeUid and locale
contentTypeUid = contentTypeUid.toLowerCase();
locale = useLowerCaseLocale ? locale.toLowerCase() : locale;
var appliedVariants = entry._applied_variants || ((_a = entry === null || entry === void 0 ? void 0 : entry.system) === null || _a === void 0 ? void 0 : _a.applied_variants) || null;
entry.$ = getTag(entry, "".concat(contentTypeUid, ".").concat(entry.uid, ".").concat(locale), tagsAsObject, locale, { _applied_variants: appliedVariants, shouldApplyVariant: !!appliedVariants, metaKey: '' });
}
}
/** @internal Exported for testing the null/undefined guard (Issue #193). */
export function getTag(content, prefix, tagsAsObject, locale, appliedVariants) {
if (content == null) {
return {};
}
var tags = {};
var shouldApplyVariant = appliedVariants.shouldApplyVariant, _applied_variants = appliedVariants._applied_variants;
Object.entries(content).forEach(function (_a) {
var _b, _c;
var key = _a[0], value = _a[1];
if (key === '$')
return;
var metaUID = (_c = (_b = value === null || value === void 0 ? void 0 : value._metadata) === null || _b === void 0 ? void 0 : _b.uid) !== null && _c !== void 0 ? _c : '';
var metaKeyPrefix = appliedVariants.metaKey ? appliedVariants.metaKey + '.' : '';
var updatedMetakey = appliedVariants.shouldApplyVariant ? "".concat(metaKeyPrefix).concat(key) : '';
if (metaUID && updatedMetakey)
updatedMetakey = updatedMetakey + '.' + metaUID;
switch (typeof value) {
case "object":
if (Array.isArray(value)) {
value.forEach(function (obj, index) {
var _a, _b, _c;
if (obj === null || obj === undefined) {
return;
}
var childKey = "".concat(key, "__").concat(index);
var parentKey = "".concat(key, "__parent");
metaUID = (_b = (_a = obj === null || obj === void 0 ? void 0 : obj._metadata) === null || _a === void 0 ? void 0 : _a.uid) !== null && _b !== void 0 ? _b : '';
updatedMetakey = appliedVariants.shouldApplyVariant ? "".concat(metaKeyPrefix).concat(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("".concat(prefix, ".").concat(key, ".").concat(index), { _applied_variants: _applied_variants, shouldApplyVariant: shouldApplyVariant, metaKey: updatedMetakey })
: getTagsValueAsString("".concat(prefix, ".").concat(key, ".").concat(index), { _applied_variants: _applied_variants, shouldApplyVariant: shouldApplyVariant, metaKey: updatedMetakey });
tags[parentKey] = tagsAsObject ? getParentTagsValueAsObject("".concat(prefix, ".").concat(key)) : getParentTagsValueAsString("".concat(prefix, ".").concat(key));
if ((obj === null || obj === void 0 ? void 0 : obj._content_type_uid) !== undefined && (obj === null || obj === void 0 ? void 0 : 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"}}
* }]
* }
*/
var newAppliedVariants = obj._applied_variants || ((_c = obj === null || obj === void 0 ? void 0 : obj.system) === null || _c === void 0 ? void 0 : _c.applied_variants) || null; //check for _applied_variants in the reference object only return null if not present , do not check in the parent object;
var newShouldApplyVariant = !!newAppliedVariants;
value[index].$ = getTag(obj, "".concat(obj._content_type_uid, ".").concat(obj.uid, ".").concat(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, "".concat(prefix, ".").concat(key, ".").concat(index), tagsAsObject, locale, { _applied_variants: _applied_variants, shouldApplyVariant: 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, "".concat(prefix, ".").concat(key), tagsAsObject, locale, { _applied_variants: _applied_variants, shouldApplyVariant: 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("".concat(prefix, ".").concat(key), { _applied_variants: _applied_variants, shouldApplyVariant: shouldApplyVariant, metaKey: updatedMetakey })
: getTagsValueAsString("".concat(prefix, ".").concat(key), { _applied_variants: _applied_variants, shouldApplyVariant: 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("".concat(prefix, ".").concat(key), { _applied_variants: _applied_variants, shouldApplyVariant: shouldApplyVariant, metaKey: updatedMetakey })
: getTagsValueAsString("".concat(prefix, ".").concat(key), { _applied_variants: _applied_variants, shouldApplyVariant: shouldApplyVariant, metaKey: updatedMetakey });
}
});
return tags;
}
function applyVariantToDataValue(dataValue, appliedVariants) {
if ((appliedVariants === null || appliedVariants === void 0 ? void 0 : appliedVariants.shouldApplyVariant) && (appliedVariants === null || appliedVariants === void 0 ? void 0 : appliedVariants._applied_variants)) {
var isFieldVariantised = appliedVariants._applied_variants[appliedVariants.metaKey];
if (isFieldVariantised) {
var variant = appliedVariants._applied_variants[appliedVariants.metaKey];
var newDataValueArray = ('v2:' + dataValue).split('.');
newDataValueArray[1] = newDataValueArray[1] + '_' + variant;
return newDataValueArray.join('.');
}
var parentVariantisedPath = getParentVariantisedPath(appliedVariants);
if (parentVariantisedPath) {
var variant = appliedVariants._applied_variants[parentVariantisedPath];
var newDataValueArray = ('v2:' + dataValue).split('.');
newDataValueArray[1] = newDataValueArray[1] + '_' + variant;
return newDataValueArray.join('.');
}
}
return dataValue;
}
function getTagsValueAsObject(dataValue, appliedVariants) {
var resolved = applyVariantToDataValue(dataValue, appliedVariants);
return { "data-cslp": resolved };
}
function getTagsValueAsString(dataValue, appliedVariants) {
var resolved = applyVariantToDataValue(dataValue, appliedVariants);
return "data-cslp=".concat(resolved);
}
function getParentTagsValueAsObject(dataValue) {
return { "data-cslp-parent-field": dataValue };
}
function getParentTagsValueAsString(dataValue) {
return "data-cslp-parent-field=".concat(dataValue);
}
function getParentVariantisedPath(appliedVariants) {
var _a;
try {
// Safety fallback
if (!appliedVariants._applied_variants)
return '';
var variantisedFieldPaths = Object.keys(appliedVariants._applied_variants).sort(function (a, b) {
return b.length - a.length;
});
var childPathFragments_1 = appliedVariants.metaKey.split('.');
// Safety fallback
if (childPathFragments_1.length === 0 || variantisedFieldPaths.length === 0)
return '';
var parentVariantisedPath = (_a = variantisedFieldPaths.find(function (path) {
var parentFragments = path.split('.');
if (parentFragments.length > childPathFragments_1.length)
return false;
return parentFragments.every(function (fragment, index) { return childPathFragments_1[index] === fragment; });
})) !== null && _a !== void 0 ? _a : '';
return parentVariantisedPath;
}
catch (e) {
return '';
}
}
//# sourceMappingURL=entry-editable.js.map