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
fix(client): run a split's button command in that split
Elian Doran57ab7f0e20ce
diff --git a/apps/client/src/widgets/react/ActionButton.spec.tsx b/apps/client/src/widgets/react/ActionButton.spec.tsxindex 4dad85b77b..595108b543 100644--- a/apps/client/src/widgets/react/ActionButton.spec.tsx+++ b/apps/client/src/widgets/react/ActionButton.spec.tsx@@ -42,6 +42,23 @@ describe("ActionButton", () => {expect(triggerCommand).toHaveBeenCalledWith("runActiveNote", { ntxId: "_popup-editor" });});+ it("names the note context of the closest legacy ancestor, such as a split, when no provider surrounds it", () => {+ const split = Object.assign(new Component(), { noteContext: new NoteContext("split-2") });+ const parentComponent = new Component();+ split.child(parentComponent);+ const triggerCommand = vi.spyOn(parentComponent, "triggerCommand").mockReturnValue(undefined);++ act(() => render(+ <ParentComponent.Provider value={parentComponent}>+ <ActionButton icon="bx bx-play" text="Run" triggerCommand="runActiveNote" />+ </ParentComponent.Provider>, container));++ const button = container.querySelector("button");+ expect(button).not.toBeNull();+ act(() => button?.click());++ expect(triggerCommand).toHaveBeenCalledWith("runActiveNote", { ntxId: "split-2" });+ });it("dismisses its tooltip when pressed, so it cannot sit on top of what the press opened", async () => {const onClick = vi.fn();diff --git a/apps/client/src/widgets/react/hooks.tsx b/apps/client/src/widgets/react/hooks.tsxindex f10613fea3..b0461c4987 100644--- a/apps/client/src/widgets/react/hooks.tsx+++ b/apps/client/src/widgets/react/hooks.tsx@@ -35,7 +35,7 @@ import BasicWidget, { ReactWrappedWidget } from "../basic_widget";import NoteContextAwareWidget from "../note_context_aware_widget";import { DragData } from "../note_tree";import { noteSavedDataStore } from "./NoteStore";-import { NoteContextContext, ParentComponent, refToJQuerySelector } from "./react_utils";+import { findClosestNoteContext, NoteContextContext, ParentComponent, refToJQuerySelector } from "./react_utils";import type FAttachment from "../../entities/fattachment";export function useTriliumEvent<T extends EventNames>(eventName: T, handler: (data: EventData<T>) => void) {@@ -507,26 +507,6 @@ export function useNoteContext() {};}-/**- * Finds the note context held by the closest legacy ancestor component (e.g. the note split's- * `NoteWrapperWidget`). Used to initialize {@link useNoteContext} for components that mount after- * the initial `setNoteContext` event has been dispatched (e.g. components rendered via- * `LazyComponent`), which would otherwise not know their context until the next note switch.- */-function findClosestNoteContext(component: Component | null): NoteContext | undefined {- let current: Component | undefined = component ?? undefined;- while (current) {- if ("noteContext" in current) {- const { noteContext } = current as { noteContext?: NoteContext };- if (noteContext) {- return noteContext;- }- }- current = current.parent as Component | undefined;- }- return undefined;-}-/*** Whether the tab holding `noteContext` has been shown since the component mounted. A tab* restored or opened in the background reads `false` until the user switches to it. Every splitdiff --git a/apps/client/src/widgets/react/react_utils.tsx b/apps/client/src/widgets/react/react_utils.tsxindex 259763db49..5652d37f1f 100644--- a/apps/client/src/widgets/react/react_utils.tsx+++ b/apps/client/src/widgets/react/react_utils.tsx@@ -14,18 +14,41 @@ export const POPUP_EDITOR_NTX_ID = "_popup-editor";/*** A click handler that triggers `command` from the surrounding component with the `ntxId` of the- * surrounding `NoteContextContext`, so a handler can act on the note of a context that is not the- * active tab, such as the quick edit popup's. Undefined when there is no command to trigger.+ * note context the component stands in: the surrounding `NoteContextContext`, or else the one held by+ * the closest legacy ancestor, such as a split's. A handler can then act on the note of a context+ * that is not the active one, such as the quick edit popup's or an inactive split's, since a split+ * activates only once the click has reached it. Undefined when there is no command to trigger.*/export function useCommandTrigger(command: CommandNames | undefined) {const parentComponent = useContext(ParentComponent);const noteContext = useContext(NoteContextContext);return command && (() => {- parentComponent?.triggerCommand(command, { ntxId: noteContext?.ntxId } as CommandMappings[typeof command]);+ const ntxId = (noteContext ?? findClosestNoteContext(parentComponent))?.ntxId;+ parentComponent?.triggerCommand(command, { ntxId } as CommandMappings[typeof command]);});}+/**+ * Finds the note context held by the closest legacy ancestor component (e.g. the note split's+ * `NoteWrapperWidget`). Used to initialize `useNoteContext()` for components that mount after+ * the initial `setNoteContext` event has been dispatched (e.g. components rendered via+ * `LazyComponent`), which would otherwise not know their context until the next note switch.+ */+export function findClosestNoteContext(component: Component | null): NoteContext | undefined {+ let current: Component | undefined = component ?? undefined;+ while (current) {+ if ("noteContext" in current) {+ const { noteContext } = current as { noteContext?: NoteContext };+ if (noteContext) {+ return noteContext;+ }+ }+ current = current.parent as Component | undefined;+ }+ return undefined;+}+/*** Whether the container (e.g. a dialog) holding the current note view is actually shown. False inside a dialog* that's hidden but kept mounted in the DOM (e.g. the quick-edit popup with `keepInDom`), so descendants like