Issue #1471Opened October 1, 2018by alfaalex812 reactions

[Question] Blockmanager and Image

Question

Hi! I use this code to add a new block:

    blockManager.add("image", {
        label: 'Image',
        category: 'Basic',
        attributes: { class: "gjs-fonts gjs-f-image" },
        content: {
            style: { color: "black" },
            type: "image",
            activeOnRender: 1,
        },
    });

How can add a custom class to the img tag when i drop into canvas?

Thanks.

Answers (3)

arthuralmeidapOctober 1, 20182 reactions

@alfaalex81 For the content object, you can add a new index called classes. This index needs to be an array of classes, which will be applied to the new Image block;

Final code:

 blockManager.add("image", {
        label: 'Image',
        category: 'Basic',
        attributes: { class: "gjs-fonts gjs-f-image" },
        content: {
            style: { color: "black" },
            type: "image",
            classes: ['class1', 'class2']
            activeOnRender: 1,
        },
    });
alfaalex81October 2, 20180 reactions
Code SnippetHTML
Awesome!

I have another question for the same argument. I need to create a custom block with an image inside, for example: 

	<div>
		<ul>
			<li>
				<div><a href="#link"><img src="" alt="Title Image" /></a></div>
				<a href="#link" >Title</a>
			</li>
		</ul>
	</div>

I add the code with block manager, but on drop into canvas asset manager doesn't open. Otherwise if I use image type attribute, it ignore my html content, I saved an example in js fiddle:

https://jsfiddle.net/fp78yon0/

Thanks!
arthuralmeidapOctober 3, 20180 reactions

This is not the current grapesjs's behavior. the AssetManager only opens if you dropping directly am img block.

You must have to select the img block within your custom block and execute a code like that:

editor.runCommand('open-assets', {
  target: editor.getSelected()
});

Related Questions and Answers

Continue research with similar issue discussions.

Paid Plugins That Match This Issue

Curated by issue keywords and label relevance to help you ship faster.

View all plugins

Loading paid plugin recommendations...

Browse Plugin Categories

Jump directly to plugin category pages on the marketplace.