alinea
Version:
Headless git-based CMS
174 lines (172 loc) • 4.77 kB
JavaScript
import {
object,
string
} from "../chunks/chunk-WD7H5L2L.js";
import "../chunks/chunk-NZLE2WMY.js";
// src/core/Type.ts
import { Field } from "./Field.js";
import { getType, hasType, internalType } from "./Internal.js";
import { Section, section } from "./Section.js";
import { RecordShape } from "./shape/RecordShape.js";
import { isValidIdentifier } from "./util/Identifiers.js";
import { entries, fromEntries, keys, values } from "./util/Objects.js";
var Type;
((Type2) => {
function label(type2) {
return getType(type2).label;
}
Type2.label = label;
function contains(type2) {
return getType(type2).contains ?? [];
}
Type2.contains = contains;
function insertOrder(type2) {
return getType(type2).insertOrder ?? "free";
}
Type2.insertOrder = insertOrder;
function isHidden(type2) {
return Boolean(getType(type2).hidden);
}
Type2.isHidden = isHidden;
function shape(type2) {
return getType(type2).shape;
}
Type2.shape = shape;
function searchableText(type2, value) {
return shape(type2).searchableText(value).trim();
}
Type2.searchableText = searchableText;
function fields(type2) {
return getType(type2).allFields;
}
function sections(type2) {
return getType(type2).sections;
}
Type2.sections = sections;
function isContainer(type2) {
return Boolean(getType(type2).contains);
}
Type2.isContainer = isContainer;
function field(type2, name) {
return getType(type2).allFields[name];
}
Type2.field = field;
function isType(type2) {
return Boolean(type2 && hasType(type2));
}
Type2.isType = isType;
function sharedData(type2, entryData) {
const res = {};
for (const [key, field2] of entries(fields(type2))) {
if (Field.options(field2).shared) res[key] = entryData[key];
}
if (keys(res).length === 0) return void 0;
return res;
}
Type2.sharedData = sharedData;
function initialValue(type2) {
const res = {};
for (const [key, field2] of entries(fields(type2))) {
res[key] = Field.initialValue(field2);
}
return res;
}
Type2.initialValue = initialValue;
function preview(type2) {
return getType(type2).preview;
}
Type2.preview = preview;
const TypeOptions = object({
view: string.optional,
summaryRow: string.optional,
summaryThumb: string.optional
});
function validate(type2) {
TypeOptions(getType(type2));
for (const [key, field2] of entries(fields(type2))) {
if (!isValidIdentifier(key))
throw new Error(
`Invalid field name "${key}" in Type "${label(
type2
)}", must match [A-Za-z][A-Za-z0-9_]*`
);
}
}
Type2.validate = validate;
function referencedViews(type2) {
const { view, summaryRow, summaryThumb } = getType(type2);
return [
view,
summaryRow,
summaryThumb,
...viewsOfDefinition(getType(type2).fields)
].filter((v) => typeof v === "string");
}
Type2.referencedViews = referencedViews;
})(Type || (Type = {}));
function viewsOfDefinition(definition) {
return values(definition).flatMap((value) => {
if (Field.isField(value)) return Field.referencedViews(value);
if (Section.isSection(value))
return Section.referencedViews(value).concat(
viewsOfDefinition(Section.fields(value))
);
return [];
});
}
function fieldsOfDefinition(definition) {
return entries(definition).flatMap(([key, value]) => {
if (Field.isField(value)) return [[key, value]];
if (Section.isSection(value)) return entries(Section.fields(value));
return [];
});
}
function type(label, config) {
const sections = [];
let current = {};
const addCurrent = () => {
if (keys(current).length > 0) sections.push(section({ definition: current }));
current = {};
};
const fields = [];
if (typeof config.fields !== "object") {
throw new Error("Type fields must be an object");
}
for (const [key, value] of entries(config.fields)) {
if (Field.isField(value)) {
current[key] = value;
fields.push([key, value]);
} else if (Section.isSection(value)) {
addCurrent();
sections.push(value);
for (const [key2, field] of entries(Section.fields(value))) {
fields.push([key2, field]);
}
}
}
addCurrent();
const allFields = fromEntries(fields);
const instance = {
...allFields,
[internalType]: {
...config,
allFields,
sections,
shape: new RecordShape(
label,
fromEntries(
fieldsOfDefinition(config.fields).map(([key, field]) => {
return [key, Field.shape(field)];
})
)
),
label
}
};
Type.validate(instance);
return instance;
}
export {
Type,
type
};