2023-10-02 23:10:49 +03:00
|
|
|
import Mustache from "mustache";
|
|
|
|
|
import {stripHtml} from "../../helpers";
|
2024-03-21 22:33:41 +02:00
|
|
|
import {getContext} from "svelte";
|
2023-10-02 23:10:49 +03:00
|
|
|
|
2024-03-24 13:46:34 +02:00
|
|
|
export function previewTitle(record) {
|
2024-03-21 22:33:41 +02:00
|
|
|
const channel = getContext("channel");
|
2024-03-25 21:26:21 +02:00
|
|
|
let schema = channel.schemas.find((aSchema) => aSchema.name === record?.schema);
|
2023-10-02 23:10:49 +03:00
|
|
|
|
|
|
|
|
if (!schema?.titleTemplate) {
|
2024-03-25 21:26:21 +02:00
|
|
|
return noTemplate(schema, record);
|
2023-10-02 23:10:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let template = Mustache.parse(schema.titleTemplate);
|
2024-03-25 21:26:21 +02:00
|
|
|
let render = Mustache.render(schema.titleTemplate, record.data);
|
2023-10-02 23:10:49 +03:00
|
|
|
|
|
|
|
|
if (!render || render === "") {
|
2024-03-25 21:26:21 +02:00
|
|
|
return noTemplate(schema, record);
|
2023-10-02 23:10:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return stripHtml(render.slice(0, 300));
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-03 16:25:59 +03:00
|
|
|
export function previewEdgeTitle(edge) {
|
|
|
|
|
const channel = getContext("channel");
|
|
|
|
|
let edgeSchemaName = channel.schemas
|
|
|
|
|
.find((aSchema) => aSchema.name === edge?.sourceSchema)
|
|
|
|
|
.fields.find(f => f.name === edge.field).data;
|
|
|
|
|
let schema = channel.schemas.find((aSchema) => aSchema.name === edgeSchemaName);
|
|
|
|
|
if (!schema?.titleTemplate) {
|
|
|
|
|
return noTemplate(schema, edge);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let template = Mustache.parse(schema.titleTemplate);
|
|
|
|
|
let render = Mustache.render(schema.titleTemplate, edge.data);
|
|
|
|
|
|
|
|
|
|
if (!render || render === "") {
|
|
|
|
|
return noTemplate(schema, edge);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return stripHtml(render.slice(0, 300));
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-02 23:10:49 +03:00
|
|
|
function noTemplate(schema, record) {
|
|
|
|
|
if (schema?.type === "files") {
|
|
|
|
|
return record._file.path;
|
|
|
|
|
}
|
2023-10-26 17:45:23 +03:00
|
|
|
|
2024-03-24 13:46:34 +02:00
|
|
|
let title = stripHtml(
|
2023-10-02 23:10:49 +03:00
|
|
|
record?.data[schema.fields.filter((f) => f.info.name === "text")[0]?.name]
|
|
|
|
|
).slice(0, 300);
|
2024-03-24 13:46:34 +02:00
|
|
|
if (title === "") {
|
2023-10-26 17:45:23 +03:00
|
|
|
return "Untitled";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return title;
|
2023-10-02 23:10:49 +03:00
|
|
|
}
|