From 1298b968f2fdc7650b407990c22eddc060664e04 Mon Sep 17 00:00:00 2001 From: Elian Doran Date: Thu, 17 Jul 2025 22:31:48 +0300 Subject: [PATCH] fix(views/table): relation display sometimes not showing up --- apps/client/src/services/link.ts | 2 +- .../view_widgets/table_view/columns.ts | 1 - .../view_widgets/table_view/formatters.ts | 41 ++++++++++++++++--- .../table_view/relation_editor.ts | 19 ++++++--- 4 files changed, 49 insertions(+), 14 deletions(-) diff --git a/apps/client/src/services/link.ts b/apps/client/src/services/link.ts index b9141ff0f..809eb58ef 100644 --- a/apps/client/src/services/link.ts +++ b/apps/client/src/services/link.ts @@ -405,7 +405,7 @@ function linkContextMenu(e: PointerEvent) { linkContextMenuService.openContextMenu(notePath, e, viewScope, null); } -export async function loadReferenceLinkTitle($el: JQuery, href: string | null | undefined = null) { +async function loadReferenceLinkTitle($el: JQuery, href: string | null | undefined = null) { const $link = $el[0].tagName === "A" ? $el : $el.find("a"); href = href || $link.attr("href"); diff --git a/apps/client/src/widgets/view_widgets/table_view/columns.ts b/apps/client/src/widgets/view_widgets/table_view/columns.ts index b99c4b86d..9c56a03ce 100644 --- a/apps/client/src/widgets/view_widgets/table_view/columns.ts +++ b/apps/client/src/widgets/view_widgets/table_view/columns.ts @@ -67,7 +67,6 @@ export function buildColumnDefinitions(info: AttributeDefinitionInformation[], m width: 400 } ]; - console.log("Log ", rowNumberHint, columnDefs[0].width); const seenFields = new Set(); for (const { name, title, type } of info) { diff --git a/apps/client/src/widgets/view_widgets/table_view/formatters.ts b/apps/client/src/widgets/view_widgets/table_view/formatters.ts index 07c9d1060..92cfda386 100644 --- a/apps/client/src/widgets/view_widgets/table_view/formatters.ts +++ b/apps/client/src/widgets/view_widgets/table_view/formatters.ts @@ -1,5 +1,6 @@ import { CellComponent } from "tabulator-tables"; -import { loadReferenceLinkTitle } from "../../../services/link.js"; +import froca from "../../../services/froca.js"; +import FNote from "../../../entities/fnote.js"; /** * Custom formatter to represent a note, with the icon and note title being rendered. @@ -12,11 +13,39 @@ export function NoteFormatter(cell: CellComponent, _formatterParams, onRendered) return ""; } - onRendered(async () => { - const { $noteRef, href } = buildNoteLink(noteId); - await loadReferenceLinkTitle($noteRef, href); - cell.getElement().appendChild($noteRef[0]); - }); + function buildLink(note: FNote | undefined) { + if (!note) { + return; + } + + const iconClass = note.getIcon(); + const title = note.title; + const { $noteRef } = buildNoteLink(noteId); + $noteRef.text(title); + $noteRef.prepend($("").addClass(iconClass)); + return $noteRef[0]; + } + + const cachedNote = froca.getNoteFromCache(noteId); + if (cachedNote) { + // Cache hit, build the link immediately + const el = buildLink(cachedNote); + return el?.outerHTML; + } else { + // Cache miss, load the note asynchronously + onRendered(async () => { + const note = await froca.getNote(noteId); + if (!note) { + return; + } + + const el = buildLink(note); + if (el) { + cell.getElement().appendChild(el); + } + }); + } + return ""; } diff --git a/apps/client/src/widgets/view_widgets/table_view/relation_editor.ts b/apps/client/src/widgets/view_widgets/table_view/relation_editor.ts index 3b3aed0d2..8e948cc2a 100644 --- a/apps/client/src/widgets/view_widgets/table_view/relation_editor.ts +++ b/apps/client/src/widgets/view_widgets/table_view/relation_editor.ts @@ -21,23 +21,30 @@ export function RelationEditor(cell: CellComponent, onRendered, success, cancel, editor.style.boxSizing = "border-box"; //Set value of editor to the current value of the cell - const noteId = cell.getValue(); - if (noteId) { - const note = froca.getNoteFromCache(noteId); + const originalNoteId = cell.getValue(); + if (originalNoteId) { + const note = froca.getNoteFromCache(originalNoteId); editor.value = note.title; + } else { + editor.value = ""; } //set focus on the select box when the editor is selected onRendered(function(){ - let noteId = ""; + let newNoteId = originalNoteId; note_autocomplete.initNoteAutocomplete($editor, { allowCreatingNotes: true, hideAllButtons: true }).on("autocomplete:noteselected", (event, suggestion, dataset) => { const notePath = suggestion.notePath; - noteId = (notePath ?? "").split("/").at(-1); - }).on("blur", () => success(noteId)); + newNoteId = (notePath ?? "").split("/").at(-1); + }).on("blur", () => { + if (!editor.value) { + newNoteId = ""; + } + success(newNoteId); + }); editor.focus(); });