Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Feature: add duplicate item #49

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/components/ItemControls/NodeControls/NodeControls.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ type Mode = keyof typeof ModeOptions;

export const NodeControls = ({ id }: Props) => {
const [mode, setMode] = useState<Mode>('SETTINGS');
const { updateModelItem, updateViewItem, deleteViewItem } = useScene();
const { updateModelItem, updateViewItem, deleteViewItem, duplicateViewItem } = useScene();
const uiStateActions = useUiStateStore((state) => {
return state.actions;
});
Expand Down Expand Up @@ -101,6 +101,9 @@ export const NodeControls = ({ id }: Props) => {
uiStateActions.setItemControls(null);
deleteViewItem(viewItem.id);
}}
onDuplicated={() => {
duplicateViewItem(viewItem);
}}
/>
)}
{mode === 'CHANGE_ICON' && (
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import React from 'react';
import { Slider, Box, TextField } from '@mui/material';
import { Slider, TextField, Grid } from '@mui/material';
import { ModelItem, ViewItem } from 'src/types';
import { MarkdownEditor } from 'src/components/MarkdownEditor/MarkdownEditor';
import { useModelItem } from 'src/hooks/useModelItem';
import { DeleteButton } from '../../components/DeleteButton';
import { DuplicateButton } from '../../components/DuplicateButton';
import { Section } from '../../components/Section';

export type NodeUpdates = {
Expand All @@ -16,13 +17,15 @@ interface Props {
onModelItemUpdated: (updates: Partial<ModelItem>) => void;
onViewItemUpdated: (updates: Partial<ViewItem>) => void;
onDeleted: () => void;
onDuplicated: () => void;
}

export const NodeSettings = ({
node,
onModelItemUpdated,
onViewItemUpdated,
onDeleted
onDeleted,
onDuplicated
}: Props) => {
const modelItem = useModelItem(node.id);

Expand Down Expand Up @@ -61,11 +64,14 @@ export const NodeSettings = ({
/>
</Section>
)}
<Section>
<Box>
<Grid container spacing={2} sx={{ pt: 3, px: 3 }}>
<Grid item>
<DeleteButton onClick={onDeleted} />
</Box>
</Section>
</Grid>
<Grid item>
<DuplicateButton onClick={onDuplicated} />
</Grid>
</Grid>
</>
);
};
21 changes: 21 additions & 0 deletions src/components/ItemControls/components/DuplicateButton.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import React from 'react';
import { DeleteOutlined as DeleteIcon } from '@mui/icons-material';
import { ContentCopy as DuplicateIcon } from '@mui/icons-material';
import { Button } from '@mui/material';

interface Props {
onClick: () => void;
}

export const DuplicateButton = ({ onClick }: Props) => {
return (
<Button
size="small"
variant="outlined"
startIcon={<DuplicateIcon color="primary" />}
onClick={onClick}
>
Duplicate
</Button>
);
};
13 changes: 13 additions & 0 deletions src/hooks/useScene.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,18 @@ export const useScene = () => {
[getState, setState, currentViewId]
);

const duplicateViewItem = useCallback(
(newViewItem: ViewItem) => {
const newState = reducers.view({
action: 'CREATE_VIEWITEM',
payload: newViewItem,
ctx: { viewId: currentViewId, state: getState() }
});
setState(newState);
},
[getState, setState, currentViewId]
)

const updateViewItem = useCallback(
(id: string, updates: Partial<ViewItem>) => {
const newState = reducers.view({
Expand Down Expand Up @@ -287,6 +299,7 @@ export const useScene = () => {
updateViewItem,
deleteViewItem,
createConnector,
duplicateViewItem,
updateConnector,
deleteConnector,
createTextBox,
Expand Down