File size: 7,402 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
import React from 'react'
import styled from 'styled-components'
import { ChartPropertyWithControl, Flavor } from '../../../types'
import { Control, Select } from '../ui'
import { CartesianOrientationControlConfig, ControlContext } from '../types'

const BOX_WIDTH = 70
const BOX_HEIGHT = 70
const BOX_PADDING = 1.5
const DEFAULT_ARROW_LENGTH = 16
const ACTIVE_ARROW_LENGTH = 35
const ARROW_HEAD_WIDTH = 14
const ARROW_HEAD_HEIGHT = 7

export type Orientation = 'top' | 'right' | 'bottom' | 'left'
export type OrientationOption = {
    label: string
    value: Orientation
}

interface CartesianOrientationControlProps {
    id: string
    property: ChartPropertyWithControl<CartesianOrientationControlConfig>
    flavors: Flavor[]
    currentFlavor: Flavor
    value: string
    onChange: (value: string) => void
    context?: ControlContext
}

const DEFAULT_MAPPING: CartesianOrientationControlConfig['mapping'] = {
    top: { label: 'top', value: 'top' },
    right: { label: 'right', value: 'right' },
    bottom: { label: 'bottom', value: 'bottom' },
    left: { label: 'left', value: 'left' },
}

export const CartesianOrientationControl = ({
    id,
    property,
    flavors,
    currentFlavor,
    value,
    onChange,
    context,
}: CartesianOrientationControlProps) => {
    const mapping = property.control.mapping ?? DEFAULT_MAPPING
    const invertedMapping: Record<string, Orientation> = {
        [mapping.top.value]: 'top',
        [mapping.right.value]: 'right',
        [mapping.bottom.value]: 'bottom',
        [mapping.left.value]: 'left',
    }
    const options: OrientationOption[] = [
        { label: mapping.top.label, value: 'top' },
        { label: mapping.right.label, value: 'right' },
        { label: mapping.bottom.label, value: 'bottom' },
        { label: mapping.left.label, value: 'left' },
    ]

    const orientation = invertedMapping[value]

    const handleChange = (orientation: Orientation) => {
        onChange(mapping[orientation].value)
    }

    const handleSelectChange = (option: OrientationOption | null) => {
        if (!option) return
        handleChange(option.value)
    }

    return (
        <Control
            id={id}
            property={property}
            flavors={flavors}
            currentFlavor={currentFlavor}
            context={context}
        >
            <Row>
                <svg width={BOX_WIDTH + BOX_PADDING * 2} height={BOX_HEIGHT + BOX_PADDING * 2}>
                    <g transform={`translate(${BOX_PADDING},${BOX_PADDING})`}>
                        <Rect width={BOX_WIDTH} height={BOX_HEIGHT} rx={2} />
                        <OrientationArea
                            orientation="top"
                            isActive={orientation === 'top'}
                            onChange={handleChange}
                        />
                        <OrientationArea
                            orientation="right"
                            isActive={orientation === 'right'}
                            onChange={handleChange}
                        />
                        <OrientationArea
                            orientation="bottom"
                            isActive={orientation === 'bottom'}
                            onChange={handleChange}
                        />
                        <OrientationArea
                            orientation="left"
                            isActive={orientation === 'left'}
                            onChange={handleChange}
                        />
                    </g>
                </svg>
                <Select<OrientationOption>
                    options={options}
                    value={options.find(option => option.value === orientation)}
                    isClearable={false}
                    onChange={handleSelectChange}
                />
            </Row>
        </Control>
    )
}

const Row = styled.div`
    display: flex;
    justify-content: flex-start;
    align-items: center;

    svg {
        margin-right: 10px;
    }

    > div {
        flex: 1;
        max-width: 260px;
    }
`

const Rect = styled.rect`
    fill: none;
    stroke: ${({ theme }) => theme.colors.border};
    stroke-width: 1px;
`

const OrientationArea = ({
    orientation,
    isActive,
    onChange,
}: {
    orientation: Orientation
    isActive: boolean
    onChange: (value: Orientation) => void
}) => {
    const [isHover, setIsHover] = React.useState(false)
    const handleMouseEnter = () => {
        setIsHover(true)
    }
    const handleMouseLeave = () => {
        setIsHover(false)
    }

    const arrowLength = isActive ? ACTIVE_ARROW_LENGTH : DEFAULT_ARROW_LENGTH

    let trianglePath: string
    let arrowPosition: [number, number]
    let arrowRotation: number = 0

    if (orientation === 'top') {
        trianglePath = `
            M 0 ${BOX_HEIGHT}
            L ${BOX_WIDTH} ${BOX_HEIGHT}
            L ${BOX_WIDTH / 2} ${BOX_HEIGHT / 2}
            Z
        `
        arrowPosition = [BOX_WIDTH / 2, BOX_HEIGHT]
        arrowRotation = -90
    } else if (orientation === 'right') {
        trianglePath = `
            M 0 0
            L ${BOX_WIDTH / 2} ${BOX_HEIGHT / 2}
            L 0 ${BOX_HEIGHT}
            Z
        `
        arrowPosition = [0, BOX_HEIGHT / 2]
    } else if (orientation === 'bottom') {
        trianglePath = `
            M 0 0
            L ${BOX_WIDTH} 0
            L ${BOX_WIDTH / 2} ${BOX_HEIGHT / 2}
            Z
        `
        arrowPosition = [BOX_WIDTH / 2, 0]
        arrowRotation = 90
    } else {
        trianglePath = `
            M ${BOX_WIDTH} 0
            L ${BOX_WIDTH} ${BOX_HEIGHT}
            L ${BOX_WIDTH / 2} ${BOX_HEIGHT / 2}
            Z
        `
        arrowPosition = [BOX_WIDTH, BOX_HEIGHT / 2]
        arrowRotation = 180
    }

    return (
        <>
            <OrientationAreaTriangle
                d={trianglePath}
                onMouseEnter={handleMouseEnter}
                onMouseLeave={handleMouseLeave}
                onClick={() => {
                    onChange(orientation)
                }}
            />
            <g
                transform={`translate(${arrowPosition[0]},${arrowPosition[1]}) rotate(${arrowRotation})`}
                style={{ pointerEvents: 'none' }}
            >
                <OrientationArrow
                    isActive={isActive}
                    isHover={isHover}
                    d={`
                        M 0 0
                        L ${arrowLength} 0
                        M ${arrowLength - ARROW_HEAD_HEIGHT} -${ARROW_HEAD_WIDTH / 2}
                        L ${arrowLength} 0
                        L ${arrowLength - ARROW_HEAD_HEIGHT} ${ARROW_HEAD_WIDTH / 2}
                    `}
                />
            </g>
        </>
    )
}

const OrientationAreaTriangle = styled.path`
    fill-opacity: 0;
    fill: ${({ theme }) => theme.colors.textLight};
    cursor: pointer;
`

const OrientationArrow = styled.path<{ isActive: boolean; isHover: boolean }>`
    pointer-events: none;
    fill: none;
    stroke: ${({ theme, isActive, isHover }) => {
        if (isActive || isHover) return theme.colors.accent
        return theme.colors.border
    }};
    stroke-width: ${({ isActive, isHover }) => {
        if (isActive) return 3
        if (isHover) return 2
        return 1
    }}px;
    stroke-linecap: round;
    opacity: 0.66;
`