| 1 |
import './awb-fallback'; |
| 2 |
|
| 3 |
import { createHigherOrderComponent } from '@wordpress/compose'; |
| 4 |
import { addFilter } from '@wordpress/hooks'; |
| 5 |
|
| 6 |
import getIcon from '../../utils/get-icon'; |
| 7 |
import metadata from './block.json'; |
| 8 |
import edit from './edit'; |
| 9 |
import getBackgroundStyles from './get-background-styles'; |
| 10 |
import save from './save'; |
| 11 |
import transforms from './transforms'; |
| 12 |
|
| 13 |
const { name } = metadata; |
| 14 |
|
| 15 |
export { metadata, name }; |
| 16 |
|
| 17 |
export const settings = { |
| 18 |
icon: getIcon('block-grid', true), |
| 19 |
ghostkit: { |
| 20 |
previewUrl: 'https://www.ghostkit.io/docs/blocks/advanced-columns/', |
| 21 |
customStylesCallback(attributes) { |
| 22 |
const { gap, gapCustom, gapVerticalCustom } = attributes; |
| 23 |
|
| 24 |
const styles = { |
| 25 |
'--gkt-grid__gap': undefined, |
| 26 |
'--gkt-grid__gap-vertical': undefined, |
| 27 |
...getBackgroundStyles(attributes), |
| 28 |
}; |
| 29 |
|
| 30 |
// Custom Gap. |
| 31 |
if (gap === 'custom') { |
| 32 |
if (typeof gapCustom !== 'undefined' && gapCustom !== '') { |
| 33 |
// we need to use `%` unit because of conflict with complex calc() and 0 value. |
| 34 |
const unit = gapCustom ? 'px' : '%'; |
| 35 |
|
| 36 |
styles['--gkt-grid__gap'] = `${gapCustom}${unit}`; |
| 37 |
} |
| 38 |
|
| 39 |
if ( |
| 40 |
typeof gapVerticalCustom !== 'undefined' && |
| 41 |
gapVerticalCustom !== '' |
| 42 |
) { |
| 43 |
// we need to use `%` unit because of conflict with complex calc() and 0 value. |
| 44 |
const unit = gapVerticalCustom ? 'px' : '%'; |
| 45 |
|
| 46 |
styles['--gkt-grid__gap-vertical'] = |
| 47 |
`${gapVerticalCustom}${unit}`; |
| 48 |
} |
| 49 |
} |
| 50 |
|
| 51 |
return styles; |
| 52 |
}, |
| 53 |
customStylesFilter(styles, data, isEditor, attributes) { |
| 54 |
// change custom styles in Editor. |
| 55 |
if (isEditor && attributes?.ghostkit?.id) { |
| 56 |
// background. |
| 57 |
styles = styles.replace( |
| 58 |
new RegExp('> .nk-awb .jarallax-img', 'g'), |
| 59 |
'> .awb-gutenberg-preview-block .jarallax-img' |
| 60 |
); |
| 61 |
} |
| 62 |
return styles; |
| 63 |
}, |
| 64 |
}, |
| 65 |
example: { |
| 66 |
attributes: { |
| 67 |
columns: 2, |
| 68 |
}, |
| 69 |
innerBlocks: [ |
| 70 |
{ |
| 71 |
name: 'ghostkit/grid-column', |
| 72 |
innerBlocks: [ |
| 73 |
{ |
| 74 |
name: 'core/paragraph', |
| 75 |
attributes: { |
| 76 |
content: |
| 77 |
'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Praesent et eros eu felis.', |
| 78 |
}, |
| 79 |
}, |
| 80 |
], |
| 81 |
}, |
| 82 |
{ |
| 83 |
name: 'ghostkit/grid-column', |
| 84 |
innerBlocks: [ |
| 85 |
{ |
| 86 |
name: 'core/paragraph', |
| 87 |
attributes: { |
| 88 |
content: |
| 89 |
'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Praesent et eros eu felis.', |
| 90 |
}, |
| 91 |
}, |
| 92 |
], |
| 93 |
}, |
| 94 |
], |
| 95 |
}, |
| 96 |
edit, |
| 97 |
save, |
| 98 |
transforms, |
| 99 |
}; |
| 100 |
|
| 101 |
/** |
| 102 |
* Add data attribute to hide block from editor when inserting templates. |
| 103 |
* |
| 104 |
* @param {Function} BlockListBlock Original component |
| 105 |
* @return {Function} Wrapped component |
| 106 |
*/ |
| 107 |
export const withClasses = createHigherOrderComponent( |
| 108 |
(BlockListBlock) => |
| 109 |
function (props) { |
| 110 |
const { name: blockName } = props; |
| 111 |
|
| 112 |
if ( |
| 113 |
blockName === 'ghostkit/grid' && |
| 114 |
props.attributes.isTemplatesModalOnly |
| 115 |
) { |
| 116 |
return ( |
| 117 |
<BlockListBlock |
| 118 |
{...props} |
| 119 |
data-ghostkit-grid-templates-modal-only="true" |
| 120 |
/> |
| 121 |
); |
| 122 |
} |
| 123 |
|
| 124 |
return <BlockListBlock {...props} />; |
| 125 |
} |
| 126 |
); |
| 127 |
|
| 128 |
addFilter('editor.BlockListBlock', 'ghostkit/grid/with-classes', withClasses); |
| 129 |
|