mirror of
https://github.com/zadam/trilium.git
synced 2025-11-02 19:36:12 +01:00
implemented mirror relations
This commit is contained in:
@@ -3,9 +3,10 @@ const scriptService = require('./script');
|
||||
const treeService = require('./tree');
|
||||
const messagingService = require('./messaging');
|
||||
const log = require('./log');
|
||||
const Attribute = require('../entities/attribute');
|
||||
|
||||
async function runAttachedRelations(note, relationName, originEntity) {
|
||||
const runRelations = (await note.getRelations()).filter(relation => relation.name === relationName);
|
||||
const runRelations = await note.getRelations(relationName);
|
||||
|
||||
for (const relation of runRelations) {
|
||||
const scriptNote = await relation.getTargetNote();
|
||||
@@ -56,4 +57,54 @@ eventService.subscribe(eventService.ENTITY_CREATED, async ({ entityName, entity
|
||||
|
||||
eventService.subscribe(eventService.CHILD_NOTE_CREATED, async ({ parentNote, childNote }) => {
|
||||
await runAttachedRelations(parentNote, 'runOnChildNoteCreation', childNote);
|
||||
});
|
||||
|
||||
async function processMirrorRelations(entityName, entity, handler) {
|
||||
if (entityName === 'attributes' && entity.type === 'relation') {
|
||||
const note = await entity.getNote();
|
||||
const attributes = (await note.getAttributes(entity.name)).filter(relation => relation.type === 'relation-definition');
|
||||
|
||||
for (const attribute of attributes) {
|
||||
const definition = attribute.value;
|
||||
|
||||
if (definition.mirrorRelation && definition.mirrorRelation.trim()) {
|
||||
const targetNote = await entity.getTargetNote();
|
||||
|
||||
await handler(definition, note, targetNote);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
eventService.subscribe(eventService.ENTITY_CHANGED, async ({ entityName, entity }) => {
|
||||
await processMirrorRelations(entityName, entity, async (definition, note, targetNote) => {
|
||||
// we need to make sure that also target's mirror attribute exists and if note, then create it
|
||||
if (!await targetNote.hasRelation(definition.mirrorRelation)) {
|
||||
await new Attribute({
|
||||
noteId: targetNote.noteId,
|
||||
type: 'relation',
|
||||
name: definition.mirrorRelation,
|
||||
value: note.noteId,
|
||||
isInheritable: entity.isInheritable
|
||||
}).save();
|
||||
|
||||
targetNote.invalidateAttributeCache();
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
eventService.subscribe(eventService.ENTITY_DELETED, async ({ entityName, entity }) => {
|
||||
await processMirrorRelations(entityName, entity, async (definition, note, targetNote) => {
|
||||
// if one mirror attribute is deleted then the other should be deleted as well
|
||||
const relations = await targetNote.getRelations(definition.mirrorRelation);
|
||||
|
||||
for (const relation of relations) {
|
||||
relation.isDeleted = true;
|
||||
await relation.save();
|
||||
}
|
||||
|
||||
if (relations.length > 0) {
|
||||
targetNote.invalidateAttributeCache();
|
||||
}
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user