Files
lucent-laravel/front/js/svelte/records/Preview.js
T

37 lines
946 B
JavaScript
Raw Normal View History

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));
}
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
}