UNPKG

alinea

Version:
74 lines (72 loc) 2.38 kB
import "../../chunks/chunk-NZLE2WMY.js"; // src/field/link/LinkField.ts import { ListField } from "alinea/core/field/ListField"; import { UnionField } from "alinea/core/field/UnionField"; import { Reference } from "alinea/core/Reference"; import { ListRow } from "alinea/core/shape/ListShape"; import { entries, fromEntries } from "alinea/core/util/Objects"; import { viewKeys } from "alinea/dashboard/editor/ViewKeys"; var LinkField = class extends UnionField { first(query) { return { edge: "entrySingle", first: true, field: this, ...query }; } }; function createLink(label, options) { const pickers = entries(options.pickers); const schema = fromEntries( pickers.filter(([type, picker]) => picker.fields).map(([type, picker]) => [type, picker.fields]) ); const shapes = fromEntries( pickers.map(([type, picker]) => [type, picker.shape]) ); return new LinkField(schema, shapes, { options: { label, ...options }, async postProcess(value, loader) { const type = value[Reference.type]; const picker = options.pickers[type]; if (!picker) return; if (picker.postProcess) await picker.postProcess(value, loader); }, view: viewKeys.SingleLinkInput }); } var LinksField = class extends ListField { find(query) { return { edge: "entryMultiple", field: this, ...query }; } first(query) { return { edge: "entryMultiple", first: true, field: this, ...query }; } count(query) { return { edge: "entryMultiple", count: true, field: this, ...query }; } }; function createLinks(label, options) { const pickers = entries(options.pickers); const schema = fromEntries( pickers.filter(([type, picker]) => picker.fields).map(([type, picker]) => [type, picker.fields]) ); const shapes = fromEntries( pickers.map(([type, picker]) => [type, picker.shape]) ); return new LinksField(schema, shapes, { options: { label, ...options }, async postProcess(rows, loader) { const tasks = []; for (const row of rows) { const type = row[ListRow.type]; const picker = options.pickers[type]; if (!picker) continue; if (picker.postProcess) tasks.push(picker.postProcess(row, loader)); } await Promise.all(tasks); }, view: viewKeys.MultipleLinksInput }); } export { LinkField, LinksField, createLink, createLinks };