Skip to content

Commit 225ff3d

Browse files
committed
feat(masonry): add bounding box calculation logic in path.ts
1 parent ee78ee0 commit 225ff3d

File tree

1 file changed

+71
-0
lines changed
  • modules/masonry/src/brick/utils

1 file changed

+71
-0
lines changed

modules/masonry/src/brick/utils/path.ts

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -393,7 +393,78 @@ export function generatePath(config: TInputType1 | TInputType2 | TInputType3): {
393393
});
394394

395395
const segments = [...top, ...right, ...bottom, ...left];
396+
396397
return {
397398
path: ['M 0,0', ...segments].join(' '),
398399
};
399400
}
401+
402+
403+
// function to calculate the bounding box values
404+
export function getBoundingBox(config: TInputUnion): TBBox {
405+
const {
406+
strokeWidth,
407+
bBoxLabel,
408+
bBoxArgs,
409+
type,
410+
} = config;
411+
412+
const hasArgs = bBoxArgs.length > 0;
413+
const labelWidth = Math.max(MIN_LABEL_WIDTH, bBoxLabel.w);
414+
415+
// Match variableTopWidth logic from _generateTop
416+
const variableTopWidth =
417+
strokeWidth / 2 +
418+
labelWidth +
419+
strokeWidth / 2 -
420+
CORNER_RADIUS -
421+
WIDTH_NOTCH_TOP -
422+
OFFSET_NOTCH_TOP -
423+
CORNER_RADIUS;
424+
425+
// Base width as per _generateTop and _generateBottom logic
426+
const baseWidth =
427+
CORNER_RADIUS + OFFSET_NOTCH_TOP + WIDTH_NOTCH_TOP + variableTopWidth;
428+
429+
const width = hasArgs
430+
? baseWidth + OFFSET_NOTCH_RIGHT + CORNER_RADIUS + strokeWidth/2
431+
: baseWidth + CORNER_RADIUS +strokeWidth/2;
432+
433+
// Get rightVertical from _generateRight
434+
const { vertical: rightVertical } = _generateRight({
435+
hasArgs,
436+
strokeWidth,
437+
bBoxLabel,
438+
bBoxArgs,
439+
});
440+
441+
let height = rightVertical + CORNER_RADIUS + (type !== 'type3' ? strokeWidth/2 : 0);
442+
443+
if (type === 'type3') {
444+
const {
445+
bBoxNesting,
446+
secondaryLabel,
447+
} = config as TInputType3;
448+
449+
// Reuse nested path logic
450+
let nestingHeight = bBoxNesting.reduce((sum, box) => sum + box.h, 0);
451+
nestingHeight = Math.max(nestingHeight, MIN_NESTED_HEIGHT);
452+
453+
const labelHeight = Math.max(MIN_LABEL_HEIGHT, bBoxLabel.h);
454+
const labelAreaHeight = secondaryLabel
455+
? strokeWidth / 2 + labelHeight + strokeWidth / 2 - CORNER_RADIUS * 2
456+
: 4;
457+
458+
const nestedTotal = OUTER_CORNER_RADIUS +
459+
(nestingHeight - (strokeWidth / 2 + OUTER_CORNER_RADIUS * 2 + strokeWidth / 2)) +
460+
OUTER_CORNER_RADIUS + CORNER_RADIUS +
461+
labelAreaHeight + CORNER_RADIUS;
462+
463+
height += nestedTotal;
464+
}
465+
466+
return {
467+
w: width,
468+
h: height,
469+
};
470+
}

0 commit comments

Comments
 (0)