Files
RuneStone/tests/unit/layout/documentPanes.spec.ts
T
Knotty Wyvern 42e18b4620 Resize panes and drag tabs into panes
- A divider between two panes changes their sizes, with the pointer or the keyboard. A double-click gives equal sizes.
- A tab that the user drags from the tab bar to a pane replaces its content, or opens a new pane on its right or bottom side.
- The file tree menu has "Open to the right", also for PDF files and boards.
- "New tab to the right" and "New tab below" of a browser tab open a new tab with the home page beside it. They replace "Split right" and "Split down".
- New smoke test: a PDF file beside a note.
2026-11-10 12:24:17 -07:00

157 lines
9.5 KiB
TypeScript

import { test } from "node:test";
import assert from "node:assert/strict";
import { initialDocumentPanes, syncDocumentPanes, splitDocumentPane, closeDocumentPane, selectPane, readDocumentPane, splitDocumentView, splitWithContent, showInActivePane, paneOf, releaseSolePlugin, dropInPane, resizeDocumentPanes } from "../../../src/lib/workspace/documentPanes.ts";
import { paneBounds, splitDividers, splitSibling, stackLayout, setSplitRatio } from "../../../src/lib/workspace/splitLayout.ts";
import { registerSelection, selectedEditorText } from "../../../src/lib/editor/selection.ts";
test("opening a link to the right keeps its source and focuses an existing pane for duplicate targets", () => {
let state = syncDocumentPanes(initialDocumentPanes(), ["a", "b", "c"], "a");
const primary = state.active;
state = readDocumentPane(state, primary, true);
state = splitDocumentPane(state, primary, "b", "right");
assert.deepEqual(state.panes.map(pane => pane.documentId), ["a", "b"]);
assert.equal(state.active, "right");
assert.deepEqual(paneBounds(state.layout).right, { left: 50, top: 0, width: 50, height: 100 });
state = splitDocumentPane(state, "right", "a", "unused");
assert.equal(state.active, primary);
assert.equal(state.panes.length, 2);
assert.equal(state.panes[0].reading, true);
state = splitDocumentPane(state, "right", "c", "third");
assert.equal(paneBounds(state.layout).third.left, 75);
});
test("Read and Split exclude each other, and a closed document resets both", () => {
let state = syncDocumentPanes(initialDocumentPanes(), ["a", "b"], "a");
const pane = state.active;
state = splitDocumentView(state, pane, true);
assert.deepEqual([state.panes[0].reading, state.panes[0].split], [false, true]);
state = readDocumentPane(state, pane, true);
assert.deepEqual([state.panes[0].reading, state.panes[0].split], [true, false]);
state = splitDocumentView(state, pane, true);
assert.deepEqual([state.panes[0].reading, state.panes[0].split], [false, true]);
// Write and Source keep the Split view.
state = readDocumentPane(state, pane, false);
assert.equal(state.panes[0].split, true);
state = syncDocumentPanes(state, ["b"], "b");
assert.deepEqual([state.panes[0].documentId, state.panes[0].reading, state.panes[0].split], ["b", false, false]);
});
test("closing a split preserves the remaining pane and a later tab selection can restore its hidden document", () => {
let state = syncDocumentPanes(initialDocumentPanes(), ["a", "b"], "a");
state = splitDocumentPane(state, state.active, "b", "right");
state = closeDocumentPane(state, "right");
assert.equal(state.panes[0].documentId, "a");
assert.equal(state.layout, state.active);
state = syncDocumentPanes(state, ["a", "b"], "b");
assert.equal(state.panes[0].documentId, "b");
});
test("tab replacement stays in its focused pane, and closing another tab collapses only its empty pane", () => {
let state = syncDocumentPanes(initialDocumentPanes(), ["a", "b"], "a");
state = splitDocumentPane(state, state.active, "b", "right");
state = syncDocumentPanes(state, ["a", "c"], "c");
assert.deepEqual(state.panes.map(pane => pane.documentId), ["a", "c"]);
assert.equal(state.active, "right");
const unchanged = syncDocumentPanes(state, ["a", "c"], "c");
assert.equal(unchanged, state);
state = syncDocumentPanes(state, ["c"], "c");
assert.equal(state.panes.length, 1);
assert.equal(state.layout, "right");
assert.equal(selectPane(state, "missing"), state);
});
test("selection readers remain available in both panes and disposal cannot clear a replacement reader", () => {
const first = registerSelection("a", () => "first selection");
const second = registerSelection("b", () => "second selection");
assert.equal(selectedEditorText("a"), "first selection");
assert.equal(selectedEditorText("b"), "second selection");
const replacement = registerSelection("a", () => "new selection");
first(); second();
assert.equal(selectedEditorText("a"), "new selection");
assert.equal(selectedEditorText("b"), "");
replacement();
});
test("a pane shows a document or a plugin view, and each tab has at most one pane", () => {
let state = syncDocumentPanes(initialDocumentPanes(), ["a", "b"], "a");
const primary = state.active;
state = splitWithContent(state, primary, { pluginKey: "browser" }, "web");
assert.deepEqual(state.panes.map(pane => [pane.documentId, pane.pluginKey]), [["a", null], [null, "browser"]]);
assert.equal(state.active, "web");
assert.equal(splitWithContent(state, primary, { pluginKey: "browser" }, "again"), selectPane(state, "web"));
// A document change does not remove the plugin pane.
assert.equal(syncDocumentPanes(state, ["a", "b"], null), state);
// A selected document replaces the view of the active pane. The view stays open as a tab.
const replaced = syncDocumentPanes(state, ["a", "b"], "b");
assert.deepEqual(replaced.panes.map(pane => [pane.documentId, pane.pluginKey]), [["a", null], ["b", null]]);
// A plugin view replaces the document of the active pane, and a shown document selects its pane.
state = showInActivePane(selectPane(replaced, primary), { pluginKey: "board" });
assert.deepEqual(state.panes.map(pane => [pane.documentId, pane.pluginKey]), [[null, "board"], ["b", null]]);
assert.equal(showInActivePane(state, { documentId: "b" }).active, "web");
assert.equal(paneOf(state, { pluginKey: "board" })?.id, primary);
});
test("a closed plugin view leaves its pane, and the last pane gives its plugin view back to the whole area", () => {
let state = syncDocumentPanes(initialDocumentPanes(), ["a"], "a");
state = splitWithContent(state, state.active, { pluginKey: "browser" }, "web", "down");
assert.deepEqual(paneBounds(state.layout).web, { left: 0, top: 50, width: 100, height: 50 });
assert.deepEqual(paneBounds(stackLayout({ direction: "right", first: "x", second: "y" })).y, { left: 0, top: 50, width: 100, height: 50 });
const closed = syncDocumentPanes(state, ["a"], null, []);
assert.deepEqual(closed.panes.map(pane => pane.id), ["document-main"]);
const remaining = closeDocumentPane(state, "document-main");
assert.equal(remaining.active, "web");
const released = releaseSolePlugin(remaining);
assert.equal(released.pluginKey, "browser");
assert.deepEqual([released.state.panes[0].documentId, released.state.panes[0].pluginKey], [null, null]);
assert.equal(releaseSolePlugin(closed).state, closed);
});
test("the split sibling names the pane that a plugin view opened beside", () => {
const tree = { direction: "right" as const, first: "a", second: { direction: "down" as const, first: "b", second: "c" } };
assert.deepEqual(splitSibling(tree, "c"), { direction: "down", sibling: "b" });
assert.deepEqual(splitSibling(tree, "a"), { direction: "right", sibling: "b" });
assert.equal(splitSibling("a", "a"), null);
});
test("a divider changes the ratio of its split, within limits, and the narrow layout keeps the ratio", () => {
let state = syncDocumentPanes(initialDocumentPanes(), ["a", "b", "c"], "a");
state = splitDocumentPane(state, state.active, "b", "right");
state = splitWithContent(state, "right", { documentId: "c" }, "low", "down");
const dividers = splitDividers(state.layout);
assert.deepEqual(dividers.map(divider => [divider.path.join("/"), divider.direction, divider.ratio]), [["", "right", 0.5], ["second", "down", 0.5]]);
state = resizeDocumentPanes(state, [], 0.3);
state = resizeDocumentPanes(state, ["second"], 0.75);
const bounds = paneBounds(state.layout);
assert.equal(bounds["document-main"].width, 30);
assert.deepEqual(bounds.right, { left: 30, top: 0, width: 70, height: 75 });
assert.equal(bounds.low.top, 75);
assert.equal(paneBounds(stackLayout(state.layout))["document-main"].height, 30);
assert.equal(paneBounds(resizeDocumentPanes(state, [], 0.02).layout)["document-main"].width, 10, "A pane keeps a tenth of the split at least");
assert.equal(setSplitRatio("only", [], 0.4), "only");
assert.equal(resizeDocumentPanes(state, ["second", "first"], 0.4), state, "A path to a pane changes nothing");
});
test("a tab drop replaces the content of a pane or opens a pane beside it, and the content leaves its old pane", () => {
let state = syncDocumentPanes(initialDocumentPanes(), ["a", "b", "c"], "a");
const main = state.active;
state = splitWithContent(state, main, { pluginKey: "web" }, "right");
// A document dropped in the browser pane replaces the browser there. The browser stays open as a tab.
let next = dropInPane(state, { documentId: "b" }, "right", "center", "new");
assert.deepEqual(next.panes.map(pane => [pane.documentId, pane.pluginKey]), [["a", null], ["b", null]]);
assert.equal(next.active, "right");
// A drop on the bottom side of a pane opens a new pane there.
next = dropInPane(state, { documentId: "c" }, main, "down", "new");
assert.deepEqual(paneBounds(next.layout).new, { left: 0, top: 50, width: 50, height: 50 });
assert.equal(next.active, "new");
// A visible tab moves: its old pane closes.
next = dropInPane(state, { pluginKey: "web" }, main, "down", "new");
assert.deepEqual(next.panes.map(pane => [pane.id, pane.pluginKey]), [[main, null], ["new", "web"]]);
assert.deepEqual(paneBounds(next.layout).new, { left: 0, top: 50, width: 100, height: 50 });
next = dropInPane(state, { pluginKey: "web" }, main, "center", "new");
assert.deepEqual(next.panes.map(pane => pane.pluginKey), ["web"]);
// A drop on the pane that shows the content only makes it active.
assert.equal(dropInPane(state, { pluginKey: "web" }, "right", "down", "new").panes.length, 2);
assert.equal(dropInPane(state, { documentId: "a" }, "missing", "center", "new"), state);
});