mirror of
https://github.com/zadam/trilium.git
synced 2025-11-09 14:55:50 +01:00
split out attribute_editor widget, WIP
This commit is contained in:
60
src/public/app/services/attribute_renderer.js
Normal file
60
src/public/app/services/attribute_renderer.js
Normal file
@@ -0,0 +1,60 @@
|
||||
import ws from "./ws.js";
|
||||
|
||||
function renderAttribute(attribute, $container, renderIsInheritable) {
|
||||
const isInheritable = renderIsInheritable && attribute.isInheritable ? `(inheritable)` : '';
|
||||
|
||||
if (attribute.type === 'label') {
|
||||
$container.append(document.createTextNode('#' + attribute.name + isInheritable));
|
||||
|
||||
if (attribute.value) {
|
||||
$container.append('=');
|
||||
$container.append(document.createTextNode(formatValue(attribute.value)));
|
||||
}
|
||||
|
||||
$container.append(' ');
|
||||
} else if (attribute.type === 'relation') {
|
||||
if (attribute.isAutoLink) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (attribute.value) {
|
||||
$container.append(document.createTextNode('~' + attribute.name + isInheritable + "="));
|
||||
$container.append(createNoteLink(attribute.value));
|
||||
$container.append(" ");
|
||||
} else {
|
||||
ws.logError(`Relation ${attribute.attributeId} has empty target`);
|
||||
}
|
||||
} else {
|
||||
ws.logError("Unknown attr type: " + attribute.type);
|
||||
}
|
||||
}
|
||||
|
||||
function formatValue(val) {
|
||||
if (/^[\p{L}\p{N}\-_,.]+$/u.test(val)) {
|
||||
return val;
|
||||
}
|
||||
else if (!val.includes('"')) {
|
||||
return '"' + val + '"';
|
||||
}
|
||||
else if (!val.includes("'")) {
|
||||
return "'" + val + "'";
|
||||
}
|
||||
else if (!val.includes("`")) {
|
||||
return "`" + val + "`";
|
||||
}
|
||||
else {
|
||||
return '"' + val.replace(/"/g, '\\"') + '"';
|
||||
}
|
||||
}
|
||||
|
||||
function createNoteLink(noteId) {
|
||||
return $("<a>", {
|
||||
href: '#' + noteId,
|
||||
class: 'reference-link',
|
||||
'data-note-path': noteId
|
||||
});
|
||||
}
|
||||
|
||||
export default {
|
||||
renderAttribute
|
||||
}
|
||||
Reference in New Issue
Block a user