Skip to content

Build your personal knowledge base with Trilium Notes

client/collections/board: fix "Search in note" from the note menu

Adorian Doranc71a637b1653
diff --git a/apps/client/src/widgets/collections/collection_filter.spec.tsx b/apps/client/src/widgets/collections/collection_filter.spec.tsx
index c1aa3ae248..04ddc9811c 100644
--- a/apps/client/src/widgets/collections/collection_filter.spec.tsx
+++ b/apps/client/src/widgets/collections/collection_filter.spec.tsx
@@ -375,10 +375,18 @@ describe("useCollectionFilter", () => {
describe("CollectionFilterInput", () => {
let container: HTMLElement;
+ let parent: Component;
beforeEach(() => {
container = document.createElement("div");
document.body.appendChild(container);
+ parent = new Component();
+ // What the pane the collection is drawn in answers as, which the box reads to tell a
+ // search of its own note from one aimed at another pane.
+ (parent as unknown as { noteContext: unknown }).noteContext = {
+ ntxId: "ntx1",
+ isActive: () => true
+ };
});
afterEach(() => {
@@ -400,12 +408,39 @@ describe("CollectionFilterInput", () => {
};
// Rendered inside act so the mount effects run now, not queued into the next act where
// they would clobber what a test has typed in the meantime.
- act(() => render(<CollectionFilterInput filter={filter} />, container));
+ act(() => render(
+ <ParentComponent.Provider value={parent}>
+ <CollectionFilterInput filter={filter} />
+ </ParentComponent.Provider>,
+ container));
const input = container.querySelector("input");
expect(input).not.toBeNull();
return { filter, input: input as HTMLInputElement };
}
+ /**
+ * A collection draws notes that live elsewhere, so the find bar has no text of its own to look
+ * through and does not open on one at all. Narrowing the collection is the search it has.
+ */
+ it("takes the note's own search, on the pane it is drawn in", async () => {
+ const { input } = mountInput({ query: "#done" });
+ input.value = "#done";
+
+ await act(async () => { await parent.handleEvent("findInText", {}); });
+ expect(document.activeElement).toBe(input);
+ // Selected, so what is typed next replaces the query rather than running on from it.
+ expect(input.selectionStart).toBe(0);
+ expect(input.selectionEnd).toBe("#done".length);
+
+ // A search aimed at another pane leaves this one alone.
+ input.blur();
+ await act(async () => { await parent.handleEvent("findInText", { ntxId: "ntx2" }); });
+ expect(document.activeElement).not.toBe(input);
+
+ await act(async () => { await parent.handleEvent("findInText", { ntxId: "ntx1" }); });
+ expect(document.activeElement).toBe(input);
+ });
+
function type(input: HTMLInputElement, value: string) {
input.value = value;
input.dispatchEvent(new Event("input", { bubbles: true }));
diff --git a/apps/client/src/widgets/collections/collection_filter.tsx b/apps/client/src/widgets/collections/collection_filter.tsx
index 0b2afa0177..35dabdd075 100644
--- a/apps/client/src/widgets/collections/collection_filter.tsx
+++ b/apps/client/src/widgets/collections/collection_filter.tsx
@@ -10,7 +10,7 @@ import type LoadResults from "../../services/load_results";
import searchService from "../../services/search";
import ActionButton from "../react/ActionButton";
import FormTextBox from "../react/FormTextBox";
-import { useTriliumEvent } from "../react/hooks";
+import { useNoteContext, useTriliumEvent } from "../react/hooks";
/** How long to wait after a change before re-running the active filter. */
const RERUN_DEBOUNCE_MS = 300;
@@ -194,10 +194,22 @@ export function CollectionFilterInput({ filter, placeholder }: {
}) {
const [ typed, setTyped ] = useState(filter.query);
const inputRef = useRef<HTMLInputElement>(null);
+ const { noteContext } = useNoteContext();
// Adopt a query submitted elsewhere, or the stored one arriving on mount.
useEffect(() => setTyped(filter.query), [ filter.query ]);
+ // Searching the note lands here: a collection holds no text of its own, so the find bar has
+ // nothing to look through, and narrowing to what is searched for is what the reader is after.
+ useTriliumEvent("findInText", ({ ntxId }) => {
+ if (ntxId ? ntxId !== noteContext?.ntxId : !noteContext?.isActive()) {
+ return;
+ }
+
+ inputRef.current?.focus();
+ inputRef.current?.select();
+ });
+
const isActive = !!filter.query;
return (