alinea
Version:
Headless git-based CMS
73 lines (71 loc) • 1.94 kB
JavaScript
import "../../chunks/chunk-NZLE2WMY.js";
// src/field/richtext/ReferenceLink.ts
import { createId } from "alinea/core/Id";
import { Reference } from "alinea/core/Reference";
import { EntryReference } from "alinea/picker/entry/EntryReference";
import { UrlReference } from "alinea/picker/url";
function referenceToAttributes(reference) {
switch (reference[Reference.type]) {
case "url": {
const ref = reference;
return {
"data-id": ref[Reference.id],
"data-entry": void 0,
"data-link": "url",
href: ref._url,
target: ref._target
};
}
case "entry": {
const ref = reference;
return {
"data-id": ref[Reference.id],
"data-entry": ref[EntryReference.entry],
"data-link": "entry",
href: void 0,
target: void 0
};
}
case "file": {
const ref = reference;
return {
"data-id": ref[Reference.id],
"data-entry": ref[EntryReference.entry],
"data-link": "file",
href: void 0,
target: void 0
};
}
default:
throw new Error(`Unexpected reference type: ${reference[Reference.type]}`);
}
}
function attributesToReference(attributes) {
const id = attributes["data-id"];
if (!id) {
if (attributes.href)
return {
[]: createId(),
[]: "url",
[]: attributes.href,
[]: attributes.target
};
return;
}
if (attributes["data-entry"])
return {
[]: id,
[]: attributes["data-link"] === "file" ? "file" : "entry",
[]: attributes["data-entry"]
};
return {
[]: id,
[]: "url",
[]: attributes.href,
[]: attributes.target
};
}
export {
attributesToReference,
referenceToAttributes
};