mirror of
https://github.com/zadam/trilium.git
synced 2025-03-01 14:22:32 +01:00
support for adding new relations from relation map with drag & drop
This commit is contained in:
parent
07cbc385e5
commit
2a129809b3
@ -8,7 +8,8 @@ const $addChildNotesButton = $("#relation-map-add-child-notes");
|
|||||||
|
|
||||||
let mapData;
|
let mapData;
|
||||||
let instance;
|
let instance;
|
||||||
let initDone = false;
|
// outside of mapData because they are not persisted in the note content
|
||||||
|
let relations;
|
||||||
|
|
||||||
const uniDirectionalOverlays = [
|
const uniDirectionalOverlays = [
|
||||||
[ "Arrow", {
|
[ "Arrow", {
|
||||||
@ -67,7 +68,7 @@ async function loadNotesAndRelations() {
|
|||||||
const noteIds = mapData.notes.map(note => note.id);
|
const noteIds = mapData.notes.map(note => note.id);
|
||||||
const data = await server.post("notes/relation-map", {noteIds});
|
const data = await server.post("notes/relation-map", {noteIds});
|
||||||
|
|
||||||
const relations = [];
|
relations = [];
|
||||||
|
|
||||||
for (const relation of data.relations) {
|
for (const relation of data.relations) {
|
||||||
const match = relations.find(rel =>
|
const match = relations.find(rel =>
|
||||||
@ -124,8 +125,6 @@ async function loadNotesAndRelations() {
|
|||||||
connection.getOverlay("label").setLabel(relation.name);
|
connection.getOverlay("label").setLabel(relation.name);
|
||||||
connection.canvas.setAttribute("data-connection-id", connection.id);
|
connection.canvas.setAttribute("data-connection-id", connection.id);
|
||||||
}
|
}
|
||||||
|
|
||||||
initDone = true;
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -165,28 +164,40 @@ async function initJsPlumb () {
|
|||||||
|
|
||||||
instance.registerConnectionType("biDirectional", { anchor:"Continuous", connector:"StateMachine", overlays: biDirectionalOverlays });
|
instance.registerConnectionType("biDirectional", { anchor:"Continuous", connector:"StateMachine", overlays: biDirectionalOverlays });
|
||||||
|
|
||||||
// instance.bind("connection", function (info) {
|
instance.bind("connection", async function (info, originalEvent) {
|
||||||
// const connection = info.connection;
|
// if there's no event, then this has been triggered programatically
|
||||||
// let name = "none";
|
if (!originalEvent) {
|
||||||
//
|
return;
|
||||||
// if (initDone) {
|
}
|
||||||
// name = prompt("Specify new connection label:");
|
|
||||||
//
|
|
||||||
// mapData.relations.push({
|
|
||||||
// connectionId: connection.id,
|
|
||||||
// source: connection.sourceId,
|
|
||||||
// target: connection.targetId,
|
|
||||||
// name: name
|
|
||||||
// });
|
|
||||||
//
|
|
||||||
// saveData();
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// connection.getOverlay("label").setLabel(name);
|
|
||||||
// });
|
|
||||||
|
|
||||||
jsPlumb.on($relationMapCanvas[0], "dblclick", function(e) {
|
const name = prompt("Specify new relation name:");
|
||||||
newNode(jsPlumbUtil.uuid(),"new", e.offsetX, e.offsetY);
|
|
||||||
|
if (!name || !name.trim()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const connection = info.connection;
|
||||||
|
|
||||||
|
const targetNoteId = connection.target.id;
|
||||||
|
const sourceNoteId = connection.source.id;
|
||||||
|
|
||||||
|
const existing = relations.some(rel =>
|
||||||
|
rel.targetNoteId === targetNoteId
|
||||||
|
&& rel.sourceNoteId === sourceNoteId
|
||||||
|
&& rel.name === name);
|
||||||
|
|
||||||
|
if (existing) {
|
||||||
|
alert("Connection '" + name + "' between these notes already exists.");
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
await server.put(`notes/${sourceNoteId}/relations/${name}/to/${targetNoteId}`);
|
||||||
|
|
||||||
|
relations.push({ targetNoteId, sourceNoteId, name });
|
||||||
|
|
||||||
|
connection.setType("uniDirectional");
|
||||||
|
connection.getOverlay("label").setLabel(name);
|
||||||
});
|
});
|
||||||
|
|
||||||
$relationMapCanvas.contextmenu({
|
$relationMapCanvas.contextmenu({
|
||||||
|
@ -107,11 +107,30 @@ async function getValuesForAttribute(req) {
|
|||||||
return await sql.getColumn("SELECT DISTINCT value FROM attributes WHERE isDeleted = 0 AND name = ? AND type = 'label' AND value != '' ORDER BY value", [attributeName]);
|
return await sql.getColumn("SELECT DISTINCT value FROM attributes WHERE isDeleted = 0 AND name = ? AND type = 'label' AND value != '' ORDER BY value", [attributeName]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async function createRelation(req) {
|
||||||
|
const sourceNoteId = req.params.noteId;
|
||||||
|
const targetNoteId = req.params.targetNoteId;
|
||||||
|
const name = req.params.name;
|
||||||
|
|
||||||
|
let attribute = await repository.getEntity(`SELECT * FROM attributes WHERE isDeleted = 0 AND noteId = ? AND type = 'relation' AND name = ? AND value = ?`, [sourceNoteId, name, targetNoteId]);
|
||||||
|
|
||||||
|
if (!attribute) {
|
||||||
|
attribute = new Attribute();
|
||||||
|
attribute.noteId = sourceNoteId;
|
||||||
|
attribute.name = name;
|
||||||
|
attribute.type = 'relation';
|
||||||
|
attribute.value = targetNoteId;
|
||||||
|
|
||||||
|
await attribute.save();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
updateNoteAttributes,
|
updateNoteAttributes,
|
||||||
updateNoteAttribute,
|
updateNoteAttribute,
|
||||||
deleteNoteAttribute,
|
deleteNoteAttribute,
|
||||||
getAttributeNames,
|
getAttributeNames,
|
||||||
getValuesForAttribute,
|
getValuesForAttribute,
|
||||||
getEffectiveNoteAttributes
|
getEffectiveNoteAttributes,
|
||||||
|
createRelation
|
||||||
};
|
};
|
@ -137,6 +137,7 @@ function register(app) {
|
|||||||
apiRoute(GET, '/api/notes/:noteId/attributes', attributesRoute.getEffectiveNoteAttributes);
|
apiRoute(GET, '/api/notes/:noteId/attributes', attributesRoute.getEffectiveNoteAttributes);
|
||||||
apiRoute(PUT, '/api/notes/:noteId/attributes', attributesRoute.updateNoteAttributes);
|
apiRoute(PUT, '/api/notes/:noteId/attributes', attributesRoute.updateNoteAttributes);
|
||||||
apiRoute(PUT, '/api/notes/:noteId/attribute', attributesRoute.updateNoteAttribute);
|
apiRoute(PUT, '/api/notes/:noteId/attribute', attributesRoute.updateNoteAttribute);
|
||||||
|
apiRoute(PUT, '/api/notes/:noteId/relations/:name/to/:targetNoteId', attributesRoute.createRelation);
|
||||||
apiRoute(DELETE, '/api/notes/:noteId/attributes/:attributeId', attributesRoute.deleteNoteAttribute);
|
apiRoute(DELETE, '/api/notes/:noteId/attributes/:attributeId', attributesRoute.deleteNoteAttribute);
|
||||||
apiRoute(GET, '/api/attributes/names', attributesRoute.getAttributeNames);
|
apiRoute(GET, '/api/attributes/names', attributesRoute.getAttributeNames);
|
||||||
apiRoute(GET, '/api/attributes/values/:attributeName', attributesRoute.getValuesForAttribute);
|
apiRoute(GET, '/api/attributes/values/:attributeName', attributesRoute.getValuesForAttribute);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user