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(text): keep the scroll position when unlocking a read-only note
Elian Dorana76a2fc010af
diff --git a/apps/client/src/widgets/containers/scrolling_container.spec.ts b/apps/client/src/widgets/containers/scrolling_container.spec.tsindex e34b9439a7..04f68bc8af 100644--- a/apps/client/src/widgets/containers/scrolling_container.spec.ts+++ b/apps/client/src/widgets/containers/scrolling_container.spec.ts@@ -1,5 +1,6 @@import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";+import type NoteContext from "../../components/note_context";import ScrollingContainer from "./scrolling_container";describe("ScrollingContainer", () => {@@ -79,6 +80,25 @@ describe("ScrollingContainer", () => {expect(frames).toHaveLength(framesBefore);});+ it("keeps the position while switching to editing, until the user scrolls", async () => {+ const { scrollingContainer, container, scrollTo } = render();+ const noteContext = { ntxId: "ntx" } as NoteContext;+ scrollingContainer.setNoteContextEvent({ noteContext });++ await scrollingContainer.handleEventInChildren("readOnlyTemporarilyDisabled", { noteContext });+ const hold = FakeResizeObserver.instances[1];+ expect(hold).toBeDefined();++ container.scrollTop = 0;+ hold.resize(500, 2000);+ expect(scrollTo).toHaveBeenCalledExactlyOnceWith({ top: 1000, behavior: "instant" });++ container.dispatchEvent(new Event("wheel"));+ hold.resize(500, 3000);+ expect(scrollTo).toHaveBeenCalledOnce();+ expect(hold.disconnected).toBe(true);+ });+/** Renders a container scrolled so that a paragraph sits right at its top edge. */function render() {const scrollingContainer = new ScrollingContainer();@@ -123,6 +143,7 @@ class FakeResizeObserver {}resize(width: number, height: number) {+ if (this.disconnected) return;const entry = { contentRect: new DOMRect(0, 0, width, height) } as ResizeObserverEntry;this.callback([ entry ], this as unknown as ResizeObserver);}diff --git a/apps/client/src/widgets/containers/scrolling_container.ts b/apps/client/src/widgets/containers/scrolling_container.tsindex a9230f53b9..005d5c84e4 100644--- a/apps/client/src/widgets/containers/scrolling_container.ts+++ b/apps/client/src/widgets/containers/scrolling_container.ts@@ -5,6 +5,8 @@ import Container from "./container.js";import { findScrollAnchor, restoreScrollAnchor, type ScrollAnchor } from "./scroll_anchor.js";import "./scrolling_container.css";+const SCROLL_HOLD_TIMEOUT_MS = 10_000;+export default class ScrollingContainer extends Container<BasicWidget> {private noteContext?: NoteContext;@@ -12,6 +14,7 @@ export default class ScrollingContainer extends Container<BasicWidget> {private pendingAnchorFrame: number | null = null;private resizeObserver?: ResizeObserver;private scrollListener?: AbortController;+ private scrollHold?: AbortController;constructor() {super();@@ -65,6 +68,7 @@ export default class ScrollingContainer extends Container<BasicWidget> {cleanup() {this.scrollListener?.abort();+ this.scrollHold?.abort();this.resizeObserver?.disconnect();if (this.pendingAnchorFrame !== null) {cancelAnimationFrame(this.pendingAnchorFrame);@@ -92,18 +96,40 @@ export default class ScrollingContainer extends Container<BasicWidget> {async handleEventInChildren<T extends EventNames>(name: T, data: EventData<T>) {if (name === "readOnlyTemporarilyDisabled" && this.noteContext && "noteContext" in data && this.noteContext.ntxId === data.noteContext?.ntxId) {- const scrollTop = this.$widget.scrollTop() ?? 0;+ this.holdScrollPosition(this.$widget[0]);+ }- const promise = super.handleEventInChildren(name, data);+ return super.handleEventInChildren(name, data);+ }- // there seems to be some asynchronicity, and we need to wait a bit before scrolling- if (promise) {- promise.then(() => setTimeout(() => this.$widget.scrollTop(scrollTop), 500));- }+ /**+ * Keeps `scrollTop` while the note detail swaps between read-only and editable text. The+ * content stays collapsed until the other type widget has loaded and filled in, which takes+ * seconds when CKEditor loads for the first time, so the position is put back on every resize+ * of the content until the user scrolls, types or clicks.+ */+ private holdScrollPosition(container: HTMLElement) {+ this.scrollHold?.abort();+ const scrollTop = container.scrollTop;+ if (!scrollTop) return;++ const hold = new AbortController();+ this.scrollHold = hold;+ const observer = new ResizeObserver(() => {+ container.scrollTo({ top: scrollTop, behavior: "instant" });+ });+ for (const child of container.children) {+ observer.observe(child);+ }- return promise;- } else {- return super.handleEventInChildren(name, data);+ const release = () => hold.abort();+ const timeout = setTimeout(release, SCROLL_HOLD_TIMEOUT_MS);+ hold.signal.addEventListener("abort", () => {+ observer.disconnect();+ clearTimeout(timeout);+ });+ for (const type of [ "wheel", "touchstart", "pointerdown", "keydown" ]) {+ container.addEventListener(type, release, { passive: true, signal: hold.signal });}}