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
client/content manager: format items
Adorian Doranb1ab213afc7d
diff --git a/apps/client/src/widgets/collections/Pagination.tsx b/apps/client/src/widgets/collections/Pagination.tsxindex 256d3a39f9..f328f23fe1 100644--- a/apps/client/src/widgets/collections/Pagination.tsx+++ b/apps/client/src/widgets/collections/Pagination.tsx@@ -122,12 +122,17 @@ function createSegment(start: number, length: number, currentPage: number, setPareturn children;}-export function usePagination(note: FNote, noteIds: string[]): PaginationContext {+/**+ * @param pageSizeOverride page size to use instead of the note's `#pageSize` label, for callers that+ * list notes which aren't the children of a collection the user can label.+ */+export function usePagination(note: FNote, noteIds: string[], pageSizeOverride?: number): PaginationContext {const [ page, setPage ] = useState(1);const [ pageNotes, setPageNotes ] = useState<FNote[]>();// Parse page size.- const [ pageSize ] = useNoteLabelInt(note, "pageSize");+ const [ labelPageSize ] = useNoteLabelInt(note, "pageSize");+ const pageSize = pageSizeOverride ?? labelPageSize;const normalizedPageSize = (pageSize && pageSize > 0 ? pageSize : 20);// Calculate start/end index.diff --git a/apps/client/src/widgets/collections/legacy/ListOrGridView.css b/apps/client/src/widgets/collections/legacy/ListOrGridView.cssindex 9e27a6b8b9..fc44a5b511 100644--- a/apps/client/src/widgets/collections/legacy/ListOrGridView.css+++ b/apps/client/src/widgets/collections/legacy/ListOrGridView.css@@ -187,6 +187,17 @@opacity: .75;}+ /* Stands in for .note-list-attributes when the caller supplies its own controls, so the item+ menu still sits at the far end of the row instead of collapsing next to the title. */+ .note-list-item-actions {+ display: flex;+ flex-grow: 1;+ justify-content: flex-end;+ align-items: center;+ gap: .5em;+ margin-inline-start: 1em;+ }+&.archived {span.tn-icon + span,.tn-icon {diff --git a/apps/client/src/widgets/collections/legacy/ListOrGridView.tsx b/apps/client/src/widgets/collections/legacy/ListOrGridView.tsxindex 9a4cb63201..5a1814add5 100644--- a/apps/client/src/widgets/collections/legacy/ListOrGridView.tsx+++ b/apps/client/src/widgets/collections/legacy/ListOrGridView.tsx@@ -23,21 +23,49 @@ import { ComponentChildren, TargetedMouseEvent } from "preact";const contentSizeObserver = new ResizeObserver(onContentResized);-export function ListView({ note, noteIds: unfilteredNoteIds, highlightedTokens, showTextRepresentation }: ViewModeProps<{}>) {- const expandDepth = useExpansionDepth(note);+/**+ * Opt-in adjustments for callers that reuse the list outside a collection note. Every field is+ * optional and defaults to the collection behaviour, so existing views are unaffected.+ */+export interface ListViewOptions {+ /** Heading shown at the top of the list's card. */+ title?: string;+ /**+ * Lays the items out the way search results are shown — the note's path under its title, and a+ * path that addresses the note directly rather than through the parent. Search notes always use+ * this layout; set it when the parent isn't the notes' real parent.+ */+ searchResultsLayout?: boolean;+ /** Omits the nested subnote tree, leaving only each note's own content preview. */+ hideSubNotes?: boolean;+ /** Overrides how many levels start expanded, otherwise read from the parent's `#expanded`. */+ expandDepth?: number;+ /** Overrides the number of notes per page, otherwise read from the parent's `#pageSize`. */+ pageSize?: number;+ /** Rendered on the title row in place of the note's attributes. */+ renderItemActions?: (note: FNote) => ComponentChildren;+}++export function ListView({ note, noteIds: unfilteredNoteIds, highlightedTokens, showTextRepresentation, listOptions }: ViewModeProps<{}> & { listOptions?: ListViewOptions }) {+ const labelExpandDepth = useExpansionDepth(note);const noteIds = useFilteredNoteIds(note, unfilteredNoteIds);- const { pageNotes, ...pagination } = usePagination(note, noteIds);+ const { pageNotes, ...pagination } = usePagination(note, noteIds, listOptions?.pageSize);const [ includeArchived ] = useNoteLabelBoolean(note, "includeArchived");const noteType = useNoteProperty(note, "type");+ const searchResultsLayout = (noteType === "search") || !!listOptions?.searchResultsLayout;+ const expandDepth = listOptions?.expandDepth ?? labelExpandDepth;return <NoteList note={note} viewMode="list-view" noteIds={noteIds} pagination={pagination}>- <Card className={clsx("nested-note-list", {"search-results": (noteType === "search")})}>+ <Card heading={listOptions?.title} className={clsx("nested-note-list", {"search-results": searchResultsLayout})}>{pageNotes?.map(childNote => (<ListNoteCardkey={childNote.noteId}note={childNote} parentNote={note}expandDepth={expandDepth} highlightedTokens={highlightedTokens}currentLevel={1} includeArchived={includeArchived}+ searchResultsLayout={searchResultsLayout}+ hideSubNotes={listOptions?.hideSubNotes}+ renderItemActions={listOptions?.renderItemActions}showTextRepresentation={showTextRepresentation} />))}</Card>@@ -93,7 +121,7 @@ function NoteList(props: NoteListProps) {</div>}-function ListNoteCard({ note, parentNote, highlightedTokens, currentLevel, expandDepth, includeArchived, showTextRepresentation }: {+function ListNoteCard({ note, parentNote, highlightedTokens, currentLevel, expandDepth, includeArchived, showTextRepresentation, searchResultsLayout, hideSubNotes, renderItemActions }: {note: FNote,parentNote: FNote,currentLevel: number,@@ -101,10 +129,10 @@ function ListNoteCard({ note, parentNote, highlightedTokens, currentLevel, expanhighlightedTokens: string[] | null | undefined;includeArchived: boolean;showTextRepresentation?: boolean;-}) {+} & Pick<ListViewOptions, "searchResultsLayout" | "hideSubNotes" | "renderItemActions">) {const [ isExpanded, setExpanded ] = useState(currentLevel <= expandDepth);- const notePath = getNotePath(parentNote, note);+ const notePath = getNotePath(parentNote, note, searchResultsLayout);// Reset expand state if switching to another note, or if user manually toggled expansion state.useEffect(() => setExpanded(currentLevel <= expandDepth), [ note, currentLevel, expandDepth ]);@@ -120,12 +148,18 @@ function ListNoteCard({ note, parentNote, highlightedTokens, currentLevel, expanshowTextRepresentation={showTextRepresentation} /></CardSection>- <NoteChildren note={note}- parentNote={parentNote}- highlightedTokens={highlightedTokens}- currentLevel={currentLevel}- expandDepth={expandDepth}- includeArchived={includeArchived} />+ {!hideSubNotes && (+ <NoteChildren+ note={note}+ parentNote={parentNote}+ highlightedTokens={highlightedTokens}+ currentLevel={currentLevel}+ expandDepth={expandDepth}+ searchResultsLayout={searchResultsLayout}+ renderItemActions={renderItemActions}+ includeArchived={includeArchived}+ />+ )}</>}@@ -147,9 +181,11 @@ function ListNoteCard({ note, parentNote, highlightedTokens, currentLevel, expan<NoteLink className="note-book-title"notePath={notePath}noPreview- showNotePath={parentNote.type === "search"}+ showNotePath={searchResultsLayout}highlightedTokens={highlightedTokens} />- <NoteAttributes note={note} />+ {renderItemActions+ ? <div className="note-list-item-actions">{renderItemActions(note)}</div>+ : <NoteAttributes note={note} />}<NoteMenuButton notePath={notePath} /></h5></CardSection>@@ -290,14 +326,14 @@ export function NoteContent({ note, trim, noChildrenList, highlightedTokens, increturn <div ref={contentRef} className={clsx("note-book-content", `type-${noteType}`, {"note-book-content-ready": ready})} />;}-function NoteChildren({ note, parentNote, highlightedTokens, currentLevel, expandDepth, includeArchived }: {+function NoteChildren({ note, parentNote, highlightedTokens, currentLevel, expandDepth, includeArchived, searchResultsLayout, renderItemActions }: {note: FNote,parentNote: FNote,currentLevel: number,expandDepth: number,highlightedTokens: string[] | null | undefinedincludeArchived: boolean;-}) {+} & Pick<ListViewOptions, "searchResultsLayout" | "renderItemActions">) {const [ childNotes, setChildNotes ] = useState<FNote[]>();useEffect(() => {@@ -311,6 +347,8 @@ function NoteChildren({ note, parentNote, highlightedTokens, currentLevel, expanhighlightedTokens={highlightedTokens}currentLevel={currentLevel + 1} expandDepth={expandDepth}includeArchived={includeArchived}+ searchResultsLayout={searchResultsLayout}+ renderItemActions={renderItemActions}/>);}@@ -326,8 +364,8 @@ function NoteMenuButton(props: {notePath: string}) {/>}-export function getNotePath(parentNote: FNote, childNote: FNote) {- if (parentNote.type === "search") {+export function getNotePath(parentNote: FNote, childNote: FNote, flat = parentNote.type === "search") {+ if (flat) {// for search note parent, we want to display a non-search pathreturn childNote.noteId;}diff --git a/apps/client/src/widgets/type_widgets/options/content_manager.css b/apps/client/src/widgets/type_widgets/options/content_manager.cssindex 6d6020d5f7..f648159785 100644--- a/apps/client/src/widgets/type_widgets/options/content_manager.css+++ b/apps/client/src/widgets/type_widgets/options/content_manager.css@@ -13,12 +13,3 @@opacity: 0.7;}-.content-manager-items {- display: flex;- flex-direction: column;- gap: 0.35em;-}--.content-manager-item {- margin: 0;-}diff --git a/apps/client/src/widgets/type_widgets/options/content_manager.tsx b/apps/client/src/widgets/type_widgets/options/content_manager.tsxindex 64bf7ed989..a706fb9250 100644--- a/apps/client/src/widgets/type_widgets/options/content_manager.tsx+++ b/apps/client/src/widgets/type_widgets/options/content_manager.tsx@@ -11,14 +11,30 @@ import {findCategoryNotes} from "../../../services/content_manager";import { t } from "../../../services/i18n";+import { ListView, type ListViewOptions } from "../../collections/legacy/ListOrGridView";import Button, { ButtonGroup } from "../../react/Button";import { useTriliumEvent, useTriliumOption } from "../../react/hooks";import NoItems from "../../react/NoItems";-import NoteLink from "../../react/NoteLink";+import type { TypeWidgetProps } from "../type_widget";import OptionsPageHeader from "./components/OptionsPageHeader";-import OptionsSection from "./components/OptionsSection";-export default function ContentManagerSettings() {+const NOOP = () => {};++const LIST_OPTIONS: ListViewOptions = {+ // These notes are scattered across the tree rather than being children of this page, so they are+ // listed flat with their real location shown underneath, the way search results are.+ searchResultsLayout: true,+ hideSubNotes: true,+ // Collapsed by default: the list is meant to be scanned, and expanding reveals the note's own+ // preview rather than a subtree.+ expandDepth: 0,+ pageSize: 50,+ // Deliberately empty for now: it suppresses the note's attributes and reserves the spot for the+ // enable/disable toggle and status badges.+ renderItemActions: () => null+};++export default function ContentManagerSettings({ note }: TypeWidgetProps) {const [ sortOrder, setSortOrder ] = useTriliumOption("contentManagerSortOrder");const categories = useCategoryNotes(sortOrder as ContentSortOrder);@@ -31,12 +47,12 @@ export default function ContentManagerSettings() {<SortOrderSelect currentValue={sortOrder} onChange={(newValue) => void setSortOrder(newValue)} /></div>- <CategoryList categories={categories} />+ <CategoryList pageNote={note} categories={categories} /></>);}-function CategoryList({ categories }: { categories: CategoryNotes[] | null }) {+function CategoryList({ pageNote, categories }: { pageNote: FNote, categories: CategoryNotes[] | null }) {if (!categories) {return <p className="content-manager-loading">{t("content_manager.loading")}</p>;}@@ -54,15 +70,18 @@ function CategoryList({ categories }: { categories: CategoryNotes[] | null }) {return (<>{populated.map(({ category, notes }) => (- <OptionsSection key={category.id} title={t(category.titleKey)}>- <div className="content-manager-items">- {notes.map((note) => (- <p key={note.noteId} className="content-manager-item">- <NoteLink notePath={note.noteId} showNoteIcon showNotePath />- </p>- ))}- </div>- </OptionsSection>+ <ListView+ key={category.id}+ note={pageNote}+ notePath={pageNote.noteId}+ noteIds={notes.map(({ noteId }) => noteId)}+ highlightedTokens={null}+ viewConfig={undefined}+ saveConfig={NOOP}+ media="screen"+ onReady={NOOP}+ listOptions={{ ...LIST_OPTIONS, title: t(category.titleKey) }}+ />))}</>);