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
refactor(views/table): reduce duplication
Elian Doran60963abe2c77
diff --git a/apps/client/src/widgets/view_widgets/table_view/formatters.ts b/apps/client/src/widgets/view_widgets/table_view/formatters.tsindex 8276bcdc40..68e529798f 100644--- a/apps/client/src/widgets/view_widgets/table_view/formatters.ts+++ b/apps/client/src/widgets/view_widgets/table_view/formatters.ts@@ -6,36 +6,40 @@ import { loadReferenceLinkTitle } from "../../../services/link.js";** The value of the cell must be the note ID.*/-export function NoteFormatter(cell: CellComponent, formatterParams, onRendered) {+export function NoteFormatter(cell: CellComponent, _formatterParams, onRendered) {let noteId = cell.getValue();if (!noteId) {return "";}onRendered(async () => {- const $noteRef = $("<span>");- const href = `#root/${noteId}`;- $noteRef.addClass("reference-link");- $noteRef.attr("data-href", href);-+ const { $noteRef, href } = buildNoteLink(noteId);await loadReferenceLinkTitle($noteRef, href);cell.getElement().appendChild($noteRef[0]);});return "";}-export function NoteTitleFormatter(cell: CellComponent, formatterParams, onRendered) {+/**+ * Custom formatter for the note title that is quite similar to {@link NoteFormatter}, but where the title and icons are read from separate fields.+ */+export function NoteTitleFormatter(cell: CellComponent) {const { noteId, iconClass } = cell.getRow().getData();if (!noteId) {return "";}- const $noteRef = $("<span>");- const href = `#root/${noteId}`;- $noteRef.addClass("reference-link");- $noteRef.attr("data-href", href);+ const { $noteRef } = buildNoteLink(noteId);$noteRef.text(cell.getValue());$noteRef.prepend($("<span>").addClass(iconClass));return $noteRef[0].outerHTML;}++function buildNoteLink(noteId: string) {+ const $noteRef = $("<span>");+ const href = `#root/${noteId}`;+ $noteRef.addClass("reference-link");+ $noteRef.attr("data-href", href);+ return { $noteRef, href };+}