| 1 |
import { BlockControls } from '@wordpress/block-editor'; |
| 2 |
import { ToolbarButton, ToolbarGroup } from '@wordpress/components'; |
| 3 |
import { applyFilters } from '@wordpress/hooks'; |
| 4 |
import { __ } from '@wordpress/i18n'; |
| 5 |
import defaultThemes from '../../defaultThemes.json'; |
| 6 |
import { useCanEditHTML } from '../../hooks/useCanEditHTML'; |
| 7 |
import { useLanguage } from '../../hooks/useLanguage'; |
| 8 |
import { useGlobalStore } from '../../state/global'; |
| 9 |
import type { AttributesPropsAndSetter, ThemeOption } from '../../types'; |
| 10 |
import { languages } from '../../util/languages'; |
| 11 |
|
| 12 |
export const ToolbarControls = ({ |
| 13 |
attributes, |
| 14 |
setAttributes, |
| 15 |
}: AttributesPropsAndSetter) => { |
| 16 |
const [language] = useLanguage({ attributes, setAttributes }); |
| 17 |
const { theme, bgColor, textColor } = attributes; |
| 18 |
const themes = applyFilters( |
| 19 |
'blocks.codeBlockPro.themes', |
| 20 |
defaultThemes, |
| 21 |
) as ThemeOption; |
| 22 |
const { setBringAttentionToPanel } = useGlobalStore(); |
| 23 |
const canEdit = useCanEditHTML(); |
| 24 |
|
| 25 |
if (canEdit === undefined) return null; |
| 26 |
|
| 27 |
return ( |
| 28 |
<BlockControls> |
| 29 |
<ToolbarGroup className="code-block-pro-editor"> |
| 30 |
<ToolbarButton |
| 31 |
icon={() => |
| 32 |
(languages[language] ?? language)?.replace( |
| 33 |
'ANSI', |
| 34 |
'ANSI (experimental)', |
| 35 |
) |
| 36 |
} |
| 37 |
onClick={() => setBringAttentionToPanel('language-select')} |
| 38 |
label={__('Press to open sidebar panel', 'code-block-pro')} |
| 39 |
style={{ width: 'auto' }} |
| 40 |
/> |
| 41 |
</ToolbarGroup> |
| 42 |
{themes[theme]?.name && ( |
| 43 |
<ToolbarGroup className="code-block-pro-editor"> |
| 44 |
<ToolbarButton |
| 45 |
icon={() => themes[theme]?.name} |
| 46 |
onClick={() => setBringAttentionToPanel('theme-select')} |
| 47 |
label={__('Press to open sidebar panel', 'code-block-pro')} |
| 48 |
style={{ width: 'auto' }} |
| 49 |
> |
| 50 |
<span className="flex mx-2"> |
| 51 |
<span |
| 52 |
style={{ |
| 53 |
backgroundColor: |
| 54 |
themes[theme]?.styles?.['color-background'] ?? bgColor, |
| 55 |
}} |
| 56 |
className="w-3 h-3 border border-gray-400 border-solid border-1 rounded-full inline-block" |
| 57 |
/> |
| 58 |
<span |
| 59 |
style={{ |
| 60 |
backgroundColor: |
| 61 |
themes[theme]?.styles?.['color-text'] ?? textColor, |
| 62 |
}} |
| 63 |
className="w-3 h-3 border border-gray-400 border-solid border-1 rounded-full inline-block transform -translate-x-1" |
| 64 |
/> |
| 65 |
</span> |
| 66 |
</ToolbarButton> |
| 67 |
</ToolbarGroup> |
| 68 |
)} |
| 69 |
</BlockControls> |
| 70 |
); |
| 71 |
}; |
| 72 |
|