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. * 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(); let noteId = cell.getValue();
if (!noteId) { if (!noteId) {
return ""; return "";
} }
onRendered(async () => { onRendered(async () => {
const $noteRef = $("<span>"); const { $noteRef, href } = buildNoteLink(noteId);
const href = `#root/${noteId}`;
$noteRef.addClass("reference-link");
$noteRef.attr("data-href", href);
await loadReferenceLinkTitle($noteRef, href); await loadReferenceLinkTitle($noteRef, href);
cell.getElement().appendChild($noteRef[0]); cell.getElement().appendChild($noteRef[0]);
}); });
return ""; 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(); const { noteId, iconClass } = cell.getRow().getData();
if (!noteId) { if (!noteId) {
return ""; return "";
} }
const $noteRef = $("<span>"); const { $noteRef } = buildNoteLink(noteId);
const href = `#root/${noteId}`;
$noteRef.addClass("reference-link");
$noteRef.attr("data-href", href);
$noteRef.text(cell.getValue()); $noteRef.text(cell.getValue());
$noteRef.prepend($("<span>").addClass(iconClass)); $noteRef.prepend($("<span>").addClass(iconClass));
return $noteRef[0].outerHTML; 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 };
}