Skip to content

Build your personal knowledge base with Trilium Notes

7.5 KB
Raw Blame
import { ClassicEditor, Essentials, FindAndReplace, Paragraph, _getModelData as getModelData } from "ckeditor5";
import { afterEach, beforeEach, describe, expect, it } from "vitest";
import { createTestEditor } from "../../../test/editor-kit.js";
import FindReveal from "../find_reveal.js";
import CollapsibleEditing from "./collapsible_editing.js";
import { TRANSIENT_OPEN_ATTRIBUTE } from "./constants.js";

/**
 * Find-in-note reveal: when the find highlight lands inside a collapsed block the
 * block opens just enough to show the match, and re-collapses the moment the
 * highlight leaves. This is transient editing-view state — it must never reach the
 * model or the saved HTML, so a user's saved open/closed layout survives a search.
 */
describe("collapsible find-in-note reveal", () => {
    let domElement: HTMLDivElement;
    let editor: ClassicEditor;
    let model: ClassicEditor["model"];

    beforeEach(async () => {
        domElement = document.createElement("div");
        document.body.appendChild(domElement);
        editor = await ClassicEditor.create(domElement, {
            licenseKey: "GPL",
            plugins: [Essentials, Paragraph, FindAndReplace, CollapsibleEditing]
        });
        model = editor.model;
    });

    afterEach(() => {
        domElement.remove();
        return editor.destroy();
    });

    const domRoot = () => editor.editing.view.getDomRoot() as HTMLElement;
    const detailsDom = (index = 0) =>
        domRoot().querySelectorAll<HTMLDetailsElement>("details.trilium-collapsible")[index];
    const isTransient = (index = 0) => detailsDom(index).hasAttribute(TRANSIENT_OPEN_ATTRIBUTE);

    const twoSiblings =
        "<details class=\"trilium-collapsible\"><summary>A</summary><p>needle one</p></details>" +
        "<details class=\"trilium-collapsible\"><summary>B</summary><p>needle two</p></details>";

    it("stays force-open through a reconversion while the match is still revealed", () => {
        editor.setData("<details class=\"trilium-collapsible\"><summary>T</summary><p>needle in body</p></details>");
        editor.execute("find", "needle");
        expect(detailsDom().open).toBe(true);
        expect(isTransient()).toBe(true);

        // Adding a body block changes the <details>'s children, which reconverts the whole
        // structure in the editing view. The structure callback has to re-apply the transient
        // open, or the block would snap shut under the highlight.
        model.change((writer) => {
            const details = model.document.getRoot()?.getChild(0);
            if (details?.is("element")) {
                writer.insertElement("paragraph", writer.createPositionAt(details, "end"));
            }
        });
        editor.editing.view.forceRender();

        expect(detailsDom().open).toBe(true);
        expect(isTransient()).toBe(true);
        // Still nothing persisted.
        expect(getModelData(model, { withoutSelection: true })).not.toContain("open=");
    });

    it("opens a collapsed block to reveal a match, marks it transient, and leaves the saved HTML untouched", () => {
        editor.setData("<details class=\"trilium-collapsible\"><summary>T</summary><p>needle in body</p></details>");
        expect(detailsDom().open).toBe(false);

        editor.execute("find", "needle");

        expect(detailsDom().open).toBe(true);
        expect(isTransient()).toBe(true);
        // Neither the model nor the saved HTML learns about the reveal.
        expect(getModelData(model, { withoutSelection: true })).not.toContain("open");
        expect(editor.getData()).toBe(
            "<details class=\"trilium-collapsible\"><summary>T</summary><p>needle in body</p></details>"
        );
    });

    it("follows the highlight: the previous block re-collapses as the next one opens", () => {
        editor.setData(twoSiblings);

        editor.execute("find", "needle"); // highlights the first match (block A)
        expect(detailsDom(0).open).toBe(true);
        expect(isTransient(0)).toBe(true);
        expect(detailsDom(1).open).toBe(false);

        editor.execute("findNext"); // moves to block B
        expect(detailsDom(0).open).toBe(false);
        expect(isTransient(0)).toBe(false);
        expect(detailsDom(1).open).toBe(true);
        expect(isTransient(1)).toBe(true);
    });

    it("opens every collapsed ancestor of a nested match", () => {
        editor.setData(
            "<details class=\"trilium-collapsible\"><summary>Outer</summary>" +
                "<details class=\"trilium-collapsible\"><summary>Inner</summary><p>needle</p></details>" +
            "</details>"
        );

        editor.execute("find", "needle");

        expect(detailsDom(0).open).toBe(true);
        expect(isTransient(0)).toBe(true);
        expect(detailsDom(1).open).toBe(true);
        expect(isTransient(1)).toBe(true);
    });

    it("does not mark a block the user already left open as transient", () => {
        editor.setData("<details class=\"trilium-collapsible\" open><summary>T</summary><p>needle</p></details>");
        expect(detailsDom().open).toBe(true);

        editor.execute("find", "needle");

        expect(detailsDom().open).toBe(true);
        expect(isTransient()).toBe(false);
        // Its genuine `open` is still saved.
        expect(editor.getData()).toContain("open");
    });

    it("re-collapses everything once the search is cleared", () => {
        editor.setData(twoSiblings);
        editor.execute("find", "needle");
        expect(detailsDom(0).open).toBe(true);

        const findEditing = editor.plugins.get("FindAndReplaceEditing");
        findEditing.state?.clear(model);

        expect(detailsDom(0).open).toBe(false);
        expect(isTransient(0)).toBe(false);
        expect(detailsDom(1).open).toBe(false);
    });

    it("keeps a block open, and drops the transient marker, if the user toggles it open mid-search", () => {
        editor.setData("<details class=\"trilium-collapsible\"><summary>T</summary><p>needle</p></details>");
        editor.execute("find", "needle");
        expect(isTransient()).toBe(true);

        // User genuinely opens it via the arrow while the match is highlighted.
        domRoot().querySelector<HTMLElement>(".trilium-collapsible-arrow")?.dispatchEvent(
            new MouseEvent("click", { bubbles: true, cancelable: true })
        );
        // Highlight moves away (search cleared).
        editor.plugins.get("FindAndReplaceEditing").state?.clear(model);

        expect(detailsDom().open).toBe(true);
        expect(isTransient()).toBe(false);
        expect(editor.getData()).toContain("open");
    });

    it("opens the blocks around a position that FindReveal shows, as for an embed", async () => {
        const revealEditor = await createTestEditor([
            Essentials, Paragraph, FindAndReplace, FindReveal, CollapsibleEditing
        ]);
        revealEditor.setData(twoSiblings);
        const details = revealEditor.model.document.getRoot()?.getChild(1);
        const body = details?.is("element") ? details.getChild(1) : null;
        if (!body) throw new Error("Expected the second block to have a body.");
        const openStates = () => [ ...revealEditor.editing.view.getDomRoot()
            ?.querySelectorAll<HTMLDetailsElement>("details.trilium-collapsible") ?? [] ]
            .map((block) => block.open);

        revealEditor.plugins.get(FindReveal).show(revealEditor.model.createPositionAt(body, 0));
        expect(openStates()).toEqual([ false, true ]);

        revealEditor.plugins.get(FindReveal).show(null);
        expect(openStates()).toEqual([ false, false ]);
        expect(revealEditor.getData()).not.toContain("open");
    });
});