I am trying to configure the GrapesJS builder with custom blocks. This possibility is mentioned in the docs:
GrapesJS comes with a set of built-in blocks, in this way you’re able to build your templates faster. If the default set isn’t enough you can always add your own custom blocks.
Unfortunately, I was not able to find any more information on how to approach this. Here is a short tutorial on adding custom code block, but I am not sure how related this is to my problem. I also found a message on Slack that asked about adding custom blocks through a template.
Obviously, I can fork Mautic and build it into the GrapheJS plugin directly, but I was wondering what would be the least intrusive way of doing it.
Any help is appreciated!
Your software
My Mautic version is: 4
My PHP version is: 7.4
There were additional requirements that made that approach infeasible. In particular, we wanted to give our designers full control, meaning they needed to be able to upload block definitions themselves. Our approach looks like this:
Use a one-time patch for the Mautic GrapesJS plugin.
Upload block definitions within the theme archives using the standard GUI.
// Get the GrapesJS editor.
const editor = ...; // depends on snippet location
// Find the theme ID from the selection field.
const themeSelect = document.getElementById("emailform_template");
if (themeSelect) {
const themeName = themeSelect.value;
// Load the blocks.json that contains the GrapeJS block config.
const url = `/themes/${themeName}/blocks.json`;
fetch(url).then((res) => res.json()).then((blocks) => {
// Replace default blocks with loaded blocks.
const bm = editor.BlockManager;
bm.getAll().reset();
for (const id in blocks) {
bm.add(id, blocks[id]);
}
}).catch(() => {
// Ignore if there is not blocks.json for this theme.
});
}
Upload block definitions
Themes can now optionally include a blocks.json file that looks something like this:
How can I incorporate custom webcam blocks into GrapesJS for creating templates? Considering the details from Mautic’s documentation and the suggested methods such as forking or directly developing a plugin, what would be the least disruptive approach?