2025-08-30 16:58:23 +03:00
|
|
|
import { useEffect, useMemo, useRef, useState } from "preact/hooks";
|
2025-08-30 15:44:49 +03:00
|
|
|
import FNote from "../../../entities/fnote";
|
|
|
|
|
import Icon from "../../react/Icon";
|
|
|
|
|
import { ViewModeProps } from "../interface";
|
2025-08-30 19:03:18 +03:00
|
|
|
import { useNoteLabelBoolean, useImperativeSearchHighlighlighting } from "../../react/hooks";
|
2025-08-30 15:44:49 +03:00
|
|
|
import NoteLink from "../../react/NoteLink";
|
2025-08-30 16:01:02 +03:00
|
|
|
import "./ListOrGridView.css";
|
|
|
|
|
import content_renderer from "../../../services/content_renderer";
|
2025-08-30 16:58:23 +03:00
|
|
|
import { Pager, usePagination } from "../Pagination";
|
2025-08-30 17:21:22 +03:00
|
|
|
import tree from "../../../services/tree";
|
|
|
|
|
import link from "../../../services/link";
|
2025-08-30 17:30:35 +03:00
|
|
|
import { t } from "../../../services/i18n";
|
2025-08-30 17:39:09 +03:00
|
|
|
import attribute_renderer from "../../../services/attribute_renderer";
|
2025-08-30 15:44:49 +03:00
|
|
|
|
2025-08-30 18:48:34 +03:00
|
|
|
export function ListView({ note, noteIds: unfilteredNoteIds, highlightedTokens }: ViewModeProps) {
|
2025-08-30 15:44:49 +03:00
|
|
|
const [ isExpanded ] = useNoteLabelBoolean(note, "expanded");
|
2025-08-30 17:21:22 +03:00
|
|
|
const noteIds = useFilteredNoteIds(note, unfilteredNoteIds);
|
|
|
|
|
const { pageNotes, ...pagination } = usePagination(note, noteIds);
|
2025-08-30 15:44:49 +03:00
|
|
|
|
2025-08-30 15:11:49 +03:00
|
|
|
return (
|
2025-08-30 17:21:22 +03:00
|
|
|
<div class="note-list list-view">
|
2025-08-30 17:39:09 +03:00
|
|
|
{ noteIds.length > 0 && <div class="note-list-wrapper">
|
2025-08-30 16:40:25 +03:00
|
|
|
<Pager {...pagination} />
|
2025-08-30 17:21:22 +03:00
|
|
|
|
2025-08-30 15:44:49 +03:00
|
|
|
<div class="note-list-container use-tn-links">
|
2025-08-30 17:21:22 +03:00
|
|
|
{pageNotes?.map(childNote => (
|
2025-08-30 18:48:34 +03:00
|
|
|
<ListNoteCard note={childNote} parentNote={note} expand={isExpanded} highlightedTokens={highlightedTokens} />
|
2025-08-30 15:44:49 +03:00
|
|
|
))}
|
|
|
|
|
</div>
|
2025-08-30 17:21:22 +03:00
|
|
|
|
2025-08-30 16:40:25 +03:00
|
|
|
<Pager {...pagination} />
|
2025-08-30 17:39:09 +03:00
|
|
|
</div>}
|
2025-08-30 15:11:49 +03:00
|
|
|
</div>
|
|
|
|
|
);
|
2025-08-30 15:44:49 +03:00
|
|
|
}
|
|
|
|
|
|
2025-08-30 18:48:34 +03:00
|
|
|
export function GridView({ note, noteIds: unfilteredNoteIds, highlightedTokens }: ViewModeProps) {
|
2025-08-30 17:21:22 +03:00
|
|
|
const noteIds = useFilteredNoteIds(note, unfilteredNoteIds);
|
|
|
|
|
const { pageNotes, ...pagination } = usePagination(note, noteIds);
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<div class="note-list grid-view">
|
|
|
|
|
<div class="note-list-wrapper">
|
|
|
|
|
<Pager {...pagination} />
|
|
|
|
|
|
|
|
|
|
<div class="note-list-container use-tn-links">
|
|
|
|
|
{pageNotes?.map(childNote => (
|
2025-08-30 18:48:34 +03:00
|
|
|
<GridNoteCard note={childNote} parentNote={note} highlightedTokens={highlightedTokens} />
|
2025-08-30 17:21:22 +03:00
|
|
|
))}
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
<Pager {...pagination} />
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-30 18:48:34 +03:00
|
|
|
function ListNoteCard({ note, parentNote, expand, highlightedTokens }: { note: FNote, parentNote: FNote, expand?: boolean, highlightedTokens: string[] | null | undefined }) {
|
2025-08-30 16:01:02 +03:00
|
|
|
const [ isExpanded, setExpanded ] = useState(expand);
|
2025-08-30 17:21:22 +03:00
|
|
|
const notePath = getNotePath(parentNote, note);
|
2025-08-30 15:44:49 +03:00
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<div
|
2025-08-30 16:01:02 +03:00
|
|
|
className={`note-book-card no-tooltip-preview ${isExpanded ? "expanded" : ""}`}
|
2025-08-30 17:21:22 +03:00
|
|
|
data-note-id={note.noteId}
|
2025-08-30 15:44:49 +03:00
|
|
|
>
|
|
|
|
|
<h5 className="note-book-header">
|
2025-08-30 16:01:02 +03:00
|
|
|
<span
|
|
|
|
|
className={`note-expander ${isExpanded ? "bx bx-chevron-down" : "bx bx-chevron-right"}`}
|
|
|
|
|
onClick={() => setExpanded(!isExpanded)}
|
|
|
|
|
/>
|
|
|
|
|
|
2025-08-30 15:44:49 +03:00
|
|
|
<Icon className="note-icon" icon={note.getIcon()} />
|
2025-08-30 18:48:34 +03:00
|
|
|
<NoteLink className="note-book-title" notePath={notePath} noPreview showNotePath={note.type === "search"} highlightedTokens={highlightedTokens} />
|
2025-08-30 17:39:09 +03:00
|
|
|
<NoteAttributes note={note} />
|
|
|
|
|
|
2025-08-30 16:19:21 +03:00
|
|
|
{isExpanded && <>
|
2025-08-30 19:03:18 +03:00
|
|
|
<NoteContent note={note} highlightedTokens={highlightedTokens} />
|
|
|
|
|
<NoteChildren note={note} parentNote={parentNote} highlightedTokens={highlightedTokens} />
|
2025-08-30 16:19:21 +03:00
|
|
|
</>}
|
2025-08-30 15:44:49 +03:00
|
|
|
</h5>
|
|
|
|
|
</div>
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-30 18:48:34 +03:00
|
|
|
function GridNoteCard({ note, parentNote, highlightedTokens }: { note: FNote, parentNote: FNote, highlightedTokens: string[] | null | undefined }) {
|
|
|
|
|
const titleRef = useRef<HTMLSpanElement>(null);
|
2025-08-30 17:21:22 +03:00
|
|
|
const [ noteTitle, setNoteTitle ] = useState<string>();
|
|
|
|
|
const notePath = getNotePath(parentNote, note);
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
tree.getNoteTitle(note.noteId, parentNote.noteId).then(setNoteTitle);
|
|
|
|
|
}, [ note ]);
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<div
|
|
|
|
|
className={`note-book-card no-tooltip-preview block-link`}
|
|
|
|
|
data-href={`#${notePath}`}
|
|
|
|
|
data-note-id={note.noteId}
|
|
|
|
|
onClick={(e) => link.goToLink(e)}
|
|
|
|
|
>
|
|
|
|
|
<h5 className="note-book-header">
|
|
|
|
|
<Icon className="note-icon" icon={note.getIcon()} />
|
2025-08-30 18:48:34 +03:00
|
|
|
<span ref={titleRef} className="note-book-title">{noteTitle}</span>
|
2025-08-30 17:39:09 +03:00
|
|
|
<NoteAttributes note={note} />
|
2025-08-30 17:21:22 +03:00
|
|
|
</h5>
|
2025-08-30 19:03:18 +03:00
|
|
|
<NoteContent note={note} trim highlightedTokens={highlightedTokens} />
|
2025-08-30 17:21:22 +03:00
|
|
|
</div>
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-30 17:39:09 +03:00
|
|
|
function NoteAttributes({ note }: { note: FNote }) {
|
|
|
|
|
const ref = useRef<HTMLSpanElement>(null);
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
attribute_renderer.renderNormalAttributes(note).then(({$renderedAttributes}) => {
|
|
|
|
|
ref.current?.replaceChildren(...$renderedAttributes);
|
|
|
|
|
});
|
|
|
|
|
}, [ note ]);
|
|
|
|
|
|
|
|
|
|
return <span className="note-list-attributes" ref={ref} />
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-30 19:03:18 +03:00
|
|
|
function NoteContent({ note, trim, highlightedTokens }: { note: FNote, trim?: boolean, highlightedTokens }) {
|
2025-08-30 16:01:02 +03:00
|
|
|
const contentRef = useRef<HTMLDivElement>(null);
|
2025-08-30 19:03:18 +03:00
|
|
|
const highlightSearch = useImperativeSearchHighlighlighting(highlightedTokens);
|
2025-08-30 16:01:02 +03:00
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
content_renderer.getRenderedContent(note, { trim })
|
|
|
|
|
.then(({ $renderedContent, type }) => {
|
2025-08-30 19:03:18 +03:00
|
|
|
if (!contentRef.current) return;
|
|
|
|
|
contentRef.current.replaceChildren(...$renderedContent);
|
|
|
|
|
contentRef.current.classList.add(`type-${type}`);
|
|
|
|
|
highlightSearch(contentRef.current);
|
2025-08-30 16:01:02 +03:00
|
|
|
})
|
|
|
|
|
.catch(e => {
|
|
|
|
|
console.warn(`Caught error while rendering note '${note.noteId}' of type '${note.type}'`);
|
|
|
|
|
console.error(e);
|
2025-08-30 17:30:35 +03:00
|
|
|
contentRef.current?.replaceChildren(t("collections.rendering_error"));
|
2025-08-30 16:01:02 +03:00
|
|
|
})
|
2025-08-30 19:03:18 +03:00
|
|
|
}, [ note, highlightedTokens ]);
|
2025-08-30 16:01:02 +03:00
|
|
|
|
|
|
|
|
return <div ref={contentRef} className="note-book-content" />;
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-30 19:03:18 +03:00
|
|
|
function NoteChildren({ note, parentNote, highlightedTokens }: { note: FNote, parentNote: FNote, highlightedTokens: string[] | null | undefined }) {
|
2025-08-30 16:19:21 +03:00
|
|
|
const imageLinks = note.getRelations("imageLink");
|
|
|
|
|
const [ childNotes, setChildNotes ] = useState<FNote[]>();
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
note.getChildNotes().then(childNotes => {
|
|
|
|
|
const filteredChildNotes = childNotes.filter((childNote) => !imageLinks.find((rel) => rel.value === childNote.noteId));
|
|
|
|
|
setChildNotes(filteredChildNotes);
|
|
|
|
|
});
|
|
|
|
|
}, [ note ]);
|
|
|
|
|
|
2025-08-30 19:03:18 +03:00
|
|
|
return childNotes?.map(childNote => <ListNoteCard note={childNote} parentNote={parentNote} highlightedTokens={highlightedTokens} />)
|
2025-08-30 17:21:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Filters the note IDs for the legacy view to filter out subnotes that are already included in the note content such as images, included notes.
|
|
|
|
|
*/
|
|
|
|
|
function useFilteredNoteIds(note: FNote, noteIds: string[]) {
|
|
|
|
|
return useMemo(() => {
|
|
|
|
|
const includedLinks = note ? note.getRelations().filter((rel) => rel.name === "imageLink" || rel.name === "includeNoteLink") : [];
|
|
|
|
|
const includedNoteIds = new Set(includedLinks.map((rel) => rel.value));
|
|
|
|
|
return noteIds.filter((noteId) => !includedNoteIds.has(noteId) && noteId !== "_hidden");
|
|
|
|
|
}, noteIds);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function getNotePath(parentNote: FNote, childNote: FNote) {
|
|
|
|
|
if (parentNote.type === "search") {
|
|
|
|
|
// for search note parent, we want to display a non-search path
|
|
|
|
|
return childNote.noteId;
|
|
|
|
|
} else {
|
|
|
|
|
return `${parentNote.noteId}/${childNote.noteId}`
|
|
|
|
|
}
|
2025-08-30 16:19:21 +03:00
|
|
|
}
|