alinea
Version:
Headless git-based CMS
52 lines (50 loc) • 1.92 kB
JavaScript
import "../../chunks/chunk-NZLE2WMY.js";
// src/picker/entry/EntryPicker.ts
import { Reference } from "alinea/core/Reference";
import { Type, type } from "alinea/core/Type";
import { ListRow } from "alinea/core/shape/ListShape";
import { RecordShape } from "alinea/core/shape/RecordShape";
import { ScalarShape } from "alinea/core/shape/ScalarShape";
import { assign, keys } from "alinea/core/util/Objects";
import { EntryReference } from "./EntryReference.js";
function entryPicker(options) {
const fieldType = Type.isType(options.fields) ? options.fields : options.fields && type("Entry fields", { fields: options.fields });
const extra = fieldType && Type.shape(fieldType);
return {
shape: new RecordShape("Entry", {
[Reference.id]: new ScalarShape("Id"),
[Reference.type]: new ScalarShape("Type"),
[EntryReference.entry]: new ScalarShape("Entry")
}).concat(extra),
fields: fieldType,
label: options.label || "Page link",
handlesMultiple: true,
options,
async postProcess(row, loader) {
const {
[Reference.id]: id,
[Reference.type]: type2,
[EntryReference.entry]: entryId,
[ListRow.index]: index,
...fields
} = row;
for (const key of keys(fields)) delete row[key];
row.fields = fields;
if (!entryId) return;
const linkIds = [entryId];
if (!options.selection) return;
const [extra2] = await loader.resolveLinks(options.selection, linkIds);
if (type2 !== "image") return assign(row, extra2);
if (!extra2) return row;
const { src: location, previewUrl, filePath, ...rest } = extra2;
if (!previewUrl) return assign(row, extra2, { src: location });
const locationAvailable = loader.includedAtBuild(filePath);
const src = locationAvailable ? location : previewUrl;
row.src = src;
assign(row, rest);
}
};
}
export {
entryPicker
};