feat: provide 'inactive' property to addPanel

This commit is contained in:
mathuo 2024-04-17 20:27:39 +01:00
parent bc8fa59557
commit 4a05fd0aed
No known key found for this signature in database
GPG Key ID: C6EEDEFD6CA07281
3 changed files with 215 additions and 13 deletions

View File

@ -4764,4 +4764,147 @@ describe('dockviewComponent', () => {
expect(panel3.api.isVisible).toBeFalsy(); expect(panel3.api.isVisible).toBeFalsy();
expect(panel4.api.isVisible).toBeTruthy(); expect(panel4.api.isVisible).toBeTruthy();
}); });
describe('addPanel', () => {
test('that can add panel', () => {
const container = document.createElement('div');
const dockview = new DockviewComponent({
parentElement: container,
components: {
default: PanelContentPartTest,
},
});
const api = new DockviewApi(dockview);
dockview.layout(1000, 1000);
const panel1 = api.addPanel({
id: 'panel_1',
component: 'default',
});
expect(api.activePanel).toBe(panel1);
api.addPanel({
id: 'panel_2',
component: 'default',
inactive: true,
});
expect(api.activePanel).toBe(panel1);
});
test('that can add panel with absolute direction', () => {
const container = document.createElement('div');
const dockview = new DockviewComponent({
parentElement: container,
components: {
default: PanelContentPartTest,
},
});
const api = new DockviewApi(dockview);
dockview.layout(1000, 1000);
const panel1 = api.addPanel({
id: 'panel_1',
component: 'default',
position: { direction: 'right' },
});
expect(api.activePanel).toBe(panel1);
api.addPanel({
id: 'panel_2',
component: 'default',
position: { direction: 'right' },
inactive: true,
});
expect(api.activePanel).toBe(panel1);
});
test('that can add floating panel', () => {
const container = document.createElement('div');
const dockview = new DockviewComponent({
parentElement: container,
components: {
default: PanelContentPartTest,
},
});
const api = new DockviewApi(dockview);
dockview.layout(1000, 1000);
const panel1 = api.addPanel({
id: 'panel_1',
component: 'default',
floating: true,
});
expect(api.activePanel).toBe(panel1);
api.addPanel({
id: 'panel_2',
component: 'default',
floating: true,
inactive: true,
});
expect(api.activePanel).toBe(panel1);
});
test('that can add panel positional to another (within)', () => {
const container = document.createElement('div');
const dockview = new DockviewComponent({
parentElement: container,
components: {
default: PanelContentPartTest,
},
});
const api = new DockviewApi(dockview);
dockview.layout(1000, 1000);
const panel1 = api.addPanel({
id: 'panel_1',
component: 'default',
});
expect(api.activePanel).toBe(panel1);
api.addPanel({
id: 'panel_2',
component: 'default',
position: { direction: 'within', referencePanel: panel1 },
inactive: true,
});
expect(api.activePanel).toBe(panel1);
});
test('that can add panel positional to another (not within)', () => {
const container = document.createElement('div');
const dockview = new DockviewComponent({
parentElement: container,
components: {
default: PanelContentPartTest,
},
});
const api = new DockviewApi(dockview);
dockview.layout(1000, 1000);
const panel1 = api.addPanel({
id: 'panel_1',
component: 'default',
});
expect(api.activePanel).toBe(panel1);
api.addPanel({
id: 'panel_2',
component: 'default',
position: { direction: 'right', referencePanel: panel1 },
inactive: true,
});
expect(api.activePanel).toBe(panel1);
});
});
}); });

View File

@ -1474,8 +1474,15 @@ export class DockviewComponent
); );
const panel = this.createPanel(options, group); const panel = this.createPanel(options, group);
group.model.openPanel(panel); group.model.openPanel(panel, {
this.doSetGroupAndPanelActive(group); skipSetActive: options.inactive,
skipSetGroupActive: options.inactive,
});
if (!options.inactive) {
this.doSetGroupAndPanelActive(group);
}
return panel; return panel;
} }
} else { } else {
@ -1507,14 +1514,23 @@ export class DockviewComponent
panel = this.createPanel(options, group); panel = this.createPanel(options, group);
group.model.openPanel(panel); group.model.openPanel(panel, {
skipSetActive: options.inactive,
skipSetGroupActive: options.inactive,
});
} else if ( } else if (
referenceGroup.api.location.type === 'floating' || referenceGroup.api.location.type === 'floating' ||
target === 'center' target === 'center'
) { ) {
panel = this.createPanel(options, referenceGroup); panel = this.createPanel(options, referenceGroup);
referenceGroup.model.openPanel(panel); referenceGroup.model.openPanel(panel, {
this.doSetGroupAndPanelActive(referenceGroup); skipSetActive: options.inactive,
skipSetGroupActive: options.inactive,
});
if (!options.inactive) {
this.doSetGroupAndPanelActive(referenceGroup);
}
} else { } else {
const location = getGridLocation(referenceGroup.element); const location = getGridLocation(referenceGroup.element);
const relativeLocation = getRelativeLocation( const relativeLocation = getRelativeLocation(
@ -1524,32 +1540,47 @@ export class DockviewComponent
); );
const group = this.createGroupAtLocation(relativeLocation); const group = this.createGroupAtLocation(relativeLocation);
panel = this.createPanel(options, group); panel = this.createPanel(options, group);
group.model.openPanel(panel); group.model.openPanel(panel, {
this.doSetGroupAndPanelActive(group); skipSetActive: options.inactive,
skipSetGroupActive: options.inactive,
});
if (!options.inactive) {
this.doSetGroupAndPanelActive(group);
}
} }
} else if (options.floating) { } else if (options.floating) {
const group = this.createGroup(); const group = this.createGroup();
this._onDidAddGroup.fire(group); this._onDidAddGroup.fire(group);
const o = const coordinates =
typeof options.floating === 'object' && typeof options.floating === 'object' &&
options.floating !== null options.floating !== null
? options.floating ? options.floating
: {}; : {};
this.addFloatingGroup(group, o, { this.addFloatingGroup(group, coordinates, {
inDragMode: false, inDragMode: false,
skipRemoveGroup: true, skipRemoveGroup: true,
skipActiveGroup: true, skipActiveGroup: true,
}); });
panel = this.createPanel(options, group); panel = this.createPanel(options, group);
group.model.openPanel(panel); group.model.openPanel(panel, {
skipSetActive: options.inactive,
skipSetGroupActive: options.inactive,
});
} else { } else {
const group = this.createGroupAtLocation(); const group = this.createGroupAtLocation();
panel = this.createPanel(options, group); panel = this.createPanel(options, group);
group.model.openPanel(panel); group.model.openPanel(panel, {
this.doSetGroupAndPanelActive(group); skipSetActive: options.inactive,
skipSetGroupActive: options.inactive,
});
if (!options.inactive) {
this.doSetGroupAndPanelActive(group);
}
} }
return panel; return panel;

View File

@ -13,7 +13,10 @@ import { ISplitviewStyles, Orientation } from '../splitview/splitview';
import { PanelTransfer } from '../dnd/dataTransfer'; import { PanelTransfer } from '../dnd/dataTransfer';
import { IDisposable } from '../lifecycle'; import { IDisposable } from '../lifecycle';
import { DroptargetOverlayModel, Position } from '../dnd/droptarget'; import { DroptargetOverlayModel, Position } from '../dnd/droptarget';
import { DockviewGroupDropLocation, GroupOptions } from './dockviewGroupPanelModel'; import {
DockviewGroupDropLocation,
GroupOptions,
} from './dockviewGroupPanelModel';
import { IDockviewPanel } from './dockviewPanel'; import { IDockviewPanel } from './dockviewPanel';
import { import {
ComponentConstructor, ComponentConstructor,
@ -171,11 +174,36 @@ type AddPanelOptionsUnion = AddPanelFloatingGroupUnion | AddPanelPositionUnion;
export type AddPanelOptions<P extends object = Parameters> = { export type AddPanelOptions<P extends object = Parameters> = {
params?: P; params?: P;
/**
* The unique id for the panel
*/
id: string; id: string;
/**
* The title for the panel which can be accessed within both the tab and component.
*
* If using the default tab renderer this title will be displayed in the tab.
*/
title?: string; title?: string;
/**
* The id of the component renderer
*/
component: string; component: string;
/**
* The id of the tab componnet renderer
*/
tabComponent?: string; tabComponent?: string;
/**
* The rendering mode of the panel.
*
* This dictates what happens to the HTML of the panel when it is hidden.
*/
renderer?: DockviewPanelRenderer; renderer?: DockviewPanelRenderer;
/**
* If true then add the panel without setting it as the active panel.
*
* Defaults to `false` which forces newly added panels to become active.
*/
inactive?: boolean;
} & Partial<AddPanelOptionsUnion>; } & Partial<AddPanelOptionsUnion>;
type AddGroupOptionsWithPanel = { type AddGroupOptionsWithPanel = {