Notes/src/public/app/services/attribute_renderer.ts

107 lines
3.2 KiB
TypeScript
Raw Normal View History

2020-07-17 00:08:28 +02:00
import ws from "./ws.js";
2021-04-16 23:01:56 +02:00
import froca from "./froca.js";
import type FAttribute from "../entities/fattribute.js";
import type FNote from "../entities/fnote.js";
2020-07-17 00:08:28 +02:00
async function renderAttribute(attribute: FAttribute, renderIsInheritable: boolean) {
2025-01-09 18:07:02 +02:00
const isInheritable = renderIsInheritable && attribute.isInheritable ? `(inheritable)` : "";
const $attr = $("<span>");
2020-07-17 00:08:28 +02:00
2025-01-09 18:07:02 +02:00
if (attribute.type === "label") {
$attr.append(document.createTextNode(`#${attribute.name}${isInheritable}`));
2020-07-17 00:08:28 +02:00
if (attribute.value) {
2025-01-09 18:07:02 +02:00
$attr.append("=");
$attr.append(document.createTextNode(formatValue(attribute.value)));
2020-07-17 00:08:28 +02:00
}
2025-01-09 18:07:02 +02:00
} else if (attribute.type === "relation") {
2020-07-17 00:08:28 +02:00
if (attribute.isAutoLink) {
return $attr;
2020-07-17 00:08:28 +02:00
}
2023-05-05 23:41:11 +02:00
// when the relation has just been created, then it might not have a value
2020-07-17 00:08:28 +02:00
if (attribute.value) {
$attr.append(document.createTextNode(`~${attribute.name}${isInheritable}=`));
const link = await createLink(attribute.value);
if (link) {
$attr.append(link);
}
2020-07-17 00:08:28 +02:00
}
} else {
ws.logError(`Unknown attr type: ${attribute.type}`);
2020-07-17 00:08:28 +02:00
}
return $attr;
2020-07-17 00:08:28 +02:00
}
function formatValue(val: string) {
2020-07-17 00:08:28 +02:00
if (/^[\p{L}\p{N}\-_,.]+$/u.test(val)) {
return val;
2025-01-09 18:07:02 +02:00
} else if (!val.includes('"')) {
return `"${val}"`;
2025-01-09 18:07:02 +02:00
} else if (!val.includes("'")) {
return `'${val}'`;
2025-01-09 18:07:02 +02:00
} else if (!val.includes("`")) {
return `\`${val}\``;
2025-01-09 18:07:02 +02:00
} else {
return `"${val.replace(/"/g, '\\"')}"`;
2020-07-17 00:08:28 +02:00
}
}
async function createLink(noteId: string) {
2021-04-16 22:57:37 +02:00
const note = await froca.getNote(noteId);
if (!note) {
return;
}
return $("<a>", {
href: `#root/${noteId}`,
2025-01-09 18:07:02 +02:00
class: "reference-link"
}).text(note.title);
}
async function renderAttributes(attributes: FAttribute[], renderIsInheritable: boolean) {
const $container = $('<span class="rendered-note-attributes">');
for (let i = 0; i < attributes.length; i++) {
const attribute = attributes[i];
const $attr = await renderAttribute(attribute, renderIsInheritable);
$container.append($attr.html()); // .html() to get only inner HTML, we don't want any spans
if (i < attributes.length - 1) {
$container.append(" ");
}
}
return $container;
2020-07-17 00:08:28 +02:00
}
2025-03-02 20:47:57 +01:00
const HIDDEN_ATTRIBUTES = ["originalFileName", "fileSize", "template", "inherit", "cssClass", "iconClass", "pageSize", "viewType", "geolocation", "docName"];
async function renderNormalAttributes(note: FNote) {
const promotedDefinitionAttributes = note.getPromotedDefinitionAttributes();
let attrs = note.getAttributes();
if (promotedDefinitionAttributes.length > 0) {
2025-01-09 18:07:02 +02:00
attrs = attrs.filter((attr) => !!promotedDefinitionAttributes.find((promAttr) => promAttr.isDefinitionFor(attr)));
} else {
attrs = attrs.filter((attr) => !attr.isDefinition() && !attr.isAutoLink && !HIDDEN_ATTRIBUTES.includes(attr.name) && attr.noteId === note.noteId);
}
const $renderedAttributes = await renderAttributes(attrs, false);
return {
count: attrs.length,
$renderedAttributes
2025-01-09 18:07:02 +02:00
};
}
2020-07-17 00:08:28 +02:00
export default {
renderAttribute,
renderAttributes,
renderNormalAttributes
2025-01-09 18:07:02 +02:00
};