Issue #1135💬 AnsweredOpened May 25, 2018by JulyanoF0 reactions

Two html tags as one (link/button)

快速解答by artf

You should create a custom component

Read full answer below ↓

Question

Is it possible to have two elements (div and lnik) and consider as a single? For example: there is the my following link block:

<div class="row"><a class="btn" href="http://test.com">Link Here</a></div>

And I would like that on click div or button, it shows the <a> as a selected element on editor views, but in wrapper, show the div as a selected.

Answers (3)

JulyanoFJune 1, 2018

@artf I tried extending original link component, but it seems not working

var linkType = editor.DomComponents.getType('link');
domComps.addType('link', {
    model: linkType.model.extend({
        defaults: Object.assign({}, linkType.model.prototype.defaults, {
            traits: [
                {
                    label: 'Link',
                    name: 'href',
                    placeholder: 'http://meusite.com.br'
                },
                {
                    label: 'Conteúdo',
                    type: 'input-conteudo',
                    placeholder: 'Botão'
                },
                {
                    label: 'Cor',
                    type: 'color',
                    name: 'background-color'
                }
            ],
            toHTML: function() {
                return '<div class="row"><a class="btn">Link Here</a></div>';
            },
        }),
    })
});

It's not changing anything..

I already tried to disable autorender and render after all changes.

I could change the content only setting a new, using BlockManager:

bm.get('c85').set({ content: '<div class"row"><a class="btn">Link Here</a></div>' }); But when click in button, it's selecting only the button (not div) and when I click div, it's not selecting my button custom settings;

JulyanoFJune 11, 2018

@artf as you said, I tried to use Components, but I didn't get expected result. Could you help me?

My link block prints this content: <div style="height:45px;margin:0 auto 10px auto;padding: 5px 5px 5px 5px;width: 100%;text-align:center"><a class="btn btn-purple">Button</a></div>

I'm trying to get button when click in div too, or when click in button, select all div.

In components, I tried:

domComps.addType('link', {
    model: linkType.model.extend({
        defaults: Object.assign({}, linkType.model.prototype.defaults, {
            traits: [
                {
                    label: 'Link',
                    name: 'href',
                    placeholder: 'http://mywebsite.com'
                },
                {
                    label: 'Content',
                    type: 'input-content',
                    placeholder: 'Button'
                }
            ]
        }),
    },{
        isComponent: function(el, sec) {
            if(el.tagName == 'A'){
                return {type: 'link'};
            }
            // I already tried to return always link:
            // return {type: 'link'};
        },
    })
});

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...

Free option

Check the open-source GrapesJS plugins on GitHub or run a quick search in our free catalog.

Browse free plugins →
Premium option

Premium plugins ship with support, regular updates, and production-ready features — save days of integration work.

Browse premium plugins →

Related tutorials

In-depth guides on the same topic.

All tutorials →

Browse Plugin Categories

Jump directly to plugin category pages on the marketplace.