Skip to content

Build your personal knowledge base with Trilium Notes

refactor(client): extract options dialog from the popup editor

Elian Doranca94469b7f24
diff --git a/apps/client/src/components/app_context.ts b/apps/client/src/components/app_context.ts
index 12f356b664..71d5a76f4f 100644
--- a/apps/client/src/components/app_context.ts
+++ b/apps/client/src/components/app_context.ts
@@ -110,7 +110,7 @@ export type CommandMappings = {
noteId?: string | null;
};
showOptions: CommandData & {
- section: string;
+ section?: string;
};
showExportDialog: CommandData & {
notePath: string;
diff --git a/apps/client/src/components/root_command_executor.ts b/apps/client/src/components/root_command_executor.ts
index 7fb1f66d51..a62c092cd5 100644
--- a/apps/client/src/components/root_command_executor.ts
+++ b/apps/client/src/components/root_command_executor.ts
@@ -113,13 +113,6 @@ export default class RootCommandExecutor extends Component {
await this.showAndHoistSubtree("_hidden");
}
- showOptionsCommand({ section }: CommandListenerData<"showOptions">) {
- // Open settings in the quick-edit popup on the first page (Appearance) by default, instead
- // of opening a new hoisted tab (which confused users by seemingly hiding the note tree).
- // The in-page settings selector lets users switch between pages from within the popup.
- void appContext.triggerCommand("openInPopup", { noteIdOrPath: section ?? "_optionsAppearance" });
- }
-
async showSQLConsoleHistoryCommand() {
await this.showAndHoistSubtree("_sqlConsole");
}
diff --git a/apps/client/src/layouts/layout_commons.tsx b/apps/client/src/layouts/layout_commons.tsx
index 1833673a40..212d6ffc68 100644
--- a/apps/client/src/layouts/layout_commons.tsx
+++ b/apps/client/src/layouts/layout_commons.tsx
@@ -19,6 +19,7 @@ import MarkdownImportDialog from "../widgets/dialogs/markdown_import.js";
import MoveToDialog from "../widgets/dialogs/move_to.js";
import NoteTypeChooserDialog from "../widgets/dialogs/note_type_chooser.js";
import OcrTextDialog from "../widgets/dialogs/ocr_text.js";
+import OptionsDialog from "../widgets/dialogs/OptionsDialog.jsx";
import PopupEditorDialog from "../widgets/dialogs/PopupEditor.jsx";
import PrintPreviewDialog from "../widgets/dialogs/print_preview.jsx";
import PromptDialog from "../widgets/dialogs/prompt.js";
@@ -56,6 +57,7 @@ export function applyModals(rootContainer: RootContainer) {
.child(<PromptDialog />)
.child(<IncorrectCpuArchDialog />)
.child(<PopupEditorDialog />)
+ .child(<OptionsDialog />)
.child(<CallToActionDialog />)
.child(<OcrTextDialog />)
.child(<UploadAttachmentsDialog />)
diff --git a/apps/client/src/widgets/dialogs/OptionsDialog.css b/apps/client/src/widgets/dialogs/OptionsDialog.css
new file mode 100644
index 0000000000..41d3882543
--- /dev/null
+++ b/apps/client/src/widgets/dialogs/OptionsDialog.css
@@ -0,0 +1,51 @@
+body.desktop .modal.options-dialog .modal-dialog {
+ max-width: min(1300px, 75vw);
+}
+
+body.mobile .modal.options-dialog .modal-dialog {
+ max-width: min(var(--preferred-max-content-width), 95vw);
+ max-height: var(--tn-modal-max-height);
+ height: 100%;
+}
+
+/* Keep the sidebar background within the rounded corners. */
+.modal.options-dialog .modal-content {
+ overflow: hidden;
+}
+
+.modal.options-dialog .modal-body {
+ padding: 0;
+ height: 75vh;
+}
+
+.modal.options-dialog .modal-sidebar {
+ background-color: var(--card-background-color);
+}
+
+.modal.options-dialog .modal-sidebar .settings-navigation {
+ flex: 1 1 0;
+ min-height: 0;
+ overflow: auto;
+ width: 240px;
+ padding: 8px;
+}
+
+/* The main header only carries the window (maximize/close) buttons, with the title moved into the
+ sidebar. Float it over the content so it doesn't leave a tall empty band at the top. */
+.modal.options-dialog .modal-main > .modal-header {
+ position: absolute;
+ top: 0;
+ right: 4px;
+ z-index: 9999;
+}
+
+.modal.options-dialog .options-section {
+ margin: auto;
+}
+
+/* Push the page content below the floating header. The variable is also read by the theme
+ (theme-next/pages.css) and zeroed on pages that manage their own spacing (e.g. shortcuts). */
+.modal.options-dialog .note-detail-content-widget-content.options {
+ --options-first-item-top-margin: 60px;
+ padding-top: var(--options-first-item-top-margin);
+}
diff --git a/apps/client/src/widgets/dialogs/OptionsDialog.tsx b/apps/client/src/widgets/dialogs/OptionsDialog.tsx
new file mode 100644
index 0000000000..5dbe510f43
--- /dev/null
+++ b/apps/client/src/widgets/dialogs/OptionsDialog.tsx
@@ -0,0 +1,94 @@
+import "./OptionsDialog.css";
+
+import { useCallback, useContext, useRef, useState } from "preact/hooks";
+
+import appContext from "../../components/app_context";
+import NoteContext from "../../components/note_context";
+import { t } from "../../services/i18n";
+import NoteDetail from "../NoteDetail";
+import { useContainedLinkNavigation, useNoteContext, useTriliumEvent } from "../react/hooks";
+import Modal from "../react/Modal";
+import { NoteContextContext, ParentComponent } from "../react/react_utils";
+import SettingsNavigation from "../type_widgets/options/components/SettingsNavigation";
+
+/** The settings page shown when no specific section was requested and none was viewed yet this session. */
+const DEFAULT_SECTION = "_optionsAppearance";
+
+/**
+ * The settings dialog, opened via the `showOptions` command. Settings open in a dialog rather than
+ * a hoisted tab (which confused users by seemingly hiding the note tree): the full-height sidebar
+ * lists the settings pages while the body renders the active one through a dedicated note context.
+ */
+export default function OptionsDialog() {
+ const [ shown, setShown ] = useState(false);
+ const parentComponent = useContext(ParentComponent);
+ const [ noteContext, setNoteContext ] = useState(() => new NoteContext("_options-dialog"));
+ // Remembers the page last viewed this session so reopening the dialog lands there instead of
+ // always on Appearance. Kept in component state (resets on reload), not persisted.
+ const [ lastSection, setLastSection ] = useState<string | null>(null);
+ const modalRef = useRef<HTMLDivElement>(null);
+
+ useTriliumEvent("showOptions", async ({ section }) => {
+ const noteContext = new NoteContext("_options-dialog");
+ await noteContext.setNote(section ?? lastSection ?? DEFAULT_SECTION, { keepActiveDialog: true });
+
+ // Events triggered at note context level (e.g. the save indicator) would not work since the note context has no parent component. Propagate events to parent component so that they can be handled properly.
+ noteContext.triggerEvent = (name, data) => parentComponent?.handleEventInChildren(name, data);
+ setNoteContext(noteContext);
+ setShown(true);
+ });
+
+ // Keep navigation between settings pages (sidebar entries, "Related settings" links) inside the
+ // dialog; links to regular notes open in the quick-edit popup instead.
+ useContainedLinkNavigation(modalRef, useCallback((notePath, viewScope) => {
+ if (notePath.split("/").at(-1)?.startsWith("_options")) {
+ void noteContext.setNote(notePath, { viewScope, keepActiveDialog: true });
+ } else {
+ appContext.triggerCommand("openInPopup", { noteIdOrPath: notePath });
+ }
+ }, [ noteContext ]));
+
+ return (
+ <NoteContextContext.Provider value={noteContext}>
+ <Modal
+ modalRef={modalRef}
+ title={t("options.title")}
+ sidebar={<SettingsSidebar />}
+ customTitleBarButtons={[{
+ iconClassName: "bx-expand-alt",
+ title: t("popup-editor.maximize"),
+ onClick: async () => {
+ if (!noteContext.noteId) return;
+ const { noteId, hoistedNoteId } = noteContext;
+ await appContext.tabManager.openInNewTab(noteId, hoistedNoteId, true);
+ setShown(false);
+ }
+ }]}
+ className="options-dialog"
+ size="lg"
+ show={shown}
+ onHidden={() => {
+ // Remember the settings page in view so the next open lands on it.
+ if (noteContext.noteId) {
+ setLastSection(noteContext.noteId);
+ }
+ setShown(false);
+ }}
+ >
+ <NoteDetail />
+ </Modal>
+ </NoteContextContext.Provider>
+ );
+}
+
+/**
+ * The settings page selector shown in the dialog's sidebar. It derives the active page from
+ * `useNoteContext()` (resolved against the dialog's own context via the surrounding provider) so the
+ * highlighted entry tracks navigation. The link clicks themselves are handled by the dialog's
+ * {@link useContainedLinkNavigation} interceptor.
+ */
+function SettingsSidebar() {
+ const { noteId } = useNoteContext();
+ if (!noteId) return null;
+ return <SettingsNavigation activeNoteId={noteId} />;
+}
diff --git a/apps/client/src/widgets/dialogs/PopupEditor.css b/apps/client/src/widgets/dialogs/PopupEditor.css
index be4b4ef806..a03b5d3cac 100644
--- a/apps/client/src/widgets/dialogs/PopupEditor.css
+++ b/apps/client/src/widgets/dialogs/PopupEditor.css
@@ -134,40 +134,3 @@ body.mobile .modal.popup-editor-dialog .modal-dialog {
margin-inline: var(--content-margin-inline);
}
}
-
-/* Settings page selector shown in the popup's full-height sidebar (revisions-style layout). */
-.modal.popup-editor-dialog .modal-sidebar {
- background-color: var(--card-background-color);
-}
-
-.modal.popup-editor-dialog .modal-sidebar .settings-navigation {
- flex: 1 1 0;
- min-height: 0;
- overflow: auto;
- width: 240px;
- padding: 8px;
-}
-
-.modal.popup-editor-dialog .options-section {
- margin: auto;
-}
-
-.modal.popup-editor-dialog .modal-content-with-sidebar .note-detail-content-widget-content.options {
- --options-first-item-top-margin: 60px;
- padding-top: var(--options-first-item-top-margin);
-}
-
-/* In the settings layout the main header only carries the window (maximize/close) buttons, with the
- title moved into the sidebar. Slim that header and drop the page's own top spacing so the content
- starts right beneath it, instead of leaving a tall empty band at the top of the content area. */
-.modal.popup-editor-dialog .modal-content-with-sidebar {
- max-width: 1300px;
- margin: auto;
-
- .modal-header {
- position: absolute;
- top: 0;
- right: 4px;
- z-index: 9999;
- }
-}
diff --git a/apps/client/src/widgets/dialogs/PopupEditor.tsx b/apps/client/src/widgets/dialogs/PopupEditor.tsx
index f1acf3e582..8fef7674e8 100644
--- a/apps/client/src/widgets/dialogs/PopupEditor.tsx
+++ b/apps/client/src/widgets/dialogs/PopupEditor.tsx
@@ -1,14 +1,13 @@
import "./PopupEditor.css";
-import { ComponentChildren, RefObject } from "preact";
-import { useContext, useEffect, useMemo, useRef, useState } from "preact/hooks";
+import { ComponentChildren } from "preact";
+import { useCallback, useContext, useEffect, useMemo, useRef, useState } from "preact/hooks";
import appContext from "../../components/app_context";
import NoteContext from "../../components/note_context";
import { isExperimentalFeatureEnabled } from "../../services/experimental_features";
import froca from "../../services/froca";
import { t } from "../../services/i18n";
-import { parseNavigationStateFromUrl } from "../../services/link";
import tree from "../../services/tree";
import utils from "../../services/utils";
import NoteList from "../collections/NoteList";
@@ -19,29 +18,21 @@ import NoteIcon from "../note_icon";
import NoteTitleWidget from "../note_title";
import NoteDetail from "../NoteDetail";
import PromotedAttributes from "../PromotedAttributes";
-import { useNoteContext, useNoteLabel, useTriliumEvent } from "../react/hooks";
+import { useContainedLinkNavigation, useNoteContext, useNoteLabel, useTriliumEvent } from "../react/hooks";
import Modal from "../react/Modal";
import { NoteContextContext, ParentComponent } from "../react/react_utils";
import ReadOnlyNoteInfoBar from "../ReadOnlyNoteInfoBar";
import StandaloneRibbonAdapter from "../ribbon/components/StandaloneRibbonAdapter";
import FormattingToolbar from "../ribbon/FormattingToolbar";
-import SettingsNavigation from "../type_widgets/options/components/SettingsNavigation";
import MobileEditorToolbar from "../type_widgets/text/mobile_editor_toolbar";
const isNewLayout = isExperimentalFeatureEnabled("new-layout");
-/** The settings page the cog opens by default — see `showOptionsCommand`. Treated as the entry point
- * that gets redirected to the last-viewed section. */
-const DEFAULT_OPTIONS_SECTION = "_optionsAppearance";
-
export default function PopupEditor() {
const [ shown, setShown ] = useState(false);
const [ stacked, setStacked ] = useState(false);
const parentComponent = useContext(ParentComponent);
const [ noteContext, setNoteContext ] = useState(new NoteContext("_popup-editor"));
- // Remembers the settings page last viewed this session so reopening the options popup lands there
- // instead of always on Appearance. Kept in component state (resets on reload), not persisted.
- const [ lastOptionsSection, setLastOptionsSection ] = useState<string | null>(null);
const modalRef = useRef<HTMLDivElement>(null);
const isMobile = utils.isMobile();
const items = useMemo(() => {
@@ -50,21 +41,22 @@ export default function PopupEditor() {
}, [ isMobile ]);
useTriliumEvent("openInPopup", async ({ noteIdOrPath }) => {
- const noteContext = new NoteContext("_popup-editor");
- setStacked(!!document.querySelector(".modal.show"));
-
- // Reopen settings on the page last viewed this session instead of always on Appearance.
- const targetIdOrPath = noteIdOrPath === DEFAULT_OPTIONS_SECTION && lastOptionsSection
- ? lastOptionsSection
- : noteIdOrPath;
-
- const noteId = tree.getNoteIdAndParentIdFromUrl(targetIdOrPath);
+ const noteId = tree.getNoteIdAndParentIdFromUrl(noteIdOrPath);
if (!noteId.noteId) return;
const note = await froca.getNote(noteId.noteId);
if (!note) return;
+ // Settings pages are displayed in their own dedicated dialog with the page selector sidebar.
+ if (note.isOptions()) {
+ void appContext.triggerCommand("showOptions", { section: noteId.noteId });
+ return;
+ }
+
+ const noteContext = new NoteContext("_popup-editor");
+ setStacked(!!document.querySelector(".modal.show"));
+
const hasUserSetNoteReadOnly = note.hasLabel("readOnly");
- await noteContext.setNote(targetIdOrPath, {
+ await noteContext.setNote(noteIdOrPath, {
viewScope: {
// Override auto-readonly notes to be editable, but respect user's choice to have a read-only note.
readOnlyTemporarilyDisabled: !hasUserSetNoteReadOnly
@@ -79,8 +71,15 @@ export default function PopupEditor() {
});
// Keep navigation that follows internal links inside the popup, rather than letting the global
- // link handler open the target in the background tab.
- useContainedLinkNavigation(modalRef, noteContext);
+ // link handler open the target in the background tab. Settings links open the options dialog.
+ useContainedLinkNavigation(modalRef, useCallback((notePath, viewScope) => {
+ const targetNoteId = notePath.split("/").at(-1);
+ if (targetNoteId?.startsWith("_options")) {
+ void appContext.triggerCommand("showOptions", { section: targetNoteId });
+ } else {
+ void noteContext.setNote(notePath, { viewScope, keepActiveDialog: true });
+ }
+ }, [ noteContext ]));
// Add a global class to be able to handle issues with z-index due to rendering in a popup.
useEffect(() => {
@@ -100,18 +99,12 @@ export default function PopupEditor() {
return () => popupBackdrop.classList.remove("popup-editor-backdrop");
}, [shown, stacked]);
- // Settings pages get the revisions-style layout: a full-height sidebar (the page selector) with
- // the "Options" title above it, instead of the per-note title bar. This avoids duplicating the
- // current page name between the title and the highlighted selector entry.
- const isOptions = noteContext.note?.isOptions() ?? false;
-
return (
<NoteContextContext.Provider value={noteContext}>
<DialogWrapper>
<Modal
modalRef={modalRef}
- title={isOptions ? t("options.title") : <TitleRow />}
- sidebar={isOptions ? <SettingsPopupSidebar /> : undefined}
+ title={<TitleRow />}
customTitleBarButtons={[{
iconClassName: "bx-expand-alt",
title: t("popup-editor.maximize"),
@@ -126,14 +119,7 @@ export default function PopupEditor() {
size="lg"
show={shown}
onShown={() => parentComponent?.handleEvent("focusOnDetail", { ntxId: noteContext.ntxId })}
- onHidden={() => {
- // Remember the settings page in view so the next open lands on it.
- const note = noteContext.note;
- if (note?.isOptions()) {
- setLastOptionsSection(note.noteId);
- }
- setShown(false);
- }}
+ onHidden={() => setShown(false)}
keepInDom // needed for faster loading
noFocus // automatic focus breaks block popup
stackable
@@ -175,52 +161,3 @@ export function TitleRow() {
</div>
);
}
-
-/**
- * The settings page selector shown in the quick-edit popup's sidebar. It derives the active page from
- * `useNoteContext()` (resolved against the popup's own context via the surrounding provider) so the
- * highlighted entry tracks navigation. The link clicks themselves are handled by the popup's
- * {@link useContainedLinkNavigation} interceptor.
- */
-function SettingsPopupSidebar() {
- const { noteId } = useNoteContext();
- if (!noteId) return null;
- return <SettingsNavigation activeNoteId={noteId} />;
-}
-
-/**
- * Keeps navigation that follows internal note links (note links, reference links, "Related settings",
- * etc.) inside the popup — whose context lives outside the tab manager — instead of letting the
- * global link handler resolve to the active tab in the background.
- *
- * The listener is attached to `containerRef` in the capture phase so it runs before the
- * document-level `goToLink` handler (and before anything that might stop propagation). Modified or
- * middle clicks and external links are left untouched so they can still open in a new tab/window or
- * externally, and clicks inside editable rich text are ignored so they keep placing the caret.
- */
-function useContainedLinkNavigation(containerRef: RefObject<HTMLElement>, noteContext: NoteContext) {
- useEffect(() => {
- const container = containerRef.current;
- if (!container) return;
-
- function onClick(e: MouseEvent) {
- if (e.defaultPrevented || e.button !== 0 || e.ctrlKey || e.metaKey || e.shiftKey || e.altKey) return;
-
- const link = (e.target as HTMLElement).closest("a");
- if (!link || link.getAttribute("target") === "_blank" || link.isContentEditable) return;
-
- const href = link.getAttribute("href") ?? link.getAttribute("data-href");
- if (!href?.startsWith("#root/")) return; // external links / in-page anchors handled elsewhere
-
- const { notePath, viewScope } = parseNavigationStateFromUrl(href);
- if (!notePath) return;
-
- e.preventDefault();
- e.stopPropagation();
- void noteContext.setNote(notePath, { viewScope, keepActiveDialog: true });
- }
-
- container.addEventListener("click", onClick, true);
- return () => container.removeEventListener("click", onClick, true);
- }, [ containerRef, noteContext ]);
-}
diff --git a/apps/client/src/widgets/react/hooks.tsx b/apps/client/src/widgets/react/hooks.tsx
index 2d1bb905a8..8627be21d7 100644
--- a/apps/client/src/widgets/react/hooks.tsx
+++ b/apps/client/src/widgets/react/hooks.tsx
@@ -14,7 +14,7 @@ import FNote from "../../entities/fnote";
import attributes from "../../services/attributes";
import froca from "../../services/froca";
import keyboard_actions from "../../services/keyboard_actions";
-import { ViewScope } from "../../services/link";
+import { parseNavigationStateFromUrl, ViewScope } from "../../services/link";
import math from "../../services/math";
import options, { type OptionValue } from "../../services/options";
import protected_session_holder from "../../services/protected_session_holder";
@@ -1600,3 +1600,45 @@ export function useMathRendering(containerRef: RefObject<HTMLElement>, deps: unk
}
}, deps); // eslint-disable-line react-hooks/exhaustive-deps
}
+
+/**
+ * Keeps navigation that follows internal note links (note links, reference links, "Related settings",
+ * etc.) inside a popup dialog — whose note context lives outside the tab manager — instead of letting
+ * the global link handler resolve to the active tab in the background. The dialog decides what to do
+ * with the parsed target via `onNavigate` (typically `noteContext.setNote()` or routing to another
+ * dialog).
+ *
+ * The listener is attached to `containerRef` in the capture phase so it runs before the
+ * document-level `goToLink` handler (and before anything that might stop propagation). Modified or
+ * middle clicks and external links are left untouched so they can still open in a new tab/window or
+ * externally, and clicks inside editable rich text are ignored so they keep placing the caret.
+ */
+export function useContainedLinkNavigation(
+ containerRef: RefObject<HTMLElement>,
+ onNavigate: (notePath: string, viewScope: ViewScope | undefined) => void
+) {
+ useEffect(() => {
+ const container = containerRef.current;
+ if (!container) return;
+
+ function onClick(e: MouseEvent) {
+ if (e.defaultPrevented || e.button !== 0 || e.ctrlKey || e.metaKey || e.shiftKey || e.altKey) return;
+
+ const link = (e.target as HTMLElement).closest("a");
+ if (!link || link.getAttribute("target") === "_blank" || link.isContentEditable) return;
+
+ const href = link.getAttribute("href") ?? link.getAttribute("data-href");
+ if (!href?.startsWith("#root/")) return; // external links / in-page anchors handled elsewhere
+
+ const { notePath, viewScope } = parseNavigationStateFromUrl(href);
+ if (!notePath) return;
+
+ e.preventDefault();
+ e.stopPropagation();
+ onNavigate(notePath, viewScope);
+ }
+
+ container.addEventListener("click", onClick, true);
+ return () => container.removeEventListener("click", onClick, true);
+ }, [ containerRef, onNavigate ]);
+}