feat: add function of undo/redo
This commit is contained in:
@@ -1,3 +1,4 @@
|
|||||||
|
import { useState, useEffect } from 'react';
|
||||||
import {
|
import {
|
||||||
StyledEdgelessToolbar,
|
StyledEdgelessToolbar,
|
||||||
StyledToolbarWrapper,
|
StyledToolbarWrapper,
|
||||||
@@ -70,8 +71,50 @@ const toolbarList2 = [
|
|||||||
disable: false,
|
disable: false,
|
||||||
},
|
},
|
||||||
];
|
];
|
||||||
|
|
||||||
|
const UndoRedo = () => {
|
||||||
|
const [canUndo, setCanUndo] = useState(false);
|
||||||
|
const [canRedo, setCanRedo] = useState(false);
|
||||||
|
const { editor } = useEditor();
|
||||||
|
useEffect(() => {
|
||||||
|
if (!editor) return;
|
||||||
|
const { store } = editor;
|
||||||
|
|
||||||
|
store.signals.historyUpdated.on(() => {
|
||||||
|
setCanUndo(store.canUndo);
|
||||||
|
setCanRedo(store.canRedo);
|
||||||
|
});
|
||||||
|
}, [editor]);
|
||||||
|
|
||||||
|
return (
|
||||||
|
<StyledToolbarWrapper>
|
||||||
|
<Tooltip content="undo" placement="right-start">
|
||||||
|
<StyledToolbarItem
|
||||||
|
disable={!canUndo}
|
||||||
|
onClick={() => {
|
||||||
|
editor?.store?.undo();
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
<UndoIcon />
|
||||||
|
</StyledToolbarItem>
|
||||||
|
</Tooltip>
|
||||||
|
<Tooltip content="redo" placement="right-start">
|
||||||
|
<StyledToolbarItem
|
||||||
|
disable={!canRedo}
|
||||||
|
onClick={() => {
|
||||||
|
editor?.store?.redo();
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
<RedoIcon />
|
||||||
|
</StyledToolbarItem>
|
||||||
|
</Tooltip>
|
||||||
|
</StyledToolbarWrapper>
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
export const EdgelessToolbar = () => {
|
export const EdgelessToolbar = () => {
|
||||||
const { mode, editor } = useEditor();
|
const { mode } = useEditor();
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Slide
|
<Slide
|
||||||
direction="right"
|
direction="right"
|
||||||
@@ -96,29 +139,7 @@ export const EdgelessToolbar = () => {
|
|||||||
);
|
);
|
||||||
})}
|
})}
|
||||||
</StyledToolbarWrapper>
|
</StyledToolbarWrapper>
|
||||||
<StyledToolbarWrapper>
|
<UndoRedo />
|
||||||
{toolbarList2.map(({ icon, toolTip, flavor, disable }, index) => {
|
|
||||||
return (
|
|
||||||
<Tooltip key={index} content={toolTip} placement="right-start">
|
|
||||||
<StyledToolbarItem
|
|
||||||
disable={disable}
|
|
||||||
onClick={() => {
|
|
||||||
switch (flavor) {
|
|
||||||
case 'undo':
|
|
||||||
editor?.store?.undo();
|
|
||||||
break;
|
|
||||||
case 'redo':
|
|
||||||
editor?.store?.redo();
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}}
|
|
||||||
>
|
|
||||||
{icon}
|
|
||||||
</StyledToolbarItem>
|
|
||||||
</Tooltip>
|
|
||||||
);
|
|
||||||
})}
|
|
||||||
</StyledToolbarWrapper>
|
|
||||||
</StyledEdgelessToolbar>
|
</StyledEdgelessToolbar>
|
||||||
</Slide>
|
</Slide>
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -20,8 +20,9 @@ export const StyledToolbarWrapper = styled.div(({ theme }) => ({
|
|||||||
marginBottom: '12px',
|
marginBottom: '12px',
|
||||||
}));
|
}));
|
||||||
|
|
||||||
export const StyledToolbarItem = styled.div<{ disable: boolean }>(
|
export const StyledToolbarItem = styled.div<{
|
||||||
({ theme, disable }) => ({
|
disable?: boolean;
|
||||||
|
}>(({ theme, disable = false }) => ({
|
||||||
width: '36px',
|
width: '36px',
|
||||||
height: '36px',
|
height: '36px',
|
||||||
...displayFlex('center', 'center'),
|
...displayFlex('center', 'center'),
|
||||||
@@ -37,5 +38,4 @@ export const StyledToolbarItem = styled.div<{ disable: boolean }>(
|
|||||||
color: theme.colors.primaryColor,
|
color: theme.colors.primaryColor,
|
||||||
background: theme.colors.hoverBackground,
|
background: theme.colors.hoverBackground,
|
||||||
},
|
},
|
||||||
})
|
}));
|
||||||
);
|
|
||||||
|
|||||||
Reference in New Issue
Block a user