Files
Trilium/src/public/app/widgets/ribbon_widgets/note_paths.ts

143 lines
4.1 KiB
TypeScript
Raw Normal View History

2021-05-29 13:06:09 +02:00
import NoteContextAwareWidget from "../note_context_aware_widget.js";
import treeService from "../../services/tree.js";
import linkService from "../../services/link.js";
import { t } from "../../services/i18n.js";
2025-01-19 20:53:52 +02:00
import type FNote from "../../entities/fnote.js";
import type { NotePathRecord } from "../../entities/fnote.js";
import type { EventData } from "../../components/app_context.js";
const TPL = `
2021-05-29 13:06:09 +02:00
<div class="note-paths-widget">
<style>
2021-05-29 13:06:09 +02:00
.note-paths-widget {
padding: 12px;
max-height: 300px;
overflow-y: auto;
}
.note-path-list {
2021-05-29 13:06:09 +02:00
margin-top: 10px;
}
.note-path-list .path-current {
font-weight: bold;
}
.note-path-list .path-archived {
color: var(--muted-text-color) !important;
}
.note-path-list .path-search {
font-style: italic;
}
</style>
<div class="note-path-intro"></div>
2021-05-29 13:06:09 +02:00
<ul class="note-path-list"></ul>
<button class="btn btn-sm" data-trigger-command="cloneNoteIdsTo">${t("note_paths.clone_button")}</button>
</div>`;
2021-05-22 12:35:41 +02:00
export default class NotePathsWidget extends NoteContextAwareWidget {
2025-01-19 20:53:52 +02:00
private $notePathIntro!: JQuery<HTMLElement>;
private $notePathList!: JQuery<HTMLElement>;
2021-06-27 12:53:05 +02:00
get name() {
return "notePaths";
}
get toggleCommand() {
return "toggleRibbonTabNotePaths";
}
2021-05-29 13:06:09 +02:00
getTitle() {
return {
2021-05-29 13:24:14 +02:00
show: true,
title: t("note_paths.title"),
2025-01-09 18:07:02 +02:00
icon: "bx bx-collection"
2021-05-29 13:06:09 +02:00
};
}
doRender() {
this.$widget = $(TPL);
2021-06-13 22:55:31 +02:00
this.contentSized();
this.$notePathIntro = this.$widget.find(".note-path-intro");
this.$notePathList = this.$widget.find(".note-path-list");
}
2025-01-19 20:53:52 +02:00
async refreshWithNote(note: FNote) {
this.$notePathList.empty();
2025-01-19 20:53:52 +02:00
if (!this.note || this.noteId === "root") {
2025-01-09 18:07:02 +02:00
this.$notePathList.empty().append(await this.getRenderedPath("root"));
return;
}
2025-01-09 18:07:02 +02:00
const sortedNotePaths = this.note.getSortedNotePathRecords(this.hoistedNoteId).filter((notePath) => !notePath.isHidden);
if (sortedNotePaths.length > 0) {
this.$notePathIntro.text(t("note_paths.intro_placed"));
} else {
this.$notePathIntro.text(t("note_paths.intro_not_placed"));
}
2021-10-21 21:10:55 +02:00
const renderedPaths = [];
for (const notePathRecord of sortedNotePaths) {
2025-01-09 18:07:02 +02:00
const notePath = notePathRecord.notePath.join("/");
2021-10-21 21:10:55 +02:00
renderedPaths.push(await this.getRenderedPath(notePath, notePathRecord));
}
2021-10-21 21:10:55 +02:00
this.$notePathList.empty().append(...renderedPaths);
}
2025-01-19 20:53:52 +02:00
async getRenderedPath(notePath: string, notePathRecord: NotePathRecord | null = null) {
2020-01-25 09:56:08 +01:00
const title = await treeService.getNotePathTitle(notePath);
2025-01-09 18:07:02 +02:00
const $noteLink = await linkService.createLink(notePath, { title });
$noteLink.find("a").addClass("no-tooltip-preview tn-link");
const icons = [];
if (this.notePath === notePath) {
$noteLink.addClass("path-current");
}
if (!notePathRecord || notePathRecord.isInHoistedSubTree) {
$noteLink.addClass("path-in-hoisted-subtree");
} else {
icons.push(`<span class="bx bx-trending-up" title="${t("note_paths.outside_hoisted")}"></span>`);
}
if (notePathRecord?.isArchived) {
$noteLink.addClass("path-archived");
icons.push(`<span class="bx bx-archive" title="${t("note_paths.archived")}"></span>`);
}
if (notePathRecord?.isSearch) {
$noteLink.addClass("path-search");
icons.push(`<span class="bx bx-search" title="${t("note_paths.search")}"></span>`);
}
if (icons.length > 0) {
2025-01-09 18:07:02 +02:00
$noteLink.append(` ${icons.join(" ")}`);
}
2021-10-21 21:10:55 +02:00
return $("<li>").append($noteLink);
}
2020-02-17 22:47:50 +01:00
2025-01-19 20:53:52 +02:00
entitiesReloadedEvent({ loadResults }: EventData<"entitiesReloaded">) {
2025-03-02 20:47:57 +01:00
if (loadResults.getBranchRows().find((branch) => branch.noteId === this.noteId) || (this.noteId != null && loadResults.isNoteReloaded(this.noteId))) {
2020-02-17 22:47:50 +01:00
this.refresh();
}
}
}