@prismicio/types-internal
Version:
Prismic types for Custom Types and Prismic Data
64 lines (63 loc) • 2.46 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.traverseCompositeSlice = exports.isCompositeSlice = exports.CompositeSlice = exports.CompositeSliceConfig = exports.CompositeSliceType = void 0;
const tslib_1 = require("tslib");
const t = (0, tslib_1.__importStar)(require("io-ts"));
const common_1 = require("../../../common");
const validators_1 = require("../../../validators");
const NestableWidget_1 = require("../nestable/NestableWidget");
exports.CompositeSliceType = "Slice";
exports.CompositeSliceConfig = t.exact(t.partial({
label: validators_1.StringOrNull,
}));
exports.CompositeSlice = t.exact(t.intersection([
t.type({
type: t.literal(exports.CompositeSliceType),
}),
t.partial({
fieldset: validators_1.StringOrNull,
description: t.string,
icon: t.string,
display: t.string,
"non-repeat": t.record(common_1.WidgetKey, NestableWidget_1.NestableWidget),
repeat: t.record(common_1.WidgetKey, NestableWidget_1.NestableWidget),
config: exports.CompositeSliceConfig,
}),
]));
function isCompositeSlice(slice) {
return slice.type === "Slice";
}
exports.isCompositeSlice = isCompositeSlice;
function traverseCompositeSlice(args) {
var _a, _b;
const { path: prevPath, slice, onField } = args;
let nonRepeat;
for (const [key, field] of Object.entries((_a = slice["non-repeat"]) !== null && _a !== void 0 ? _a : {})) {
const path = [...prevPath, "non-repeat", key];
const newField = onField({ path, key, field });
if (field !== newField) {
if (!nonRepeat)
nonRepeat = { ...slice["non-repeat"] };
nonRepeat[key] = newField;
}
}
let repeat;
for (const [key, field] of Object.entries((_b = slice.repeat) !== null && _b !== void 0 ? _b : {})) {
const path = [...prevPath, "repeat", key];
const newField = onField({ path, key, field });
if (field !== newField) {
if (!repeat)
repeat = { ...slice.repeat };
repeat[key] = newField;
}
}
// returns the traversed model instead of a new one if it didn't change
return repeat || nonRepeat
? {
...slice,
...(nonRepeat && { "non-repeat": nonRepeat }),
...(repeat && { repeat }),
}
: slice;
}
exports.traverseCompositeSlice = traverseCompositeSlice;