trilium/src/public/javascripts/services/note_detail_relation_map.js

460 lines
13 KiB
JavaScript
Raw Normal View History

import server from "./server.js";
import noteDetailService from "./note_detail.js";
2018-10-30 15:53:30 +08:00
import linkService from "./link.js";
2018-10-18 17:46:07 +08:00
import libraryLoader from "./library_loader.js";
import treeService from "./tree.js";
const $noteDetailRelationMap = $("#note-detail-relation-map");
2018-10-18 17:53:01 +08:00
const $relationMapCanvas = $("#relation-map-canvas");
2018-10-21 16:26:14 +08:00
const $addChildNotesButton = $("#relation-map-add-child-notes");
const $createChildNote = $("#relation-map-create-child-note");
2018-10-31 03:22:05 +08:00
const $zoomInButton = $("#relation-map-zoom-in");
const $zoomOutButton = $("#relation-map-zoom-out");
2018-10-21 16:26:14 +08:00
let mapData;
let jsPlumbInstance;
// outside of mapData because they are not persisted in the note content
let relations;
let pzInstance;
2018-10-30 02:25:45 +08:00
const uniDirectionalOverlays = [
[ "Arrow", {
location: 1,
id: "arrow",
length: 14,
foldback: 0.8
} ],
2018-10-30 15:53:30 +08:00
[ "Label", { label: "", id: "label", cssClass: "connection-label" }]
2018-10-30 02:25:45 +08:00
];
const biDirectionalOverlays = [
[ "Arrow", {
location: 1,
id: "arrow",
length: 14,
foldback: 0.8
} ],
2018-10-30 15:53:30 +08:00
[ "Label", { label: "", id: "label", cssClass: "connection-label" }],
2018-10-30 02:25:45 +08:00
[ "Arrow", {
location: 0,
id: "arrow2",
length: 14,
direction: -1,
foldback: 0.8
} ]
];
function loadMapData() {
2018-10-21 16:26:14 +08:00
const currentNote = noteDetailService.getCurrentNote();
mapData = {
notes: []
2018-10-21 16:26:14 +08:00
};
if (currentNote.content) {
try {
mapData = JSON.parse(currentNote.content);
2018-10-30 02:25:45 +08:00
} catch (e) {
2018-10-21 16:26:14 +08:00
console.log("Could not parse content: ", e);
}
}
2018-10-30 02:25:45 +08:00
}
2018-10-21 16:26:14 +08:00
2018-10-30 02:25:45 +08:00
async function show() {
$noteDetailRelationMap.show();
2018-10-18 17:53:01 +08:00
2018-10-30 02:25:45 +08:00
await libraryLoader.requireLibrary(libraryLoader.RELATION_MAP);
2018-10-18 17:53:01 +08:00
2018-10-30 02:25:45 +08:00
loadMapData();
2018-10-18 17:53:01 +08:00
jsPlumb.ready(() => {
initJsPlumbInstance();
2018-11-01 16:41:03 +08:00
initPanZoom();
loadNotesAndRelations();
});
2018-10-30 02:25:45 +08:00
}
2018-10-18 17:53:01 +08:00
2018-10-30 02:25:45 +08:00
async function loadNotesAndRelations() {
const noteIds = mapData.notes.map(note => note.id);
const data = await server.post("notes/relation-map", {noteIds});
2018-10-18 17:53:01 +08:00
relations = [];
2018-10-18 17:53:01 +08:00
2018-10-30 02:25:45 +08:00
for (const relation of data.relations) {
const match = relations.find(rel =>
rel.name === relation.name
&& ((rel.sourceNoteId === relation.sourceNoteId && rel.targetNoteId === relation.targetNoteId)
|| (rel.sourceNoteId === relation.targetNoteId && rel.targetNoteId === relation.sourceNoteId)));
2018-10-18 17:53:01 +08:00
2018-10-30 02:25:45 +08:00
if (match) {
match.type = 'biDirectional';
} else {
relation.type = 'uniDirectional';
relations.push(relation);
}
}
2018-10-18 17:53:01 +08:00
2018-10-30 02:25:45 +08:00
mapData.notes = mapData.notes.filter(note => note.id in data.noteTitles);
2018-10-18 17:53:01 +08:00
jsPlumbInstance.batch(async function () {
2018-10-30 02:25:45 +08:00
for (const note of mapData.notes) {
const title = data.noteTitles[note.id];
2018-10-18 17:53:01 +08:00
2018-10-31 03:22:05 +08:00
await createNoteBox(note.id, title, note.x, note.y);
2018-10-30 02:25:45 +08:00
}
2018-10-18 17:53:01 +08:00
2018-10-30 02:25:45 +08:00
for (const relation of relations) {
if (relation.name === 'isChildOf') {
continue;
}
2018-10-18 17:53:01 +08:00
const connection = jsPlumbInstance.connect({
2018-10-30 02:25:45 +08:00
source: relation.sourceNoteId,
target: relation.targetNoteId,
type: relation.type
});
2018-10-18 17:53:01 +08:00
connection.id = relation.attributeId;
2018-10-30 02:25:45 +08:00
connection.getOverlay("label").setLabel(relation.name);
connection.canvas.setAttribute("data-connection-id", connection.id);
}
});
}
2018-10-25 20:01:03 +08:00
2018-11-01 16:41:03 +08:00
function getMousePos(canvas, evt) {
var rect = canvas.getBoundingClientRect();
console.log(rect);
console.log(canvas);
console.log(`(${evt.clientX} - ${rect.left}) / (${rect.right} - ${rect.left}) * ${canvas.width}`);
return {
x: (evt.clientX - rect.left) / (rect.right - rect.left) * canvas.width,
y: (evt.clientY - rect.top) / (rect.bottom - rect.top) * canvas.height
};
}
2018-10-30 02:25:45 +08:00
function initPanZoom() {
2018-11-01 16:41:03 +08:00
if (pzInstance) {
return;
}
pzInstance = panzoom($relationMapCanvas[0], {
2018-10-30 02:25:45 +08:00
maxZoom: 2,
minZoom: 0.1,
2018-11-01 16:41:03 +08:00
smoothScroll: false,
onMouseDown: function(event) {
if (clipboard) {
const {x, y} = getMousePos($relationMapCanvas[0].getContext("2d"), event);
console.log(x, y);
createNoteBox(clipboard.id, clipboard.title, x, y);
mapData.notes.push({ id: clipboard.id, x, y });
clipboard = null;
}
return true;
}
2018-10-30 02:25:45 +08:00
});
2018-10-25 20:01:03 +08:00
2018-10-30 02:25:45 +08:00
if (mapData.transform) {
pzInstance.zoomTo(0, 0, mapData.transform.scale);
pzInstance.moveTo(mapData.transform.x, mapData.transform.y);
2018-10-30 02:25:45 +08:00
}
2018-10-25 20:01:03 +08:00
pzInstance.on('zoom', function (e) {
mapData.transform = pzInstance.getTransform();
2018-10-31 03:22:05 +08:00
2018-10-30 02:25:45 +08:00
saveData();
});
2018-10-18 17:53:01 +08:00
pzInstance.on('panend', function (e) {
mapData.transform = pzInstance.getTransform();
2018-10-31 03:22:05 +08:00
2018-10-30 02:25:45 +08:00
saveData();
}, true);
2018-10-31 03:22:05 +08:00
$zoomInButton.click(() => pzInstance.zoomTo(0, 0, 1.2));
$zoomOutButton.click(() => pzInstance.zoomTo(0, 0, 0.8));
2018-10-30 02:25:45 +08:00
}
2018-10-25 20:01:03 +08:00
function cleanup() {
if (jsPlumbInstance) {
// delete all endpoints and connections
jsPlumbInstance.deleteEveryEndpoint();
// without this we still end up with note boxes remaining in the canvas
$relationMapCanvas.empty();
}
2018-11-01 16:41:03 +08:00
if (pzInstance) {
pzInstance.dispose();
pzInstance = null;
}
}
function initJsPlumbInstance () {
if (jsPlumbInstance) {
cleanup();
return;
}
jsPlumbInstance = jsPlumb.getInstance({
2018-10-30 02:25:45 +08:00
Endpoint: ["Dot", {radius: 2}],
Connector: "StateMachine",
ConnectionOverlays: uniDirectionalOverlays,
HoverPaintStyle: { stroke: "#777", strokeWidth: 1 },
2018-10-30 02:25:45 +08:00
Container: "relation-map-canvas"
});
2018-10-18 17:53:01 +08:00
jsPlumbInstance.registerConnectionType("uniDirectional", { anchor:"Continuous", connector:"StateMachine", overlays: uniDirectionalOverlays });
2018-10-30 02:25:45 +08:00
jsPlumbInstance.registerConnectionType("biDirectional", { anchor:"Continuous", connector:"StateMachine", overlays: biDirectionalOverlays });
2018-10-30 02:25:45 +08:00
jsPlumbInstance.bind("connection", connectionCreatedHandler);
2018-10-30 02:25:45 +08:00
$relationMapCanvas.contextmenu({
delegate: ".note-box",
menu: [
{title: "Remove note", cmd: "remove", uiIcon: "ui-icon-trash"},
{title: "Edit title", cmd: "edit-title", uiIcon: "ui-icon-pencil"},
],
select: noteContextMenuHandler
});
2018-10-18 17:53:01 +08:00
2018-10-30 02:25:45 +08:00
$.widget("moogle.contextmenuRelation", $.moogle.contextmenu, {});
2018-10-18 17:53:01 +08:00
2018-10-30 02:25:45 +08:00
$relationMapCanvas.contextmenuRelation({
2018-10-30 15:53:30 +08:00
delegate: ".connection-label,.jtk-connector",
2018-10-30 02:25:45 +08:00
autoTrigger: false, // it doesn't open automatically, needs to be triggered explicitly by .open() call
menu: [
{title: "Remove relation", cmd: "remove", uiIcon: "ui-icon-trash"}
2018-10-30 02:25:45 +08:00
],
select: relationContextMenuHandler
});
jsPlumbInstance.bind("contextmenu", function (c, e) {
2018-10-30 02:25:45 +08:00
e.preventDefault();
2018-10-18 17:53:01 +08:00
2018-10-30 02:25:45 +08:00
$relationMapCanvas.contextmenuRelation("open", e, { connection: c });
});
// so that canvas is not panned when clicking/dragging note box
2018-10-30 15:53:30 +08:00
$relationMapCanvas.on('mousedown touchstart', '.note-box, .connection-label', e => e.stopPropagation());
2018-10-30 02:25:45 +08:00
}
2018-10-18 17:53:01 +08:00
2018-10-30 15:53:30 +08:00
async function connectionCreatedHandler(info, originalEvent) {
// if there's no event, then this has been triggered programatically
if (!originalEvent) {
return;
}
2018-10-31 03:22:05 +08:00
const connection = info.connection;
2018-10-30 15:53:30 +08:00
const name = prompt("Specify new relation name:");
if (!name || !name.trim()) {
jsPlumbInstance.deleteConnection(connection);
2018-10-30 15:53:30 +08:00
return;
}
const targetNoteId = connection.target.id;
const sourceNoteId = connection.source.id;
const relationExists = relations.some(rel =>
2018-10-30 15:53:30 +08:00
rel.targetNoteId === targetNoteId
&& rel.sourceNoteId === sourceNoteId
&& rel.name === name);
if (relationExists) {
2018-10-30 15:53:30 +08:00
alert("Connection '" + name + "' between these notes already exists.");
jsPlumbInstance.deleteConnection(connection);
2018-10-30 15:53:30 +08:00
return;
}
const attribute = await server.put(`notes/${sourceNoteId}/relations/${name}/to/${targetNoteId}`);
2018-10-30 15:53:30 +08:00
relations.push({ attributeId: attribute.attributeId , targetNoteId, sourceNoteId, name });
2018-10-30 15:53:30 +08:00
connection.id = attribute.attributeId;
2018-10-30 15:53:30 +08:00
connection.getOverlay("label").setLabel(name);
}
async function relationContextMenuHandler(event, ui) {
2018-10-30 02:25:45 +08:00
const {connection} = ui.extraData;
2018-10-25 21:45:14 +08:00
2018-10-30 02:25:45 +08:00
if (ui.cmd === 'remove') {
if (!confirm("Are you sure you want to remove the relation?")) {
return;
2018-10-25 21:45:14 +08:00
}
const relation = relations.find(rel => rel.attributeId === connection.id);
2018-10-30 02:25:45 +08:00
await server.remove(`notes/${relation.sourceNoteId}/relations/${relation.name}/to/${relation.targetNoteId}`);
2018-10-30 02:25:45 +08:00
jsPlumbInstance.deleteConnection(connection);
2018-10-30 02:25:45 +08:00
relations = relations.filter(relation => relation.attributeId !== connection.id);
2018-10-30 02:25:45 +08:00
}
}
async function noteContextMenuHandler(event, ui) {
2018-10-30 02:25:45 +08:00
const $noteBox = ui.target.closest(".note-box");
const noteId = $noteBox.prop("id");
if (ui.cmd === "remove") {
if (!confirm("Are you sure you want to remove the note from this diagram?")) {
2018-10-30 02:25:45 +08:00
return;
}
jsPlumbInstance.remove(noteId);
2018-10-30 02:25:45 +08:00
mapData.notes = mapData.notes.filter(note => note.id !== noteId);
relations = relations.filter(relation => relation.sourceNoteId !== noteId && relation.targetNoteId !== noteId);
2018-10-30 02:25:45 +08:00
saveData();
}
else if (ui.cmd === "edit-title") {
const $title = $noteBox.find(".title a");
const title = prompt("Enter new note title:", $title.text());
2018-10-30 02:25:45 +08:00
if (!title) {
return;
}
await server.put(`notes/${noteId}/change-title`, { title });
2018-10-30 02:25:45 +08:00
treeService.setNoteTitle(noteId, title);
2018-10-30 02:25:45 +08:00
$title.text(title);
2018-10-30 02:25:45 +08:00
}
}
2018-10-21 16:26:14 +08:00
function saveData() {
2018-10-25 21:45:14 +08:00
noteDetailService.noteChanged();
2018-10-21 16:26:14 +08:00
}
async function createNoteBox(id, title, x, y) {
const $noteBox = $("<div>")
.addClass("note-box")
.prop("id", id)
.append($("<span>").addClass("title").html(await linkService.createNoteLink(id, title)))
2018-10-31 03:22:05 +08:00
.append($("<div>").addClass("endpoint").attr("title", "Start dragging relations from here and drop them on another note."))
.css("left", x + "px")
.css("top", y + "px");
jsPlumbInstance.getContainer().appendChild($noteBox[0]);
2018-10-21 16:26:14 +08:00
jsPlumbInstance.draggable($noteBox[0], {
start:function(params) {},
drag:function(params) {},
2018-10-21 16:26:14 +08:00
stop:function(params) {
const note = mapData.notes.find(note => note.id === params.el.id);
if (!note) {
console.error(`Note ${params.el.id} not found!`);
return;
}
[note.x, note.y] = params.finalPos;
saveData();
}
});
jsPlumbInstance.makeSource($noteBox[0], {
2018-10-21 16:26:14 +08:00
filter: ".endpoint",
anchor: "Continuous",
connectorStyle: { stroke: "#000", strokeWidth: 1 },
2018-10-21 16:26:14 +08:00
connectionType: "basic",
extract:{
"action": "the-action"
}
});
jsPlumbInstance.makeTarget($noteBox[0], {
2018-10-21 16:26:14 +08:00
dropOptions: { hoverClass: "dragHover" },
anchor: "Continuous",
allowLoopback: true
});
}
2018-10-31 03:22:05 +08:00
function getFreePosition() {
const maxY = mapData.notes.filter(note => !!note.y).map(note => note.y).reduce((a, b) => Math.max(a, b), 0);
return [100, maxY + 200];
}
2018-10-21 16:26:14 +08:00
$addChildNotesButton.click(async () => {
const children = await server.get("notes/" + noteDetailService.getCurrentNoteId() + "/children");
2018-10-31 03:22:05 +08:00
let [curX, curY] = getFreePosition();
2018-10-21 16:26:14 +08:00
for (const child of children) {
2018-10-25 18:06:36 +08:00
if (mapData.notes.some(note => note.id === child.noteId)) {
// note already exists
continue;
}
2018-10-31 03:22:05 +08:00
mapData.notes.push({
id: child.noteId,
x: curX,
y: curY
});
2018-10-21 16:26:14 +08:00
if (curX > 1000) {
curX = 100;
curY += 200;
}
else {
curX += 200;
}
}
2018-10-31 03:22:05 +08:00
saveData();
2018-10-21 16:26:14 +08:00
2018-10-31 03:22:05 +08:00
// delete all endpoints and connections
jsPlumbInstance.deleteEveryEndpoint();
2018-10-21 16:26:14 +08:00
2018-10-31 03:22:05 +08:00
await loadNotesAndRelations();
2018-10-21 16:26:14 +08:00
});
let clipboard = null;
$createChildNote.click(async () => {
const title = prompt("Enter title of new note", "new note");
if (!title.trim()) {
return;
}
const {note} = await server.post(`notes/${noteDetailService.getCurrentNoteId()}/children`, {
title,
target: 'into'
});
2018-11-01 01:21:58 +08:00
// reloading tree so that the new note appears there
// no need to wait for it to finish
treeService.reload();
clipboard = { id: note.noteId, title };
});
export default {
2018-10-18 17:46:07 +08:00
show,
2018-10-21 16:26:14 +08:00
getContent: () => JSON.stringify(mapData),
focus: () => null,
onNoteChange: () => null,
cleanup
}