Skip to content

Commit c14b66e

Browse files
authored
Merge pull request #811 from mathuo/784-class-dv-single-tab-not-updated-on-drag-drop-of-tabs
bug: fix classname enablement
2 parents a53665a + cc5037d commit c14b66e

File tree

2 files changed

+64
-42
lines changed

2 files changed

+64
-42
lines changed

packages/dockview-core/src/__tests__/dockview/components/titlebar/tabsContainer.spec.ts

+32
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import { fireEvent } from '@testing-library/dom';
1010
import { TestPanel } from '../../dockviewGroupPanelModel.spec';
1111
import { IDockviewPanel } from '../../../../dockview/dockviewPanel';
1212
import { fromPartial } from '@total-typescript/shoehorn';
13+
import { DockviewPanelApi } from '../../../../api/dockviewPanelApi';
1314

1415
describe('tabsContainer', () => {
1516
test('that an external event does not render a drop target and calls through to the group mode', () => {
@@ -815,4 +816,35 @@ describe('tabsContainer', () => {
815816
expect(result).toBeTruthy();
816817
expect(result!.childNodes.length).toBe(0);
817818
});
819+
820+
test('class dv-single-tab is present when only one tab exists`', () => {
821+
const cut = new TabsContainer(
822+
fromPartial<DockviewComponent>({
823+
options: {},
824+
}),
825+
fromPartial<DockviewGroupPanel>({})
826+
);
827+
828+
expect(cut.element.classList.contains('dv-single-tab')).toBeFalsy();
829+
830+
const panel1 = new TestPanel(
831+
'panel_1',
832+
fromPartial<DockviewPanelApi>({})
833+
);
834+
cut.openPanel(panel1);
835+
expect(cut.element.classList.contains('dv-single-tab')).toBeTruthy();
836+
837+
const panel2 = new TestPanel(
838+
'panel_2',
839+
fromPartial<DockviewPanelApi>({})
840+
);
841+
cut.openPanel(panel2);
842+
expect(cut.element.classList.contains('dv-single-tab')).toBeFalsy();
843+
844+
cut.closePanel(panel1);
845+
expect(cut.element.classList.contains('dv-single-tab')).toBeTruthy();
846+
847+
cut.closePanel(panel2);
848+
expect(cut.element.classList.contains('dv-single-tab')).toBeFalsy();
849+
});
818850
});

packages/dockview-core/src/dockview/components/titlebar/tabsContainer.ts

+32-42
Original file line numberDiff line numberDiff line change
@@ -205,24 +205,6 @@ export class TabsContainer
205205
this._element.appendChild(this.rightActionsContainer);
206206

207207
this.addDisposables(
208-
this.accessor.onDidAddPanel((e) => {
209-
if (e.api.group === this.group) {
210-
toggleClass(
211-
this._element,
212-
'dv-single-tab',
213-
this.size === 1
214-
);
215-
}
216-
}),
217-
this.accessor.onDidRemovePanel((e) => {
218-
if (e.api.group === this.group) {
219-
toggleClass(
220-
this._element,
221-
'dv-single-tab',
222-
this.size === 1
223-
);
224-
}
225-
}),
226208
this._onWillShowOverlay,
227209
this._onDrop,
228210
this._onTabDragStart,
@@ -296,30 +278,6 @@ export class TabsContainer
296278
// noop
297279
}
298280

299-
private addTab(
300-
tab: IValueDisposable<Tab>,
301-
index: number = this.tabs.length
302-
): void {
303-
if (index < 0 || index > this.tabs.length) {
304-
throw new Error('invalid location');
305-
}
306-
307-
this.tabContainer.insertBefore(
308-
tab.value.element,
309-
this.tabContainer.children[index]
310-
);
311-
312-
this.tabs = [
313-
...this.tabs.slice(0, index),
314-
tab,
315-
...this.tabs.slice(index),
316-
];
317-
318-
if (this.selectedIndex < 0) {
319-
this.selectedIndex = index;
320-
}
321-
}
322-
323281
public delete(id: string): void {
324282
const index = this.tabs.findIndex((tab) => tab.value.panel.id === id);
325283

@@ -330,6 +288,8 @@ export class TabsContainer
330288
disposable.dispose();
331289
value.dispose();
332290
value.element.remove();
291+
292+
this.updateClassnames();
333293
}
334294

335295
public setActivePanel(panel: IDockviewPanel): void {
@@ -430,4 +390,34 @@ export class TabsContainer
430390

431391
this.tabs = [];
432392
}
393+
394+
private addTab(
395+
tab: IValueDisposable<Tab>,
396+
index: number = this.tabs.length
397+
): void {
398+
if (index < 0 || index > this.tabs.length) {
399+
throw new Error('invalid location');
400+
}
401+
402+
this.tabContainer.insertBefore(
403+
tab.value.element,
404+
this.tabContainer.children[index]
405+
);
406+
407+
this.tabs = [
408+
...this.tabs.slice(0, index),
409+
tab,
410+
...this.tabs.slice(index),
411+
];
412+
413+
if (this.selectedIndex < 0) {
414+
this.selectedIndex = index;
415+
}
416+
417+
this.updateClassnames();
418+
}
419+
420+
private updateClassnames(): void {
421+
toggleClass(this._element, 'dv-single-tab', this.size === 1);
422+
}
433423
}

0 commit comments

Comments
 (0)