Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
added "what links here" widget
zadam02c9dabcffc8
diff --git a/src/public/javascripts/entities/link.js b/src/public/javascripts/entities/link.jsdeleted file mode 100644index 89e7ec380d..0000000000--- a/src/public/javascripts/entities/link.js+++ /dev/null@@ -1,33 +0,0 @@-class Link {- constructor(treeCache, row) {- this.treeCache = treeCache;- /** @param {string} linkId */- this.linkId = row.linkId;- /** @param {string} noteId */- this.noteId = row.noteId;- /** @param {string} type */- this.type = row.type;- /** @param {string} targetNoteId */- this.targetNoteId = row.targetNoteId;- /** @param {string} utcDateCreated */- this.utcDateCreated = row.utcDateCreated;- /** @param {string} utcDateModified */- this.utcDateModified = row.utcDateModified;- }-- /** @returns {NoteShort} */- async getNote() {- return await this.treeCache.getNote(this.noteId);- }-- /** @returns {NoteShort} */- async getTargetNote() {- return await this.treeCache.getNote(this.targetNoteId);- }-- get toString() {- return `Link(linkId=${this.linkId}, type=${this.type}, note=${this.noteId}, targetNoteId=${this.targetNoteId})`;- }-}--export default Link;\ No newline at end of filediff --git a/src/public/javascripts/entities/note_short.js b/src/public/javascripts/entities/note_short.jsindex dccf148506..0c559657ec 100644--- a/src/public/javascripts/entities/note_short.js+++ b/src/public/javascripts/entities/note_short.js@@ -1,6 +1,5 @@import server from '../services/server.js';import Attribute from './attribute.js';-import Link from './link.js';const LABEL = 'label';const LABEL_DEFINITION = 'label-definition';@@ -231,11 +230,13 @@ class NoteShort {}/**- * @return {Promise<Link[]>}+ * Get relations which target this note+ *+ * @returns {Promise<Attribute[]>}*/- async getLinks() {- return (await server.get('notes/' + this.noteId + '/links'))- .map(linkRow => new Link(this.treeCache, linkRow));+ async getTargetRelations() {+ return (await server.get('notes/' + this.noteId + '/target-relations'))+ .map(attrRow => new Attribute(this.treeCache, attrRow));}get toString() {diff --git a/src/public/javascripts/services/sidebar.js b/src/public/javascripts/services/sidebar.jsindex bb84362035..15270c9d91 100644--- a/src/public/javascripts/services/sidebar.js+++ b/src/public/javascripts/services/sidebar.js@@ -2,6 +2,7 @@ import NoteInfoWidget from "../widgets/note_info.js";import LinkMapWidget from "../widgets/link_map.js";import NoteRevisionsWidget from "../widgets/note_revisions.js";import AttributesWidget from "../widgets/attributes.js";+import WhatLinksHereWidget from "../widgets/what_links_here.js";import bundleService from "./bundle.js";import messagingService from "./messaging.js";@@ -52,7 +53,7 @@ class Sidebar {this.widgets = [];this.$widgetContainer.empty();- const widgetClasses = [AttributesWidget, LinkMapWidget, NoteRevisionsWidget, NoteInfoWidget];+ const widgetClasses = [AttributesWidget, LinkMapWidget, WhatLinksHereWidget, NoteRevisionsWidget, NoteInfoWidget];const widgetRelations = await this.ctx.note.getRelations('widget');diff --git a/src/public/javascripts/widgets/standard_widget.js b/src/public/javascripts/widgets/standard_widget.jsindex fea570439a..1de2b5ef93 100644--- a/src/public/javascripts/widgets/standard_widget.js+++ b/src/public/javascripts/widgets/standard_widget.js@@ -37,6 +37,13 @@ class StandardWidget {this.$body = this.$bodyWrapper.find('.card-body');+ const maxHeight = this.getMaxHeight();++ if (maxHeight) {+ this.$body.css("max-height", maxHeight);+ this.$body.css("overflow", "auto");+ }+this.$widget.on('shown.bs.collapse', () => this.renderBody());this.$widget.on('shown.bs.collapse', () => this.ctx.stateChanged());this.$widget.on('hidden.bs.collapse', () => this.ctx.stateChanged());@@ -50,6 +57,8 @@ class StandardWidget {getHeaderActions() { return []; }+ getMaxHeight() { return null; }+async renderBody() {if (!this.isVisible() || this.rendered) {return;diff --git a/src/public/javascripts/widgets/what_links_here.js b/src/public/javascripts/widgets/what_links_here.jsindex 3227180130..283d0ad9f8 100644--- a/src/public/javascripts/widgets/what_links_here.js+++ b/src/public/javascripts/widgets/what_links_here.js@@ -1,30 +1,30 @@import StandardWidget from "./standard_widget.js";+import linkService from "../services/link.js";class WhatLinksHereWidget extends StandardWidget {getWidgetTitle() { return "What links here"; }+ getMaxHeight() { return "200px"; }+async doRenderBody() {+ const targetRelations = await this.ctx.note.getTargetRelations();+ if (targetRelations.length === 0) {+ this.$body.text("Nothing links here yet ...");+ return;+ }- const $noteId = this.$body.find(".note-info-note-id");- const $dateCreated = this.$body.find(".note-info-date-created");- const $dateModified = this.$body.find(".note-info-date-modified");- const $type = this.$body.find(".note-info-type");- const $mime = this.$body.find(".note-info-mime");+ const $list = $("<ul>");- const note = this.ctx.note;+ for (const rel of targetRelations) {+ const $item = $("<li>")+ .append(await linkService.createNoteLink(rel.noteId))+ .append($("<span>").text(" (" + rel.name + ")"));- $noteId.text(note.noteId);- $dateCreated.text(note.dateCreated);- $dateModified.text(note.dateModified);- $type.text(note.type);- $mime.text(note.mime);- }-- syncDataReceived(syncData) {- if (syncData.find(sd => sd.entityName === 'notes' && sd.entityId === this.ctx.note.noteId)) {- this.doRenderBody();+ $list.append($item);}++ this.$body.empty().append($list);}}diff --git a/src/routes/api/attributes.js b/src/routes/api/attributes.jsindex ac751dab17..5a058fff05 100644--- a/src/routes/api/attributes.js+++ b/src/routes/api/attributes.js@@ -157,6 +157,12 @@ async function deleteRelation(req) {}}+async function getTargetRelations(req) {+ const note = await repository.getNote(req.params.noteId);++ return await note.getTargetRelations();+}+module.exports = {updateNoteAttributes,updateNoteAttribute,@@ -165,5 +171,6 @@ module.exports = {getValuesForAttribute,getEffectiveNoteAttributes,createRelation,- deleteRelation+ deleteRelation,+ getTargetRelations};\ No newline at end of filediff --git a/src/routes/routes.js b/src/routes/routes.jsindex 8792e51973..78aca685db 100644--- a/src/routes/routes.js+++ b/src/routes/routes.js@@ -157,6 +157,7 @@ function register(app) {apiRoute(DELETE, '/api/notes/:noteId/attributes/:attributeId', attributesRoute.deleteNoteAttribute);apiRoute(GET, '/api/attributes/names', attributesRoute.getAttributeNames);apiRoute(GET, '/api/attributes/values/:attributeName', attributesRoute.getValuesForAttribute);+ apiRoute(GET, '/api/notes/:noteId/target-relations', attributesRoute.getTargetRelations);apiRoute(POST, '/api/notes/:noteId/link-map', linkMapRoute.getLinkMap);apiRoute(GET, '/api/notes/:noteId/links', linksRoute.getLinks);