2020-07-17 06:08:28 +08:00
|
|
|
import ws from "./ws.js";
|
2020-09-09 03:45:07 +08:00
|
|
|
import treeCache from "./tree_cache.js";
|
2020-07-17 06:08:28 +08:00
|
|
|
|
2020-09-09 03:45:07 +08:00
|
|
|
async function renderAttribute(attribute, renderIsInheritable) {
|
2020-07-17 06:08:28 +08:00
|
|
|
const isInheritable = renderIsInheritable && attribute.isInheritable ? `(inheritable)` : '';
|
2020-09-09 03:45:07 +08:00
|
|
|
const $attr = $("<span>");
|
2020-07-17 06:08:28 +08:00
|
|
|
|
|
|
|
if (attribute.type === 'label') {
|
2020-09-09 03:45:07 +08:00
|
|
|
$attr.append(document.createTextNode('#' + attribute.name + isInheritable));
|
2020-07-17 06:08:28 +08:00
|
|
|
|
|
|
|
if (attribute.value) {
|
2020-09-09 03:45:07 +08:00
|
|
|
$attr.append('=');
|
|
|
|
$attr.append(document.createTextNode(formatValue(attribute.value)));
|
2020-07-17 06:08:28 +08:00
|
|
|
}
|
|
|
|
} else if (attribute.type === 'relation') {
|
|
|
|
if (attribute.isAutoLink) {
|
2020-09-09 03:45:07 +08:00
|
|
|
return $attr;
|
2020-07-17 06:08:28 +08:00
|
|
|
}
|
|
|
|
|
2020-09-05 05:35:10 +08:00
|
|
|
// when the relation has just been created then it might not have a value
|
2020-07-17 06:08:28 +08:00
|
|
|
if (attribute.value) {
|
2020-09-09 03:45:07 +08:00
|
|
|
$attr.append(document.createTextNode('~' + attribute.name + isInheritable + "="));
|
|
|
|
$attr.append(await createNoteLink(attribute.value));
|
2020-07-17 06:08:28 +08:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
ws.logError("Unknown attr type: " + attribute.type);
|
|
|
|
}
|
2020-09-09 03:45:07 +08:00
|
|
|
|
|
|
|
return $attr;
|
2020-07-17 06:08:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
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, '\\"') + '"';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-09 03:45:07 +08:00
|
|
|
async function createNoteLink(noteId) {
|
|
|
|
const note = await treeCache.getNote(noteId);
|
|
|
|
|
|
|
|
if (!note) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $("<a>", {
|
2020-07-17 06:08:28 +08:00
|
|
|
href: '#' + noteId,
|
|
|
|
class: 'reference-link',
|
|
|
|
'data-note-path': noteId
|
2020-09-09 03:45:07 +08:00
|
|
|
})
|
|
|
|
.text(note.title);
|
|
|
|
}
|
2020-08-27 20:54:56 +08:00
|
|
|
|
2020-09-09 03:45:07 +08:00
|
|
|
async function renderAttributes(attributes, renderIsInheritable) {
|
|
|
|
const $container = $("<span>");
|
|
|
|
|
|
|
|
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(" ");
|
|
|
|
}
|
|
|
|
}
|
2020-08-27 20:54:56 +08:00
|
|
|
|
2020-09-09 03:45:07 +08:00
|
|
|
return $container;
|
2020-07-17 06:08:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
export default {
|
2020-09-09 03:45:07 +08:00
|
|
|
renderAttribute,
|
|
|
|
renderAttributes
|
2020-07-17 06:08:28 +08:00
|
|
|
}
|