UNPKG

@prismicio/types-internal

Version:
131 lines (130 loc) 4.42 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.defaultCtx = exports.getFieldCtx = exports.LegacyContentCtx = exports.FieldOrSliceType = void 0; const tslib_1 = require("tslib"); const t = (0, tslib_1.__importStar)(require("io-ts")); const uuid_1 = require("uuid"); const widgets_1 = require("../customtypes/widgets"); exports.FieldOrSliceType = t.union([ t.union([widgets_1.FieldType, widgets_1.SlicesTypes]), t.literal("Repeatable.Link"), ]); class LegacyContentCtx { constructor({ fieldKey, contentKey, contentPath, fieldPath, allTypes, allKeys, }) { var _a; Object.defineProperty(this, "fieldKey", { enumerable: true, configurable: true, writable: true, value: void 0 }); Object.defineProperty(this, "prefixedKey", { enumerable: true, configurable: true, writable: true, value: void 0 }); Object.defineProperty(this, "keyOfType", { enumerable: true, configurable: true, writable: true, value: void 0 }); Object.defineProperty(this, "keyOfKey", { enumerable: true, configurable: true, writable: true, value: void 0 }); Object.defineProperty(this, "fieldPath", { enumerable: true, configurable: true, writable: true, value: void 0 }); Object.defineProperty(this, "fieldType", { enumerable: true, configurable: true, writable: true, value: void 0 }); Object.defineProperty(this, "allTypes", { enumerable: true, configurable: true, writable: true, value: void 0 }); Object.defineProperty(this, "allKeys", { enumerable: true, configurable: true, writable: true, value: void 0 }); Object.defineProperty(this, "contentKey", { enumerable: true, configurable: true, writable: true, value: void 0 }); Object.defineProperty(this, "contentPath", { enumerable: true, configurable: true, writable: true, value: void 0 }); Object.defineProperty(this, "fieldContentKey", { enumerable: true, configurable: true, writable: true, value: void 0 }); this.fieldKey = fieldKey; this.contentKey = contentKey || fieldKey; this.fieldPath = fieldPath || []; this.contentPath = contentPath || []; this.allTypes = allTypes || new Map(); this.allKeys = allKeys || new Map(); const prefixedKey = Array.of(this.fieldPath, [this.fieldKey]) .flat() .join("."); const prefixedContentKey = Array.of(this.contentPath, [this.contentKey]) .flat() .join("."); this.prefixedKey = prefixedKey; this.keyOfType = `${prefixedKey}_TYPE`; this.keyOfKey = `${prefixedContentKey}_KEY`; this.fieldType = this.allTypes.get(this.prefixedKey); this.fieldContentKey = (_a = this.allKeys.get(prefixedContentKey)) !== null && _a !== void 0 ? _a : (0, uuid_1.v4)(); } withContentKey(contentKey) { return new LegacyContentCtx({ ...this, contentPath: [...this.contentPath, this.contentKey], contentKey, }); } } exports.LegacyContentCtx = LegacyContentCtx; function getFieldCtx({ fieldKey, contentKey: contentKeyParam, ctx, prefixes, }) { const contentKey = contentKeyParam || fieldKey; return new LegacyContentCtx({ fieldKey, contentKey, contentPath: [...ctx.contentPath, ctx.contentKey, ...(prefixes || [])], fieldPath: [...ctx.fieldPath, ctx.fieldKey, ...(prefixes || [])], allTypes: ctx.allTypes, allKeys: ctx.allKeys, }); } exports.getFieldCtx = getFieldCtx; function defaultCtx(key, allTypes = new Map(), allKeys = new Map()) { return new LegacyContentCtx({ fieldKey: key, contentKey: key, contentPath: [], fieldPath: [], allTypes, allKeys, }); } exports.defaultCtx = defaultCtx;