| 1 |
import { InspectorControls } from '@wordpress/block-editor'; |
| 2 |
import { |
| 3 |
PanelBody, |
| 4 |
BaseControl, |
| 5 |
SelectControl, |
| 6 |
CheckboxControl, |
| 7 |
} from '@wordpress/components'; |
| 8 |
import { sprintf, __ } from '@wordpress/i18n'; |
| 9 |
import { Theme } from 'shiki'; |
| 10 |
import defaultLanguages from '../../defaultLanguages.json'; |
| 11 |
import defaultThemes from '../../defaultThemes.json'; |
| 12 |
import { useLanguage } from '../../hooks/useLanguage'; |
| 13 |
import { useGlobalStore } from '../../state/global'; |
| 14 |
import { useThemeStore } from '../../state/theme'; |
| 15 |
import { AttributesPropsAndSetter } from '../../types'; |
| 16 |
import { Notice } from '../components/Notice'; |
| 17 |
import { ThemePreview } from '../components/ThemePreview'; |
| 18 |
|
| 19 |
export const SidebarControls = ({ |
| 20 |
attributes, |
| 21 |
setAttributes, |
| 22 |
}: AttributesPropsAndSetter) => { |
| 23 |
const [language, setLanguage] = useLanguage({ attributes, setAttributes }); |
| 24 |
const { setPreviousTheme } = useThemeStore(); |
| 25 |
const { setPreviousSettings } = useGlobalStore(); |
| 26 |
|
| 27 |
return ( |
| 28 |
<InspectorControls> |
| 29 |
<PanelBody |
| 30 |
title={__('Settings', 'code-block-pro')} |
| 31 |
initialOpen={true}> |
| 32 |
<div className="code-block-pro-editor"> |
| 33 |
<BaseControl id="code-block-pro-settings"> |
| 34 |
<SelectControl |
| 35 |
label={__('Language', 'code-block-pro')} |
| 36 |
value={language} |
| 37 |
onChange={setLanguage} |
| 38 |
options={defaultLanguages.map((value) => ({ |
| 39 |
label: value, |
| 40 |
value, |
| 41 |
}))} |
| 42 |
/> |
| 43 |
<CheckboxControl |
| 44 |
label={__('Copy Button', 'code-block-pro')} |
| 45 |
help={__( |
| 46 |
'If checked, users will be able to copy your code snippet to their clipboard.', |
| 47 |
'code-block-pro', |
| 48 |
)} |
| 49 |
checked={attributes.copyButton} |
| 50 |
onChange={(value) => { |
| 51 |
setPreviousSettings({ copyButton: value }); |
| 52 |
setAttributes({ copyButton: value }); |
| 53 |
}} |
| 54 |
/> |
| 55 |
</BaseControl> |
| 56 |
<Notice /> |
| 57 |
</div> |
| 58 |
</PanelBody> |
| 59 |
<PanelBody |
| 60 |
title={__('Themes', 'code-block-pro')} |
| 61 |
initialOpen={false}> |
| 62 |
<div className="code-block-pro-editor"> |
| 63 |
{Object.entries(defaultThemes).map(([slug, name]) => ( |
| 64 |
<BaseControl |
| 65 |
id={`code-block-pro-theme-${slug}`} |
| 66 |
label={ |
| 67 |
attributes.theme === slug |
| 68 |
? sprintf( |
| 69 |
__('%s (current)', 'code-block-pro'), |
| 70 |
name, |
| 71 |
) |
| 72 |
: name |
| 73 |
} |
| 74 |
key={slug}> |
| 75 |
<ThemePreview |
| 76 |
theme={slug as Theme} |
| 77 |
lang={language} |
| 78 |
onClick={() => { |
| 79 |
setAttributes({ theme: slug as Theme }); |
| 80 |
setPreviousTheme(slug as Theme); |
| 81 |
}} |
| 82 |
code={attributes.code} |
| 83 |
/> |
| 84 |
</BaseControl> |
| 85 |
))} |
| 86 |
</div> |
| 87 |
</PanelBody> |
| 88 |
</InspectorControls> |
| 89 |
); |
| 90 |
}; |
| 91 |
|