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
feat(client): configure the highlights list without covering it
Elian Doran31f83d3b67c5
diff --git a/apps/client/src/translations/en/translation.json b/apps/client/src/translations/en/translation.jsonindex b3d142743c..b1d33cda18 100644--- a/apps/client/src/translations/en/translation.json+++ b/apps/client/src/translations/en/translation.json@@ -1396,7 +1396,7 @@"attributes_definitions_what": "A definition is the schema behind an attribute name: what kind of value it takes, whether it may be given more than once, and whether it is promoted.","attributes_definitions_promoted": "A promoted attribute is shown on the note itself as a form field rather than being left to this panel. Definitions usually arrive from a template, and each row names the note it lives on.","highlights_what": "The passages this note picks out by their formatting. That is a wider net than it sounds: an ordinary bold or italic sentence counts, not only text you have highlighted.",- "highlights_configure": "Which formats count is yours to set: bold, italic, underlined, coloured text and text on a background colour can each be listed or left out, from the configure entry in this card's menu. Pressing an entry scrolls the note to it.",+ "highlights_configure": "Which formats count is yours to set: bold, italic, underlined, coloured text and text on a background colour can each be listed or left out, from the gear in this card's header. Pressing an entry scrolls the note to it.","note_map_what": "A map of what this note connects to. The link map draws the internal links and relations going in and out of it; the tree map draws where it sits in the note tree. The switcher in this header chooses between the two.","note_map_interaction": "Hovering a note brings its own connections forward and fades the rest. The expand button opens the same map at the size of the window.","note_paths_what": "A note can sit in more than one place in the note tree at once, and each of those places is listed here as a path.",@@ -2461,7 +2461,6 @@"title_with_count_one": "{{count}} highlight","title_with_count_other": "{{count}} highlights","options": "Options",- "modal_title": "Configure Highlights List","menu_configure": "Configure highlights list...","no_highlights": "No highlights found."},diff --git a/apps/client/src/widgets/sidebar/HighlightsList.tsx b/apps/client/src/widgets/sidebar/HighlightsList.tsxindex 5fc3489e3c..0f6108d588 100644--- a/apps/client/src/widgets/sidebar/HighlightsList.tsx+++ b/apps/client/src/widgets/sidebar/HighlightsList.tsx@@ -1,13 +1,13 @@import type { CKTextEditor, ModelText } from "@triliumnext/ckeditor5";-import { createPortal } from "preact/compat";import { useCallback, useEffect, useRef, useState } from "preact/hooks";import { t } from "../../services/i18n";import { randomString } from "../../services/utils";+import Dropdown from "../react/Dropdown";+import { FormListToggleableItem } from "../react/FormList";import { useActiveNoteContext, useContentElement, useIsNoteReadOnly, useMathRendering, useNoteProperty, useTextEditor, useTriliumOptionJson } from "../react/hooks";-import Modal from "../react/Modal";import RawHtml from "../react/RawHtml";-import { HighlightsListOptions } from "../type_widgets/options/highlights_list_options";+import { HIGHLIGHT_FORMATS, HighlightFormat } from "../type_widgets/options/highlights_list_options";import RightPanelWidget from "./RightPanelWidget";import SidebarHelp from "./SidebarHelp";@@ -36,25 +36,11 @@ export default function HighlightsList() {);}-function HighlightListOptionsModal({ shown, setShown }: { shown: boolean, setShown(value: boolean): void }) {- return (- <Modal- className="highlights-list-options-modal"- size="md"- title={t("highlights_list_2.modal_title")}- show={shown}- onHidden={() => setShown(false)}- >- <HighlightsListOptions />- </Modal>- );-}-function AbstractHighlightsList<T extends RawHighlight>({ highlights, scrollToHighlight }: {highlights: T[],scrollToHighlight(highlight: T): void;}) {- const [ highlightsList ] = useTriliumOptionJson<["bold" | "italic" | "underline" | "color" | "bgColor"]>("highlightsList");+ const [ highlightsList, setHighlightsList ] = useTriliumOptionJson<HighlightFormat[]>("highlightsList");const highlightsListSet = new Set(highlightsList || []);const filteredHighlights = highlights.filter(highlight => {const { attrs } = highlight;@@ -67,42 +53,76 @@ function AbstractHighlightsList<T extends RawHighlight>({ highlights, scrollToHi);});- const [ shown, setShown ] = useState(false);return (- <>- <RightPanelWidget- id="highlights"- title={t("highlights_list_2.title_with_count", { count: filteredHighlights.length })}- buttons={<SidebarHelp section="highlights" />}- contextMenuItems={[- {- title: t("highlights_list_2.menu_configure"),- uiIcon: "bx bx-cog",- handler: () => setShown(true)- }- ]}- grow- >- <span className="highlights-list">- {filteredHighlights.length > 0 ? (- <ol>- {filteredHighlights.map(highlight => (- <HighlightItem- key={highlight.id}- highlight={highlight}- onClick={() => scrollToHighlight(highlight)}- />- ))}- </ol>- ) : (- <div className="no-highlights">- {t("highlights_list_2.no_highlights")}- </div>- )}- </span>- </RightPanelWidget>- {createPortal(<HighlightListOptionsModal shown={shown} setShown={setShown} />, document.body)}- </>+ <RightPanelWidget+ id="highlights"+ title={t("highlights_list_2.title_with_count", { count: filteredHighlights.length })}+ // What the list counts as a highlight is the question the card raises and the setting that+ // answers it, so the setting sits in the header rather than behind a menu holding nothing+ // else — and as a menu of its own rather than a modal, see HighlightsListMenu.+ buttons={<>+ <SidebarHelp section="highlights" />+ <HighlightsListMenu currentValue={highlightsList} onChange={setHighlightsList} />+ </>}+ grow+ >+ <span className="highlights-list">+ {filteredHighlights.length > 0 ? (+ <ol>+ {filteredHighlights.map(highlight => (+ <HighlightItem+ key={highlight.id}+ highlight={highlight}+ onClick={() => scrollToHighlight(highlight)}+ />+ ))}+ </ol>+ ) : (+ <div className="no-highlights">+ {t("highlights_list_2.no_highlights")}+ </div>+ )}+ </span>+ </RightPanelWidget>+ );+}++/**+ * The choice of what the list counts as a highlight, offered from the card's own header.+ *+ * A menu rather than a modal, because the toggles are a filter on the list right behind them: a modal+ * covers the very thing being filtered, so the effect of a toggle is only seen once it is dismissed,+ * whereas the list stays on show under a menu and re-filters as each format is turned over.+ *+ * Which is also why the rows are {@link FormListToggleableItem}s: they stop the press reaching+ * Bootstrap's own dismiss, so the menu survives several toggles instead of closing after the first.+ */+function HighlightsListMenu({ currentValue, onChange }: {+ currentValue: HighlightFormat[];+ onChange(newValue: HighlightFormat[]): Promise<void>;+}) {+ return (+ <Dropdown+ buttonClassName="bx bx-cog"+ title={t("highlights_list_2.menu_configure")}+ iconAction+ hideToggleArrow+ noSelectButtonStyle+ // The card establishes a stacking context of its own and clips its overflow, so the menu+ // is rendered into the body to stand clear of it — as the card's help popup is.+ portalToBody+ >+ {HIGHLIGHT_FORMATS.map(({ val, titleKey }) => (+ <FormListToggleableItem+ key={val}+ title={t(titleKey)}+ currentValue={currentValue.includes(val)}+ onChange={(checked) => onChange(checked+ ? [ ...currentValue, val ]+ : currentValue.filter((format) => format !== val))}+ />+ ))}+ </Dropdown>);}diff --git a/apps/client/src/widgets/type_widgets/options/highlights_list_options.tsx b/apps/client/src/widgets/type_widgets/options/highlights_list_options.tsxindex 5258d8eb59..d8bc0aab04 100644--- a/apps/client/src/widgets/type_widgets/options/highlights_list_options.tsx+++ b/apps/client/src/widgets/type_widgets/options/highlights_list_options.tsx@@ -4,27 +4,41 @@ import { useTriliumOptionJson } from "../../react/hooks";import CheckboxList from "./components/CheckboxList";/**- * Lives in its own module (rather than in `text_notes.tsx`) because it is also rendered by the- * always-loaded highlights list sidebar, while the rest of the text note options pull in heavy- * dependencies such as highlight.js.+ * Lives in its own module (rather than in `text_notes.tsx`) because the always-loaded highlights list+ * sidebar reads {@link HIGHLIGHT_FORMATS} from here — and used to render this page itself, before it+ * grew a menu of its own — while the rest of the text note options pull in heavy dependencies such as+ * highlight.js.*/export function HighlightsListOptions() {- const [ highlightsList, setHighlightsList ] = useTriliumOptionJson<string[]>("highlightsList");+ const [ highlightsList, setHighlightsList ] = useTriliumOptionJson<HighlightFormat[]>("highlightsList");return (<><FormText>{t("highlights_list.description")}</FormText><CheckboxList- values={[- { val: "bold", title: t("highlights_list.bold") },- { val: "italic", title: t("highlights_list.italic") },- { val: "underline", title: t("highlights_list.underline") },- { val: "color", title: t("highlights_list.color") },- { val: "bgColor", title: t("highlights_list.bg_color") }- ]}+ values={HIGHLIGHT_FORMATS.map(({ val, titleKey }) => ({ val, title: t(titleKey) }))}keyProperty="val" titleProperty="title"- currentValue={highlightsList} onChange={setHighlightsList}+ currentValue={highlightsList}+ onChange={(values) => setHighlightsList(values as HighlightFormat[])}/></>);}++/**+ * The formats that can each be counted as a highlight or left out, in the order they are offered.+ * Shared with the sidebar's highlights list, which offers the same choice from its own gear menu.+ *+ * The titles are held as keys rather than resolved here: a module-level `t()` would run before the+ * translations have loaded.+ */+export const HIGHLIGHT_FORMATS = [+ { val: "bold", titleKey: "highlights_list.bold" },+ { val: "italic", titleKey: "highlights_list.italic" },+ { val: "underline", titleKey: "highlights_list.underline" },+ { val: "color", titleKey: "highlights_list.color" },+ { val: "bgColor", titleKey: "highlights_list.bg_color" }+] as const;++/** One of the formats the list can be told to count; the option holds a set of these. */+export type HighlightFormat = typeof HIGHLIGHT_FORMATS[number]["val"];