Skip to content

Build your personal knowledge base with Trilium Notes

feat(splits): add keyboard actions for creating, closing and moving a split (closes #5475)

Elian Doran8be00d0c7fc9
diff --git a/apps/client/src/components/app_context.ts b/apps/client/src/components/app_context.ts
index af96f80865..b2116f2a2b 100644
--- a/apps/client/src/components/app_context.ts
+++ b/apps/client/src/components/app_context.ts
@@ -305,6 +305,14 @@ export type CommandMappings = {
scrollToEnd: CommandData;
closeThisNoteSplit: CommandData;
moveThisNoteSplit: CommandData & { isMovingLeft: boolean };
+ /**
+ * Keyboard-action counterparts of the pane buttons. The buttons bubble `closeThisNoteSplit` /
+ * `moveThisNoteSplit` up from the pane they sit in; these are dispatched from `appContext`,
+ * which has no pane to start from and so distributes them downwards as events instead.
+ */
+ closeActiveNoteSplit: CommandData;
+ moveActiveNoteSplitLeft: CommandData;
+ moveActiveNoteSplitRight: CommandData;
jumpToNote: CommandData;
openTodayNote: CommandData;
commandPalette: CommandData;
diff --git a/apps/client/src/widgets/containers/split_note_container.spec.tsx b/apps/client/src/widgets/containers/split_note_container.spec.tsx
index 7beb7f51d0..2e9c0f0d7c 100644
--- a/apps/client/src/widgets/containers/split_note_container.spec.tsx
+++ b/apps/client/src/widgets/containers/split_note_container.spec.tsx
@@ -8,27 +8,42 @@ import LoadResults from "../../services/load_results";
import NoteWrapperWidget from "../note_wrapper";
import SplitNoteContainer from "./split_note_container";
+// The resizer drives Split.js against the real #center-pane layout, on an animation frame that
+// outlives the test. These tests are about which splits the container opens, closes and moves.
+vi.mock("../../services/resizer", () => ({
+ default: {
+ setupNoteSplitResizer: vi.fn(),
+ delNoteSplitResizer: vi.fn(),
+ moveNoteSplitResizer: vi.fn()
+ }
+}));
+
describe("SplitNoteContainer", () => {
let noteContexts: NoteContext[];
+ let removeNoteContext: ReturnType<typeof vi.fn>;
beforeEach(() => {
noteContexts = [];
+ removeNoteContext = vi.fn();
- // Only the lookups the container makes while opening and closing splits.
+ // Only the lookups the container makes while opening, closing and moving splits.
appContext.tabManager = {
activeNtxId: null,
+ noteContexts,
getActiveMainContext: () => noteContexts[0],
getNoteContextById: (ntxId: string) => noteContexts.find((c) => c.ntxId === ntxId),
- activateNoteContext: () => {}
+ activateNoteContext: () => {},
+ removeNoteContext
} as unknown as TabManager;
});
+ /** Opens one tab: the first id becomes its main context, the rest its splits. */
async function openSplits(ntxIds: string[], widgetFactory = () => new NoteWrapperWidget()) {
const container = new SplitNoteContainer(widgetFactory);
container.render();
- for (const ntxId of ntxIds) {
- const noteContext = new NoteContext(ntxId);
+ for (const [ index, ntxId ] of ntxIds.entries()) {
+ const noteContext = new NoteContext(ntxId, "root", index === 0 ? null : ntxIds[0]);
noteContexts.push(noteContext);
await container.newNoteContextCreatedEvent({ noteContext });
}
@@ -80,4 +95,41 @@ describe("SplitNoteContainer", () => {
// DOM it still points at.
expect(isMounted).toBe(false);
});
+
+ 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" ]);
+ // a second tab, whose single pane is not a split
+ noteContexts.push(new NoteContext("other-tab"));
+
+ await container.closeActiveNoteSplitEvent({ ntxId: "split-b" });
+ expect(removeNoteContext).toHaveBeenCalledWith("split-b");
+
+ removeNoteContext.mockClear();
+ await container.closeActiveNoteSplitEvent({ ntxId: "other-tab" });
+ expect(removeNoteContext).not.toHaveBeenCalled();
+ });
+
+ it("moves the active split within its tab, but not past either end of it", async () => {
+ const container = await openSplits([ "split-a", "split-b" ]);
+ noteContexts.push(new NoteContext("other-tab"));
+ // two empty splits swap to nothing, so give every pane a note
+ for (const noteContext of noteContexts) {
+ noteContext.noteId = "note";
+ }
+
+ const triggerCommand = vi.spyOn(container, "triggerCommand");
+
+ await container.moveActiveNoteSplitLeftEvent({ ntxId: "split-b" });
+ expect(triggerCommand).toHaveBeenCalledWith("noteContextReorder",
+ expect.objectContaining({ ntxIdsInOrder: [ "split-b", "split-a", "other-tab" ] }));
+
+ triggerCommand.mockClear();
+ // split-b is the last pane of its tab -- moving it right would drag it into the next tab
+ await container.moveActiveNoteSplitRightEvent({ ntxId: "split-b" });
+ // and split-a is the first pane of the window
+ await container.moveActiveNoteSplitLeftEvent({ ntxId: "split-a" });
+ expect(triggerCommand).not.toHaveBeenCalled();
+ });
+ });
});
diff --git a/apps/client/src/widgets/containers/split_note_container.ts b/apps/client/src/widgets/containers/split_note_container.ts
index b7690bcf62..42ad8a751a 100644
--- a/apps/client/src/widgets/containers/split_note_container.ts
+++ b/apps/client/src/widgets/containers/split_note_container.ts
@@ -121,6 +121,12 @@ export default class SplitNoteContainer extends FlexContainer<SplitNoteWidget> {
return;
}
+ // A tab whose only pane is this one has no split to close -- closing it would take the whole
+ // tab down. ClosePaneButton hides itself there; the keyboard action can still ask for it.
+ if (contexts[currentIndex].getMainContext().getSubContexts().length <= 1) {
+ return;
+ }
+
const isRemoveMainContext = contexts[currentIndex].isMainContext();
if (isRemoveMainContext && currentIndex + 1 < contexts.length) {
const ntxIds = contexts.map((c) => c.ntxId).filter((c) => !!c) as string[];
@@ -143,10 +149,18 @@ export default class SplitNoteContainer extends FlexContainer<SplitNoteWidget> {
const contexts = appContext.tabManager.noteContexts;
const currentIndex = contexts.findIndex((c) => c.ntxId === ntxId);
+ if (currentIndex === -1) {
+ logError(`invalid context! ntxId: ${ntxId}`);
+ return;
+ }
+
const leftIndex = isMovingLeft ? currentIndex - 1 : currentIndex;
- if (currentIndex === -1 || leftIndex < 0 || leftIndex + 1 >= contexts.length) {
- logError(`invalid context! currentIndex: ${currentIndex}, leftIndex: ${leftIndex}, contexts.length: ${contexts.length}`);
+ // A tab lays its panes out as the main context followed by its sub-contexts, so the right
+ // half of the swapped pair is a sub-context whenever the pair sits inside one tab. Anything
+ // else is at a tab boundary, where a swap would move a pane into the neighbouring tab.
+ // MovePaneButton hides itself in that state; the keyboard actions can still ask for it.
+ if (leftIndex < 0 || leftIndex + 1 >= contexts.length || !contexts[leftIndex + 1].mainNtxId) {
return;
}
@@ -178,6 +192,24 @@ export default class SplitNoteContainer extends FlexContainer<SplitNoteWidget> {
splitService.moveNoteSplitResizer(ntxIds[leftIndex]);
}
+ /*
+ * Entry points for the split keyboard actions. They dispatch from `appContext`, which finds no
+ * matching command on its top-level components and distributes downwards as an event instead, so
+ * they cannot reuse the command handlers above -- those only run when a pane button bubbles the
+ * command up from the pane it belongs to.
+ */
+ async closeActiveNoteSplitEvent({ ntxId }: EventData<"closeActiveNoteSplit">) {
+ await this.closeThisNoteSplitCommand({ ntxId });
+ }
+
+ async moveActiveNoteSplitLeftEvent({ ntxId }: EventData<"moveActiveNoteSplitLeft">) {
+ await this.moveThisNoteSplitCommand({ ntxId, isMovingLeft: true });
+ }
+
+ async moveActiveNoteSplitRightEvent({ ntxId }: EventData<"moveActiveNoteSplitRight">) {
+ await this.moveThisNoteSplitCommand({ ntxId, isMovingLeft: false });
+ }
+
activeContextChangedEvent() {
this.refresh();
}
diff --git a/apps/server/src/assets/translations/en/server.json b/apps/server/src/assets/translations/en/server.json
index 28877c7606..4e9c23851d 100644
--- a/apps/server/src/assets/translations/en/server.json
+++ b/apps/server/src/assets/translations/en/server.json
@@ -70,7 +70,9 @@
"save-named-revision": "Save a named revision of the active note",
"toggle-book-properties": "Toggle Collection Properties",
"toggle-classic-editor-toolbar": "Toggle the Formatting tab for the editor with fixed toolbar",
- "toggle-zen-mode": "Enables/disables the zen mode (minimal UI for more focused editing)"
+ "toggle-zen-mode": "Enables/disables the zen mode (minimal UI for more focused editing)",
+ "create-new-split": "Open a new empty split to the right of the active one",
+ "close-active-split": "Close the active split, keeping the other splits of the tab open"
},
"keyboard_action_names": {
"back-in-note-history": "Back in Note History",
@@ -173,7 +175,11 @@
"reset-zoom-level": "Reset Zoom Level",
"copy-without-formatting": "Copy Without Formatting",
"force-save-revision": "Force Save Revision",
- "save-named-revision": "Save Named Revision"
+ "save-named-revision": "Save Named Revision",
+ "create-new-split": "Create New Split",
+ "close-active-split": "Close Active Split",
+ "move-split-left": "Move Split Left",
+ "move-split-right": "Move Split Right"
},
"login": {
"title": "Login",
diff --git a/packages/commons/src/lib/keyboard_actions_interface.ts b/packages/commons/src/lib/keyboard_actions_interface.ts
index 2a0ee53858..7e9391fc03 100644
--- a/packages/commons/src/lib/keyboard_actions_interface.ts
+++ b/packages/commons/src/lib/keyboard_actions_interface.ts
@@ -49,6 +49,10 @@ const enum KeyboardActionNamesEnum {
eigthTab,
ninthTab,
lastTab,
+ openNewNoteSplit,
+ closeActiveNoteSplit,
+ moveActiveNoteSplitLeft,
+ moveActiveNoteSplitRight,
showNoteSource,
showOptions,
showRevisions,
diff --git a/packages/trilium-core/src/services/keyboard_actions.spec.ts b/packages/trilium-core/src/services/keyboard_actions.spec.ts
index 169d53a6e0..9b98cd00b5 100644
--- a/packages/trilium-core/src/services/keyboard_actions.spec.ts
+++ b/packages/trilium-core/src/services/keyboard_actions.spec.ts
@@ -51,6 +51,16 @@ describe("keyboard_actions service", () => {
}
});
+ it("ships the split actions unbound, so no layout is left unable to press them", () => {
+ const actions = keyboardActions.getDefaultKeyboardActions();
+
+ for (const actionName of [ "openNewNoteSplit", "closeActiveNoteSplit", "moveActiveNoteSplitLeft", "moveActiveNoteSplitRight" ]) {
+ const action = actions.find((a) => "actionName" in a && a.actionName === actionName);
+ expect(action, actionName).toBeDefined();
+ expect(action && "defaultShortcuts" in action && action.defaultShortcuts, actionName).toEqual([]);
+ }
+ });
+
it("throws if loaded before translations are available", async () => {
// keyboard_actions was already evaluated during the bootstrap (binding the
// real i18next), so re-import a fresh copy that picks up the mocked module.
diff --git a/packages/trilium-core/src/services/keyboard_actions.ts b/packages/trilium-core/src/services/keyboard_actions.ts
index 8a0cebc32a..0342e1bc10 100644
--- a/packages/trilium-core/src/services/keyboard_actions.ts
+++ b/packages/trilium-core/src/services/keyboard_actions.ts
@@ -412,6 +412,36 @@ function getDefaultKeyboardActions() {
defaultShortcuts: [],
scope: "window"
},
+ {
+ actionName: "openNewNoteSplit",
+ friendlyName: t("keyboard_action_names.create-new-split"),
+ iconClass: "bx bx-dock-right",
+ defaultShortcuts: [],
+ description: t("keyboard_actions.create-new-split"),
+ scope: "window"
+ },
+ {
+ actionName: "closeActiveNoteSplit",
+ friendlyName: t("keyboard_action_names.close-active-split"),
+ iconClass: "bx bx-x",
+ defaultShortcuts: [],
+ description: t("keyboard_actions.close-active-split"),
+ scope: "window"
+ },
+ {
+ actionName: "moveActiveNoteSplitLeft",
+ friendlyName: t("keyboard_action_names.move-split-left"),
+ iconClass: "bx bx-chevron-left",
+ defaultShortcuts: [],
+ scope: "window"
+ },
+ {
+ actionName: "moveActiveNoteSplitRight",
+ friendlyName: t("keyboard_action_names.move-split-right"),
+ iconClass: "bx bx-chevron-right",
+ defaultShortcuts: [],
+ scope: "window"
+ },
{
separator: t("keyboard_actions.dialogs")