frontblocks-edge-alignment-frontend.js
7 months ago
frontblocks-edge-alignment-option.jsx
1 month ago
frontblocks-edge-alignment.css
1 month ago
frontblocks-edge-alignment.js
1 month ago
frontblocks-edge-alignment.js
135 lines
| 1 | "use strict"; |
| 2 | |
| 3 | function _extends() { return _extends = Object.assign ? Object.assign.bind() : function (n) { for (var e = 1; e < arguments.length; e++) { var t = arguments[e]; for (var r in t) ({}).hasOwnProperty.call(t, r) && (n[r] = t[r]); } return n; }, _extends.apply(null, arguments); } |
| 4 | function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); } |
| 5 | var addFilter = wp.hooks.addFilter; |
| 6 | var createHigherOrderComponent = wp.compose.createHigherOrderComponent; |
| 7 | var Fragment = wp.element.Fragment; |
| 8 | var InspectorControls = wp.blockEditor.InspectorControls; |
| 9 | var _wp$components = wp.components, |
| 10 | PanelBody = _wp$components.PanelBody, |
| 11 | SelectControl = _wp$components.SelectControl; |
| 12 | var __ = wp.i18n.__; |
| 13 | var GB_BLOCKS = ['generateblocks/container', 'generateblocks/element']; |
| 14 | var NATIVE_BLOCKS = ['core/group', 'core/columns']; |
| 15 | var ALL_SUPPORTED_BLOCKS = [].concat(GB_BLOCKS, NATIVE_BLOCKS); |
| 16 | |
| 17 | /** |
| 18 | * Add custom attributes to supported blocks. |
| 19 | */ |
| 20 | function addEdgeAlignmentAttributes(settings, name) { |
| 21 | if (!ALL_SUPPORTED_BLOCKS.includes(name)) { |
| 22 | return settings; |
| 23 | } |
| 24 | settings.attributes = Object.assign(settings.attributes, { |
| 25 | frblEdgeAlignment: { |
| 26 | type: 'string', |
| 27 | default: '' |
| 28 | } |
| 29 | }); |
| 30 | return settings; |
| 31 | } |
| 32 | addFilter('blocks.registerBlockType', 'frontblocks/edge-alignment-attributes', addEdgeAlignmentAttributes); |
| 33 | |
| 34 | /** |
| 35 | * Check if a GenerateBlocks container uses the global container width. |
| 36 | * Only containers with var(--gb-container-width) should have edge alignment. |
| 37 | */ |
| 38 | function usesGlobalMaxWidth(attributes) { |
| 39 | if (!attributes.styles || _typeof(attributes.styles) !== 'object') { |
| 40 | return false; |
| 41 | } |
| 42 | if (attributes.styles.maxWidth && attributes.styles.maxWidth.includes('var(--gb-container-width)')) { |
| 43 | if (attributes.styles.marginLeft === 'auto' && attributes.styles.marginRight === 'auto') { |
| 44 | return true; |
| 45 | } |
| 46 | } |
| 47 | return false; |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | * Check if a native block uses a constrained (centered, max-width) layout. |
| 52 | * Matches core/group and core/columns with constrained layout or inherited layout. |
| 53 | */ |
| 54 | function usesConstrainedLayout(attributes) { |
| 55 | // No explicit layout — inherits from theme, which is typically constrained. |
| 56 | if (!attributes.layout) { |
| 57 | return true; |
| 58 | } |
| 59 | return attributes.layout.type === 'constrained'; |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * Add edge alignment controls to supported block inspector panels. |
| 64 | */ |
| 65 | var withEdgeAlignmentControls = createHigherOrderComponent(function (BlockEdit) { |
| 66 | return function (props) { |
| 67 | if (!ALL_SUPPORTED_BLOCKS.includes(props.name)) { |
| 68 | return /*#__PURE__*/React.createElement(BlockEdit, props); |
| 69 | } |
| 70 | var attributes = props.attributes, |
| 71 | setAttributes = props.setAttributes; |
| 72 | var frblEdgeAlignment = attributes.frblEdgeAlignment; |
| 73 | var isGBBlock = GB_BLOCKS.includes(props.name); |
| 74 | |
| 75 | // Guard: only show panel when the block uses a centered/constrained layout. |
| 76 | var shouldShowPanel = isGBBlock ? usesGlobalMaxWidth(attributes) : usesConstrainedLayout(attributes); |
| 77 | if (!shouldShowPanel) { |
| 78 | return /*#__PURE__*/React.createElement(BlockEdit, props); |
| 79 | } |
| 80 | return /*#__PURE__*/React.createElement(Fragment, null, /*#__PURE__*/React.createElement(BlockEdit, props), /*#__PURE__*/React.createElement(InspectorControls, null, /*#__PURE__*/React.createElement(PanelBody, { |
| 81 | title: __('FrontBlocks Edge Alignment', 'frontblocks'), |
| 82 | initialOpen: false, |
| 83 | className: "frbl-edge-alignment-panel" |
| 84 | }, /*#__PURE__*/React.createElement("p", { |
| 85 | className: "frbl-edge-alignment-description" |
| 86 | }, __('Remove padding from one side to create an edge-to-edge effect. Perfect for asymmetric layouts where content extends to the browser edge on one side.', 'frontblocks')), /*#__PURE__*/React.createElement(SelectControl, { |
| 87 | label: __('Align to Edge', 'frontblocks'), |
| 88 | value: frblEdgeAlignment, |
| 89 | options: [{ |
| 90 | label: __('None', 'frontblocks'), |
| 91 | value: '' |
| 92 | }, { |
| 93 | label: __('Remove Left Padding', 'frontblocks'), |
| 94 | value: 'left' |
| 95 | }, { |
| 96 | label: __('Remove Right Padding', 'frontblocks'), |
| 97 | value: 'right' |
| 98 | }], |
| 99 | onChange: function onChange(value) { |
| 100 | return setAttributes({ |
| 101 | frblEdgeAlignment: value |
| 102 | }); |
| 103 | }, |
| 104 | help: __('Choose which side should extend to the browser edge.', 'frontblocks') |
| 105 | })))); |
| 106 | }; |
| 107 | }, 'withEdgeAlignmentControls'); |
| 108 | addFilter('editor.BlockEdit', 'frontblocks/edge-alignment-controls', withEdgeAlignmentControls); |
| 109 | |
| 110 | /** |
| 111 | * Add visual feedback classes in the editor. |
| 112 | */ |
| 113 | var addEdgeAlignmentClass = createHigherOrderComponent(function (BlockListBlock) { |
| 114 | return function (props) { |
| 115 | if (!ALL_SUPPORTED_BLOCKS.includes(props.name)) { |
| 116 | return /*#__PURE__*/React.createElement(BlockListBlock, props); |
| 117 | } |
| 118 | var attributes = props.attributes; |
| 119 | var frblEdgeAlignment = attributes.frblEdgeAlignment; |
| 120 | var additionalClasses = ''; |
| 121 | if (frblEdgeAlignment === 'left') { |
| 122 | additionalClasses = ' frbl-edge-left'; |
| 123 | } else if (frblEdgeAlignment === 'right') { |
| 124 | additionalClasses = ' frbl-edge-right'; |
| 125 | } |
| 126 | if (additionalClasses) { |
| 127 | return /*#__PURE__*/React.createElement(BlockListBlock, _extends({}, props, { |
| 128 | className: props.className + additionalClasses |
| 129 | })); |
| 130 | } |
| 131 | return /*#__PURE__*/React.createElement(BlockListBlock, props); |
| 132 | }; |
| 133 | }, 'addEdgeAlignmentClass'); |
| 134 | addFilter('editor.BlockListBlock', 'frontblocks/edge-alignment-class', addEdgeAlignmentClass); |
| 135 |