| 1 |
import { |
| 2 |
__experimentalToolsPanelItem as ExperimentalToolsPanelItem, |
| 3 |
__stableToolsPanelItem as StableToolsPanelItem, |
| 4 |
} from '@wordpress/components'; |
| 5 |
import { addFilter } from '@wordpress/hooks'; |
| 6 |
import { __ } from '@wordpress/i18n'; |
| 7 |
|
| 8 |
import ResponsiveToggle from '../../../components/responsive-toggle'; |
| 9 |
import ToggleGroup from '../../../components/toggle-group'; |
| 10 |
import useResponsive from '../../../hooks/use-responsive'; |
| 11 |
import { |
| 12 |
addClass, |
| 13 |
getActiveClass, |
| 14 |
hasClass, |
| 15 |
removeClass, |
| 16 |
replaceClass, |
| 17 |
} from '../../../utils/classes-replacer'; |
| 18 |
|
| 19 |
const ToolsPanelItem = StableToolsPanelItem || ExperimentalToolsPanelItem; |
| 20 |
|
| 21 |
import { getBlockSupport, hasBlockSupport } from '@wordpress/blocks'; |
| 22 |
|
| 23 |
/** |
| 24 |
* Get array for Select element. |
| 25 |
* |
| 26 |
* @param {string} screen - screen size |
| 27 |
* |
| 28 |
* @return {Array} array for Select. |
| 29 |
*/ |
| 30 |
const getDefaultDisplay = function (screen = '') { |
| 31 |
return [ |
| 32 |
{ |
| 33 |
label: !screen |
| 34 |
? __('Default', 'ghostkit') |
| 35 |
: __('Inherit', 'ghostkit'), |
| 36 |
value: '', |
| 37 |
}, |
| 38 |
{ |
| 39 |
label: __('Show', 'ghostkit'), |
| 40 |
value: 'block', |
| 41 |
}, |
| 42 |
{ |
| 43 |
label: __('Hide', 'ghostkit'), |
| 44 |
value: 'none', |
| 45 |
}, |
| 46 |
]; |
| 47 |
}; |
| 48 |
|
| 49 |
/** |
| 50 |
* Get current display for selected screen size. |
| 51 |
* |
| 52 |
* @param {string} className - block className. |
| 53 |
* @param {string} screen - name of screen size. |
| 54 |
* |
| 55 |
* @return {string} display value. |
| 56 |
*/ |
| 57 |
function getCurrentDisplay(className, screen) { |
| 58 |
if (!screen) { |
| 59 |
if (hasClass(className, 'ghostkit-d-none')) { |
| 60 |
return 'none'; |
| 61 |
} |
| 62 |
if (hasClass(className, 'ghostkit-d-block')) { |
| 63 |
return 'block'; |
| 64 |
} |
| 65 |
} |
| 66 |
|
| 67 |
return getActiveClass(className, `ghostkit-d-${screen}`, true); |
| 68 |
} |
| 69 |
|
| 70 |
function DisplayScreenSizeTools(props) { |
| 71 |
const { attributes, setAttributes } = props; |
| 72 |
const { className } = attributes; |
| 73 |
|
| 74 |
const { device, allDevices } = useResponsive(); |
| 75 |
|
| 76 |
let hasDisplayScreenSize = false; |
| 77 |
|
| 78 |
['', ...Object.keys(allDevices)].forEach((thisDevice) => { |
| 79 |
hasDisplayScreenSize = |
| 80 |
hasDisplayScreenSize || getCurrentDisplay(className, thisDevice); |
| 81 |
}); |
| 82 |
|
| 83 |
/** |
| 84 |
* Update display object. |
| 85 |
* |
| 86 |
* @param {string} screen - name of screen size. |
| 87 |
* @param {string} val - value for new display. |
| 88 |
*/ |
| 89 |
function updateDisplay(screen, val) { |
| 90 |
let newClassName = className; |
| 91 |
|
| 92 |
if (screen) { |
| 93 |
newClassName = replaceClass( |
| 94 |
newClassName, |
| 95 |
`ghostkit-d-${screen}`, |
| 96 |
val |
| 97 |
); |
| 98 |
} else { |
| 99 |
newClassName = removeClass(newClassName, 'ghostkit-d-none'); |
| 100 |
newClassName = removeClass(newClassName, 'ghostkit-d-block'); |
| 101 |
|
| 102 |
if (val) { |
| 103 |
newClassName = addClass(newClassName, `ghostkit-d-${val}`); |
| 104 |
} |
| 105 |
} |
| 106 |
|
| 107 |
setAttributes({ |
| 108 |
className: newClassName, |
| 109 |
}); |
| 110 |
} |
| 111 |
|
| 112 |
return ( |
| 113 |
<ToolsPanelItem |
| 114 |
label={__('Screen Size', 'ghostkit')} |
| 115 |
hasValue={() => !!hasDisplayScreenSize} |
| 116 |
onDeselect={() => { |
| 117 |
let newClassName = className; |
| 118 |
|
| 119 |
['', ...Object.keys(allDevices)].forEach((thisDevice) => { |
| 120 |
if (thisDevice) { |
| 121 |
newClassName = removeClass( |
| 122 |
newClassName, |
| 123 |
`ghostkit-d-${thisDevice}-none` |
| 124 |
); |
| 125 |
newClassName = removeClass( |
| 126 |
newClassName, |
| 127 |
`ghostkit-d-${thisDevice}-block` |
| 128 |
); |
| 129 |
} else { |
| 130 |
newClassName = removeClass( |
| 131 |
newClassName, |
| 132 |
'ghostkit-d-none' |
| 133 |
); |
| 134 |
newClassName = removeClass( |
| 135 |
newClassName, |
| 136 |
'ghostkit-d-block' |
| 137 |
); |
| 138 |
} |
| 139 |
}); |
| 140 |
|
| 141 |
setAttributes({ |
| 142 |
className: newClassName, |
| 143 |
}); |
| 144 |
}} |
| 145 |
isShownByDefault={false} |
| 146 |
> |
| 147 |
<ToggleGroup |
| 148 |
label={ |
| 149 |
<> |
| 150 |
{__('Screen Size', 'ghostkit')} |
| 151 |
<ResponsiveToggle |
| 152 |
checkActive={(checkMedia) => { |
| 153 |
return ( |
| 154 |
hasClass( |
| 155 |
className, |
| 156 |
`ghostkit-d-${checkMedia}-none` |
| 157 |
) || |
| 158 |
hasClass( |
| 159 |
className, |
| 160 |
`ghostkit-d-${checkMedia}-block` |
| 161 |
) |
| 162 |
); |
| 163 |
}} |
| 164 |
/> |
| 165 |
</> |
| 166 |
} |
| 167 |
value={getCurrentDisplay(className, device)} |
| 168 |
options={getDefaultDisplay(device).map((val) => ({ |
| 169 |
value: val.value, |
| 170 |
label: val.label, |
| 171 |
}))} |
| 172 |
onChange={(value) => { |
| 173 |
updateDisplay(device, value); |
| 174 |
}} |
| 175 |
isBlock |
| 176 |
/> |
| 177 |
</ToolsPanelItem> |
| 178 |
); |
| 179 |
} |
| 180 |
|
| 181 |
addFilter( |
| 182 |
'ghostkit.extension.display.tools', |
| 183 |
'ghostkit/extension/display/tools/screenSize', |
| 184 |
(children, { props }) => { |
| 185 |
const hasDisplayScreenSizeSupport = |
| 186 |
hasBlockSupport(props.name, [ |
| 187 |
'ghostkit', |
| 188 |
'display', |
| 189 |
'screenSize', |
| 190 |
]) || getBlockSupport(props.name, ['ghostkit', 'display']) === true; |
| 191 |
const hasCustomClassNameSupport = hasBlockSupport( |
| 192 |
props.name, |
| 193 |
'customClassName', |
| 194 |
true |
| 195 |
); |
| 196 |
|
| 197 |
if (!hasDisplayScreenSizeSupport || !hasCustomClassNameSupport) { |
| 198 |
return children; |
| 199 |
} |
| 200 |
|
| 201 |
return ( |
| 202 |
<> |
| 203 |
{children} |
| 204 |
<DisplayScreenSizeTools {...props} /> |
| 205 |
</> |
| 206 |
); |
| 207 |
} |
| 208 |
); |
| 209 |
|