| 1 |
import { InspectorControls } from '@wordpress/block-editor'; |
| 2 |
import { PanelBody } from '@wordpress/components'; |
| 3 |
import { createHigherOrderComponent } from '@wordpress/compose'; |
| 4 |
import { addFilter } from '@wordpress/hooks'; |
| 5 |
import { __ } from '@wordpress/i18n'; |
| 6 |
|
| 7 |
import RangeControl from '../../components/range-control'; |
| 8 |
import ResponsiveToggle from '../../components/responsive-toggle'; |
| 9 |
import useResponsive from '../../hooks/use-responsive'; |
| 10 |
import { |
| 11 |
addClass, |
| 12 |
getActiveClass, |
| 13 |
hasClass, |
| 14 |
removeClass, |
| 15 |
replaceClass, |
| 16 |
} from '../../utils/classes-replacer'; |
| 17 |
|
| 18 |
const COLUMNS_COUNT_MAX = 6; |
| 19 |
|
| 20 |
/** |
| 21 |
* Get current columns count for selected screen size. |
| 22 |
* |
| 23 |
* @param {string} className - block className. |
| 24 |
* @param {string} screen - name of screen size. |
| 25 |
* |
| 26 |
* @return {string} columns value. |
| 27 |
*/ |
| 28 |
function getCurrentColumns(className, screen) { |
| 29 |
if (!screen) { |
| 30 |
for (let k = 1; COLUMNS_COUNT_MAX >= k; k += 1) { |
| 31 |
if (hasClass(className, `ghostkit-paragraph-columns-${k}`)) { |
| 32 |
return `${k}`; |
| 33 |
} |
| 34 |
} |
| 35 |
} |
| 36 |
|
| 37 |
return getActiveClass( |
| 38 |
className, |
| 39 |
`ghostkit-paragraph-columns-${screen}`, |
| 40 |
true |
| 41 |
); |
| 42 |
} |
| 43 |
|
| 44 |
/** |
| 45 |
* Paragraph Columns |
| 46 |
* |
| 47 |
* @param props |
| 48 |
*/ |
| 49 |
function GhostKitParagraphColumns(props) { |
| 50 |
const { attributes, setAttributes } = props; |
| 51 |
const { className } = attributes; |
| 52 |
|
| 53 |
const { device } = useResponsive(); |
| 54 |
|
| 55 |
/** |
| 56 |
* Update columns count class. |
| 57 |
* |
| 58 |
* @param {string} screen - name of screen size. |
| 59 |
* @param {string} val - value for columns count. |
| 60 |
*/ |
| 61 |
function updateColumns(screen, val) { |
| 62 |
let newClassName = className; |
| 63 |
|
| 64 |
if (screen) { |
| 65 |
newClassName = replaceClass( |
| 66 |
newClassName, |
| 67 |
`ghostkit-paragraph-columns-${screen}`, |
| 68 |
val |
| 69 |
); |
| 70 |
} else { |
| 71 |
for (let k = 1; COLUMNS_COUNT_MAX >= k; k += 1) { |
| 72 |
newClassName = removeClass( |
| 73 |
newClassName, |
| 74 |
`ghostkit-paragraph-columns-${k}` |
| 75 |
); |
| 76 |
} |
| 77 |
|
| 78 |
if (val) { |
| 79 |
newClassName = addClass( |
| 80 |
newClassName, |
| 81 |
`ghostkit-paragraph-columns-${val}` |
| 82 |
); |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
setAttributes({ |
| 87 |
className: newClassName, |
| 88 |
}); |
| 89 |
} |
| 90 |
|
| 91 |
// add new display controls. |
| 92 |
return ( |
| 93 |
<InspectorControls> |
| 94 |
<PanelBody> |
| 95 |
<RangeControl |
| 96 |
label={ |
| 97 |
<> |
| 98 |
{__('Columns Count', 'ghostkit')} |
| 99 |
<ResponsiveToggle |
| 100 |
checkActive={(checkMedia) => { |
| 101 |
return !!getCurrentColumns( |
| 102 |
className, |
| 103 |
checkMedia |
| 104 |
); |
| 105 |
}} |
| 106 |
/> |
| 107 |
</> |
| 108 |
} |
| 109 |
value={parseInt(getCurrentColumns(className, device), 10)} |
| 110 |
onChange={(value) => updateColumns(device, value)} |
| 111 |
min={1} |
| 112 |
max={COLUMNS_COUNT_MAX} |
| 113 |
/> |
| 114 |
</PanelBody> |
| 115 |
</InspectorControls> |
| 116 |
); |
| 117 |
} |
| 118 |
|
| 119 |
/** |
| 120 |
* Override the default edit UI to include a new block inspector control for |
| 121 |
* assigning the custom display if needed. |
| 122 |
* |
| 123 |
* @param {Function | Component} BlockEdit Original component. |
| 124 |
* |
| 125 |
* @return {string} Wrapped component. |
| 126 |
*/ |
| 127 |
const withInspectorControl = createHigherOrderComponent((OriginalComponent) => { |
| 128 |
function GhostKitParagraphWrapper(props) { |
| 129 |
const { name } = props; |
| 130 |
|
| 131 |
if (name !== 'core/paragraph') { |
| 132 |
return <OriginalComponent {...props} />; |
| 133 |
} |
| 134 |
|
| 135 |
// add new display controls. |
| 136 |
return ( |
| 137 |
<> |
| 138 |
<OriginalComponent {...props} /> |
| 139 |
<GhostKitParagraphColumns {...props} /> |
| 140 |
</> |
| 141 |
); |
| 142 |
} |
| 143 |
|
| 144 |
return GhostKitParagraphWrapper; |
| 145 |
}, 'withInspectorControl'); |
| 146 |
|
| 147 |
// Init filters. |
| 148 |
addFilter( |
| 149 |
'editor.BlockEdit', |
| 150 |
'ghostkit/core-paragraph/additional-attributes', |
| 151 |
withInspectorControl, |
| 152 |
9 |
| 153 |
); |
| 154 |
|