Skip to content

Commit 65cc57e

Browse files
authored
feat(split): allow closing any split pane (#7763)
2 parents 806c9a5 + 529f9a2 commit 65cc57e

File tree

4 files changed

+62
-15
lines changed

4 files changed

+62
-15
lines changed

apps/client/src/components/tab_manager.ts

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -647,7 +647,32 @@ export default class TabManager extends Component {
647647
...this.noteContexts.slice(-noteContexts.length),
648648
...this.noteContexts.slice(lastClosedTab.position, -noteContexts.length)
649649
];
650-
this.noteContextReorderEvent({ ntxIdsInOrder: ntxsInOrder.map((nc) => nc.ntxId).filter((id) => id !== null) });
650+
651+
// Update mainNtxId if the restored pane is the main pane in the split pane
652+
const { oldMainNtxId, newMainNtxId } = (() => {
653+
if (noteContexts.length !== 1) {
654+
return { oldMainNtxId: undefined, newMainNtxId: undefined };
655+
}
656+
657+
const mainNtxId = noteContexts[0]?.mainNtxId;
658+
const index = this.noteContexts.findIndex(c => c.ntxId === mainNtxId);
659+
660+
// No need to update if the restored position is after mainNtxId
661+
if (index === -1 || lastClosedTab.position > index) {
662+
return { oldMainNtxId: undefined, newMainNtxId: undefined };
663+
}
664+
665+
return {
666+
oldMainNtxId: this.noteContexts[index].ntxId ?? undefined,
667+
newMainNtxId: noteContexts[0]?.ntxId ?? undefined
668+
};
669+
})();
670+
671+
this.triggerCommand("noteContextReorder", {
672+
ntxIdsInOrder: ntxsInOrder.map((nc) => nc.ntxId).filter((id) => id !== null),
673+
oldMainNtxId,
674+
newMainNtxId
675+
});
651676

652677
let mainNtx = noteContexts.find((nc) => nc.isMainContext());
653678
if (mainNtx) {

apps/client/src/widgets/buttons/close_pane_button.tsx

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,20 @@
11
import { useEffect, useState } from "preact/hooks";
22
import { t } from "../../services/i18n";
33
import ActionButton from "../react/ActionButton";
4-
import { useNoteContext, useTriliumEvent } from "../react/hooks";
4+
import { useNoteContext, useTriliumEvents } from "../react/hooks";
5+
import appContext from "../../components/app_context";
56

67
export default function ClosePaneButton() {
78
const { noteContext, ntxId, parentComponent } = useNoteContext();
8-
const [ isEnabled, setIsEnabled ] = useState(false);
9+
const [isEnabled, setIsEnabled] = useState(false);
910

1011
function refresh() {
11-
setIsEnabled(!!(noteContext && !!noteContext.mainNtxId));
12+
const isMainOfSomeContext = appContext.tabManager.noteContexts.some(c => c.mainNtxId === ntxId);
13+
setIsEnabled(!!(noteContext && (!!noteContext.mainNtxId || isMainOfSomeContext)));
1214
}
1315

14-
useTriliumEvent("noteContextReorder", refresh);
15-
useEffect(refresh, [ ntxId ]);
16+
useTriliumEvents(["noteContextRemoved", "noteContextReorder", "newNoteContextCreated"], refresh);
17+
useEffect(refresh, [ntxId]);
1618

1719
return (
1820
<ActionButton

apps/client/src/widgets/containers/split_note_container.ts

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -100,9 +100,22 @@ export default class SplitNoteContainer extends FlexContainer<SplitNoteWidget> {
100100
}
101101

102102
async closeThisNoteSplitCommand({ ntxId }: CommandListenerData<"closeThisNoteSplit">) {
103-
if (ntxId) {
104-
await appContext.tabManager.removeNoteContext(ntxId);
103+
if (!ntxId) return;
104+
const contexts = appContext.tabManager.noteContexts;
105+
const currentIndex = contexts.findIndex((c) => c.ntxId === ntxId);
106+
if (currentIndex === -1) return;
107+
108+
const isRemoveMainContext = contexts[currentIndex].isMainContext();
109+
if (isRemoveMainContext && currentIndex + 1 < contexts.length) {
110+
const ntxIds = contexts.map((c) => c.ntxId).filter((c) => !!c) as string[];
111+
this.triggerCommand("noteContextReorder", {
112+
ntxIdsInOrder: ntxIds,
113+
oldMainNtxId: ntxId,
114+
newMainNtxId: ntxIds[currentIndex + 1]
115+
});
105116
}
117+
118+
await appContext.tabManager.removeNoteContext(ntxId);
106119
}
107120

108121
async moveThisNoteSplitCommand({ ntxId, isMovingLeft }: CommandListenerData<"moveThisNoteSplit">) {
@@ -167,12 +180,16 @@ export default class SplitNoteContainer extends FlexContainer<SplitNoteWidget> {
167180
splitService.delNoteSplitResizer(ntxIds);
168181
}
169182

170-
contextsReopenedEvent({ ntxId, afterNtxId }: EventData<"contextsReopened">) {
171-
if (ntxId === undefined || afterNtxId === undefined) {
172-
// no single split reopened
173-
return;
183+
contextsReopenedEvent({ ntxId, mainNtxId, afterNtxId }: EventData<"contextsReopened">) {
184+
if (ntxId !== undefined && afterNtxId !== undefined) {
185+
this.$widget.find(`[data-ntx-id="${ntxId}"]`).insertAfter(this.$widget.find(`[data-ntx-id="${afterNtxId}"]`));
186+
} else if (mainNtxId) {
187+
const contexts = appContext.tabManager.noteContexts;
188+
const nextIndex = contexts.findIndex(c => c.ntxId === mainNtxId);
189+
const beforeNtxId = (nextIndex !== -1 && nextIndex + 1 < contexts.length) ? contexts[nextIndex + 1].ntxId : null;
190+
191+
this.$widget.find(`[data-ntx-id="${mainNtxId}"]`).insertBefore(this.$widget.find(`[data-ntx-id="${beforeNtxId}"]`));
174192
}
175-
this.$widget.find(`[data-ntx-id="${ntxId}"]`).insertAfter(this.$widget.find(`[data-ntx-id="${afterNtxId}"]`));
176193
}
177194

178195
async refresh() {

apps/client/src/widgets/tab_row.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -820,12 +820,15 @@ export default class TabRowWidget extends BasicWidget {
820820
}
821821

822822
contextsReopenedEvent({ mainNtxId, tabPosition }: EventData<"contextsReopened">) {
823-
if (!mainNtxId || !tabPosition) {
823+
if (!mainNtxId || tabPosition < 0) {
824824
// no tab reopened
825825
return;
826826
}
827827
const tabEl = this.getTabById(mainNtxId)[0];
828-
tabEl.parentNode?.insertBefore(tabEl, this.tabEls[tabPosition]);
828+
829+
if ( tabEl && tabEl.parentNode ){
830+
tabEl.parentNode.insertBefore(tabEl, this.tabEls[tabPosition]);
831+
}
829832
}
830833

831834
updateTabById(ntxId: string | null) {

0 commit comments

Comments
 (0)