Files
Trilium/src/public/app/services/attributes.ts

70 lines
1.9 KiB
TypeScript
Raw Normal View History

2020-10-23 00:11:44 +02:00
import server from './server.js';
2021-08-25 22:49:24 +02:00
import froca from './froca.js';
import FNote from '../entities/fnote.js';
2020-10-23 00:11:44 +02:00
async function addLabel(noteId: string, name: string, value: string = "") {
2020-10-23 00:11:44 +02:00
await server.put(`notes/${noteId}/attribute`, {
type: 'label',
name: name,
value: value
});
}
async function setLabel(noteId: string, name: string, value: string = "") {
await server.put(`notes/${noteId}/set-attribute`, {
type: 'label',
name: name,
value: value
});
}
async function removeAttributeById(noteId: string, attributeId: string) {
2020-10-23 00:11:44 +02:00
await server.remove(`notes/${noteId}/attributes/${attributeId}`);
}
2021-08-25 22:49:24 +02:00
/**
2023-01-05 23:38:41 +01:00
* @returns {boolean} - returns true if this attribute has the potential to influence the note in the argument.
2021-08-25 22:49:24 +02:00
* That can happen in multiple ways:
* 1. attribute is owned by the note
* 2. attribute is owned by the template of the note
* 3. attribute is owned by some note's ancestor and is inheritable
*/
function isAffecting(this: { isInheritable: boolean }, attrRow: { noteId: string }, affectedNote: FNote) {
2021-08-25 22:49:24 +02:00
if (!affectedNote || !attrRow) {
return false;
}
const attrNote = froca.notes[attrRow.noteId];
if (!attrNote) {
// the note (owner of the attribute) is not even loaded into the cache, so it should not affect anything else
2021-08-25 22:49:24 +02:00
return false;
}
2023-01-06 20:31:55 +01:00
const owningNotes = [affectedNote, ...affectedNote.getNotesToInheritAttributesFrom()];
2021-08-25 22:49:24 +02:00
for (const owningNote of owningNotes) {
if (owningNote.noteId === attrNote.noteId) {
return true;
}
}
// TODO: This doesn't seem right.
2021-08-25 22:49:24 +02:00
if (this.isInheritable) {
for (const owningNote of owningNotes) {
2023-02-01 22:55:31 +01:00
if (owningNote.hasAncestor(attrNote.noteId, true)) {
2021-08-25 22:49:24 +02:00
return true;
}
}
}
return false;
}
2020-10-23 00:11:44 +02:00
export default {
addLabel,
setLabel,
2021-08-25 22:49:24 +02:00
removeAttributeById,
isAffecting
2020-10-23 00:11:44 +02:00
}