From 20b6db22022b83afa42e95b8d0bef9b3fda10242 Mon Sep 17 00:00:00 2001 From: mathuo <6710312+mathuo@users.noreply.github.com> Date: Sun, 1 May 2022 21:08:59 +0100 Subject: [PATCH] feat: api improvements --- .../src/__tests__/api/component.api.spec.ts | 4 +- packages/dockview/src/api/component.api.ts | 25 ++++++----- packages/dockview/src/api/groupPanelApi.ts | 3 +- .../src/paneview/draggablePaneviewPanel.ts | 4 +- .../src/paneview/paneviewComponent.ts | 14 +++---- .../src/splitview/splitviewComponent.ts | 42 +++++++++---------- 6 files changed, 48 insertions(+), 44 deletions(-) diff --git a/packages/dockview/src/__tests__/api/component.api.spec.ts b/packages/dockview/src/__tests__/api/component.api.spec.ts index bd79d0f88..5a61fb9a8 100644 --- a/packages/dockview/src/__tests__/api/component.api.spec.ts +++ b/packages/dockview/src/__tests__/api/component.api.spec.ts @@ -22,7 +22,7 @@ describe('component.api', () => { 'onDidLayoutChange', 'onDidAddView', 'onDidRemoveView', - 'getPanels', + 'panels', 'focus', 'resizeToFit', 'toJSON', @@ -54,7 +54,7 @@ describe('component.api', () => { 'onDidLayoutChange', 'onDidAddView', 'onDidRemoveView', - 'getPanels', + 'panels', 'focus', 'resizeToFit', 'toJSON', diff --git a/packages/dockview/src/api/component.api.ts b/packages/dockview/src/api/component.api.ts index 41430f0d2..cfac34f64 100644 --- a/packages/dockview/src/api/component.api.ts +++ b/packages/dockview/src/api/component.api.ts @@ -1,4 +1,5 @@ import { + DockviewDropEvent, IDockviewComponent, SerializedDockview, } from '../dockview/dockviewComponent'; @@ -71,6 +72,10 @@ export class SplitviewApi implements CommonApi { return this.component.orientation; } + get panels(): ISplitviewPanel[] { + return this.component.panels; + } + get onDidLayoutFromJSON(): Event { return this.component.onDidLayoutFromJSON; } @@ -101,10 +106,6 @@ export class SplitviewApi implements CommonApi { this.component.setVisible(panel, isVisible); } - getPanels(): ISplitviewPanel[] { - return this.component.getPanels(); - } - focus(): void { this.component.focus(); } @@ -159,6 +160,10 @@ export class PaneviewApi implements CommonApi { return this.component.width; } + get panels(): IPaneviewPanel[] { + return this.component.panels; + } + get onDidLayoutChange(): Event { return this.component.onDidLayoutChange; } @@ -192,8 +197,8 @@ export class PaneviewApi implements CommonApi { constructor(private readonly component: IPaneviewComponent) {} - getPanels(): IPaneviewPanel[] { - return this.component.getPanels(); + addPanel(options: AddPaneviewComponentOptions): IPaneviewPanel { + return this.component.addPanel(options); } removePanel(panel: IPaneviewPanel): void { @@ -216,10 +221,6 @@ export class PaneviewApi implements CommonApi { this.component.layout(width, height); } - addPanel(options: AddPaneviewComponentOptions): void { - this.component.addPanel(options); - } - resizeToFit(): void { this.component.resizeToFit(); } @@ -409,6 +410,10 @@ export class DockviewApi implements CommonApi { return this.component.onDidLayoutChange; } + get onDidDrop(): Event { + return this.component.onDidDrop; + } + get panels(): IGroupPanel[] { return this.component.panels; } diff --git a/packages/dockview/src/api/groupPanelApi.ts b/packages/dockview/src/api/groupPanelApi.ts index f8fa40f78..4bface8ef 100644 --- a/packages/dockview/src/api/groupPanelApi.ts +++ b/packages/dockview/src/api/groupPanelApi.ts @@ -16,8 +16,7 @@ export interface SuppressClosableEvent { * omit visibility modifiers since the visibility of a single group doesn't make sense * because it belongs to a groupview */ -export interface DockviewPanelApi - extends Omit { +export interface DockviewPanelApi extends Omit { readonly group: GroupviewPanel | undefined; readonly isGroupActive: boolean; readonly title: string; diff --git a/packages/dockview/src/paneview/draggablePaneviewPanel.ts b/packages/dockview/src/paneview/draggablePaneviewPanel.ts index 7cc0b6626..ff3309eec 100644 --- a/packages/dockview/src/paneview/draggablePaneviewPanel.ts +++ b/packages/dockview/src/paneview/draggablePaneviewPanel.ts @@ -115,10 +115,10 @@ export abstract class DraggablePaneviewPanel extends PaneviewPanel { return; } - const allPanels = containerApi.getPanels(); + const allPanels = containerApi.panels; const fromIndex = allPanels.indexOf(existingPanel); - let toIndex = containerApi.getPanels().indexOf(this); + let toIndex = containerApi.panels.indexOf(this); if ( event.position === Position.Left || diff --git a/packages/dockview/src/paneview/paneviewComponent.ts b/packages/dockview/src/paneview/paneviewComponent.ts index 65e251c08..72a7cbeb9 100644 --- a/packages/dockview/src/paneview/paneviewComponent.ts +++ b/packages/dockview/src/paneview/paneviewComponent.ts @@ -98,6 +98,7 @@ export interface IPaneviewComponent extends IDisposable { readonly height: number; readonly minimumSize: number; readonly maximumSize: number; + readonly panels: IPaneviewPanel[]; readonly onDidAddView: Event; readonly onDidRemoveView: Event; readonly onDidDrop: Event; @@ -109,7 +110,6 @@ export interface IPaneviewComponent extends IDisposable { fromJSON(serializedPaneview: SerializedPaneview): void; resizeToFit(): void; focus(): void; - getPanels(): IPaneviewPanel[]; removePanel(panel: IPaneviewPanel): void; getPanel(id: string): IPaneviewPanel | undefined; movePanel(from: number, to: number): void; @@ -139,6 +139,10 @@ export class PaneviewComponent private readonly _onDidRemoveView = new Emitter(); readonly onDidRemoveView = this._onDidRemoveView.event; + get panels(): PaneviewPanel[] { + return this.paneview.getPanes(); + } + set paneview(value: Paneview) { this._paneview = value; @@ -288,12 +292,8 @@ export class PaneviewComponent return view; } - getPanels(): PaneviewPanel[] { - return this.paneview.getPanes(); - } - removePanel(panel: PaneviewPanel) { - const views = this.getPanels(); + const views = this.panels; const index = views.findIndex((_) => _ === panel); this.paneview.removePane(index); @@ -305,7 +305,7 @@ export class PaneviewComponent } getPanel(id: string): PaneviewPanel | undefined { - return this.getPanels().find((view) => view.id === id); + return this.panels.find((view) => view.id === id); } layout(width: number, height: number): void { diff --git a/packages/dockview/src/splitview/splitviewComponent.ts b/packages/dockview/src/splitview/splitviewComponent.ts index a0e223f39..45b78a5bc 100644 --- a/packages/dockview/src/splitview/splitviewComponent.ts +++ b/packages/dockview/src/splitview/splitviewComponent.ts @@ -61,6 +61,7 @@ export interface ISplitviewComponent extends IDisposable { readonly onDidAddView: Event; readonly onDidRemoveView: Event; readonly onDidLayoutFromJSON: Event; + readonly panels: SplitviewPanel[]; updateOptions(options: Partial): void; addPanel(options: AddSplitviewComponentOptions): void; layout(width: number, height: number): void; @@ -72,7 +73,6 @@ export interface ISplitviewComponent extends IDisposable { getPanel(id: string): ISplitviewPanel | undefined; setActive(view: ISplitviewPanel, skipFocus?: boolean): void; removePanel(panel: ISplitviewPanel, sizing?: Sizing): void; - getPanels(): SplitviewPanel[]; setVisible(panel: ISplitviewPanel, visible: boolean): void; movePanel(from: number, to: number): void; } @@ -87,7 +87,7 @@ export class SplitviewComponent private _disposable = new MutableDisposable(); private _splitview!: Splitview; private _activePanel: SplitviewPanel | undefined; - private panels = new Map>(); + private _panels = new Map>(); private _options: SplitviewComponentOptions; private readonly _onDidLayoutfromJSON = new Emitter(); @@ -102,6 +102,10 @@ export class SplitviewComponent private readonly _onDidLayoutChange = new Emitter(); readonly onDidLayoutChange: Event = this._onDidLayoutChange.event; + get panels(): SplitviewPanel[] { + return this.splitview.getViews(); + } + get options() { return this._options; } @@ -149,7 +153,7 @@ export class SplitviewComponent } get length() { - return this.panels.size; + return this._panels.size; } constructor( @@ -204,14 +208,14 @@ export class SplitviewComponent } setVisible(panel: SplitviewPanel, visible: boolean) { - const index = this.getPanels().indexOf(panel); + const index = this.panels.indexOf(panel); this.splitview.setViewVisible(index, visible); } setActive(view: SplitviewPanel, skipFocus?: boolean) { this._activePanel = view; - this.getPanels() + this.panels .filter((v) => v !== view) .forEach((v) => { v.api._onDidActiveChange.fire({ isActive: false }); @@ -225,12 +229,8 @@ export class SplitviewComponent } } - getPanels(): SplitviewPanel[] { - return this.splitview.getViews(); - } - removePanel(panel: SplitviewPanel, sizing?: Sizing) { - const disposable = this.panels.get(panel.id); + const disposable = this._panels.get(panel.id); if (!disposable) { throw new Error(`unknown splitview panel ${panel.id}`); @@ -239,23 +239,23 @@ export class SplitviewComponent disposable.disposable.dispose(); disposable.value.dispose(); - this.panels.delete(panel.id); + this._panels.delete(panel.id); - const index = this.getPanels().findIndex((_) => _ === panel); + const index = this.panels.findIndex((_) => _ === panel); this.splitview.removeView(index, sizing); - const panels = this.getPanels(); + const panels = this.panels; if (panels.length > 0) { this.setActive(panels[panels.length - 1]); } } getPanel(id: string): SplitviewPanel | undefined { - return this.getPanels().find((view) => view.id === id); + return this.panels.find((view) => view.id === id); } addPanel(options: AddSplitviewComponentOptions): void { - if (this.panels.has(options.id)) { + if (this._panels.has(options.id)) { throw new Error(`panel ${options.id} already exists`); } @@ -322,7 +322,7 @@ export class SplitviewComponent this.setActive(view, true); }); - this.panels.set(view.id, { disposable, value: view }); + this._panels.set(view.id, { disposable, value: view }); } toJSON(): SerializedSplitview { @@ -349,11 +349,11 @@ export class SplitviewComponent fromJSON(serializedSplitview: SerializedSplitview): void { const { views, orientation, size, activeView } = serializedSplitview; - for (const [_, value] of this.panels.entries()) { + for (const [_, value] of this._panels.entries()) { value.disposable.dispose(); value.value.dispose(); } - this.panels.clear(); + this._panels.clear(); this.splitview.dispose(); const queue: Function[] = []; @@ -366,7 +366,7 @@ export class SplitviewComponent views: views.map((view) => { const data = view.data; - if (this.panels.has(data.id)) { + if (this._panels.has(data.id)) { throw new Error(`panel ${data.id} already exists`); } @@ -423,11 +423,11 @@ export class SplitviewComponent } dispose() { - for (const [_, value] of this.panels.entries()) { + for (const [_, value] of this._panels.entries()) { value.disposable.dispose(); value.value.dispose(); } - this.panels.clear(); + this._panels.clear(); this.splitview.dispose();