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
perf(mobile): render the tabs next to the active one when idle
Elian Doran11601929ef8c
diff --git a/apps/client/src/widgets/containers/split_note_container.spec.tsx b/apps/client/src/widgets/containers/split_note_container.spec.tsxindex b8d6ab8199..e6fc8dfb2e 100644--- a/apps/client/src/widgets/containers/split_note_container.spec.tsx+++ b/apps/client/src/widgets/containers/split_note_container.spec.tsx@@ -38,6 +38,7 @@ describe("SplitNoteContainer", () => {activeNtxId: null,noteContexts,getActiveMainContext: () => noteContexts[0],+ getMainNoteContexts: () => noteContexts.filter((c) => c.isMainContext()),getNoteContextById: (ntxId: string) => noteContexts.find((c) => c.ntxId === ntxId),activateNoteContext: () => {},removeNoteContext@@ -155,6 +156,74 @@ describe("SplitNoteContainer", () => {expect(readProbe("background-split")).toBe("background-split-note");});+ it("renders the tabs next to the active one on mobile, a while after it is shown", async () => {+ function Probe() {+ const { note } = useNoteContext();+ return <div className="probe">{note?.noteId ?? ""}</div>;+ }++ const { device } = window.glob;+ window.glob.device = "mobile";+ vi.useFakeTimers({ toFake: [ "setTimeout", "clearTimeout" ] });+ try {+ const container = new SplitNoteContainer(() => new NoteWrapperWidget().child(<Probe />));+ container.render();+ // The active tab, and three in the background; the last is before the first, around the end.+ const tabs = [ "first", "second", "third", "fourth" ].map((ntxId) => new NoteContext(ntxId));+ await act(async () => {+ for (const noteContext of tabs) {+ noteContext.setEmpty();+ noteContexts.push(noteContext);+ await container.newNoteContextCreatedEvent({ noteContext });++ const { noteId } = buildNote({ id: `${noteContext.ntxId}-note`, title: "Note" });+ noteContext.notePath = noteId;+ noteContext.noteId = noteId;+ await container.handleEvent("noteSwitched", { noteContext, notePath: noteId });+ }+ await container.handleEvent("activeContextChanged", { noteContext: tabs[0] });+ });++ const readProbes = () => tabs.map(({ ntxId }) => container.$widget.find(`[data-ntx-id="${ntxId}"] .probe`).text());+ expect(readProbes()).toEqual([ "first-note", "", "", "" ]);++ await act(async () => {+ await vi.advanceTimersByTimeAsync(1000);+ });+ expect(readProbes()).toEqual([ "first-note", "second-note", "", "" ]);+ await act(async () => {+ await vi.advanceTimersByTimeAsync(1000);+ });+ expect(readProbes()).toEqual([ "first-note", "second-note", "", "fourth-note" ]);+ // Still hidden: only the active tab is shown.+ expect(container.$widget.find("[data-ntx-id=second]").hasClass("hidden-ext")).toBe(true);++ await act(async () => {+ await vi.advanceTimersByTimeAsync(5000);+ });+ expect(readProbes()[2]).toBe("");+ } finally {+ vi.useRealTimers();+ window.glob.device = device;+ }+ });++ it("leaves the background tabs alone on desktop", async () => {+ vi.useFakeTimers({ toFake: [ "setTimeout", "clearTimeout" ] });+ try {+ const container = await openSplits([ "shown" ]);+ const background = new NoteContext("background");+ noteContexts.push(background);+ await container.newNoteContextCreatedEvent({ noteContext: background });+ await container.handleEvent("activeContextChanged", { noteContext: noteContexts[0] });++ await vi.advanceTimersByTimeAsync(5000);+ expect(container.$widget.find("[data-ntx-id=background]").hasClass("empty-note")).toBe(true);+ } finally {+ vi.useRealTimers();+ }+ });+describe("keyboard actions", () => {it("closes the active split, leaving a tab that has no split alone", async () => {const container = await openSplits([ "split-a", "split-b" ]);diff --git a/apps/client/src/widgets/containers/split_note_container.ts b/apps/client/src/widgets/containers/split_note_container.tsindex f7141d682f..8cda34cc36 100644--- a/apps/client/src/widgets/containers/split_note_container.ts+++ b/apps/client/src/widgets/containers/split_note_container.ts@@ -16,10 +16,14 @@ interface SplitNoteWidget extends BasicWidget {type WidgetFactory = () => SplitNoteWidget;+/** How long, in ms, the panes of the next tab to render wait on mobile after a tab is shown or rendered. */+const NEIGHBOUR_RENDER_DELAY = 1000;+export default class SplitNoteContainer extends FlexContainer<SplitNoteWidget> {private widgetFactory: WidgetFactory;private widgets: Record<string, SplitNoteWidget>;+ private neighbourRenderTimer?: ReturnType<typeof setTimeout>;constructor(widgetFactory: WidgetFactory) {super("row");@@ -216,6 +220,7 @@ export default class SplitNoteContainer extends FlexContainer<SplitNoteWidget> {activeContextChangedEvent() {this.refresh();+ this.renderNeighbourTabsLater();}noteSwitchedAndActivatedEvent() {@@ -351,6 +356,29 @@ export default class SplitNoteContainer extends FlexContainer<SplitNoteWidget> {return Promise.all(promises);}+ /**+ * On mobile, renders the panes of the tabs before and after the active one while they stay hidden,+ * one tab per delay, so that a swipe to either slides in a rendered note.+ */+ private renderNeighbourTabsLater() {+ if (!isMobile()) return;++ clearTimeout(this.neighbourRenderTimer);+ const renderNext = () => {+ const tab = getNeighbourTabs().find((neighbour) => neighbour.getSubContexts()+ .some((subContext) => subContext.ntxId && !this.widgets[subContext.ntxId]?.hasBeenAlreadyShown));+ if (!tab) return;++ const timer = this.neighbourRenderTimer;+ void this.refreshNotShown({ noteContext: tab }).then(() => {+ if (this.neighbourRenderTimer === timer) {+ this.neighbourRenderTimer = setTimeout(renderNext, NEIGHBOUR_RENDER_DELAY);+ }+ });+ };+ this.neighbourRenderTimer = setTimeout(renderNext, NEIGHBOUR_RENDER_DELAY);+ }+/** Sends `setNoteContext` to the widget, unless the widget has already received it. */private async provideNoteContext(widget: SplitNoteWidget, noteContext: NoteContext) {if (widget.hasNoteContext) {@@ -370,3 +398,14 @@ function recursiveCleanup(widget: Component) {widget.cleanup();}}++/** The tabs before and after the active one, around the ends. */+function getNeighbourTabs() {+ const tabs = appContext.tabManager.getMainNoteContexts();+ const index = tabs.findIndex((tab) => tab.ntxId === appContext.tabManager.getActiveMainContext()?.ntxId);+ if (index < 0 || tabs.length < 2) return [];++ const next = tabs[(index + 1) % tabs.length];+ const previous = tabs[(index - 1 + tabs.length) % tabs.length];+ return next === previous ? [ next ] : [ next, previous ];+}