| 1 |
import { __ } from "@wordpress/i18n"; |
| 2 |
import { |
| 3 |
InspectorControls, |
| 4 |
MediaUpload, |
| 5 |
MediaUploadCheck, |
| 6 |
} from "@wordpress/block-editor"; |
| 7 |
import { |
| 8 |
PanelBody, |
| 9 |
ToggleControl, |
| 10 |
TextControl, |
| 11 |
Button, |
| 12 |
} from "@wordpress/components"; |
| 13 |
|
| 14 |
// Fonction pour ajouter l'attribut fixedBg aux blocs core/group |
| 15 |
const addFixedBgAttribute = () => { |
| 16 |
if (window.wp && window.wp.hooks && window.wp.hooks.addFilter) { |
| 17 |
window.wp.hooks.addFilter( |
| 18 |
"blocks.registerBlockType", |
| 19 |
"ai-builder/add-fixed-bg-attribute", |
| 20 |
(settings, name) => { |
| 21 |
if (name === "core/group") { |
| 22 |
if (!settings.attributes.fixedBg) { |
| 23 |
settings.attributes.fixedBg = { |
| 24 |
type: "boolean", |
| 25 |
default: false, |
| 26 |
}; |
| 27 |
} |
| 28 |
if (!settings.attributes.fixedBgImage) { |
| 29 |
settings.attributes.fixedBgImage = { |
| 30 |
type: "object", |
| 31 |
default: null, |
| 32 |
}; |
| 33 |
} |
| 34 |
if (!settings.attributes.fixedBgOverlay) { |
| 35 |
settings.attributes.fixedBgOverlay = { |
| 36 |
type: "string", |
| 37 |
default: "rgba(63, 23, 99, 0.8)", |
| 38 |
}; |
| 39 |
} |
| 40 |
} |
| 41 |
return settings; |
| 42 |
} |
| 43 |
); |
| 44 |
} |
| 45 |
}; |
| 46 |
|
| 47 |
// Appliquer le filtre quand WordPress est prêt |
| 48 |
if (window.wp && window.wp.hooks) { |
| 49 |
addFixedBgAttribute(); |
| 50 |
} else { |
| 51 |
// Attendre que WordPress soit chargé |
| 52 |
const checkWp = setInterval(() => { |
| 53 |
if (window.wp && window.wp.hooks) { |
| 54 |
addFixedBgAttribute(); |
| 55 |
clearInterval(checkWp); |
| 56 |
} |
| 57 |
}, 100); |
| 58 |
} |
| 59 |
|
| 60 |
// HOC pour injecter les contrôles Fixed BG |
| 61 |
const withFixedBgControls = (BlockEdit) => { |
| 62 |
return (props) => { |
| 63 |
const { attributes, setAttributes } = props; |
| 64 |
|
| 65 |
// Vérifier si c'est un bloc core/group avec la classe aibui-group-block |
| 66 |
if ( |
| 67 |
props.name === "core/group" && |
| 68 |
attributes.className && |
| 69 |
attributes.className.includes("aibui-group-block") |
| 70 |
) { |
| 71 |
return ( |
| 72 |
<> |
| 73 |
<BlockEdit {...props} /> |
| 74 |
<InspectorControls> |
| 75 |
<PanelBody title={__("Fixed Background Settings", "ai-builder")}> |
| 76 |
<ToggleControl |
| 77 |
label={__("Enable Fixed Background", "ai-builder")} |
| 78 |
checked={attributes.fixedBg || false} |
| 79 |
onChange={(value) => setAttributes({ fixedBg: value })} |
| 80 |
help={__( |
| 81 |
"Enable fixed background with shadow effects", |
| 82 |
"ai-builder" |
| 83 |
)} |
| 84 |
/> |
| 85 |
|
| 86 |
{attributes.fixedBg && ( |
| 87 |
<> |
| 88 |
<MediaUploadCheck> |
| 89 |
<MediaUpload |
| 90 |
onSelect={(media) => |
| 91 |
setAttributes({ fixedBgImage: media }) |
| 92 |
} |
| 93 |
allowedTypes={["image"]} |
| 94 |
value={attributes.fixedBgImage?.id} |
| 95 |
render={({ open }) => ( |
| 96 |
<div> |
| 97 |
<Button |
| 98 |
onClick={open} |
| 99 |
variant="secondary" |
| 100 |
style={{ marginBottom: "10px" }} |
| 101 |
> |
| 102 |
{attributes.fixedBgImage |
| 103 |
? __("Change Background Image", "ai-builder") |
| 104 |
: __("Select Background Image", "ai-builder")} |
| 105 |
</Button> |
| 106 |
{attributes.fixedBgImage && ( |
| 107 |
<div> |
| 108 |
<img |
| 109 |
src={ |
| 110 |
attributes.fixedBgImage.sizes?.medium?.url || |
| 111 |
attributes.fixedBgImage.url |
| 112 |
} |
| 113 |
alt={attributes.fixedBgImage.alt} |
| 114 |
style={{ |
| 115 |
width: "100%", |
| 116 |
height: "auto", |
| 117 |
borderRadius: "4px", |
| 118 |
}} |
| 119 |
/> |
| 120 |
<Button |
| 121 |
onClick={() => |
| 122 |
setAttributes({ fixedBgImage: null }) |
| 123 |
} |
| 124 |
variant="link" |
| 125 |
isDestructive |
| 126 |
style={{ marginTop: "5px" }} |
| 127 |
> |
| 128 |
{__("Remove Image", "ai-builder")} |
| 129 |
</Button> |
| 130 |
</div> |
| 131 |
)} |
| 132 |
</div> |
| 133 |
)} |
| 134 |
/> |
| 135 |
</MediaUploadCheck> |
| 136 |
|
| 137 |
<TextControl |
| 138 |
label={__("Overlay Color", "ai-builder")} |
| 139 |
value={attributes.fixedBgOverlay || "rgba(63, 23, 99, 0.8)"} |
| 140 |
onChange={(value) => |
| 141 |
setAttributes({ fixedBgOverlay: value }) |
| 142 |
} |
| 143 |
help={__( |
| 144 |
"Color and opacity for the overlay effect (e.g., rgba(63, 23, 99, 0.8))", |
| 145 |
"ai-builder" |
| 146 |
)} |
| 147 |
/> |
| 148 |
</> |
| 149 |
)} |
| 150 |
</PanelBody> |
| 151 |
</InspectorControls> |
| 152 |
</> |
| 153 |
); |
| 154 |
} |
| 155 |
|
| 156 |
return <BlockEdit {...props} />; |
| 157 |
}; |
| 158 |
}; |
| 159 |
|
| 160 |
// Enregistrer le HOC quand WordPress est prêt |
| 161 |
const registerFixedBgControls = () => { |
| 162 |
if (window.wp && window.wp.hooks && window.wp.hooks.addFilter) { |
| 163 |
window.wp.hooks.addFilter( |
| 164 |
"editor.BlockEdit", |
| 165 |
"ai-builder/with-fixed-bg-controls", |
| 166 |
withFixedBgControls |
| 167 |
); |
| 168 |
} |
| 169 |
}; |
| 170 |
|
| 171 |
if (window.wp && window.wp.hooks) { |
| 172 |
registerFixedBgControls(); |
| 173 |
} else { |
| 174 |
// Attendre que WordPress soit chargé |
| 175 |
const checkWpForControls = setInterval(() => { |
| 176 |
if (window.wp && window.wp.hooks) { |
| 177 |
registerFixedBgControls(); |
| 178 |
clearInterval(checkWpForControls); |
| 179 |
} |
| 180 |
}, 100); |
| 181 |
} |
| 182 |
|
| 183 |
// Fonction pour ajouter la variation du bloc core/group |
| 184 |
const addFixedBgGroupVariation = () => { |
| 185 |
try { |
| 186 |
if (window.wp && window.wp.blocks && window.wp.blocks.addBlockVariation) { |
| 187 |
window.wp.blocks.addBlockVariation("core/group", { |
| 188 |
name: "fixed-bg-group", |
| 189 |
title: __("Fixed Background Group", "ai-builder"), |
| 190 |
description: __( |
| 191 |
"A group block with fixed background and shadow effects", |
| 192 |
"ai-builder" |
| 193 |
), |
| 194 |
icon: "cover-image", |
| 195 |
scope: ["inserter"], |
| 196 |
attributes: { |
| 197 |
className: "aibui-group-block", |
| 198 |
fixedBg: false, |
| 199 |
fixedBgImage: null, |
| 200 |
fixedBgOverlay: "rgba(63, 23, 99, 0.8)", |
| 201 |
}, |
| 202 |
isActive: (blockAttributes) => |
| 203 |
blockAttributes.className && |
| 204 |
blockAttributes.className.includes("aibui-group-block"), |
| 205 |
}); |
| 206 |
console.log("Fixed Background Group variation registered successfully"); |
| 207 |
} |
| 208 |
} catch (error) { |
| 209 |
console.error("Error registering Fixed Background Group variation:", error); |
| 210 |
} |
| 211 |
}; |
| 212 |
|
| 213 |
// Essayer d'ajouter la variation quand WordPress est prêt |
| 214 |
const registerFixedBgVariation = () => { |
| 215 |
if (window.wp && window.wp.blocks && window.wp.blocks.addBlockVariation) { |
| 216 |
addFixedBgGroupVariation(); |
| 217 |
} |
| 218 |
}; |
| 219 |
|
| 220 |
if (window.wp && window.wp.blocks) { |
| 221 |
registerFixedBgVariation(); |
| 222 |
} else { |
| 223 |
// Attendre que WordPress soit chargé |
| 224 |
const checkWpForVariation = setInterval(() => { |
| 225 |
if (window.wp && window.wp.blocks && window.wp.blocks.addBlockVariation) { |
| 226 |
addFixedBgGroupVariation(); |
| 227 |
clearInterval(checkWpForVariation); |
| 228 |
} |
| 229 |
}, 100); |
| 230 |
} |
| 231 |
|
| 232 |
// Essayer aussi après un délai plus long |
| 233 |
setTimeout(() => { |
| 234 |
if (window.wp && window.wp.blocks && window.wp.blocks.addBlockVariation) { |
| 235 |
addFixedBgGroupVariation(); |
| 236 |
} |
| 237 |
}, 2000); |
| 238 |
|
| 239 |
export default {}; |
| 240 |
|