2022-10-24 15:14:10 +08:00
|
|
|
import { useState, useEffect } from 'react';
|
2022-10-20 18:55:03 +08:00
|
|
|
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';
|
2022-10-20 18:55:03 +08:00
|
|
|
import Slide from '@mui/material/Slide';
|
2022-12-30 21:40:15 +08:00
|
|
|
import useCurrentPageMeta from '@/hooks/use-current-page-meta';
|
|
|
|
import { useAppState } from '@/providers/app-state-provider';
|
|
|
|
import useHistoryUpdated from '@/hooks/use-history-update';
|
2022-10-20 18:55:03 +08:00
|
|
|
|
|
|
|
const toolbarList1 = [
|
|
|
|
{
|
2022-10-24 11:42:11 +08:00
|
|
|
flavor: 'select',
|
2022-10-20 18:55:03 +08:00
|
|
|
icon: <SelectIcon />,
|
|
|
|
toolTip: 'Select',
|
|
|
|
disable: false,
|
2022-12-30 21:40:15 +08:00
|
|
|
callback: () => {
|
|
|
|
window.dispatchEvent(
|
|
|
|
new CustomEvent('affine.switch-mouse-mode', {
|
|
|
|
detail: {
|
|
|
|
type: 'default',
|
|
|
|
},
|
|
|
|
})
|
|
|
|
);
|
|
|
|
},
|
2022-10-20 18:55:03 +08:00
|
|
|
},
|
|
|
|
{
|
2022-10-24 11:42:11 +08:00
|
|
|
flavor: 'text',
|
2022-10-20 18:55:03 +08:00
|
|
|
icon: <TextIcon />,
|
2022-10-25 16:26:31 +08:00
|
|
|
toolTip: 'Text (coming soon)',
|
2022-10-20 18:55:03 +08:00
|
|
|
disable: true,
|
|
|
|
},
|
|
|
|
{
|
2022-10-24 11:42:11 +08:00
|
|
|
flavor: 'shape',
|
2022-10-20 18:55:03 +08:00
|
|
|
icon: <ShapeIcon />,
|
2022-12-30 21:40:15 +08:00
|
|
|
toolTip: 'Shape',
|
|
|
|
disable: false,
|
|
|
|
callback: () => {
|
|
|
|
window.dispatchEvent(
|
|
|
|
new CustomEvent('affine.switch-mouse-mode', {
|
|
|
|
detail: {
|
|
|
|
type: 'shape',
|
|
|
|
color: 'black',
|
|
|
|
shape: 'rectangle',
|
|
|
|
},
|
|
|
|
})
|
|
|
|
);
|
|
|
|
},
|
2022-10-20 18:55:03 +08:00
|
|
|
},
|
|
|
|
{
|
2022-10-24 11:42:11 +08:00
|
|
|
flavor: 'sticky',
|
2022-10-20 18:55:03 +08:00
|
|
|
icon: <StickerIcon />,
|
2022-10-25 16:26:31 +08:00
|
|
|
toolTip: 'Sticky (coming soon)',
|
2022-10-20 18:55:03 +08:00
|
|
|
disable: true,
|
|
|
|
},
|
|
|
|
{
|
2022-10-24 11:42:11 +08:00
|
|
|
flavor: 'pen',
|
2022-10-20 18:55:03 +08:00
|
|
|
icon: <PenIcon />,
|
2022-10-25 16:26:31 +08:00
|
|
|
toolTip: 'Pen (coming soon)',
|
2022-10-20 18:55:03 +08:00
|
|
|
disable: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
2022-10-24 11:42:11 +08:00
|
|
|
flavor: 'connector',
|
2022-10-20 18:55:03 +08:00
|
|
|
icon: <ConnectorIcon />,
|
2022-10-25 16:26:31 +08:00
|
|
|
toolTip: 'Connector (coming soon)',
|
2022-10-20 18:55:03 +08:00
|
|
|
disable: true,
|
|
|
|
},
|
|
|
|
];
|
2022-10-24 15:14:10 +08:00
|
|
|
|
|
|
|
const UndoRedo = () => {
|
|
|
|
const [canUndo, setCanUndo] = useState(false);
|
|
|
|
const [canRedo, setCanRedo] = useState(false);
|
2022-12-30 21:40:15 +08:00
|
|
|
const { currentPage } = useAppState();
|
|
|
|
const onHistoryUpdated = useHistoryUpdated();
|
2022-10-24 15:14:10 +08:00
|
|
|
|
2022-12-30 21:40:15 +08:00
|
|
|
useEffect(() => {
|
|
|
|
onHistoryUpdated(page => {
|
2022-12-01 20:45:18 +00:00
|
|
|
setCanUndo(page.canUndo);
|
|
|
|
setCanRedo(page.canRedo);
|
2022-10-24 15:14:10 +08:00
|
|
|
});
|
2022-12-30 21:40:15 +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={() => {
|
2022-12-30 21:40:15 +08:00
|
|
|
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={() => {
|
2022-12-30 21:40:15 +08:00
|
|
|
currentPage?.redo();
|
2022-10-24 15:14:10 +08:00
|
|
|
}}
|
|
|
|
>
|
|
|
|
<RedoIcon />
|
|
|
|
</StyledToolbarItem>
|
|
|
|
</Tooltip>
|
|
|
|
</StyledToolbarWrapper>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2022-10-20 18:55:03 +08:00
|
|
|
export const EdgelessToolbar = () => {
|
2022-12-30 21:40:15 +08:00
|
|
|
const { mode } = useCurrentPageMeta() || {};
|
2022-10-24 15:14:10 +08:00
|
|
|
|
2022-10-20 18:55:03 +08:00
|
|
|
return (
|
|
|
|
<Slide
|
|
|
|
direction="right"
|
|
|
|
in={mode === 'edgeless'}
|
|
|
|
mountOnEnter
|
|
|
|
unmountOnExit
|
|
|
|
>
|
2022-12-30 21:40:15 +08:00
|
|
|
<StyledEdgelessToolbar aria-label="edgeless-toolbar">
|
2022-10-20 18:55:03 +08:00
|
|
|
<StyledToolbarWrapper>
|
2022-12-30 21:40:15 +08:00
|
|
|
{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>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
)}
|
2022-10-20 18:55:03 +08:00
|
|
|
</StyledToolbarWrapper>
|
2022-10-24 15:14:10 +08:00
|
|
|
<UndoRedo />
|
2022-10-20 18:55:03 +08:00
|
|
|
</StyledEdgelessToolbar>
|
|
|
|
</Slide>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default EdgelessToolbar;
|