feat: ContextMenu-更高效\好用地计算右键菜单的位置

This commit is contained in:
jinye 2024-03-26 15:19:51 +08:00 committed by lmaomaoz
parent ea7a306143
commit a2d75f6098
2 changed files with 34 additions and 1 deletions

View File

@ -115,6 +115,39 @@ export function calculatePosition(
arrowOffsetTop: any = '', arrowOffsetTop: any = '',
activePlacement: string = placement; activePlacement: string = placement;
// 如果是作为菜单浮层,不再基于四个方向对齐,而是模拟原生右键菜单定位:
// 1.计算放右边,还是左边
// 2.计算能否放在下面,如果不能则贴底
// 3.由于是基于一个点此时不考虑 arrow
if (placement === 'asContextMenu') {
if (childOffset.left + 1 + overlayWidth <= clip.width) {
// 放右边
positionLeft = childOffset.left + 1;
} else if (childOffset.left - overlayWidth >= 0) {
// 放左边
positionLeft = childOffset.left - overlayWidth;
} else {
// 兜底贴右边
positionLeft = Math.max(clip.width - overlayWidth, 10);
}
if (childOffset.top + 1 + overlayHeight <= clip.height) {
// 放下面
positionTop = childOffset.top + 1;
} else {
// 贴底边预留5px避免完全贴底
positionTop = Math.max(clip.height - overlayHeight - 5, 10);
}
const [offSetX = 0, offSetY = 0] = customOffset;
return {
positionLeft: (positionLeft + offSetX) / scaleX,
positionTop: (positionTop + offSetY) / scaleY,
arrowOffsetLeft,
arrowOffsetTop,
activePlacement
};
}
if (~placement.indexOf('-')) { if (~placement.indexOf('-')) {
const tests = placement.split(/\s+/); const tests = placement.split(/\s+/);

View File

@ -265,7 +265,7 @@ export class ContextMenu extends React.Component<
autoCalculatePosition(menu: HTMLElement) { autoCalculatePosition(menu: HTMLElement) {
// 智能定位,选择一个合适的对齐方式。 // 智能定位,选择一个合适的对齐方式。
const info = calculatePosition( const info = calculatePosition(
'auto', 'asContextMenu',
menu.lastChild, menu.lastChild,
menu.children[1] as HTMLElement, menu.children[1] as HTMLElement,
menu.children[0] menu.children[0]