2024-04-25 16:31:18 +02:00
|
|
|
import * as React from 'react';
|
|
|
|
import { useCallback } from 'react';
|
2024-10-15 18:59:51 +02:00
|
|
|
import { StyledHeader, StyledHeaderIcon, StyledHeaderLabel } from '../styles';
|
2024-04-27 12:44:01 +02:00
|
|
|
import { HeaderId, HeaderListItem } from '../types';
|
|
|
|
import bridge from '../../../services/bridge';
|
|
|
|
import MenuUtils from '@joplin/lib/services/commands/MenuUtils';
|
|
|
|
import CommandService from '@joplin/lib/services/CommandService';
|
2024-10-15 18:59:51 +02:00
|
|
|
import ListItemWrapper, { ListItemRef } from './ListItemWrapper';
|
2024-04-27 12:44:01 +02:00
|
|
|
|
|
|
|
const Menu = bridge().Menu;
|
|
|
|
const MenuItem = bridge().MenuItem;
|
|
|
|
const menuUtils = new MenuUtils(CommandService.instance());
|
|
|
|
|
2024-04-25 16:31:18 +02:00
|
|
|
|
|
|
|
interface Props {
|
2024-10-15 18:59:51 +02:00
|
|
|
anchorRef: ListItemRef;
|
2024-04-25 16:31:18 +02:00
|
|
|
item: HeaderListItem;
|
2024-10-15 18:59:51 +02:00
|
|
|
isSelected: boolean;
|
2024-04-25 16:31:18 +02:00
|
|
|
onDrop: React.DragEventHandler|null;
|
2024-10-15 18:59:51 +02:00
|
|
|
index: number;
|
|
|
|
itemCount: number;
|
2024-04-25 16:31:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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')),
|
|
|
|
);
|
|
|
|
|
2024-11-08 17:32:05 +02:00
|
|
|
menu.popup({ window: bridge().activeWindow() });
|
2024-04-27 12:44:01 +02:00
|
|
|
}
|
|
|
|
}, [itemId]);
|
|
|
|
|
2024-04-25 16:31:18 +02:00
|
|
|
return (
|
2024-10-15 18:59:51 +02:00
|
|
|
<ListItemWrapper
|
|
|
|
containerRef={props.anchorRef}
|
|
|
|
selected={props.isSelected}
|
|
|
|
itemIndex={props.index}
|
|
|
|
itemCount={props.itemCount}
|
|
|
|
expanded={props.item.expanded}
|
|
|
|
onContextMenu={onContextMenu}
|
|
|
|
depth={0}
|
|
|
|
highlightOnHover={false}
|
2024-04-25 16:31:18 +02:00
|
|
|
className='sidebar-header-container'
|
|
|
|
{...item.extraProps}
|
|
|
|
onDrop={props.onDrop}
|
|
|
|
>
|
2024-10-15 18:59:51 +02:00
|
|
|
<StyledHeader onClick={onClick}>
|
2024-11-09 14:50:06 +02:00
|
|
|
<StyledHeaderIcon aria-hidden='true' role='img' className={item.iconName}/>
|
2024-04-25 16:31:18 +02:00
|
|
|
<StyledHeaderLabel>{item.label}</StyledHeaderLabel>
|
|
|
|
</StyledHeader>
|
2024-10-15 18:59:51 +02:00
|
|
|
</ListItemWrapper>
|
2024-04-25 16:31:18 +02:00
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default HeaderItem;
|