2024-04-25 16:31:18 +02:00
|
|
|
import * as React from 'react';
|
|
|
|
import { useCallback } from 'react';
|
|
|
|
import { ButtonLevel } from '../../Button/Button';
|
|
|
|
import { StyledAddButton, StyledHeader, StyledHeaderIcon, StyledHeaderLabel } from '../styles';
|
2024-04-27 12:44:01 +02:00
|
|
|
import { HeaderId, HeaderListItem } from '../types';
|
2024-04-25 16:31:18 +02:00
|
|
|
import { _ } from '@joplin/lib/locale';
|
2024-04-27 12:44:01 +02:00
|
|
|
import bridge from '../../../services/bridge';
|
|
|
|
import MenuUtils from '@joplin/lib/services/commands/MenuUtils';
|
|
|
|
import CommandService from '@joplin/lib/services/CommandService';
|
|
|
|
|
|
|
|
const Menu = bridge().Menu;
|
|
|
|
const MenuItem = bridge().MenuItem;
|
|
|
|
const menuUtils = new MenuUtils(CommandService.instance());
|
|
|
|
|
2024-04-25 16:31:18 +02:00
|
|
|
|
|
|
|
interface Props {
|
|
|
|
item: HeaderListItem;
|
|
|
|
onDrop: React.DragEventHandler|null;
|
|
|
|
anchorRef: React.Ref<HTMLElement>;
|
|
|
|
}
|
|
|
|
|
|
|
|
const HeaderItem: React.FC<Props> = props => {
|
|
|
|
const item = props.item;
|
|
|
|
const onItemClick = item.onClick;
|
|
|
|
const itemId = item.id;
|
|
|
|
|
|
|
|
const onClick: React.MouseEventHandler<HTMLElement> = useCallback(event => {
|
|
|
|
if (onItemClick) {
|
|
|
|
onItemClick(itemId, event);
|
|
|
|
}
|
|
|
|
}, [onItemClick, itemId]);
|
|
|
|
|
2024-04-27 12:44:01 +02:00
|
|
|
const onContextMenu = useCallback(async () => {
|
|
|
|
if (itemId === HeaderId.FolderHeader) {
|
|
|
|
const menu = new Menu();
|
|
|
|
|
|
|
|
menu.append(
|
|
|
|
new MenuItem(menuUtils.commandToStatefulMenuItem('newFolder')),
|
|
|
|
);
|
|
|
|
|
|
|
|
menu.popup({ window: bridge().window() });
|
|
|
|
}
|
|
|
|
}, [itemId]);
|
|
|
|
|
2024-04-25 16:31:18 +02:00
|
|
|
const addButton = <StyledAddButton
|
|
|
|
iconLabel={_('New')}
|
|
|
|
onClick={item.onPlusButtonClick}
|
|
|
|
iconName='fas fa-plus'
|
|
|
|
level={ButtonLevel.SidebarSecondary}
|
|
|
|
/>;
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div
|
|
|
|
className='sidebar-header-container'
|
|
|
|
{...item.extraProps}
|
|
|
|
onDrop={props.onDrop}
|
|
|
|
>
|
|
|
|
<StyledHeader
|
2024-04-27 12:44:01 +02:00
|
|
|
onContextMenu={onContextMenu}
|
2024-04-25 16:31:18 +02:00
|
|
|
onClick={onClick}
|
|
|
|
tabIndex={0}
|
|
|
|
ref={props.anchorRef}
|
|
|
|
>
|
|
|
|
<StyledHeaderIcon className={item.iconName}/>
|
|
|
|
<StyledHeaderLabel>{item.label}</StyledHeaderLabel>
|
|
|
|
</StyledHeader>
|
|
|
|
{ item.onPlusButtonClick && addButton }
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default HeaderItem;
|