| 1 |
import classnames from 'classnames/dedupe'; |
| 2 |
|
| 3 |
import { |
| 4 |
InnerBlocks, |
| 5 |
InspectorControls, |
| 6 |
useBlockProps, |
| 7 |
useInnerBlocksProps, |
| 8 |
} from '@wordpress/block-editor'; |
| 9 |
import { PanelBody, SelectControl } from '@wordpress/components'; |
| 10 |
import { useSelect } from '@wordpress/data'; |
| 11 |
import { applyFilters } from '@wordpress/hooks'; |
| 12 |
import { __, sprintf } from '@wordpress/i18n'; |
| 13 |
|
| 14 |
import ApplyFilters from '../../components/apply-filters'; |
| 15 |
import RangeControl from '../../components/range-control'; |
| 16 |
import ResponsiveToggle from '../../components/responsive-toggle'; |
| 17 |
import ToggleGroup from '../../components/toggle-group'; |
| 18 |
import useResponsive from '../../hooks/use-responsive'; |
| 19 |
import getIcon from '../../utils/get-icon'; |
| 20 |
import getColClass from './get-col-class'; |
| 21 |
|
| 22 |
/** |
| 23 |
* Get array for Select element. |
| 24 |
* |
| 25 |
* @return {Array} array for Select. |
| 26 |
*/ |
| 27 |
const getDefaultColumnSizes = function () { |
| 28 |
const result = [ |
| 29 |
{ |
| 30 |
label: __('Inherit from larger', 'ghostkit'), |
| 31 |
value: '', |
| 32 |
}, |
| 33 |
{ |
| 34 |
label: __('Auto', 'ghostkit'), |
| 35 |
value: 'auto', |
| 36 |
}, |
| 37 |
{ |
| 38 |
label: __('Grow', 'ghostkit'), |
| 39 |
value: 'grow', |
| 40 |
}, |
| 41 |
]; |
| 42 |
|
| 43 |
for (let k = 1; k <= 12; k += 1) { |
| 44 |
result.push({ |
| 45 |
// eslint-disable-next-line @wordpress/valid-sprintf |
| 46 |
label: sprintf( |
| 47 |
k === 1 |
| 48 |
? __('%d Column (%s)', 'ghostkit') |
| 49 |
: __('%d Columns (%s)', 'ghostkit'), |
| 50 |
k, |
| 51 |
`${Math.round(((100 * k) / 12) * 100) / 100}%` |
| 52 |
), |
| 53 |
value: k, |
| 54 |
}); |
| 55 |
} |
| 56 |
return result; |
| 57 |
}; |
| 58 |
|
| 59 |
/** |
| 60 |
* Get array for Select element. |
| 61 |
* |
| 62 |
* @param {number} columns - number of available columns. |
| 63 |
* |
| 64 |
* @return {Array} array for Select. |
| 65 |
*/ |
| 66 |
const getDefaultColumnOrders = function (columns = 12) { |
| 67 |
const result = [ |
| 68 |
{ |
| 69 |
label: __('Inherit from larger', 'ghostkit'), |
| 70 |
value: '', |
| 71 |
}, |
| 72 |
{ |
| 73 |
label: __('Auto', 'ghostkit'), |
| 74 |
value: 'auto', |
| 75 |
}, |
| 76 |
{ |
| 77 |
label: __('First', 'ghostkit'), |
| 78 |
value: 'first', |
| 79 |
}, |
| 80 |
]; |
| 81 |
|
| 82 |
for (let k = 1; k <= columns; k += 1) { |
| 83 |
result.push({ |
| 84 |
label: k, |
| 85 |
value: k, |
| 86 |
}); |
| 87 |
} |
| 88 |
|
| 89 |
result.push({ |
| 90 |
label: __('Last', 'ghostkit'), |
| 91 |
value: 'last', |
| 92 |
}); |
| 93 |
|
| 94 |
return result; |
| 95 |
}; |
| 96 |
|
| 97 |
/** |
| 98 |
* Block Edit Class. |
| 99 |
* |
| 100 |
* @param props |
| 101 |
*/ |
| 102 |
export default function BlockEdit(props) { |
| 103 |
const { clientId, attributes, setAttributes } = props; |
| 104 |
|
| 105 |
const { stickyContent, stickyContentOffset } = attributes; |
| 106 |
|
| 107 |
const { device } = useResponsive(); |
| 108 |
|
| 109 |
const { hasChildBlocks } = useSelect( |
| 110 |
(select) => { |
| 111 |
const blockEditor = select('core/block-editor'); |
| 112 |
|
| 113 |
return { |
| 114 |
hasChildBlocks: blockEditor |
| 115 |
? blockEditor.getBlockOrder(clientId).length > 0 |
| 116 |
: false, |
| 117 |
}; |
| 118 |
}, |
| 119 |
[clientId] |
| 120 |
); |
| 121 |
|
| 122 |
// background |
| 123 |
const background = applyFilters( |
| 124 |
'ghostkit.editor.grid-column.background', |
| 125 |
'', |
| 126 |
props |
| 127 |
); |
| 128 |
|
| 129 |
const blockProps = useBlockProps({ |
| 130 |
className: classnames(props.attributes.className, getColClass(props)), |
| 131 |
}); |
| 132 |
|
| 133 |
const innerBlocksProps = useInnerBlocksProps( |
| 134 |
{ className: 'ghostkit-col-content' }, |
| 135 |
{ |
| 136 |
templateLock: false, |
| 137 |
renderAppender: hasChildBlocks |
| 138 |
? undefined |
| 139 |
: InnerBlocks.ButtonBlockAppender, |
| 140 |
} |
| 141 |
); |
| 142 |
|
| 143 |
let sizeName = 'size'; |
| 144 |
let orderName = 'order'; |
| 145 |
let verticalAlignName = 'verticalAlign'; |
| 146 |
|
| 147 |
if (device) { |
| 148 |
sizeName = `${device}_${sizeName}`; |
| 149 |
orderName = `${device}_${orderName}`; |
| 150 |
verticalAlignName = `${device}_${verticalAlignName}`; |
| 151 |
} |
| 152 |
|
| 153 |
return ( |
| 154 |
<div {...blockProps}> |
| 155 |
<InspectorControls> |
| 156 |
<ApplyFilters |
| 157 |
name="ghostkit.editor.controls" |
| 158 |
attribute="columnSettings" |
| 159 |
props={props} |
| 160 |
> |
| 161 |
<PanelBody> |
| 162 |
<SelectControl |
| 163 |
label={ |
| 164 |
<> |
| 165 |
{__('Size', 'ghostkit')} |
| 166 |
<ResponsiveToggle |
| 167 |
checkActive={(checkMedia) => { |
| 168 |
return !!attributes[ |
| 169 |
`${checkMedia}_size` |
| 170 |
]; |
| 171 |
}} |
| 172 |
/> |
| 173 |
</> |
| 174 |
} |
| 175 |
value={attributes[sizeName]} |
| 176 |
onChange={(value) => { |
| 177 |
setAttributes({ |
| 178 |
[sizeName]: value, |
| 179 |
}); |
| 180 |
}} |
| 181 |
options={getDefaultColumnSizes()} |
| 182 |
/> |
| 183 |
<SelectControl |
| 184 |
label={ |
| 185 |
<> |
| 186 |
{__('Order', 'ghostkit')} |
| 187 |
<ResponsiveToggle |
| 188 |
checkActive={(checkMedia) => { |
| 189 |
return !!attributes[ |
| 190 |
`${checkMedia}_order` |
| 191 |
]; |
| 192 |
}} |
| 193 |
/> |
| 194 |
</> |
| 195 |
} |
| 196 |
value={attributes[orderName]} |
| 197 |
onChange={(value) => { |
| 198 |
setAttributes({ |
| 199 |
[orderName]: value, |
| 200 |
}); |
| 201 |
}} |
| 202 |
options={getDefaultColumnOrders()} |
| 203 |
/> |
| 204 |
<ToggleGroup |
| 205 |
label={ |
| 206 |
<> |
| 207 |
{__('Vertical Alignment', 'ghostkit')} |
| 208 |
<ResponsiveToggle |
| 209 |
checkActive={(checkMedia) => { |
| 210 |
return !!attributes[ |
| 211 |
`${checkMedia}_verticalAlign` |
| 212 |
]; |
| 213 |
}} |
| 214 |
/> |
| 215 |
</> |
| 216 |
} |
| 217 |
value={attributes[verticalAlignName]} |
| 218 |
options={[ |
| 219 |
{ |
| 220 |
icon: getIcon('icon-vertical-top'), |
| 221 |
label: __('Top', 'ghostkit'), |
| 222 |
value: '', |
| 223 |
}, |
| 224 |
{ |
| 225 |
icon: getIcon('icon-vertical-center'), |
| 226 |
label: __('Center', 'ghostkit'), |
| 227 |
value: 'center', |
| 228 |
}, |
| 229 |
{ |
| 230 |
icon: getIcon('icon-vertical-bottom'), |
| 231 |
label: __('Bottom', 'ghostkit'), |
| 232 |
value: 'end', |
| 233 |
}, |
| 234 |
]} |
| 235 |
onChange={(value) => { |
| 236 |
setAttributes({ [verticalAlignName]: value }); |
| 237 |
}} |
| 238 |
isDeselectable |
| 239 |
/> |
| 240 |
</PanelBody> |
| 241 |
</ApplyFilters> |
| 242 |
<PanelBody> |
| 243 |
<ToggleGroup |
| 244 |
label={__('Sticky Content', 'ghostkit')} |
| 245 |
value={stickyContent} |
| 246 |
options={[ |
| 247 |
{ |
| 248 |
label: __('Top', 'ghostkit'), |
| 249 |
value: 'top', |
| 250 |
}, |
| 251 |
{ |
| 252 |
label: __('Bottom', 'ghostkit'), |
| 253 |
value: 'bottom', |
| 254 |
}, |
| 255 |
]} |
| 256 |
onChange={(value) => { |
| 257 |
setAttributes({ stickyContent: value }); |
| 258 |
}} |
| 259 |
isDeselectable |
| 260 |
/> |
| 261 |
{stickyContent ? ( |
| 262 |
<RangeControl |
| 263 |
label={__('Sticky Offset', 'ghostkit')} |
| 264 |
value={stickyContentOffset} |
| 265 |
onChange={(value) => |
| 266 |
setAttributes({ stickyContentOffset: value }) |
| 267 |
} |
| 268 |
allowCustomMax |
| 269 |
/> |
| 270 |
) : null} |
| 271 |
</PanelBody> |
| 272 |
<div className="ghostkit-background-controls"> |
| 273 |
<ApplyFilters |
| 274 |
name="ghostkit.editor.controls" |
| 275 |
attribute="background" |
| 276 |
props={props} |
| 277 |
/> |
| 278 |
</div> |
| 279 |
</InspectorControls> |
| 280 |
{background} |
| 281 |
<div {...innerBlocksProps} /> |
| 282 |
</div> |
| 283 |
); |
| 284 |
} |
| 285 |
|