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
client: fix some performance issues
Adorian Doran1fa30565c5b4
diff --git a/apps/client/src/widgets/react/peek_pane.css b/apps/client/src/widgets/react/peek_pane.cssindex 0f75f88289..3cbfa5e821 100644--- a/apps/client/src/widgets/react/peek_pane.css+++ b/apps/client/src/widgets/react/peek_pane.css@@ -71,8 +71,9 @@isolation: isolate;}-/* The text editor draws the grip of the focused block in `.ck-body`, outside `#center-pane`. */-body:has(.peek-pane-host.peek) .ck-block-toolbar-button {+/* The text editor draws the grip of the focused block in `.ck-body`, outside `#center-pane`.+ A class, not `body:has()`: Chromium restyles all of `body` when a `:has()` on it can change. */+body.peek-pane-open .ck-block-toolbar-button {visibility: hidden;}diff --git a/apps/client/src/widgets/react/peek_pane.spec.tsx b/apps/client/src/widgets/react/peek_pane.spec.tsxindex 6517896269..d0e8f8641d 100644--- a/apps/client/src/widgets/react/peek_pane.spec.tsx+++ b/apps/client/src/widgets/react/peek_pane.spec.tsx@@ -298,6 +298,19 @@ describe("usePeekDismiss", () => {expect(onDismiss).toHaveBeenCalledTimes(1);});+ it("marks the body while any peek is open, including two that overlap", () => {+ const stopLeft = watchPeekDismiss(vi.fn(), { keepOpenSelector: "#left-pane" });+ expect(document.body.classList.contains("peek-pane-open")).toBe(true);++ // The next peek starts before the previous one is dismissed.+ const stopRight = watchPeekDismiss(vi.fn(), { keepOpenSelector: "#right-pane" });+ stopLeft();+ expect(document.body.classList.contains("peek-pane-open")).toBe(true);++ stopRight();+ expect(document.body.classList.contains("peek-pane-open")).toBe(false);+ });+it("hands the focus back to what opened the peek, where it was told what that is", () => {document.body.innerHTML = `<button class="peek-button"></button>`;const onDismiss = vi.fn();diff --git a/apps/client/src/widgets/react/peek_pane.ts b/apps/client/src/widgets/react/peek_pane.tsindex 474e562981..c6a8134038 100644--- a/apps/client/src/widgets/react/peek_pane.ts+++ b/apps/client/src/widgets/react/peek_pane.ts@@ -159,11 +159,15 @@ export function usePeekDismiss(active: boolean, onDismiss: () => void, { keepOpe}, [ active, onDismiss, keepOpenSelector, focusSelector ]);}+/** Running {@link watchPeekDismiss} calls; a new peek starts before the old one stops. */+let openPeekCount = 0;+/*** Closes a peek on an outside press or Escape until the returned function is called; the* framework-free core of {@link usePeekDismiss}. Presses over the content area land on the pane's* backdrop and so reach this listener; presses on chrome the backdrop doesn't cover (toolbar, tabs)* are caught directly. Capture phase so a child's `stopPropagation` can't keep a stale peek open.+ * While it runs, `body` has the `peek-pane-open` class.*/export function watchPeekDismiss(onDismiss: () => void, dismissOptions: PeekDismissOptions) {const { keepOpenSelector, focusSelector } = dismissOptions;@@ -181,8 +185,12 @@ export function watchPeekDismiss(onDismiss: () => void, dismissOptions: PeekDismdocument.addEventListener("pointerdown", onPointerDown, true);document.addEventListener("keydown", onKeyDown);+ openPeekCount++;+ document.body.classList.add("peek-pane-open");return () => {document.removeEventListener("pointerdown", onPointerDown, true);document.removeEventListener("keydown", onKeyDown);+ openPeekCount--;+ document.body.classList.toggle("peek-pane-open", openPeekCount > 0);};}