refactor(views/table): reduce duplication

This commit is contained in:
Elian Doran 2025-07-04 20:32:53 +03:00
parent 08cf95aa38
commit 60963abe2c
No known key found for this signature in database

View File

@ -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 };
}