forked from apache/superset
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: improve margin merging (apache#159)
- Loading branch information
1 parent
58f606b
commit aedc1b4
Showing
2 changed files
with
37 additions
and
8 deletions.
There are no files selected for viewing
21 changes: 13 additions & 8 deletions
21
...ntend/temporary_superset_ui/superset-ui/packages/superset-ui-dimension/src/mergeMargin.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,24 @@ | ||
import { Margin } from './types'; | ||
|
||
function mergeOneSide(a: number = 0, b: number = 0, operation: (a: number, b: number) => number) { | ||
if (Number.isNaN(a) || a === null) return b; | ||
else if (Number.isNaN(b) || b === null) return a; | ||
|
||
return operation(a, b); | ||
} | ||
|
||
export default function mergeMargin( | ||
margin1: Partial<Margin> = {}, | ||
margin2: Partial<Margin> = {}, | ||
mode: 'expand' | 'shrink' = 'expand', | ||
) { | ||
const { top = 0, left = 0, bottom = 0, right = 0 } = margin1; | ||
const { top: top2 = 0, left: left2 = 0, bottom: bottom2 = 0, right: right2 = 0 } = margin2; | ||
|
||
const func = mode === 'expand' ? Math.max : Math.min; | ||
const { top, left, bottom, right } = margin1; | ||
const operation = mode === 'expand' ? Math.max : Math.min; | ||
|
||
return { | ||
bottom: func(bottom, bottom2), | ||
left: func(left, left2), | ||
right: func(right, right2), | ||
top: func(top, top2), | ||
bottom: mergeOneSide(bottom, margin2.bottom, operation), | ||
left: mergeOneSide(left, margin2.left, operation), | ||
right: mergeOneSide(right, margin2.right, operation), | ||
top: mergeOneSide(top, margin2.top, operation), | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters