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

72 lines
2.0 KiB
TypeScript
Raw Normal View History

2025-01-09 18:07:02 +02:00
import server from "./server.js";
import froca from "./froca.js";
import FNote from "../entities/fnote.js";
import type { AttributeRow } from "./load_results.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`, {
2025-01-09 18:07:02 +02:00
type: "label",
2020-10-23 00:11:44 +02:00
name: name,
value: value
});
}
async function setLabel(noteId: string, name: string, value: string = "") {
await server.put(`notes/${noteId}/set-attribute`, {
2025-01-09 18:07:02 +02:00
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
/**
* @returns - 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
*/
2025-01-07 12:34:10 +02:00
function isAffecting(attrRow: AttributeRow, affectedNote: FNote | null | undefined) {
2021-08-25 22:49:24 +02:00
if (!affectedNote || !attrRow) {
return false;
}
const attrNote = attrRow.noteId && froca.notes[attrRow.noteId];
2021-08-25 22:49:24 +02:00
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.
//@ts-ignore
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
2025-01-09 18:07:02 +02:00
};