button-container.js
2 years ago
button.js
2 years ago
container.js
2 years ago
default.js
2 years ago
grid.js
3 years ago
headline.js
2 years ago
image.js
2 years ago
index.js
2 years ago
index.js
138 lines
| 1 | import { createContext } from '@wordpress/element'; |
| 2 | import defaultContext from './default'; |
| 3 | import headlineContext from './headline'; |
| 4 | import buttonContainerContext from './button-container'; |
| 5 | import buttonContext from './button'; |
| 6 | import imageContext from './image'; |
| 7 | import gridContext from './grid'; |
| 8 | import containerContext from './container'; |
| 9 | import getTypographyAttributes from '../extend/inspector-control/controls/typography/attributes'; |
| 10 | import getSpacingAttributes from '../extend/inspector-control/controls/spacing/attributes'; |
| 11 | import getColorsAttributes from '../extend/inspector-control/controls/colors/attributes'; |
| 12 | import getIconAttributes from '../extend/inspector-control/controls/icon/attributes'; |
| 13 | import getBackgroundGradientAttributes from '../extend/inspector-control/controls/background-panel/attributes'; |
| 14 | import getLayoutAttributes from '../extend/inspector-control/controls/layout/attributes'; |
| 15 | import getSizingAttributes from '../extend/inspector-control/controls/sizing/attributes'; |
| 16 | import getFlexChildAttributes from '../extend/inspector-control/controls/flex-child-panel/attributes'; |
| 17 | import { addFilter, applyFilters } from '@wordpress/hooks'; |
| 18 | import getBorderAttributes from '../extend/inspector-control/controls/borders/attributes'; |
| 19 | |
| 20 | /** |
| 21 | * The BlockContext represents the layer to build the block components. |
| 22 | * |
| 23 | * @type {Object<{hasResponsiveTabs: boolean, hasTypography: boolean}>} |
| 24 | */ |
| 25 | const BlockContext = createContext( defaultContext ); |
| 26 | |
| 27 | /** |
| 28 | * Given a block name return the correct context. |
| 29 | * |
| 30 | * @param {string} blockName The block name. |
| 31 | * @return {Object} The block context. |
| 32 | */ |
| 33 | export function getBlockContext( blockName ) { |
| 34 | return { |
| 35 | 'generateblocks/headline': headlineContext, |
| 36 | 'generateblocks/button-container': buttonContainerContext, |
| 37 | 'generateblocks/button': buttonContext, |
| 38 | 'generateblocks/image': imageContext, |
| 39 | 'generateblocks/grid': gridContext, |
| 40 | 'generateblocks/container': containerContext, |
| 41 | }[ blockName ]; |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * High order component to wrap a component within the BlockContext provider. |
| 46 | * |
| 47 | * @param {function(*)} WrappedComponent The component to add the context provider. |
| 48 | * @return {function(*)} The component with context provider. |
| 49 | */ |
| 50 | export const withBlockContext = ( WrappedComponent ) => ( ( props ) => { |
| 51 | const blockContext = applyFilters( |
| 52 | 'generateblocks.editor.blockContext', |
| 53 | getBlockContext( props.name ), |
| 54 | props |
| 55 | ); |
| 56 | |
| 57 | return ( |
| 58 | <BlockContext.Provider value={ blockContext }> |
| 59 | <WrappedComponent { ...props } /> |
| 60 | </BlockContext.Provider> |
| 61 | ); |
| 62 | } ); |
| 63 | |
| 64 | /** |
| 65 | * GenerateBlocks default context rules. |
| 66 | * |
| 67 | * @param {Object} blockContext The block context. |
| 68 | * @param {Object} props The component props. |
| 69 | * @return {Object} The block context with default rules. |
| 70 | */ |
| 71 | function defaultBlockContextRules( blockContext, props ) { |
| 72 | const isInQueryLoop = 'undefined' !== typeof props.context[ 'generateblocks/queryId' ]; |
| 73 | const blockName = props.name; |
| 74 | const clientId = props.clientId; |
| 75 | const deviceType = props.deviceType; |
| 76 | |
| 77 | return Object.assign( {}, blockContext, { isInQueryLoop, blockName, clientId, deviceType } ); |
| 78 | } |
| 79 | |
| 80 | addFilter( |
| 81 | 'generateblocks.editor.blockContext', |
| 82 | 'generateblocks/editor/blockContext/default', |
| 83 | defaultBlockContextRules |
| 84 | ); |
| 85 | |
| 86 | /** |
| 87 | * Returns the attributes list based on the context. |
| 88 | * |
| 89 | * @param {Object} blockAttributes The block attributes. |
| 90 | * @param {Object} context The context. |
| 91 | * @param {Object} defaults The default values. |
| 92 | * |
| 93 | * @return {Object} The complete list of attributes. |
| 94 | */ |
| 95 | export function getBlockAttributes( blockAttributes, context, defaults ) { |
| 96 | let attributes = Object.assign( {}, blockAttributes ); |
| 97 | |
| 98 | if ( context.supports.layout.enabled ) { |
| 99 | attributes = Object.assign( {}, attributes, getLayoutAttributes( defaults ) ); |
| 100 | } |
| 101 | |
| 102 | if ( context.supports.flexChildPanel.enabled ) { |
| 103 | attributes = Object.assign( {}, attributes, getFlexChildAttributes( defaults ) ); |
| 104 | } |
| 105 | |
| 106 | if ( context.supports.sizingPanel.enabled ) { |
| 107 | attributes = Object.assign( {}, attributes, getSizingAttributes( defaults ) ); |
| 108 | } |
| 109 | |
| 110 | if ( context.supports.typography.enabled ) { |
| 111 | attributes = Object.assign( {}, attributes, getTypographyAttributes( defaults ) ); |
| 112 | } |
| 113 | |
| 114 | if ( context.supports.spacing.enabled ) { |
| 115 | attributes = Object.assign( {}, attributes, getSpacingAttributes( defaults ) ); |
| 116 | } |
| 117 | |
| 118 | if ( context.supports.borders.enabled ) { |
| 119 | attributes = Object.assign( {}, attributes, getBorderAttributes() ); |
| 120 | } |
| 121 | |
| 122 | if ( context.supports.colors.enabled ) { |
| 123 | attributes = Object.assign( {}, attributes, getColorsAttributes( defaults ) ); |
| 124 | } |
| 125 | |
| 126 | if ( context.supports.backgroundPanel.enabled ) { |
| 127 | attributes = Object.assign( {}, attributes, getBackgroundGradientAttributes( defaults ) ); |
| 128 | } |
| 129 | |
| 130 | if ( context.supports.icon.enabled ) { |
| 131 | attributes = Object.assign( {}, attributes, getIconAttributes( defaults ) ); |
| 132 | } |
| 133 | |
| 134 | return attributes; |
| 135 | } |
| 136 | |
| 137 | export default BlockContext; |
| 138 |