feat: add "Listed actions" option in note context menu (#891)

This commit is contained in:
Aman Harwara
2022-02-23 20:51:34 +05:30
committed by GitHub
parent 5265a0d010
commit 209bd99fe5
7 changed files with 392 additions and 402 deletions

View File

@@ -0,0 +1,77 @@
import {
MAX_MENU_SIZE_MULTIPLIER,
MENU_MARGIN_FROM_APP_BORDER,
} from '@/constants';
export type SubmenuStyle = {
top?: number | 'auto';
right?: number | 'auto';
bottom: number | 'auto';
left?: number | 'auto';
visibility?: 'hidden' | 'visible';
maxHeight: number | 'auto';
};
export const calculateSubmenuStyle = (
button: HTMLButtonElement | null,
menu?: HTMLDivElement | null
): SubmenuStyle | undefined => {
const defaultFontSize = window.getComputedStyle(
document.documentElement
).fontSize;
const maxChangeEditorMenuSize =
parseFloat(defaultFontSize) * MAX_MENU_SIZE_MULTIPLIER;
const { clientWidth, clientHeight } = document.documentElement;
const buttonRect = button?.getBoundingClientRect();
const buttonParentRect = button?.parentElement?.getBoundingClientRect();
const menuBoundingRect = menu?.getBoundingClientRect();
const footerElementRect = document
.getElementById('footer-bar')
?.getBoundingClientRect();
const footerHeightInPx = footerElementRect?.height ?? 0;
let position: SubmenuStyle = {
bottom: 'auto',
maxHeight: 'auto',
};
if (buttonRect && buttonParentRect) {
let positionBottom =
clientHeight - buttonRect.bottom - buttonRect.height / 2;
if (positionBottom < footerHeightInPx) {
positionBottom = footerHeightInPx + MENU_MARGIN_FROM_APP_BORDER;
}
position = {
bottom: positionBottom,
visibility: 'hidden',
maxHeight: 'auto',
};
if (buttonRect.right + maxChangeEditorMenuSize > clientWidth) {
position.right = clientWidth - buttonRect.left;
} else {
position.left = buttonRect.right;
}
}
if (menuBoundingRect?.height && buttonRect && position.bottom !== 'auto') {
position.visibility = 'visible';
if (menuBoundingRect.y < MENU_MARGIN_FROM_APP_BORDER) {
position.bottom =
position.bottom + menuBoundingRect.y - MENU_MARGIN_FROM_APP_BORDER * 2;
}
if (footerElementRect && menuBoundingRect.height > footerElementRect.y) {
position.bottom = footerElementRect.height + MENU_MARGIN_FROM_APP_BORDER;
position.maxHeight =
clientHeight -
footerElementRect.height -
MENU_MARGIN_FROM_APP_BORDER * 2;
}
}
return position;
};