Skip to content

Commit

Permalink
Merge pull request #205 from irahopkinson/fix-and-improve-types
Browse files Browse the repository at this point in the history
Fix `find` return type and improve external types
  • Loading branch information
rinick authored Jun 8, 2023
2 parents 13c7463 + 13f441e commit 8999608
Show file tree
Hide file tree
Showing 13 changed files with 78 additions and 69 deletions.
2 changes: 1 addition & 1 deletion es/Algorithm.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export declare enum Filter {
AnyTabPanel = 123,
All = 127
}
export declare function find(layout: LayoutData, id: string, filter?: Filter): PanelData | TabData | BoxData;
export declare function find(layout: LayoutData, id: string, filter?: Filter): PanelData | TabData | BoxData | undefined;
export declare function addNextToTab(layout: LayoutData, source: TabData | PanelData, target: TabData, direction: DropDirection): LayoutData;
export declare function addTabToPanel(layout: LayoutData, source: TabData | PanelData, panel: PanelData, idx?: number): LayoutData;
export declare function converToPanel(source: TabData | PanelData): PanelData;
Expand Down
7 changes: 5 additions & 2 deletions es/Algorithm.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,16 @@ function findInPanel(panel, id, filter) {
}
}
}
return null;
return undefined;
}
function findInBox(box, id, filter) {
let result;
if ((filter | Filter.Box) && box.id === id) {
if ((filter | Filter.Box) && (box === null || box === void 0 ? void 0 : box.id) === id) {
return box;
}
if (!(box === null || box === void 0 ? void 0 : box.children)) {
return undefined;
}
for (let child of box.children) {
if ('children' in child) {
if (result = findInBox(child, id, filter)) {
Expand Down
23 changes: 12 additions & 11 deletions es/DockData.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,15 @@ export interface LayoutData extends LayoutBase {
loadedFrom?: LayoutBase;
}
export declare type DropDirection = 'left' | 'right' | 'bottom' | 'top' | 'middle' | 'remove' | 'before-tab' | 'after-tab' | 'float' | 'front' | 'maximize' | 'new-window' | 'move' | 'active' | 'update';
export interface FloatSize {
width: number;
height: number;
}
export interface FloatPosition extends FloatSize {
left: number;
top: number;
}
export declare type LayoutSize = FloatSize;
export interface DockContext {
/** @ignore */
getDockId(): any;
Expand All @@ -254,10 +263,7 @@ export interface DockContext {
clientY: number;
}, panelSize?: [number, number]): void;
/** @ignore */
getLayoutSize(): {
width: number;
height: number;
};
getLayoutSize(): LayoutSize;
/** @ignore
* When a state change happen to the layout that's handled locally, like inside DockPanel or DockBox.
* It still need to tell the context there is a change so DockLayout can call onLayoutChange callback.
Expand All @@ -276,20 +282,15 @@ export interface DockContext {
* - when direction is 'float', target doesn't matter. If this is called directly from code without any user interaction, source must be PanelData with x,y,w,h properties
* @param floatPosition position of float panel, used only when direction="float"
*/
dockMove(source: TabData | PanelData, target: string | TabData | PanelData | BoxData | null, direction: DropDirection, floatPosition?: {
left: number;
top: number;
width: number;
height: number;
}): void;
dockMove(source: TabData | PanelData, target: string | TabData | PanelData | BoxData | null, direction: DropDirection, floatPosition?: FloatPosition): void;
/**
* Get the TabGroup defined in defaultLayout
*/
getGroup(name: string): TabGroup;
/**
* Find PanelData or TabData by id
*/
find(id: string, filter?: Filter): PanelData | TabData | BoxData;
find(id: string, filter?: Filter): PanelData | TabData | BoxData | undefined;
/**
* Update a tab with new TabData
* @param id tab id to update
Expand Down
16 changes: 4 additions & 12 deletions es/DockLayout.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as React from "react";
import { BoxData, DockContext, DropDirection, LayoutBase, LayoutData, PanelBase, PanelData, TabBase, TabData, TabGroup, TabPaneCache } from "./DockData";
import { BoxData, DockContext, DropDirection, FloatPosition, LayoutBase, LayoutData, LayoutSize, PanelBase, PanelData, TabBase, TabData, TabGroup, TabPaneCache } from "./DockData";
import * as Algorithm from "./Algorithm";
export interface LayoutProps {
/**
Expand Down Expand Up @@ -109,19 +109,11 @@ export declare class DockLayout extends DockPortalManager implements DockContext
* @param direction @inheritDoc
* @param floatPosition @inheritDoc
*/
dockMove(source: TabData | PanelData, target: string | TabData | PanelData | BoxData | null, direction: DropDirection, floatPosition?: {
left: number;
top: number;
width: number;
height: number;
}): void;
dockMove(source: TabData | PanelData, target: string | TabData | PanelData | BoxData | null, direction: DropDirection, floatPosition?: FloatPosition): void;
/** @inheritDoc */
find(id: string, filter?: Algorithm.Filter): PanelData | TabData | BoxData;
find(id: string, filter?: Algorithm.Filter): PanelData | TabData | BoxData | undefined;
/** @ignore */
getLayoutSize(): {
width: number;
height: number;
};
getLayoutSize(): LayoutSize;
/** @inheritDoc */
updateTab(id: string, newTab: TabData | null, makeActive?: boolean): boolean;
/** @inheritDoc */
Expand Down
2 changes: 1 addition & 1 deletion es/DockLayout.js
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ export class DockLayout extends DockPortalManager {
}
else if (target) {
if ('tabs' in target) {
// pandel target
// panel target
if (direction === 'middle') {
layout = Algorithm.addTabToPanel(layout, source, target);
}
Expand Down
2 changes: 1 addition & 1 deletion lib/Algorithm.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export declare enum Filter {
AnyTabPanel = 123,
All = 127
}
export declare function find(layout: LayoutData, id: string, filter?: Filter): PanelData | TabData | BoxData;
export declare function find(layout: LayoutData, id: string, filter?: Filter): PanelData | TabData | BoxData | undefined;
export declare function addNextToTab(layout: LayoutData, source: TabData | PanelData, target: TabData, direction: DropDirection): LayoutData;
export declare function addTabToPanel(layout: LayoutData, source: TabData | PanelData, panel: PanelData, idx?: number): LayoutData;
export declare function converToPanel(source: TabData | PanelData): PanelData;
Expand Down
7 changes: 5 additions & 2 deletions lib/Algorithm.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,13 +77,16 @@ function findInPanel(panel, id, filter) {
}
}
}
return null;
return undefined;
}
function findInBox(box, id, filter) {
let result;
if ((filter | Filter.Box) && box.id === id) {
if ((filter | Filter.Box) && (box === null || box === void 0 ? void 0 : box.id) === id) {
return box;
}
if (!(box === null || box === void 0 ? void 0 : box.children)) {
return undefined;
}
for (let child of box.children) {
if ('children' in child) {
if (result = findInBox(child, id, filter)) {
Expand Down
23 changes: 12 additions & 11 deletions lib/DockData.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,15 @@ export interface LayoutData extends LayoutBase {
loadedFrom?: LayoutBase;
}
export declare type DropDirection = 'left' | 'right' | 'bottom' | 'top' | 'middle' | 'remove' | 'before-tab' | 'after-tab' | 'float' | 'front' | 'maximize' | 'new-window' | 'move' | 'active' | 'update';
export interface FloatSize {
width: number;
height: number;
}
export interface FloatPosition extends FloatSize {
left: number;
top: number;
}
export declare type LayoutSize = FloatSize;
export interface DockContext {
/** @ignore */
getDockId(): any;
Expand All @@ -254,10 +263,7 @@ export interface DockContext {
clientY: number;
}, panelSize?: [number, number]): void;
/** @ignore */
getLayoutSize(): {
width: number;
height: number;
};
getLayoutSize(): LayoutSize;
/** @ignore
* When a state change happen to the layout that's handled locally, like inside DockPanel or DockBox.
* It still need to tell the context there is a change so DockLayout can call onLayoutChange callback.
Expand All @@ -276,20 +282,15 @@ export interface DockContext {
* - when direction is 'float', target doesn't matter. If this is called directly from code without any user interaction, source must be PanelData with x,y,w,h properties
* @param floatPosition position of float panel, used only when direction="float"
*/
dockMove(source: TabData | PanelData, target: string | TabData | PanelData | BoxData | null, direction: DropDirection, floatPosition?: {
left: number;
top: number;
width: number;
height: number;
}): void;
dockMove(source: TabData | PanelData, target: string | TabData | PanelData | BoxData | null, direction: DropDirection, floatPosition?: FloatPosition): void;
/**
* Get the TabGroup defined in defaultLayout
*/
getGroup(name: string): TabGroup;
/**
* Find PanelData or TabData by id
*/
find(id: string, filter?: Filter): PanelData | TabData | BoxData;
find(id: string, filter?: Filter): PanelData | TabData | BoxData | undefined;
/**
* Update a tab with new TabData
* @param id tab id to update
Expand Down
16 changes: 4 additions & 12 deletions lib/DockLayout.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as React from "react";
import { BoxData, DockContext, DropDirection, LayoutBase, LayoutData, PanelBase, PanelData, TabBase, TabData, TabGroup, TabPaneCache } from "./DockData";
import { BoxData, DockContext, DropDirection, FloatPosition, LayoutBase, LayoutData, LayoutSize, PanelBase, PanelData, TabBase, TabData, TabGroup, TabPaneCache } from "./DockData";
import * as Algorithm from "./Algorithm";
export interface LayoutProps {
/**
Expand Down Expand Up @@ -109,19 +109,11 @@ export declare class DockLayout extends DockPortalManager implements DockContext
* @param direction @inheritDoc
* @param floatPosition @inheritDoc
*/
dockMove(source: TabData | PanelData, target: string | TabData | PanelData | BoxData | null, direction: DropDirection, floatPosition?: {
left: number;
top: number;
width: number;
height: number;
}): void;
dockMove(source: TabData | PanelData, target: string | TabData | PanelData | BoxData | null, direction: DropDirection, floatPosition?: FloatPosition): void;
/** @inheritDoc */
find(id: string, filter?: Algorithm.Filter): PanelData | TabData | BoxData;
find(id: string, filter?: Algorithm.Filter): PanelData | TabData | BoxData | undefined;
/** @ignore */
getLayoutSize(): {
width: number;
height: number;
};
getLayoutSize(): LayoutSize;
/** @inheritDoc */
updateTab(id: string, newTab: TabData | null, makeActive?: boolean): boolean;
/** @inheritDoc */
Expand Down
2 changes: 1 addition & 1 deletion lib/DockLayout.js
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ class DockLayout extends DockPortalManager {
}
else if (target) {
if ('tabs' in target) {
// pandel target
// panel target
if (direction === 'middle') {
layout = Algorithm.addTabToPanel(layout, source, target);
}
Expand Down
17 changes: 10 additions & 7 deletions src/Algorithm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ export function nextZIndex(current?: number): number {
}


function findInPanel(panel: PanelData, id: string, filter: Filter): PanelData | TabData {
function findInPanel(panel: PanelData, id: string, filter: Filter): PanelData | TabData | undefined {
if (panel.id === id && (filter & Filter.Panel)) {
return panel;
}
Expand All @@ -91,14 +91,17 @@ function findInPanel(panel: PanelData, id: string, filter: Filter): PanelData |
}
}
}
return null;
return undefined;
}

function findInBox(box: BoxData, id: string, filter: Filter): PanelData | TabData | BoxData {
let result: PanelData | TabData | BoxData;
if ((filter | Filter.Box) && box.id === id) {
function findInBox(box: BoxData | undefined, id: string, filter: Filter): PanelData | TabData | BoxData | undefined {
let result: PanelData | TabData | BoxData | undefined;
if ((filter | Filter.Box) && box?.id === id) {
return box;
}
if (!box?.children) {
return undefined;
}
for (let child of box.children) {
if ('children' in child) {
if (result = findInBox(child, id, filter)) {
Expand Down Expand Up @@ -130,8 +133,8 @@ export enum Filter {
}


export function find(layout: LayoutData, id: string, filter: Filter = Filter.AnyTabPanel): PanelData | TabData | BoxData {
let result: PanelData | TabData | BoxData;
export function find(layout: LayoutData, id: string, filter: Filter = Filter.AnyTabPanel): PanelData | TabData | BoxData | undefined {
let result: PanelData | TabData | BoxData | undefined;

if (filter & Filter.Docked) {
result = findInBox(layout.dockbox, id, filter);
Expand Down
20 changes: 16 additions & 4 deletions src/DockData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,18 @@ export type DropDirection =
| 'update' // tab updated with updateTab
;

export interface FloatSize {
width: number;
height: number;
}

export interface FloatPosition extends FloatSize {
left: number;
top: number;
}

export type LayoutSize = FloatSize;

export interface DockContext {
/** @ignore */
getDockId(): any;
Expand All @@ -321,7 +333,7 @@ export interface DockContext {
setDropRect(element: HTMLElement, direction?: DropDirection, source?: any, event?: {clientX: number, clientY: number}, panelSize?: [number, number]): void;

/** @ignore */
getLayoutSize(): {width: number, height: number};
getLayoutSize(): LayoutSize;

/** @ignore
* When a state change happen to the layout that's handled locally, like inside DockPanel or DockBox.
Expand All @@ -346,7 +358,7 @@ export interface DockContext {
source: TabData | PanelData,
target: string | TabData | PanelData | BoxData | null,
direction: DropDirection,
floatPosition?: { left: number, top: number, width: number, height: number }
floatPosition?: FloatPosition
): void;

/**
Expand All @@ -357,7 +369,7 @@ export interface DockContext {
/**
* Find PanelData or TabData by id
*/
find(id: string, filter?: Filter): PanelData | TabData | BoxData;
find(id: string, filter?: Filter): PanelData | TabData | BoxData | undefined;

/**
* Update a tab with new TabData
Expand Down Expand Up @@ -389,7 +401,7 @@ export interface DockContext {
}

/** @ignore */
export const DockContextType = React.createContext<DockContext>(null);
export const DockContextType = React.createContext<DockContext>(null!);
/** @ignore */
export const DockContextProvider = DockContextType.Provider;
/** @ignore */
Expand Down
10 changes: 6 additions & 4 deletions src/DockLayout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ import {
DockContext,
DockContextProvider,
DropDirection,
FloatPosition,
LayoutBase,
LayoutData,
LayoutSize,
PanelBase,
PanelData,
placeHolderGroup,
Expand Down Expand Up @@ -215,7 +217,7 @@ export class DockLayout extends DockPortalManager implements DockContext {
source: TabData | PanelData,
target: string | TabData | PanelData | BoxData | null,
direction: DropDirection,
floatPosition?: {left: number, top: number, width: number, height: number}
floatPosition?: FloatPosition
) {
let layout = this.getLayout();
if (direction === 'maximize') {
Expand Down Expand Up @@ -249,7 +251,7 @@ export class DockLayout extends DockPortalManager implements DockContext {
layout = Algorithm.panelToWindow(layout, newPanel);
} else if (target) {
if ('tabs' in (target as PanelData)) {
// pandel target
// panel target
if (direction === 'middle') {
layout = Algorithm.addTabToPanel(layout, source, target as PanelData);
} else {
Expand All @@ -275,12 +277,12 @@ export class DockLayout extends DockPortalManager implements DockContext {
}

/** @inheritDoc */
find(id: string, filter?: Algorithm.Filter): PanelData | TabData | BoxData {
find(id: string, filter?: Algorithm.Filter): PanelData | TabData | BoxData | undefined {
return Algorithm.find(this.getLayout(), id, filter);
}

/** @ignore */
getLayoutSize() {
getLayoutSize(): LayoutSize {
if (this._ref) {
return {width: this._ref.offsetWidth, height: this._ref.offsetHeight};
}
Expand Down

0 comments on commit 8999608

Please sign in to comment.