tldraw/components/tools-panel/tools-panel.tsx

207 lines
5.7 KiB
TypeScript
Raw Normal View History

import {
2021-05-31 19:13:43 +00:00
ArrowTopRightIcon,
CircleIcon,
CursorArrowIcon,
DividerHorizontalIcon,
DotIcon,
LockClosedIcon,
LockOpen1Icon,
Pencil1Icon,
Pencil2Icon,
SewingPinIcon,
SquareIcon,
} from '@radix-ui/react-icons'
import { IconButton } from 'components/shared'
import React from 'react'
import state, { useSelector } from 'state'
import styled from 'styles'
import { ShapeType } from 'types'
2021-05-29 14:06:23 +00:00
import UndoRedo from './undo-redo'
2021-05-29 13:59:11 +00:00
import Zoom from './zoom'
2021-05-31 19:13:43 +00:00
const selectArrowTool = () => state.send('SELECTED_ARROW_TOOL')
const selectCircleTool = () => state.send('SELECTED_CIRCLE_TOOL')
2021-05-31 19:13:43 +00:00
const selectDotTool = () => state.send('SELECTED_DOT_TOOL')
const selectDrawTool = () => state.send('SELECTED_DRAW_TOOL')
const selectEllipseTool = () => state.send('SELECTED_ELLIPSE_TOOL')
const selectLineTool = () => state.send('SELECTED_LINE_TOOL')
const selectPolylineTool = () => state.send('SELECTED_POLYLINE_TOOL')
2021-05-31 19:13:43 +00:00
const selectRayTool = () => state.send('SELECTED_RAY_TOOL')
const selectRectangleTool = () => state.send('SELECTED_RECTANGLE_TOOL')
2021-05-31 19:13:43 +00:00
const selectSelectTool = () => state.send('SELECTED_SELECT_TOOL')
const selectToolLock = () => state.send('TOGGLED_TOOL_LOCK')
export default function ToolsPanel() {
const activeTool = useSelector((state) =>
state.whenIn({
2021-05-31 19:13:43 +00:00
arrow: ShapeType.Arrow,
circle: ShapeType.Circle,
2021-05-31 19:13:43 +00:00
dot: ShapeType.Dot,
draw: ShapeType.Draw,
ellipse: ShapeType.Ellipse,
line: ShapeType.Line,
polyline: ShapeType.Polyline,
2021-05-31 19:13:43 +00:00
ray: ShapeType.Ray,
rectangle: ShapeType.Rectangle,
2021-05-31 19:13:43 +00:00
selecting: 'select',
})
)
const isToolLocked = useSelector((s) => s.data.settings.isToolLocked)
const isPenLocked = useSelector((s) => s.data.settings.isPenLocked)
return (
<OuterContainer>
2021-05-29 13:59:11 +00:00
<Zoom />
2021-06-01 08:56:41 +00:00
<Flex size={{ '@sm': 'small' }}>
<Container>
<IconButton
name="select"
size={{ '@sm': 'small', '@md': 'large' }}
onClick={selectSelectTool}
isActive={activeTool === 'select'}
>
<CursorArrowIcon />
</IconButton>
</Container>
<Container>
<IconButton
name={ShapeType.Draw}
size={{ '@sm': 'small', '@md': 'large' }}
onClick={selectDrawTool}
isActive={activeTool === ShapeType.Draw}
>
<Pencil1Icon />
</IconButton>
<IconButton
name={ShapeType.Rectangle}
size={{ '@sm': 'small', '@md': 'large' }}
onClick={selectRectangleTool}
isActive={activeTool === ShapeType.Rectangle}
>
<SquareIcon />
</IconButton>
<IconButton
name={ShapeType.Circle}
size={{ '@sm': 'small', '@md': 'large' }}
2021-05-31 19:13:43 +00:00
onClick={selectEllipseTool}
isActive={activeTool === ShapeType.Ellipse}
>
<CircleIcon />
</IconButton>
<IconButton
2021-05-31 19:13:43 +00:00
name={ShapeType.Arrow}
size={{ '@sm': 'small', '@md': 'large' }}
2021-05-31 19:13:43 +00:00
onClick={selectArrowTool}
isActive={activeTool === ShapeType.Arrow}
>
2021-05-31 19:13:43 +00:00
<ArrowTopRightIcon />
</IconButton>
2021-05-31 19:13:43 +00:00
{/* <IconButton
name={ShapeType.Circle}
size={{ '@sm': 'small', '@md': 'large' }}
onClick={selectCircleTool}
isActive={activeTool === ShapeType.Circle}
>
<CircleIcon />
</IconButton> */}
{/* <IconButton
name={ShapeType.Line}
size={{ '@sm': 'small', '@md': 'large' }}
onClick={selectLineTool}
isActive={activeTool === ShapeType.Line}
>
<DividerHorizontalIcon transform="rotate(-45)" />
</IconButton>
<IconButton
name={ShapeType.Ray}
size={{ '@sm': 'small', '@md': 'large' }}
onClick={selectRayTool}
isActive={activeTool === ShapeType.Ray}
>
<SewingPinIcon transform="rotate(-135)" />
</IconButton>
<IconButton
name={ShapeType.Dot}
size={{ '@sm': 'small', '@md': 'large' }}
onClick={selectDotTool}
isActive={activeTool === ShapeType.Dot}
>
<DotIcon />
</IconButton> */}
</Container>
<Container>
<IconButton
size={{ '@sm': 'small', '@md': 'large' }}
onClick={selectToolLock}
>
{isToolLocked ? <LockClosedIcon /> : <LockOpen1Icon />}
</IconButton>
{isPenLocked && (
<IconButton
size={{ '@sm': 'small', '@md': 'large' }}
onClick={selectToolLock}
>
<Pencil2Icon />
</IconButton>
)}
</Container>
</Flex>
2021-05-29 14:06:23 +00:00
<UndoRedo />
</OuterContainer>
)
}
const OuterContainer = styled('div', {
position: 'fixed',
bottom: 40,
left: 0,
right: 0,
padding: '0 8px 12px 8px',
width: '100%',
display: 'flex',
alignItems: 'center',
justifyContent: 'center',
flexWrap: 'wrap',
gap: 16,
zIndex: 200,
})
const Flex = styled('div', {
display: 'flex',
2021-06-01 08:56:41 +00:00
justifyContent: 'space-between',
width: '100%',
padding: '0 4px',
variants: {
size: {
small: {
width: 'auto',
padding: '0',
'& > *:nth-child(n+2)': {
marginLeft: 16,
},
},
},
},
})
const Container = styled('div', {
position: 'relative',
backgroundColor: '$panel',
borderRadius: '4px',
overflow: 'hidden',
2021-05-30 13:49:33 +00:00
border: '1px solid $panel',
pointerEvents: 'all',
userSelect: 'none',
height: '100%',
display: 'flex',
padding: 4,
2021-05-30 13:49:33 +00:00
boxShadow: '0px 2px 4px rgba(0,0,0,.12)',
'& svg': {
strokeWidth: 0,
},
})