Add Undo/Redo to contextmenu + add separator between categories

This commit is contained in:
Eric NGUYEN 2022-10-04 17:22:28 +02:00
parent cf3c966170
commit 1086bf58a1
2 changed files with 84 additions and 19 deletions

View file

@ -91,23 +91,9 @@ export function Menu(props: IMenuProps): JSX.Element {
if (target !== undefined) {
let count = 0;
for (const className of target.classList) {
count++;
const actions = props.actions.get(className);
if (actions === undefined) {
continue;
}
actions.forEach((action, index) => {
children.push(<MenuItem
key={`contextmenu-item-${count}-${index}`}
className={'contextmenu-item'}
text={action.text}
title={action.title}
shortcut={action.shortcut}
onClick={() => action.action(target)} />);
});
};
count = AddClassSpecificActions(target, count, props, children);
// Add universal actions
AddUniversalActions(props, children, count, target);
}
const visible = isOpen && children.length > 0 ? 'visible opacity-1' : 'invisible opacity-0';
@ -127,3 +113,51 @@ export function Menu(props: IMenuProps): JSX.Element {
</div>
);
}
function AddClassSpecificActions(
target: HTMLElement,
count: number,
props: IMenuProps,
children: JSX.Element[]
): number {
for (const className of target.classList) {
count++;
const actions = props.actions.get(className);
// Only select action where classname matches
if (actions === undefined) {
continue;
}
actions.forEach((action, index) => {
children.push(<MenuItem
key={`contextmenu-item-${count}-${index}`}
className={'contextmenu-item'}
text={action.text}
title={action.title}
shortcut={action.shortcut}
onClick={() => action.action(target)} />);
});
children.push(<hr className='border-slate-400' />);
};
return count;
}
function AddUniversalActions(props: IMenuProps, children: JSX.Element[], count: number, target: HTMLElement): number {
const actions = props.actions.get('');
if (actions !== undefined) {
count++;
actions.forEach((action, index) => {
children.push(<MenuItem
key={`contextmenu-item-${count}-${index}`}
className={'contextmenu-item'}
text={action.text}
title={action.title}
shortcut={action.shortcut}
onClick={() => action.action(target)} />);
});
}
return count;
}