Skip to content

Commit

Permalink
feat: add drawer calc move size function (#1800)
Browse files Browse the repository at this point in the history
* feat: add drawer calc move size function

* fix: format
  • Loading branch information
ZWkang committed May 22, 2024
1 parent bf95e6a commit 93737c5
Showing 1 changed file with 79 additions and 0 deletions.
79 changes: 79 additions & 0 deletions js/drawer/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
type Placement = 'left' | 'right' | 'top' | 'bottom';

interface SizeDragLimit {
max: number;
min: number;
}

export function getSizeDraggable(
sizeDraggable: boolean | SizeDragLimit,
limit: { max: number; min: number }
) {
if (typeof sizeDraggable === 'boolean') {
return {
allowSizeDraggable: sizeDraggable,
max: limit.max,
min: limit.min,
};
}

return {
allowSizeDraggable: true,
max: sizeDraggable.max,
min: sizeDraggable.min,
};
}

type IOptions = {
x: number;
y: number;
maxWidth: number;
maxHeight: number;
min: number;
max: number;
};

// > min && < max
function calcSizeRange(size: number, min: number, max: number) {
return Math.min(Math.max(size, min), max);
}

export function calcMoveSize(placement: Placement, opts: IOptions) {
const { x, y, max, min, maxWidth, maxHeight } = opts;
let moveSize: number | undefined;
switch (placement) {
case 'right':
// |<--- x --->| |
// | maxWidth |
// | size | > min && < max
moveSize = calcSizeRange(maxWidth - x, min, max);
break;
case 'left':
// |<-- x -->| |
// x > min && < max
moveSize = calcSizeRange(x, min, max);
break;
case 'top':
// - - - - - - - -
// | y |
// | |
// - - - - - - - -
// > min && < max
// moveSize = Math.min(Math.max(y, min), max);
moveSize = calcSizeRange(y, min, max);
break;
case 'bottom':
// - - - - - - - -
// | y |
// | | maxHeight
// - - - - - - - -
// | size |
// > min && < max
moveSize = calcSizeRange(maxHeight - y, min, max);
break;
default:
// 参数缺失直接返回
return moveSize;
}
return moveSize;
}

0 comments on commit 93737c5

Please sign in to comment.