| 1 |
import './pro-transforms'; |
| 2 |
|
| 3 |
import { throttle } from 'throttle-debounce'; |
| 4 |
|
| 5 |
import { InspectorControls } from '@wordpress/block-editor'; |
| 6 |
import { hasBlockSupport } from '@wordpress/blocks'; |
| 7 |
import { |
| 8 |
__experimentalToolsPanel as ExperimentalToolsPanel, |
| 9 |
__stableToolsPanel as StableToolsPanel, |
| 10 |
} from '@wordpress/components'; |
| 11 |
import { createHigherOrderComponent } from '@wordpress/compose'; |
| 12 |
import { useEffect, useRef } from '@wordpress/element'; |
| 13 |
import { addFilter, applyFilters } from '@wordpress/hooks'; |
| 14 |
import { __ } from '@wordpress/i18n'; |
| 15 |
|
| 16 |
import ApplyFilters from '../../components/apply-filters'; |
| 17 |
import useStyles from '../../hooks/use-styles'; |
| 18 |
import { addClass, hasClass, removeClass } from '../../utils/classes-replacer'; |
| 19 |
import getIcon from '../../utils/get-icon'; |
| 20 |
import { EXTENSIONS } from '../constants'; |
| 21 |
|
| 22 |
const ToolsPanel = StableToolsPanel || ExperimentalToolsPanel; |
| 23 |
|
| 24 |
const allProps = EXTENSIONS.transform.styles; |
| 25 |
|
| 26 |
const keyExists = (obj, key) => { |
| 27 |
let result = false; |
| 28 |
|
| 29 |
if (typeof obj === 'object') { |
| 30 |
if (key in obj) { |
| 31 |
result = true; |
| 32 |
} else { |
| 33 |
Object.keys(obj).forEach((k) => { |
| 34 |
result = result || keyExists(obj[k], key); |
| 35 |
}); |
| 36 |
} |
| 37 |
} |
| 38 |
|
| 39 |
return result; |
| 40 |
}; |
| 41 |
|
| 42 |
/** |
| 43 |
* Add inspector controls. |
| 44 |
* |
| 45 |
* @param original |
| 46 |
* @param root0 |
| 47 |
* @param root0.props |
| 48 |
*/ |
| 49 |
function GhostKitExtensionTransformInspector(original, { props }) { |
| 50 |
const { name } = props; |
| 51 |
|
| 52 |
const hasTransformSupport = hasBlockSupport(name, [ |
| 53 |
'ghostkit', |
| 54 |
'transform', |
| 55 |
]); |
| 56 |
|
| 57 |
if (!hasTransformSupport) { |
| 58 |
return original; |
| 59 |
} |
| 60 |
|
| 61 |
const { resetStyles } = useStyles(props); |
| 62 |
|
| 63 |
return ( |
| 64 |
<> |
| 65 |
{original} |
| 66 |
<InspectorControls group="styles"> |
| 67 |
<ToolsPanel |
| 68 |
label={ |
| 69 |
<> |
| 70 |
<span className="ghostkit-ext-icon"> |
| 71 |
{getIcon('extension-transform')} |
| 72 |
</span> |
| 73 |
<span>{__('Transform', 'ghostkit')}</span> |
| 74 |
</> |
| 75 |
} |
| 76 |
resetAll={() => { |
| 77 |
resetStyles(allProps, true, ['', '&:hover']); |
| 78 |
}} |
| 79 |
> |
| 80 |
<div className="ghostkit-tools-panel-transform"> |
| 81 |
<ApplyFilters |
| 82 |
name="ghostkit.extension.transform.tools" |
| 83 |
props={props} |
| 84 |
/> |
| 85 |
</div> |
| 86 |
</ToolsPanel> |
| 87 |
</InspectorControls> |
| 88 |
</> |
| 89 |
); |
| 90 |
} |
| 91 |
|
| 92 |
function CustomClassComponent(props) { |
| 93 |
const { setAttributes, attributes } = props; |
| 94 |
|
| 95 |
const { getStyles } = useStyles(props); |
| 96 |
|
| 97 |
let hasTransform = false; |
| 98 |
|
| 99 |
const styles = getStyles(); |
| 100 |
allProps.forEach((transformProp) => { |
| 101 |
hasTransform = hasTransform || keyExists(styles, transformProp); |
| 102 |
}); |
| 103 |
|
| 104 |
function onUpdate() { |
| 105 |
let { className } = attributes; |
| 106 |
|
| 107 |
const allowTransformClassName = applyFilters( |
| 108 |
'ghostkit.extension.transform.allowClassName', |
| 109 |
hasTransform, |
| 110 |
props |
| 111 |
); |
| 112 |
const hasTransformClassName = hasClass( |
| 113 |
className, |
| 114 |
'ghostkit-has-transform' |
| 115 |
); |
| 116 |
|
| 117 |
if (allowTransformClassName && !hasTransformClassName) { |
| 118 |
className = addClass(className, 'ghostkit-has-transform'); |
| 119 |
|
| 120 |
setAttributes({ className }); |
| 121 |
} else if (!allowTransformClassName && hasTransformClassName) { |
| 122 |
className = removeClass(className, 'ghostkit-has-transform'); |
| 123 |
|
| 124 |
setAttributes({ className }); |
| 125 |
} |
| 126 |
} |
| 127 |
|
| 128 |
const onUpdateThrottle = throttle(60, onUpdate); |
| 129 |
|
| 130 |
const didMountRef = useRef(false); |
| 131 |
|
| 132 |
useEffect(() => { |
| 133 |
// Did update. |
| 134 |
if (didMountRef.current) { |
| 135 |
onUpdateThrottle(); |
| 136 |
|
| 137 |
// Did mount. |
| 138 |
} else { |
| 139 |
didMountRef.current = true; |
| 140 |
|
| 141 |
onUpdate(true); |
| 142 |
} |
| 143 |
// eslint-disable-next-line react-hooks/exhaustive-deps |
| 144 |
}, [attributes]); |
| 145 |
|
| 146 |
return null; |
| 147 |
} |
| 148 |
|
| 149 |
/** |
| 150 |
* Override the default edit UI to include a new block inspector control for |
| 151 |
* assigning the custom styles if needed. |
| 152 |
* |
| 153 |
* @param {Function | Component} BlockEdit Original component. |
| 154 |
* |
| 155 |
* @return {string} Wrapped component. |
| 156 |
*/ |
| 157 |
const withNewAttrs = createHigherOrderComponent( |
| 158 |
(BlockEdit) => |
| 159 |
function (props) { |
| 160 |
return ( |
| 161 |
<> |
| 162 |
<BlockEdit {...props} /> |
| 163 |
<CustomClassComponent {...props} /> |
| 164 |
</> |
| 165 |
); |
| 166 |
}, |
| 167 |
'withNewAttrs' |
| 168 |
); |
| 169 |
|
| 170 |
// Init filters. |
| 171 |
addFilter( |
| 172 |
'ghostkit.editor.extensions', |
| 173 |
'ghostkit/extension/transform/inspector', |
| 174 |
GhostKitExtensionTransformInspector, |
| 175 |
15 |
| 176 |
); |
| 177 |
addFilter( |
| 178 |
'editor.BlockEdit', |
| 179 |
'ghostkit/extension/transform/classname', |
| 180 |
withNewAttrs |
| 181 |
); |
| 182 |
|