@prismicio/types-internal
Version:
Prismic types for Custom Types and Prismic Data
80 lines (79 loc) • 3.92 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.repeatableContentWithDefaultNestableContentValues = exports.withDefaultNestableContentValues = exports.withDefaultSlicePrimaryContentValues = void 0;
const customtypes_1 = require("../../customtypes");
const GroupContent_1 = require("./GroupContent");
const nestable_1 = require("./nestable");
function withDefaultSlicePrimaryContentValues(customType, content) {
const updatedContent = content;
for (const [widgetKey, widgetDef] of Object.entries(customType)) {
const widgetContent = content[widgetKey];
if (widgetContent === undefined) {
if (widgetDef.type === customtypes_1.GroupFieldType) {
updatedContent[widgetKey] = (0, GroupContent_1.groupContentWithDefaultValues)(widgetDef, GroupContent_1.GroupContentDefaultValue);
}
else {
const defaultValue = (0, nestable_1.NestableContentDefaultValue)(widgetDef);
if (defaultValue) {
updatedContent[widgetKey] = defaultValue;
}
}
continue;
}
if (widgetDef.type === customtypes_1.GroupFieldType && (0, GroupContent_1.isGroupContent)(widgetContent)) {
updatedContent[widgetKey] = (0, GroupContent_1.groupContentWithDefaultValues)(widgetDef, widgetContent);
}
}
return updatedContent;
}
exports.withDefaultSlicePrimaryContentValues = withDefaultSlicePrimaryContentValues;
function withDefaultNestableContentValues(customType, content) {
return Object.entries(customType).reduce((updatedContent, [widgetKey, widgetDef]) => {
if (content[widgetKey] === undefined) {
const defaultValue = (0, nestable_1.NestableContentDefaultValue)(widgetDef);
return defaultValue !== undefined
? {
...updatedContent,
[widgetKey]: defaultValue,
}
: updatedContent;
}
return updatedContent;
}, content);
}
exports.withDefaultNestableContentValues = withDefaultNestableContentValues;
function repeatableContentWithDefaultNestableContentValues(fields, content) {
const groupKeys = Object.entries(fields);
return content.map((contentItem) => {
return groupKeys.reduce((updatedContentItem, [key, fieldDef]) => {
var _a;
const maybeContentItemValue = contentItem.value.find(([_key]) => _key === key);
if (fieldDef.type === "Group") {
const defaultValue = (0, GroupContent_1.groupContentWithDefaultValues)(fieldDef, (_a = maybeContentItemValue === null || maybeContentItemValue === void 0 ? void 0 : maybeContentItemValue[1]) !== null && _a !== void 0 ? _a : {
__TYPE__: GroupContent_1.GroupContentType,
value: [],
});
return {
...updatedContentItem,
value: [
// Since there was default content for the group, we need to remove the old one
...updatedContentItem.value.filter(([_key]) => _key !== key),
...(defaultValue ? [[key, defaultValue]] : []),
],
};
}
else if (!maybeContentItemValue) {
const defaultValue = (0, nestable_1.NestableContentDefaultValue)(fieldDef);
return {
...updatedContentItem,
value: [
...updatedContentItem.value,
...(defaultValue ? [[key, defaultValue]] : []),
],
};
}
return updatedContentItem;
}, contentItem);
});
}
exports.repeatableContentWithDefaultNestableContentValues = repeatableContentWithDefaultNestableContentValues;