159 lines
3.6 KiB
TypeScript
Raw Normal View History

2022-10-24 15:14:10 +08:00
import { useState, useEffect } from 'react';
import {
StyledEdgelessToolbar,
StyledToolbarWrapper,
StyledToolbarItem,
} from './style';
import {
SelectIcon,
TextIcon,
ShapeIcon,
PenIcon,
StickerIcon,
ConnectorIcon,
UndoIcon,
RedoIcon,
} from './icons';
2022-10-25 11:51:59 +08:00
import { Tooltip } from '@/ui/tooltip';
import Slide from '@mui/material/Slide';
import useCurrentPageMeta from '@/hooks/use-current-page-meta';
import { useAppState } from '@/providers/app-state-provider';
import useHistoryUpdated from '@/hooks/use-history-update';
const toolbarList1 = [
{
2022-10-24 11:42:11 +08:00
flavor: 'select',
icon: <SelectIcon />,
toolTip: 'Select',
disable: false,
callback: () => {
window.dispatchEvent(
new CustomEvent('affine.switch-mouse-mode', {
detail: {
type: 'default',
},
})
);
},
},
{
2022-10-24 11:42:11 +08:00
flavor: 'text',
icon: <TextIcon />,
2022-10-25 16:26:31 +08:00
toolTip: 'Text (coming soon)',
disable: true,
},
{
2022-10-24 11:42:11 +08:00
flavor: 'shape',
icon: <ShapeIcon />,
toolTip: 'Shape',
disable: false,
callback: () => {
window.dispatchEvent(
new CustomEvent('affine.switch-mouse-mode', {
detail: {
type: 'shape',
color: 'black',
shape: 'rectangle',
},
})
);
},
},
{
2022-10-24 11:42:11 +08:00
flavor: 'sticky',
icon: <StickerIcon />,
2022-10-25 16:26:31 +08:00
toolTip: 'Sticky (coming soon)',
disable: true,
},
{
2022-10-24 11:42:11 +08:00
flavor: 'pen',
icon: <PenIcon />,
2022-10-25 16:26:31 +08:00
toolTip: 'Pen (coming soon)',
disable: true,
},
{
2022-10-24 11:42:11 +08:00
flavor: 'connector',
icon: <ConnectorIcon />,
2022-10-25 16:26:31 +08:00
toolTip: 'Connector (coming soon)',
disable: true,
},
];
2022-10-24 15:14:10 +08:00
const UndoRedo = () => {
const [canUndo, setCanUndo] = useState(false);
const [canRedo, setCanRedo] = useState(false);
const { currentPage } = useAppState();
const onHistoryUpdated = useHistoryUpdated();
2022-10-24 15:14:10 +08:00
useEffect(() => {
onHistoryUpdated(page => {
setCanUndo(page.canUndo);
setCanRedo(page.canRedo);
2022-10-24 15:14:10 +08:00
});
}, [onHistoryUpdated]);
2022-10-24 15:14:10 +08:00
return (
<StyledToolbarWrapper>
2022-10-25 16:26:31 +08:00
<Tooltip content="Undo" placement="right-start">
2022-10-24 15:14:10 +08:00
<StyledToolbarItem
disable={!canUndo}
onClick={() => {
currentPage?.undo();
2022-10-24 15:14:10 +08:00
}}
>
<UndoIcon />
</StyledToolbarItem>
</Tooltip>
2022-10-25 16:26:31 +08:00
<Tooltip content="Redo" placement="right-start">
2022-10-24 15:14:10 +08:00
<StyledToolbarItem
disable={!canRedo}
onClick={() => {
currentPage?.redo();
2022-10-24 15:14:10 +08:00
}}
>
<RedoIcon />
</StyledToolbarItem>
</Tooltip>
</StyledToolbarWrapper>
);
};
export const EdgelessToolbar = () => {
const { mode } = useCurrentPageMeta() || {};
2022-10-24 15:14:10 +08:00
return (
<Slide
direction="right"
in={mode === 'edgeless'}
mountOnEnter
unmountOnExit
>
<StyledEdgelessToolbar aria-label="edgeless-toolbar">
<StyledToolbarWrapper>
{toolbarList1.map(
({ icon, toolTip, flavor, disable, callback }, index) => {
return (
<Tooltip key={index} content={toolTip} placement="right-start">
<StyledToolbarItem
disable={disable}
onClick={() => {
console.log('click toolbar button:', flavor);
callback?.();
}}
>
{icon}
</StyledToolbarItem>
</Tooltip>
);
}
)}
</StyledToolbarWrapper>
2022-10-24 15:14:10 +08:00
<UndoRedo />
</StyledEdgelessToolbar>
</Slide>
);
};
export default EdgelessToolbar;