2021-05-28 20:30:27 +00:00
|
|
|
import styled from 'styles'
|
|
|
|
import state, { useSelector } from 'state'
|
|
|
|
import * as Panel from 'components/panel'
|
|
|
|
import { useRef } from 'react'
|
|
|
|
import { IconButton } from 'components/shared'
|
|
|
|
import { Circle, Copy, Lock, Trash, Unlock, X } from 'react-feather'
|
2021-05-29 10:12:28 +00:00
|
|
|
import {
|
|
|
|
deepCompare,
|
|
|
|
deepCompareArrays,
|
|
|
|
getPage,
|
|
|
|
getSelectedShapes,
|
|
|
|
} from 'utils/utils'
|
2021-05-28 20:30:27 +00:00
|
|
|
import { shades, fills, strokes } from 'lib/colors'
|
|
|
|
|
|
|
|
import ColorPicker from './color-picker'
|
|
|
|
import AlignDistribute from './align-distribute'
|
2021-05-29 22:27:19 +00:00
|
|
|
import { MoveType, ShapeStyles } from 'types'
|
2021-05-28 20:30:27 +00:00
|
|
|
import WidthPicker from './width-picker'
|
2021-05-29 10:12:28 +00:00
|
|
|
import {
|
2021-05-29 22:27:19 +00:00
|
|
|
AlignTopIcon,
|
|
|
|
ArrowDownIcon,
|
|
|
|
ArrowUpIcon,
|
2021-05-29 10:12:28 +00:00
|
|
|
AspectRatioIcon,
|
|
|
|
BoxIcon,
|
|
|
|
CopyIcon,
|
|
|
|
EyeClosedIcon,
|
|
|
|
EyeOpenIcon,
|
|
|
|
LockClosedIcon,
|
|
|
|
LockOpen1Icon,
|
2021-05-29 22:27:19 +00:00
|
|
|
PinBottomIcon,
|
|
|
|
PinTopIcon,
|
2021-05-29 10:12:28 +00:00
|
|
|
RotateCounterClockwiseIcon,
|
|
|
|
TrashIcon,
|
|
|
|
} from '@radix-ui/react-icons'
|
2021-05-26 21:47:46 +00:00
|
|
|
|
|
|
|
const fillColors = { ...shades, ...fills }
|
|
|
|
const strokeColors = { ...shades, ...strokes }
|
2021-05-26 10:34:10 +00:00
|
|
|
|
|
|
|
export default function StylePanel() {
|
|
|
|
const rContainer = useRef<HTMLDivElement>(null)
|
|
|
|
const isOpen = useSelector((s) => s.data.settings.isStyleOpen)
|
|
|
|
|
|
|
|
return (
|
|
|
|
<StylePanelRoot ref={rContainer} isOpen={isOpen}>
|
|
|
|
{isOpen ? (
|
|
|
|
<SelectedShapeStyles />
|
|
|
|
) : (
|
2021-05-28 20:30:27 +00:00
|
|
|
<IconButton onClick={() => state.send('TOGGLED_STYLE_PANEL_OPEN')}>
|
2021-05-26 10:34:10 +00:00
|
|
|
<Circle />
|
|
|
|
</IconButton>
|
|
|
|
)}
|
|
|
|
</StylePanelRoot>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
// This panel is going to be hard to keep cool, as we're selecting computed
|
|
|
|
// information, based on the user's current selection. We might have to keep
|
|
|
|
// track of this data manually within our state.
|
|
|
|
|
|
|
|
function SelectedShapeStyles({}: {}) {
|
|
|
|
const selectedIds = useSelector(
|
|
|
|
(s) => Array.from(s.data.selectedIds.values()),
|
|
|
|
deepCompareArrays
|
|
|
|
)
|
|
|
|
|
2021-05-29 10:12:28 +00:00
|
|
|
const isAllLocked = useSelector((s) => {
|
|
|
|
const page = getPage(s.data)
|
|
|
|
return selectedIds.every((id) => page.shapes[id].isLocked)
|
|
|
|
})
|
|
|
|
|
|
|
|
const isAllAspectLocked = useSelector((s) => {
|
|
|
|
const page = getPage(s.data)
|
|
|
|
return selectedIds.every((id) => page.shapes[id].isAspectRatioLocked)
|
|
|
|
})
|
|
|
|
|
|
|
|
const isAllHidden = useSelector((s) => {
|
|
|
|
const page = getPage(s.data)
|
|
|
|
return selectedIds.every((id) => page.shapes[id].isHidden)
|
|
|
|
})
|
|
|
|
|
|
|
|
const commonStyle = useSelector((s) => {
|
2021-05-26 10:34:10 +00:00
|
|
|
const { currentStyle } = s.data
|
|
|
|
|
2021-05-29 10:12:28 +00:00
|
|
|
if (selectedIds.length === 0) {
|
2021-05-26 10:34:10 +00:00
|
|
|
return currentStyle
|
|
|
|
}
|
2021-05-29 10:12:28 +00:00
|
|
|
const page = getPage(s.data)
|
|
|
|
const shapeStyles = selectedIds.map((id) => page.shapes[id].style)
|
2021-05-26 10:34:10 +00:00
|
|
|
|
2021-05-29 10:12:28 +00:00
|
|
|
const commonStyle: Partial<ShapeStyles> = {}
|
2021-05-26 10:34:10 +00:00
|
|
|
const overrides = new Set<string>([])
|
|
|
|
|
2021-05-29 10:12:28 +00:00
|
|
|
for (const shapeStyle of shapeStyles) {
|
2021-05-26 10:34:10 +00:00
|
|
|
for (let key in currentStyle) {
|
|
|
|
if (overrides.has(key)) continue
|
2021-05-29 10:12:28 +00:00
|
|
|
if (commonStyle[key] === undefined) {
|
|
|
|
commonStyle[key] = shapeStyle[key]
|
2021-05-26 10:34:10 +00:00
|
|
|
} else {
|
2021-05-29 10:12:28 +00:00
|
|
|
if (commonStyle[key] === shapeStyle[key]) continue
|
|
|
|
commonStyle[key] = currentStyle[key]
|
2021-05-26 10:34:10 +00:00
|
|
|
overrides.add(key)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-29 10:12:28 +00:00
|
|
|
return commonStyle
|
2021-05-26 10:34:10 +00:00
|
|
|
}, deepCompare)
|
|
|
|
|
2021-05-26 21:47:46 +00:00
|
|
|
const hasSelection = selectedIds.length > 0
|
|
|
|
|
2021-05-26 10:34:10 +00:00
|
|
|
return (
|
|
|
|
<Panel.Layout>
|
2021-05-28 20:30:27 +00:00
|
|
|
<Panel.Header side="right">
|
2021-05-26 10:34:10 +00:00
|
|
|
<h3>Style</h3>
|
2021-05-28 20:30:27 +00:00
|
|
|
<IconButton onClick={() => state.send('TOGGLED_STYLE_PANEL_OPEN')}>
|
2021-05-27 17:59:40 +00:00
|
|
|
<X />
|
|
|
|
</IconButton>
|
2021-05-26 10:34:10 +00:00
|
|
|
</Panel.Header>
|
2021-05-26 19:20:52 +00:00
|
|
|
<Content>
|
|
|
|
<ColorPicker
|
|
|
|
label="Fill"
|
2021-05-29 10:12:28 +00:00
|
|
|
color={commonStyle.fill}
|
2021-05-26 21:47:46 +00:00
|
|
|
colors={fillColors}
|
2021-05-28 20:30:27 +00:00
|
|
|
onChange={(color) => state.send('CHANGED_STYLE', { fill: color })}
|
2021-05-26 19:20:52 +00:00
|
|
|
/>
|
|
|
|
<ColorPicker
|
|
|
|
label="Stroke"
|
2021-05-29 10:12:28 +00:00
|
|
|
color={commonStyle.stroke}
|
2021-05-26 21:47:46 +00:00
|
|
|
colors={strokeColors}
|
2021-05-28 20:30:27 +00:00
|
|
|
onChange={(color) => state.send('CHANGED_STYLE', { stroke: color })}
|
2021-05-26 19:20:52 +00:00
|
|
|
/>
|
2021-05-28 20:30:27 +00:00
|
|
|
<Row>
|
|
|
|
<label htmlFor="width">Width</label>
|
2021-05-29 10:12:28 +00:00
|
|
|
<WidthPicker strokeWidth={Number(commonStyle.strokeWidth)} />
|
2021-05-28 20:30:27 +00:00
|
|
|
</Row>
|
|
|
|
<ButtonsRow>
|
2021-05-29 10:12:28 +00:00
|
|
|
<IconButton
|
|
|
|
disabled={!hasSelection}
|
|
|
|
onClick={() => state.send('DUPLICATED')}
|
|
|
|
>
|
|
|
|
<CopyIcon />
|
|
|
|
</IconButton>
|
|
|
|
<IconButton
|
|
|
|
disabled={!hasSelection}
|
|
|
|
onClick={() => state.send('ROTATED_CCW')}
|
|
|
|
>
|
|
|
|
<RotateCounterClockwiseIcon />
|
|
|
|
</IconButton>
|
2021-05-28 20:30:27 +00:00
|
|
|
<IconButton
|
|
|
|
disabled={!hasSelection}
|
2021-05-29 10:12:28 +00:00
|
|
|
onClick={() => state.send('TOGGLED_SHAPE_HIDE')}
|
2021-05-28 20:30:27 +00:00
|
|
|
>
|
2021-05-29 10:12:28 +00:00
|
|
|
{isAllHidden ? <EyeClosedIcon /> : <EyeOpenIcon />}
|
2021-05-28 20:30:27 +00:00
|
|
|
</IconButton>
|
2021-05-29 10:12:28 +00:00
|
|
|
<IconButton
|
|
|
|
disabled={!hasSelection}
|
|
|
|
onClick={() => state.send('TOGGLED_SHAPE_LOCK')}
|
|
|
|
>
|
|
|
|
{isAllLocked ? <LockClosedIcon /> : <LockOpen1Icon />}
|
|
|
|
</IconButton>
|
|
|
|
<IconButton
|
|
|
|
disabled={!hasSelection}
|
|
|
|
onClick={() => state.send('TOGGLED_SHAPE_ASPECT_LOCK')}
|
|
|
|
>
|
|
|
|
{isAllAspectLocked ? <AspectRatioIcon /> : <BoxIcon />}
|
2021-05-28 20:30:27 +00:00
|
|
|
</IconButton>
|
2021-05-29 22:27:19 +00:00
|
|
|
</ButtonsRow>
|
|
|
|
<ButtonsRow>
|
|
|
|
<IconButton
|
|
|
|
disabled={!hasSelection}
|
|
|
|
onClick={() => state.send('MOVED', { type: MoveType.ToBack })}
|
|
|
|
>
|
|
|
|
<PinBottomIcon />
|
|
|
|
</IconButton>
|
|
|
|
<IconButton
|
|
|
|
disabled={!hasSelection}
|
|
|
|
onClick={() => state.send('MOVED', { type: MoveType.Backward })}
|
|
|
|
>
|
|
|
|
<ArrowDownIcon />
|
|
|
|
</IconButton>
|
|
|
|
<IconButton
|
|
|
|
disabled={!hasSelection}
|
|
|
|
onClick={() => state.send('MOVED', { type: MoveType.Forward })}
|
|
|
|
>
|
|
|
|
<ArrowUpIcon />
|
|
|
|
</IconButton>
|
|
|
|
<IconButton
|
|
|
|
disabled={!hasSelection}
|
|
|
|
onClick={() => state.send('MOVED', { type: MoveType.ToFront })}
|
|
|
|
>
|
|
|
|
<PinTopIcon />
|
|
|
|
</IconButton>
|
2021-05-29 13:59:11 +00:00
|
|
|
<IconButton
|
|
|
|
disabled={!hasSelection}
|
|
|
|
onClick={() => state.send('DELETED')}
|
|
|
|
>
|
|
|
|
<TrashIcon />
|
|
|
|
</IconButton>
|
2021-05-28 20:30:27 +00:00
|
|
|
</ButtonsRow>
|
2021-05-29 22:27:19 +00:00
|
|
|
<AlignDistribute
|
|
|
|
hasTwoOrMore={selectedIds.length > 1}
|
|
|
|
hasThreeOrMore={selectedIds.length > 2}
|
|
|
|
/>
|
2021-05-26 19:20:52 +00:00
|
|
|
</Content>
|
2021-05-26 10:34:10 +00:00
|
|
|
</Panel.Layout>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
const StylePanelRoot = styled(Panel.Root, {
|
2021-05-26 19:20:52 +00:00
|
|
|
minWidth: 1,
|
|
|
|
width: 184,
|
|
|
|
maxWidth: 184,
|
2021-05-28 20:30:27 +00:00
|
|
|
overflow: 'hidden',
|
|
|
|
position: 'relative',
|
2021-05-30 13:49:33 +00:00
|
|
|
border: '1px solid $panel',
|
|
|
|
boxShadow: '0px 2px 4px rgba(0,0,0,.12)',
|
2021-05-26 10:34:10 +00:00
|
|
|
|
|
|
|
variants: {
|
|
|
|
isOpen: {
|
2021-05-26 19:20:52 +00:00
|
|
|
true: {},
|
2021-05-26 10:34:10 +00:00
|
|
|
false: {
|
2021-05-30 13:49:33 +00:00
|
|
|
padding: 2,
|
|
|
|
height: 38,
|
|
|
|
width: 38,
|
2021-05-26 10:34:10 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2021-05-26 19:20:52 +00:00
|
|
|
const Content = styled(Panel.Content, {
|
|
|
|
padding: 8,
|
2021-05-26 10:34:10 +00:00
|
|
|
})
|
2021-05-28 20:30:27 +00:00
|
|
|
|
|
|
|
const Row = styled('div', {
|
|
|
|
position: 'relative',
|
|
|
|
display: 'flex',
|
|
|
|
width: '100%',
|
|
|
|
background: 'none',
|
|
|
|
border: 'none',
|
|
|
|
cursor: 'pointer',
|
|
|
|
outline: 'none',
|
|
|
|
alignItems: 'center',
|
|
|
|
justifyContent: 'space-between',
|
|
|
|
padding: '4px 2px 4px 12px',
|
|
|
|
|
|
|
|
'& label': {
|
|
|
|
fontFamily: '$ui',
|
|
|
|
fontSize: '$2',
|
|
|
|
fontWeight: '$1',
|
|
|
|
margin: 0,
|
|
|
|
padding: 0,
|
|
|
|
},
|
|
|
|
|
|
|
|
'& > svg': {
|
|
|
|
position: 'relative',
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
const ButtonsRow = styled('div', {
|
|
|
|
position: 'relative',
|
|
|
|
display: 'flex',
|
|
|
|
width: '100%',
|
|
|
|
background: 'none',
|
|
|
|
border: 'none',
|
|
|
|
cursor: 'pointer',
|
|
|
|
outline: 'none',
|
|
|
|
alignItems: 'center',
|
|
|
|
justifyContent: 'flex-start',
|
|
|
|
padding: 4,
|
|
|
|
})
|