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
import "./InlineTitle.css";
import { NoteType } from "@triliumnext/commons";
import { Tooltip } from "bootstrap";
import clsx from "clsx";
import { ComponentChild } from "preact";
import { useLayoutEffect, useMemo, useRef, useState } from "preact/hooks";
import type React from "react";
import { Trans } from "react-i18next";
import FNote from "../../entities/fnote";
import { ViewScope } from "../../services/link";
import { formatDateTime } from "../../utils/formatters";
import NoteIcon from "../note_icon";
import NoteTitleWidget from "../note_title";
import { useNoteContext, useNoteProperty, useStaticTooltip } from "../react/hooks";
import { joinElements } from "../react/react_utils";
import { useNoteMetadata } from "./note_metadata";
export default function InlineTitle() {
const { note, parentComponent, viewScope } = useNoteContext();
const type = useNoteProperty(note, "type");
const mime = useNoteProperty(note, "mime");
const isOptions = note?.noteId?.startsWith("_options") ?? false;
const [ shown, setShown ] = useState(shouldShowInlineTitle(note, type, viewScope));
const containerRef = useRef<HTMLDivElement>(null);
const [ titleHidden, setTitleHidden ] = useState(false);
useLayoutEffect(() => {
setShown(shouldShowInlineTitle(note, type, viewScope));
}, [ note, type, mime, viewScope ]);
// Options pages render their own title (OptionsPageHeader) inside the content, so collapse the
// note's own sticky title-row for them — `hide-title` only fades it (keeping its height), which
// would leave an empty band above the embedded header, so hide the row outright instead.
useLayoutEffect(() => {
if (!isOptions) return;
const titleRow = parentComponent.$widget[0].closest(".note-split")?.querySelector<HTMLElement>(":scope > .title-row");
if (!titleRow) return;
const previousDisplay = titleRow.style.display;
titleRow.style.display = "none";
return () => { titleRow.style.display = previousDisplay; };
}, [ isOptions, parentComponent ]);
useLayoutEffect(() => {
if (!shown) return;
const titleRow = parentComponent.$widget[0].closest(".note-split")?.querySelector(":scope > .title-row");
if (!titleRow) return;
titleRow.classList.toggle("hide-title", true);
const stopObserving = containerRef.current && observeTitleInView(containerRef.current, (isInView) => {
titleRow.classList.toggle("hide-title", isInView);
setTitleHidden(!isInView);
});
return () => {
titleRow.classList.remove("hide-title");
stopObserving?.();
};
}, [ shown, parentComponent ]);
return (
<div
ref={containerRef}
className={clsx("inline-title", !shown && "hidden")}
>
<div class={clsx("inline-title-row", titleHidden && "hidden")}>
<NoteIcon />
<div class="note-title-caption">
<NoteTitleWidget />
<NoteTitleDetails />
</div>
</div>
</div>
);
}
/**
* Calls `onChange` as `title` comes into view in its scrolling container or leaves it. Measured
* against the container rather than the viewport, so a note sliding in from off-screen (the
* navigation stack on iPhones) keeps its title in view while it moves.
*/
export function observeTitleInView(title: HTMLElement, onChange: (isInView: boolean) => void) {
const observer = new IntersectionObserver(([ entry ]) => {
// A title with no box sits in a hidden tab, which shows it again as it was left.
const { width, height } = entry.boundingClientRect;
if (!entry.isIntersecting && !width && !height) return;
onChange(entry.isIntersecting);
}, {
root: title.closest(".scrolling-container"),
threshold: 0.85
});
observer.observe(title);
return () => observer.disconnect();
}
export function shouldShowInlineTitle(note: FNote | null | undefined, type: NoteType | undefined, viewScope: ViewScope | undefined) {
if (viewScope?.viewMode !== "default") return false;
// Options pages provide their own title via OptionsPageHeader, so this inline title stays hidden
// for them (and the sticky title-row is hidden too — see the effect in InlineTitle).
if (note?.noteId?.startsWith("_options")) return false;
// A code note fills the pane and scrolls inside its own editor, so nothing ever scrolls past
// this title. The sticky title row in .note-split carries the title for those notes instead.
return type === "text";
}
//#region Title details
export function NoteTitleDetails() {
const { note } = useNoteContext();
const { metadata } = useNoteMetadata(note);
const isHiddenNote = note?.noteId.startsWith("_");
const items: ComponentChild[] = [
(!isHiddenNote && metadata?.dateCreated &&
<TextWithValue
i18nKey="note_title.created_on"
value={formatDateTime(metadata.dateCreated, "medium", "none")}
valueTooltip={formatDateTime(metadata.dateCreated, "full", "long")}
/>),
(!isHiddenNote && metadata?.dateModified &&
<TextWithValue
i18nKey="note_title.last_modified"
value={formatDateTime(metadata.dateModified, "medium", "none")}
valueTooltip={formatDateTime(metadata.dateModified, "full", "long")}
/>)
].filter(item => !!item);
return items.length > 0 && (
<div className="title-details">
{joinElements(items, " • ")}
</div>
);
}
function TextWithValue({ i18nKey, value, valueTooltip }: {
i18nKey: string;
value: string;
valueTooltip: string;
}) {
const listItemRef = useRef<HTMLLIElement>(null);
const tooltipConfig: Partial<Tooltip.Options> = useMemo(() => ({
selector: "span.value",
title: valueTooltip,
popperConfig: { placement: "bottom" },
animation: false
}), [ valueTooltip ]);
useStaticTooltip(listItemRef, tooltipConfig);
return (
<li ref={listItemRef}>
<Trans
i18nKey={i18nKey}
components={{
Value: <span className="value">{value}</span> as React.ReactElement
}}
/>
</li>
);
}
//#endregion