-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
#140 - Added Copy/Paste Button
- Loading branch information
Showing
15 changed files
with
180 additions
and
70 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
export const CopyIcon = () => { | ||
return ( | ||
<svg | ||
xmlns="http://www.w3.org/2000/svg" | ||
width="1.2em" | ||
height="1.2em" | ||
viewBox="0 0 256 256" | ||
> | ||
<path | ||
fill="currentColor" | ||
d="M216 32H88a8 8 0 0 0-8 8v40H40a8 8 0 0 0-8 8v128a8 8 0 0 0 8 8h128a8 8 0 0 0 8-8v-40h40a8 8 0 0 0 8-8V40a8 8 0 0 0-8-8m-56 176H48V96h112Zm48-48h-32V88a8 8 0 0 0-8-8H96V48h112Z" | ||
/> | ||
</svg> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
export const PasteIcon = () => { | ||
return ( | ||
<svg | ||
xmlns="http://www.w3.org/2000/svg" | ||
width="1.2em" | ||
height="1.2em" | ||
viewBox="0 0 256 256" | ||
> | ||
<path | ||
fill="currentColor" | ||
d="M200 32h-36.26a47.92 47.92 0 0 0-71.48 0H56a16 16 0 0 0-16 16v168a16 16 0 0 0 16 16h144a16 16 0 0 0 16-16V48a16 16 0 0 0-16-16m-72 0a32 32 0 0 1 32 32H96a32 32 0 0 1 32-32m72 184H56V48h26.75A47.9 47.9 0 0 0 80 64v8a8 8 0 0 0 8 8h80a8 8 0 0 0 8-8v-8a47.9 47.9 0 0 0-2.75-16H200Z" | ||
/> | ||
</svg> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { useMemo, useRef, useState } from 'react'; | ||
import { ShapeModel } from '@/core/model'; | ||
import { | ||
adjustShapePosition, | ||
cloneShape, | ||
findShapeById, | ||
validateShape, | ||
} from '../../../pods/canvas/clipboard.utils'; | ||
|
||
export const useClipboard = ( | ||
pasteShape: (shape: ShapeModel) => void, | ||
shapes: ShapeModel[], | ||
selectionInfo: { selectedShapeId: string | null } | ||
) => { | ||
const [clipboardShape, setClipboardShape] = useState<ShapeModel | null>(null); | ||
const clipboardShapeRef = useRef<ShapeModel | null>(null); | ||
const copyCount = useRef(1); | ||
|
||
const copyShapeToClipboard = () => { | ||
const selectedShape = findShapeById( | ||
selectionInfo.selectedShapeId ?? '', | ||
shapes | ||
); | ||
if (selectedShape) { | ||
clipboardShapeRef.current = cloneShape(selectedShape); | ||
setClipboardShape(clipboardShapeRef.current); | ||
copyCount.current = 1; | ||
} | ||
}; | ||
|
||
const pasteShapeFromClipboard = () => { | ||
if (clipboardShapeRef.current) { | ||
const newShape: ShapeModel = cloneShape(clipboardShapeRef.current); | ||
validateShape(newShape); | ||
adjustShapePosition(newShape, copyCount.current); | ||
pasteShape(newShape); | ||
copyCount.current++; | ||
} | ||
}; | ||
|
||
const canCopy: boolean = useMemo(() => { | ||
return !!selectionInfo.selectedShapeId; | ||
}, [selectionInfo.selectedShapeId]); | ||
|
||
const canPaste: boolean = useMemo(() => { | ||
return clipboardShapeRef.current !== null; | ||
}, [clipboardShape]); | ||
|
||
return { copyShapeToClipboard, pasteShapeFromClipboard, canCopy, canPaste }; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
49 changes: 49 additions & 0 deletions
49
src/pods/toolbar/components/copy-paste-button/copy-paste-button.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import { CopyIcon, PasteIcon } from '@/common/components/icons'; | ||
import { ToolbarButton } from '../toolbar-button'; | ||
import classes from '@/pods/toolbar/toolbar.pod.module.css'; | ||
import { useCanvasContext } from '@/core/providers'; | ||
import { SHORTCUTS } from '../../shortcut/shortcut.const'; | ||
|
||
export const CopyButton = () => { | ||
const { canCopy, canPaste, copyShapeToClipboard, pasteShapeFromClipboard } = | ||
useCanvasContext(); | ||
|
||
const handleCopyClick = () => { | ||
if (canCopy) { | ||
copyShapeToClipboard(); | ||
} | ||
}; | ||
|
||
const handlePasteClick = () => { | ||
if (canPaste) { | ||
pasteShapeFromClipboard(); | ||
} | ||
}; | ||
|
||
return ( | ||
<div> | ||
<ul className={classes.buttonGroup}> | ||
<li> | ||
<ToolbarButton | ||
icon={<CopyIcon />} | ||
label="Copy" | ||
onClick={handleCopyClick} | ||
className={classes.button} | ||
disabled={!canCopy} | ||
shortcutOptions={SHORTCUTS.copy} | ||
/> | ||
</li> | ||
<li> | ||
<ToolbarButton | ||
icon={<PasteIcon />} | ||
label="Paste" | ||
onClick={handlePasteClick} | ||
className={classes.button} | ||
disabled={!canPaste} // Disable the button if the clipboard is not filled | ||
shortcutOptions={SHORTCUTS.paste} | ||
/> | ||
</li> | ||
</ul> | ||
</div> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './copy-paste-button'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters