Spaces:
Sleeping
Sleeping
File size: 10,723 Bytes
e1753d8 | 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 | import {
StylePanelSection,
StylePanelSubheading,
StylePanelColorPicker,
StylePanelDashPicker,
StylePanelFillPicker,
StylePanelGeoShapePicker,
StylePanelSizePicker,
StylePanelSplinePicker,
StylePanelFontPicker,
StylePanelTextAlignPicker,
StylePanelArrowheadPicker,
StylePanelArrowKindPicker,
TldrawUiSlider,
StylePanelContextProvider,
TLUiStylePanelProps,
useEditor,
track,
getDefaultColorTheme,
TldrawUiButton,
TldrawUiButtonCheck
} from 'tldraw'
import { useState } from 'react'
import { getBrushOpacityForTool, updateBrushOpacityForTool, toolBrushOpacityAtom } from '../utils/brushUtils'
import { getEraserSettings } from '../utils/eraserUtils'
const COLORS = [
'black', 'grey', 'light-grey', 'white',
'blue', 'light-blue', 'turquoise', 'green',
'light-green', 'yellow', 'orange', 'light-red',
'red', 'light-violet', 'violet'
]
export const CustomStylePanel = track((props: TLUiStylePanelProps) => {
const editor = useEditor()
const styles = props.styles ?? editor.getSharedStyles()
const selectedShapes = editor.getSelectedShapes()
const toolId = editor.getCurrentToolId()
const toolBrush = getBrushOpacityForTool(toolId)
const isGeo = selectedShapes.some(s => s.type === 'geo')
const hasText = selectedShapes.some(s => 'text' in s.props || s.type === 'text')
const isArrow = selectedShapes.some(s => s.type === 'arrow' || s.type === 'line')
// Eraser Settings
const [eraserSettings, setEraserSettings] = useState(getEraserSettings)
const updateEraserSetting = (key: string) => {
const newSettings = { ...eraserSettings, [key]: !(eraserSettings as any)[key] }
setEraserSettings(newSettings)
localStorage.setItem('tldraw_eraser_settings', JSON.stringify(newSettings))
}
// Drawing tools don't use fill
const drawingTools = ['draw', 'highlight', 'eraser', 'laser']
const isDrawingTool = drawingTools.includes(toolId) && selectedShapes.length === 0
// Get color theme for swatches
const theme = getDefaultColorTheme({ isDarkMode: editor.user.getIsDarkMode() })
const getOpacity = (key: 'borderOpacity' | 'fillOpacity', defaultValue: number) => {
if (selectedShapes.length === 0) {
const toolVal = toolBrush[key]
return toolVal !== undefined ? toolVal : defaultValue
}
const values = selectedShapes.map(s => (s.meta[key] as number) ?? defaultValue)
return values.every(v => v === values[0]) ? values[0] : defaultValue
}
const borderOpacity = getOpacity('borderOpacity', 1.0)
const fillOpacity = getOpacity('fillOpacity', 0.6)
const handleOpacityChange = (key: 'borderOpacity' | 'fillOpacity', value: number) => {
updateBrushOpacityForTool(toolId, { [key]: value })
if (selectedShapes.length > 0) {
editor.updateShapes(selectedShapes.map(s => ({
id: s.id,
type: s.type,
meta: { ...s.meta, [key]: value }
})))
}
}
// Get/set fill color (stored in meta.fillColor)
const getFillColor = () => {
if (selectedShapes.length === 0) return 'black'
const colors = selectedShapes.map(s => (s.meta.fillColor as string) || 'black')
return colors.every(c => c === colors[0]) ? colors[0] : 'black'
}
const setFillColor = (color: string) => {
if (selectedShapes.length > 0) {
editor.updateShapes(selectedShapes.map(s => ({
id: s.id,
type: s.type,
meta: { ...s.meta, fillColor: color }
})))
}
}
return (
<StylePanelContextProvider styles={styles}>
<div className="tlui-style-panel" style={{
overflowY: 'auto',
overflowX: 'hidden',
height: '100%',
background: editor.user.getIsDarkMode() ? 'rgba(30, 30, 30, 0.4)' : 'rgba(255, 255, 255, 0.4)',
backdropFilter: 'blur(10px)',
padding: '8px'
}}>
{toolId === 'eraser' && (
<StylePanelSection>
<StylePanelSubheading>Erase Only</StylePanelSubheading>
<div style={{ display: 'flex', flexDirection: 'column', gap: '2px' }}>
{[
{ label: 'Scribble', key: 'scribble' },
{ label: 'Text', key: 'text' },
{ label: 'Shapes', key: 'shapes' },
{ label: 'Images', key: 'images' }
].map(({ label, key }) => (
<TldrawUiButton
key={key}
type="low"
onClick={() => updateEraserSetting(key)}
style={{
justifyContent: 'space-between',
width: '100%',
padding: '4px 8px',
height: '32px',
background: (eraserSettings as any)[key] ? 'var(--color-selected-primary)' : 'transparent',
color: (eraserSettings as any)[key] ? 'var(--color-selected-contrast)' : 'inherit',
}}
>
<span style={{ fontSize: '12px', fontWeight: 500 }}>{label}</span>
<TldrawUiButtonCheck checked={(eraserSettings as any)[key]} />
</TldrawUiButton>
))}
</div>
</StylePanelSection>
)}
{isGeo && (
<StylePanelSection>
<StylePanelSubheading>Shape</StylePanelSubheading>
<StylePanelGeoShapePicker />
</StylePanelSection>
)}
{isArrow && <StylePanelSplinePicker />}
<StylePanelSection>
<StylePanelSubheading>Stroke</StylePanelSubheading>
<StylePanelColorPicker />
<StylePanelDashPicker />
<StylePanelSizePicker />
<TldrawUiSlider
value={Math.round(borderOpacity * 100)}
onValueChange={(value) => handleOpacityChange('borderOpacity', value / 100)}
min={0}
steps={100}
label="Stroke Opacity"
title="Stroke Opacity"
/>
</StylePanelSection>
{isGeo && !isDrawingTool && (
<StylePanelSection>
<StylePanelSubheading>Fill</StylePanelSubheading>
<div style={{ marginBottom: '8px' }}>
<div style={{
display: 'flex',
flexWrap: 'wrap',
gap: '4px'
}}>
{COLORS.map(color => {
const currentFill = getFillColor()
const isActive = currentFill === color
const themeColor = (theme as any)[color]
const colorValue = themeColor?.solid || '#000'
return (
<button
key={color}
onClick={() => setFillColor(color)}
data-state={isActive ? 'selected' : undefined}
aria-label={color}
title={`Fill: ${color}`}
style={{
width: '28px',
height: '28px',
padding: '4px',
border: isActive ? '2px solid var(--color-selected)' : '1px solid var(--color-panel-contrast)',
borderRadius: '4px',
background: colorValue,
cursor: 'pointer',
flexShrink: 0
}}
/>
)
})}
</div>
</div>
<StylePanelFillPicker />
<TldrawUiSlider
value={Math.round(fillOpacity * 100)}
onValueChange={(value) => handleOpacityChange('fillOpacity', value / 100)}
min={0}
steps={100}
label="Fill Opacity"
title="Fill Opacity"
/>
</StylePanelSection>
)}
{hasText && (
<StylePanelSection>
<StylePanelSubheading>Text</StylePanelSubheading>
<StylePanelFontPicker />
<StylePanelTextAlignPicker />
</StylePanelSection>
)}
{isArrow && (
<StylePanelSection>
<StylePanelSubheading>Arrow</StylePanelSubheading>
<StylePanelArrowheadPicker />
<StylePanelArrowKindPicker />
</StylePanelSection>
)}
</div>
</StylePanelContextProvider>
)
})
export function getInitialMetaForOpacity(editor: any) {
const toolId = editor.getCurrentToolId()
const all = toolBrushOpacityAtom.get()
const brush = all[toolId] || all.default
return {
fillOpacity: brush.fillOpacity,
borderOpacity: brush.borderOpacity,
fillColor: 'black', // Default fill color
}
}
|