| 1 |
import { InspectorAdvancedControls } from '@wordpress/block-editor' |
| 2 |
import { FormTokenField } from '@wordpress/components' |
| 3 |
import { createHigherOrderComponent } from '@wordpress/compose' |
| 4 |
import { addFilter } from '@wordpress/hooks' |
| 5 |
import { __ } from '@wordpress/i18n' |
| 6 |
import suggestions from '../../../utility-framework/suggestions.json' |
| 7 |
|
| 8 |
function addAttributes(settings) { |
| 9 |
// Add new extUtilities attribute to block settings. |
| 10 |
return { |
| 11 |
...settings, |
| 12 |
attributes: { |
| 13 |
...settings.attributes, |
| 14 |
extUtilities: { |
| 15 |
type: 'array', |
| 16 |
default: [], |
| 17 |
}, |
| 18 |
}, |
| 19 |
} |
| 20 |
} |
| 21 |
|
| 22 |
function addEditProps(settings) { |
| 23 |
const existingGetEditWrapperProps = settings.getEditWrapperProps |
| 24 |
settings.getEditWrapperProps = (attributes) => { |
| 25 |
let props = {} |
| 26 |
|
| 27 |
if (existingGetEditWrapperProps) { |
| 28 |
props = existingGetEditWrapperProps(attributes) |
| 29 |
} |
| 30 |
|
| 31 |
return addSaveProps(props, settings, attributes) |
| 32 |
} |
| 33 |
|
| 34 |
return settings |
| 35 |
} |
| 36 |
|
| 37 |
// Create HOC to add Extendify Utility to Advanced Panel of block. |
| 38 |
const utilityClassEdit = createHigherOrderComponent((BlockEdit) => { |
| 39 |
return function editPanel(props) { |
| 40 |
const classes = props?.attributes?.extUtilities ?? [] |
| 41 |
const suggestionList = suggestions.suggestions.map((s) => { |
| 42 |
// Remove all extra // and . from classnames |
| 43 |
return s.replace('.', '').replace(new RegExp('\\\\', 'g'), '') |
| 44 |
}) |
| 45 |
|
| 46 |
return ( |
| 47 |
<> |
| 48 |
<BlockEdit {...props} /> |
| 49 |
{classes && ( |
| 50 |
<InspectorAdvancedControls> |
| 51 |
<FormTokenField |
| 52 |
label={__('Extendify Utilities', 'extendify')} |
| 53 |
tokenizeOnSpace={true} |
| 54 |
value={classes} |
| 55 |
suggestions={suggestionList} |
| 56 |
onChange={(value) => { |
| 57 |
props.setAttributes({ |
| 58 |
extUtilities: value, |
| 59 |
}) |
| 60 |
}} |
| 61 |
/> |
| 62 |
</InspectorAdvancedControls> |
| 63 |
)} |
| 64 |
</> |
| 65 |
) |
| 66 |
} |
| 67 |
}, 'utilityClassEdit') |
| 68 |
|
| 69 |
function addSaveProps(saveElementProps, blockType, attributes) { |
| 70 |
const generatedClasses = saveElementProps?.className ?? [] |
| 71 |
const classes = attributes?.extUtilities ?? [] |
| 72 |
const additionalClasses = attributes?.className ?? [] |
| 73 |
|
| 74 |
if (!classes || !Object.keys(classes).length) { |
| 75 |
return saveElementProps |
| 76 |
} |
| 77 |
|
| 78 |
// EK seems to be converting string values to objects in some situations |
| 79 |
const normalizeAsArray = (item) => { |
| 80 |
switch (Object.prototype.toString.call(item)) { |
| 81 |
case '[object String]': |
| 82 |
return item.split(' ') |
| 83 |
case '[object Array]': |
| 84 |
return item |
| 85 |
default: |
| 86 |
return [] |
| 87 |
} |
| 88 |
} |
| 89 |
const classesCombined = new Set([ |
| 90 |
...normalizeAsArray(additionalClasses), |
| 91 |
...normalizeAsArray(generatedClasses), |
| 92 |
...normalizeAsArray(classes), |
| 93 |
]) |
| 94 |
|
| 95 |
return Object.assign({}, saveElementProps, { |
| 96 |
className: [...classesCombined].join(' '), |
| 97 |
}) |
| 98 |
} |
| 99 |
|
| 100 |
addFilter( |
| 101 |
'blocks.registerBlockType', |
| 102 |
'extendify/utilities/attributes', |
| 103 |
addAttributes, |
| 104 |
) |
| 105 |
|
| 106 |
addFilter( |
| 107 |
'blocks.registerBlockType', |
| 108 |
'extendify/utilities/addEditProps', |
| 109 |
addEditProps, |
| 110 |
) |
| 111 |
|
| 112 |
addFilter( |
| 113 |
'editor.BlockEdit', |
| 114 |
'extendify/utilities/advancedClassControls', |
| 115 |
utilityClassEdit, |
| 116 |
) |
| 117 |
|
| 118 |
addFilter( |
| 119 |
'blocks.getSaveContent.extraProps', |
| 120 |
'extendify/utilities/extra-props', |
| 121 |
addSaveProps, |
| 122 |
) |
| 123 |
|