Skip to content

Build your personal knowledge base with Trilium Notes

fix(text): drop the scroll padding from read-only notes

Elian Doran30b1abd165cc
diff --git a/apps/client/src/widgets/scroll_padding.spec.tsx b/apps/client/src/widgets/scroll_padding.spec.tsx
new file mode 100644
index 0000000000..6a817c0cfc
--- /dev/null
+++ b/apps/client/src/widgets/scroll_padding.spec.tsx
@@ -0,0 +1,139 @@
+import { render } from "preact";
+import { act } from "preact/test-utils";
+import { beforeEach, describe, expect, it, vi } from "vitest";
+
+// The widget is rendered on its own, outside a note split, so the context it would pick up from the
+// tree is handed to it instead — a real note context, so the read-only decision is the actual one.
+const shownContext = vi.hoisted(() => ({ current: null as Record<string, unknown> | null }));
+vi.mock("./react/hooks", async (importOriginal) => ({
+ ...(await importOriginal<typeof import("./react/hooks")>()),
+ useNoteContext: () => shownContext.current
+}));
+
+import Component from "../components/component";
+import NoteContext from "../components/note_context";
+import type FNote from "../entities/fnote";
+import type { ViewScope } from "../services/link";
+import options from "../services/options";
+import { buildNote } from "../test/easy-froca";
+import { renderInto } from "../test/render";
+import { ParentComponent } from "./react/react_utils";
+import ScrollPadding from "./scroll_padding";
+
+describe("ScrollPadding", () => {
+ let parentComponent: Component;
+ let noteContext: NoteContext;
+ let container: HTMLElement;
+
+ beforeEach(() => {
+ options.load({ autoReadonlySizeText: 100, autoReadonlySizeCode: 100 });
+ parentComponent = new Component();
+ noteContext = new NoteContext();
+ container = renderInto(null);
+ });
+
+ /**
+ * Shows the given note in the tab, inside the scrolling container the widget measures itself
+ * against. The context handed over here is not the reactive one the widget subscribes to in the
+ * app, so a note switch is staged as a fresh mount.
+ */
+ async function show(note: FNote, viewScope: ViewScope = { viewMode: "default" }) {
+ noteContext.noteId = note.noteId;
+ noteContext.notePath = note.noteId;
+ noteContext.viewScope = viewScope;
+ shownContext.current = {
+ note,
+ noteContext,
+ parentComponent,
+ ntxId: noteContext.ntxId,
+ viewScope
+ };
+
+ await act(async () => {
+ render(null, container);
+ render(
+ <ParentComponent.Provider value={parentComponent}>
+ <div className="scrolling-container"><ScrollPadding /></div>
+ </ParentComponent.Provider>,
+ container
+ );
+ });
+ await settle();
+ }
+
+ /** Lets the asynchronous read-only check resolve, along with the re-render it schedules. */
+ async function settle() {
+ await act(async () => { await new Promise((resolve) => setTimeout(resolve, 0)); });
+ }
+
+ function isShown() {
+ return !!container.querySelector(".scroll-padding-widget");
+ }
+
+ /** What the read-only badge's edit button does: flips the toggle, then announces it. */
+ async function setTemporarilyEditable(editable: boolean) {
+ if (noteContext.viewScope) {
+ noteContext.viewScope.readOnlyTemporarilyDisabled = editable;
+ }
+ await act(async () => {
+ parentComponent.handleEvent("readOnlyTemporarilyDisabled", { noteContext });
+ });
+ await settle();
+ }
+
+ it("pads an editable note, but not one its label holds read-only", async () => {
+ await show(buildNote({ title: "Editable" }));
+ expect(isShown()).toBe(true);
+
+ await show(buildNote({ title: "Locked", "#readOnly": "true" }));
+ expect(isShown()).toBe(false);
+ });
+
+ it("pads a read-only note for as long as it is temporarily editable", async () => {
+ await show(buildNote({ title: "Locked", "#readOnly": "true" }));
+ expect(isShown()).toBe(false);
+
+ await setTemporarilyEditable(true);
+ expect(isShown()).toBe(true);
+
+ await setTemporarilyEditable(false);
+ expect(isShown()).toBe(false);
+ });
+
+ it("skips a note the size limit turned read-only on its own", async () => {
+ await show(buildNote({ title: "Huge", content: "a".repeat(200) }));
+ expect(isShown()).toBe(false);
+
+ await setTemporarilyEditable(true);
+ expect(isShown()).toBe(true);
+ });
+
+ it("skips every note while the database itself is read-only", async () => {
+ options.set("databaseReadonly", "true");
+
+ await show(buildNote({ title: "Editable" }));
+ expect(isShown()).toBe(false);
+ });
+
+ it("only pads text and code notes shown in their default view", async () => {
+ await show(buildNote({ title: "Code", type: "code", mime: "text/plain" }));
+ expect(isShown()).toBe(true);
+
+ await show(buildNote({ title: "Canvas", type: "canvas" }));
+ expect(isShown()).toBe(false);
+
+ await show(buildNote({ title: "Sourced" }), { viewMode: "source" });
+ expect(isShown()).toBe(false);
+ });
+
+ it("scrolls the note to its end when clicked", async () => {
+ const triggerCommand = vi.spyOn(parentComponent, "triggerCommand");
+ await show(buildNote({ title: "Editable" }));
+
+ await act(async () => {
+ container.querySelector<HTMLElement>(".scroll-padding-widget")?.click();
+ });
+
+ expect(triggerCommand).toHaveBeenCalledWith("scrollToEnd", { ntxId: noteContext.ntxId });
+ });
+});
diff --git a/apps/client/src/widgets/scroll_padding.tsx b/apps/client/src/widgets/scroll_padding.tsx
index 4cf83020af..bc545e6920 100644
--- a/apps/client/src/widgets/scroll_padding.tsx
+++ b/apps/client/src/widgets/scroll_padding.tsx
@@ -1,17 +1,24 @@
import { useEffect, useRef, useState } from "preact/hooks";
-import { useNoteContext } from "./react/hooks";
+import options from "../services/options";
+import { useIsNoteReadOnly, useNoteContext } from "./react/hooks";
export default function ScrollPadding() {
- const { note, parentComponent, ntxId, viewScope } = useNoteContext();
+ const { note, noteContext, parentComponent, ntxId, viewScope } = useNoteContext();
const ref = useRef<HTMLDivElement>(null);
const [height, setHeight] = useState<number>(10);
+ // There is nothing to place the caret at the end of when the note can't be edited. The check is
+ // asynchronous, answering `undefined` until it settles; a read-only database is reported as not
+ // read-only (there is no editing to switch to), so it has to be ruled out separately.
+ const { isReadOnly } = useIsNoteReadOnly(note, noteContext);
const isEnabled = ["text", "code"].includes(note?.type ?? "")
&& viewScope?.viewMode === "default"
&& note?.isContentAvailable()
&& !note?.isTriliumSqlite()
&& !note?.isMarkdown()
- && !note?.isIconPack();
+ && !note?.isIconPack()
+ && isReadOnly === false
+ && !options.is("databaseReadonly");
const refreshHeight = () => {
if (!ref.current) return;