style.js
149 lines
| 1 | /** |
| 2 | * group-style block type |
| 3 | * |
| 4 | */ |
| 5 | import { convertColorClass } from '@vkblocks/utils/color-code-to-class.js'; |
| 6 | import { assign } from 'lodash'; |
| 7 | import { __ } from '@wordpress/i18n'; |
| 8 | import { addFilter } from '@wordpress/hooks'; |
| 9 | import { PanelBody } from '@wordpress/components'; |
| 10 | import { InspectorControls, ColorPalette } from '@wordpress/block-editor'; |
| 11 | import { createHigherOrderComponent } from '@wordpress/compose'; |
| 12 | import { registerBlockStyle } from '@wordpress/blocks'; |
| 13 | |
| 14 | const isValidBlockType = (name) => { |
| 15 | const validBlockTypes = ['core/group']; |
| 16 | return validBlockTypes.includes(name); |
| 17 | }; |
| 18 | |
| 19 | export const addAttribute = (settings) => { |
| 20 | if (isValidBlockType(settings.name)) { |
| 21 | settings.attributes = assign(settings.attributes, { |
| 22 | color: { |
| 23 | type: 'string', |
| 24 | }, |
| 25 | }); |
| 26 | } |
| 27 | return settings; |
| 28 | }; |
| 29 | addFilter('blocks.registerBlockType', 'vk-blocks/group-style', addAttribute); |
| 30 | |
| 31 | export const addBlockControl = createHigherOrderComponent((BlockEdit) => { |
| 32 | let activeColor = ''; |
| 33 | |
| 34 | return (props) => { |
| 35 | if (isValidBlockType(props.name) && props.isSelected) { |
| 36 | if (props.attributes.color) { |
| 37 | activeColor = props.attributes.color; |
| 38 | } else { |
| 39 | activeColor = '#fffd6b'; |
| 40 | } |
| 41 | |
| 42 | return ( |
| 43 | <> |
| 44 | <BlockEdit {...props} /> |
| 45 | <InspectorControls> |
| 46 | <PanelBody |
| 47 | title={__('Border Color', 'vk-blocks')} |
| 48 | initialOpen={false} |
| 49 | className="group-border-color-controle" |
| 50 | > |
| 51 | <ColorPalette |
| 52 | value={activeColor} |
| 53 | disableCustomColors={true} |
| 54 | onChange={(newColor) => { |
| 55 | let newClassName = |
| 56 | convertColorClass(newColor); |
| 57 | |
| 58 | if (props.attributes.className) { |
| 59 | let inputClassName = |
| 60 | props.attributes.className; |
| 61 | |
| 62 | inputClassName = |
| 63 | inputClassName.split(' '); |
| 64 | |
| 65 | const filterClassName = |
| 66 | inputClassName.filter(function ( |
| 67 | name |
| 68 | ) { |
| 69 | return ( |
| 70 | -1 === |
| 71 | name.indexOf('vk-has-') |
| 72 | ); |
| 73 | }); |
| 74 | |
| 75 | filterClassName.push(newClassName); |
| 76 | |
| 77 | newClassName = |
| 78 | filterClassName.join(' '); |
| 79 | } |
| 80 | |
| 81 | activeColor = newColor; |
| 82 | props.setAttributes({ |
| 83 | className: newClassName, |
| 84 | color: newColor, |
| 85 | }); |
| 86 | }} |
| 87 | /> |
| 88 | </PanelBody> |
| 89 | </InspectorControls> |
| 90 | </> |
| 91 | ); |
| 92 | } |
| 93 | return <BlockEdit {...props} />; |
| 94 | }; |
| 95 | }, 'addMyCustomBlockControls'); |
| 96 | |
| 97 | addFilter('editor.BlockEdit', 'vk-blocks/group-style', addBlockControl); |
| 98 | |
| 99 | registerBlockStyle('core/group', [ |
| 100 | { |
| 101 | name: 'vk-group-solid', |
| 102 | label: __('Solid', 'vk-blocks'), |
| 103 | }, |
| 104 | { |
| 105 | name: 'vk-group-solid-roundcorner', |
| 106 | label: __('Solid Roundcorner', 'vk-blocks'), |
| 107 | }, |
| 108 | { |
| 109 | name: 'vk-group-dotted', |
| 110 | label: __('Dotted', 'vk-blocks'), |
| 111 | }, |
| 112 | { |
| 113 | name: 'vk-group-dashed', |
| 114 | label: __('Dashed', 'vk-blocks'), |
| 115 | }, |
| 116 | { |
| 117 | name: 'vk-group-double', |
| 118 | label: __('Double', 'vk-blocks'), |
| 119 | }, |
| 120 | { |
| 121 | name: 'vk-group-stitch', |
| 122 | label: __('Stitch', 'vk-blocks'), |
| 123 | }, |
| 124 | { |
| 125 | name: 'vk-group-top-bottom-border', |
| 126 | label: __('Border Top Bottom', 'vk-blocks'), |
| 127 | }, |
| 128 | { |
| 129 | name: 'vk-group-shadow', |
| 130 | label: __('Shadow', 'vk-blocks'), |
| 131 | }, |
| 132 | { |
| 133 | name: 'vk-group-alert-info', |
| 134 | label: __('Info', 'vk-blocks'), |
| 135 | }, |
| 136 | { |
| 137 | name: 'vk-group-alert-success', |
| 138 | label: __('Success', 'vk-blocks'), |
| 139 | }, |
| 140 | { |
| 141 | name: 'vk-group-alert-warning', |
| 142 | label: __('Warning', 'vk-blocks'), |
| 143 | }, |
| 144 | { |
| 145 | name: 'vk-group-alert-danger', |
| 146 | label: __('Danger', 'vk-blocks'), |
| 147 | }, |
| 148 | ]); |
| 149 |