/
DefaultStylePanelContent.tsx
398 lines (365 loc) · 10.6 KB
/
DefaultStylePanelContent.tsx
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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
import {
ArrowShapeArrowheadEndStyle,
ArrowShapeArrowheadStartStyle,
DefaultColorStyle,
DefaultDashStyle,
DefaultFillStyle,
DefaultFontStyle,
DefaultHorizontalAlignStyle,
DefaultSizeStyle,
DefaultTextAlignStyle,
DefaultVerticalAlignStyle,
GeoShapeGeoStyle,
LineShapeSplineStyle,
ReadonlySharedStyleMap,
StyleProp,
TLArrowShapeArrowheadStyle,
TLDefaultColorTheme,
getDefaultColorTheme,
minBy,
useEditor,
useIsDarkMode,
useValue,
} from '@tldraw/editor'
import React from 'react'
import { STYLES } from '../../../styles'
import { kickoutOccludedShapes } from '../../../tools/SelectTool/selectHelpers'
import { useUiEvents } from '../../context/events'
import { useRelevantStyles } from '../../hooks/useRelevantStyles'
import { useTranslation } from '../../hooks/useTranslation/useTranslation'
import { TldrawUiButton } from '../primitives/Button/TldrawUiButton'
import { TldrawUiButtonIcon } from '../primitives/Button/TldrawUiButtonIcon'
import { TldrawUiButtonPicker } from '../primitives/TldrawUiButtonPicker'
import { TldrawUiSlider } from '../primitives/TldrawUiSlider'
import { DoubleDropdownPicker } from './DoubleDropdownPicker'
import { DropdownPicker } from './DropdownPicker'
/** @public */
export type TLUiStylePanelContentProps = {
styles: ReturnType<typeof useRelevantStyles>
}
/** @public */
export function DefaultStylePanelContent({ styles }: TLUiStylePanelContentProps) {
const isDarkMode = useIsDarkMode()
if (!styles) return null
const geo = styles.get(GeoShapeGeoStyle)
const arrowheadEnd = styles.get(ArrowShapeArrowheadEndStyle)
const arrowheadStart = styles.get(ArrowShapeArrowheadStartStyle)
const spline = styles.get(LineShapeSplineStyle)
const font = styles.get(DefaultFontStyle)
const hideGeo = geo === undefined
const hideArrowHeads = arrowheadEnd === undefined && arrowheadStart === undefined
const hideSpline = spline === undefined
const hideText = font === undefined
const theme = getDefaultColorTheme({ isDarkMode: isDarkMode })
return (
<>
<CommonStylePickerSet theme={theme} styles={styles} />
{!hideText && <TextStylePickerSet theme={theme} styles={styles} />}
{!(hideGeo && hideArrowHeads && hideSpline) && (
<div className="tlui-style-panel__section" aria-label="style panel styles">
<GeoStylePickerSet styles={styles} />
<ArrowheadStylePickerSet styles={styles} />
<SplineStylePickerSet styles={styles} />
</div>
)}
</>
)
}
function useStyleChangeCallback() {
const editor = useEditor()
const trackEvent = useUiEvents()
return React.useMemo(
() =>
function handleStyleChange<T>(style: StyleProp<T>, value: T) {
editor.batch(() => {
if (editor.isIn('select')) {
editor.setStyleForSelectedShapes(style, value)
}
editor.setStyleForNextShapes(style, value)
editor.updateInstanceState({ isChangingStyle: true })
})
trackEvent('set-style', { source: 'style-panel', id: style.id, value: value as string })
},
[editor, trackEvent]
)
}
/** @public */
export function CommonStylePickerSet({
styles,
theme,
}: {
styles: ReadonlySharedStyleMap
theme: TLDefaultColorTheme
}) {
const msg = useTranslation()
const editor = useEditor()
const handleValueChange = useStyleChangeCallback()
const color = styles.get(DefaultColorStyle)
const fill = styles.get(DefaultFillStyle)
const dash = styles.get(DefaultDashStyle)
const size = styles.get(DefaultSizeStyle)
const showPickers = fill !== undefined || dash !== undefined || size !== undefined
return (
<>
<div
tabIndex={-1}
className="tlui-style-panel__section__common"
aria-label="style panel styles"
data-testid="style.panel"
>
{color === undefined ? null : (
<TldrawUiButtonPicker
title={msg('style-panel.color')}
uiType="color"
style={DefaultColorStyle}
items={STYLES.color}
value={color}
onValueChange={handleValueChange}
theme={theme}
/>
)}
<OpacitySlider />
</div>
{showPickers && (
<div className="tlui-style-panel__section" aria-label="style panel styles">
{fill === undefined ? null : (
<TldrawUiButtonPicker
title={msg('style-panel.fill')}
uiType="fill"
style={DefaultFillStyle}
items={STYLES.fill}
value={fill}
onValueChange={handleValueChange}
theme={theme}
/>
)}
{dash === undefined ? null : (
<TldrawUiButtonPicker
title={msg('style-panel.dash')}
uiType="dash"
style={DefaultDashStyle}
items={STYLES.dash}
value={dash}
onValueChange={handleValueChange}
theme={theme}
/>
)}
{size === undefined ? null : (
<TldrawUiButtonPicker
title={msg('style-panel.size')}
uiType="size"
style={DefaultSizeStyle}
items={STYLES.size}
value={size}
onValueChange={(style, value) => {
handleValueChange(style, value)
const selectedShapeIds = editor.getSelectedShapeIds()
if (selectedShapeIds.length > 0) {
kickoutOccludedShapes(editor, selectedShapeIds)
}
}}
theme={theme}
/>
)}
</div>
)}
</>
)
}
/** @public */
export function TextStylePickerSet({
theme,
styles,
}: {
theme: TLDefaultColorTheme
styles: ReadonlySharedStyleMap
}) {
const msg = useTranslation()
const handleValueChange = useStyleChangeCallback()
const font = styles.get(DefaultFontStyle)
const textAlign = styles.get(DefaultTextAlignStyle)
const labelAlign = styles.get(DefaultHorizontalAlignStyle)
const verticalLabelAlign = styles.get(DefaultVerticalAlignStyle)
if (font === undefined && labelAlign === undefined) {
return null
}
return (
<div className="tlui-style-panel__section" aria-label="style panel text">
{font === undefined ? null : (
<TldrawUiButtonPicker
title={msg('style-panel.font')}
uiType="font"
style={DefaultFontStyle}
items={STYLES.font}
value={font}
onValueChange={handleValueChange}
theme={theme}
/>
)}
{textAlign === undefined ? null : (
<div className="tlui-style-panel__row">
<TldrawUiButtonPicker
title={msg('style-panel.align')}
uiType="align"
style={DefaultTextAlignStyle}
items={STYLES.textAlign}
value={textAlign}
onValueChange={handleValueChange}
theme={theme}
/>
<div className="tlui-style-panel__row__extra-button">
<TldrawUiButton
type="icon"
title={msg('style-panel.vertical-align')}
data-testid="vertical-align"
disabled
>
<TldrawUiButtonIcon icon="vertical-align-middle" />
</TldrawUiButton>
</div>
</div>
)}
{labelAlign === undefined ? null : (
<div className="tlui-style-panel__row">
<TldrawUiButtonPicker
title={msg('style-panel.align')}
uiType="align"
style={DefaultHorizontalAlignStyle}
items={STYLES.horizontalAlign}
value={labelAlign}
onValueChange={handleValueChange}
theme={theme}
/>
<div className="tlui-style-panel__row__extra-button">
{verticalLabelAlign === undefined ? (
<TldrawUiButton
type="icon"
title={msg('style-panel.vertical-align')}
data-testid="vertical-align"
disabled
>
<TldrawUiButtonIcon icon="vertical-align-middle" />
</TldrawUiButton>
) : (
<DropdownPicker
type="icon"
id="geo-vertical-alignment"
uiType="verticalAlign"
style={DefaultVerticalAlignStyle}
items={STYLES.verticalAlign}
value={verticalLabelAlign}
onValueChange={handleValueChange}
/>
)}
</div>
</div>
)}
</div>
)
}
/** @public */
export function GeoStylePickerSet({ styles }: { styles: ReadonlySharedStyleMap }) {
const handleValueChange = useStyleChangeCallback()
const geo = styles.get(GeoShapeGeoStyle)
if (geo === undefined) {
return null
}
return (
<DropdownPicker
id="geo"
type="menu"
label={'style-panel.geo'}
uiType="geo"
style={GeoShapeGeoStyle}
items={STYLES.geo}
value={geo}
onValueChange={handleValueChange}
/>
)
}
/** @public */
export function SplineStylePickerSet({ styles }: { styles: ReadonlySharedStyleMap }) {
const handleValueChange = useStyleChangeCallback()
const spline = styles.get(LineShapeSplineStyle)
if (spline === undefined) {
return null
}
return (
<DropdownPicker
id="spline"
type="menu"
label={'style-panel.spline'}
uiType="spline"
style={LineShapeSplineStyle}
items={STYLES.spline}
value={spline}
onValueChange={handleValueChange}
/>
)
}
/** @public */
export function ArrowheadStylePickerSet({ styles }: { styles: ReadonlySharedStyleMap }) {
const handleValueChange = useStyleChangeCallback()
const arrowheadEnd = styles.get(ArrowShapeArrowheadEndStyle)
const arrowheadStart = styles.get(ArrowShapeArrowheadStartStyle)
if (!arrowheadEnd || !arrowheadStart) {
return null
}
return (
<DoubleDropdownPicker<TLArrowShapeArrowheadStyle>
label={'style-panel.arrowheads'}
uiTypeA="arrowheadStart"
styleA={ArrowShapeArrowheadStartStyle}
itemsA={STYLES.arrowheadStart}
valueA={arrowheadStart}
uiTypeB="arrowheadEnd"
styleB={ArrowShapeArrowheadEndStyle}
itemsB={STYLES.arrowheadEnd}
valueB={arrowheadEnd}
onValueChange={handleValueChange}
labelA="style-panel.arrowhead-start"
labelB="style-panel.arrowhead-end"
/>
)
}
const tldrawSupportedOpacities = [0.1, 0.25, 0.5, 0.75, 1] as const
/** @public */
export function OpacitySlider() {
const editor = useEditor()
const opacity = useValue('opacity', () => editor.getSharedOpacity(), [editor])
const trackEvent = useUiEvents()
const msg = useTranslation()
const handleOpacityValueChange = React.useCallback(
(value: number) => {
const item = tldrawSupportedOpacities[value]
editor.batch(() => {
if (editor.isIn('select')) {
editor.setOpacityForSelectedShapes(item)
}
editor.setOpacityForNextShapes(item)
editor.updateInstanceState({ isChangingStyle: true })
})
trackEvent('set-style', { source: 'style-panel', id: 'opacity', value })
},
[editor, trackEvent]
)
if (opacity === undefined) return null
const opacityIndex =
opacity.type === 'mixed'
? -1
: tldrawSupportedOpacities.indexOf(
minBy(tldrawSupportedOpacities, (supportedOpacity) =>
Math.abs(supportedOpacity - opacity.value)
)!
)
return (
<TldrawUiSlider
data-testid="style.opacity"
value={opacityIndex >= 0 ? opacityIndex : tldrawSupportedOpacities.length - 1}
label={opacity.type === 'mixed' ? 'style-panel.mixed' : `opacity-style.${opacity.value}`}
onValueChange={handleOpacityValueChange}
steps={tldrawSupportedOpacities.length - 1}
title={msg('style-panel.opacity')}
/>
)
}