Issue #2952πŸ’¬ AnsweredOpened August 11, 2020by krishnaeverestengineering0 reactions

Custom component is not working after saving.

Quick answerby DmitryTar1

You should add some type to your component and then parse it in isComponent For example the similar button link const components = editor.DomComponents; const link = components.getType('link'); components.addType('custom-link-button', { model: link.model.extend({ defaults: Object.assign({}, link.model.prototype.defaul...

Read full answer below ↓

Question

Hi,

I have Button-Link custom component created, and when I dragged into canvas for the first time component is detecting its attributes and styles. But after saving the component and reloads the page, custom component type is missing from that component instead its showing Link type component. How to preserve the custom components even after saving?

Answers (3)

DmitryTar1β€’ August 13, 2020
Code SnippetTEXT
You should add some type to your component and then parse it in isComponent
For example the similar button link
const components = editor.DomComponents;
  const link = components.getType('link');

components.addType('custom-link-button', {
    model: link.model.extend({
      defaults: Object.assign({}, link.model.prototype.defaults, {
        attributes: { 'link-extended': 'custom-link-button },
      }),
    }, {
      isComponent: (el) => {
        let result;

        if (el.tagName == 'A' && el.attributes['link-extended'] 
          && el.attributes['link-extended'].value === 'custom-link-button') {
          result = {
            type: 'custom-link-button',
            editable: 0
          };

          // The link is editable only if, at least, one of its
          // children is a text node (not empty one)
          const children = el.childNodes;
          const len = children.length;
          if (!len) delete result.editable;

          for (let i = 0; i < len; i++) {
            const child = children[i];

            if (child.nodeType == 3 && child.textContent.trim() != '') {
              delete result.editable;
              break;
            }
          }
        }
        
        return result;
      }
    }),
    view: link.view
  });
Ju99ernautβ€’ August 17, 2020

@krishnaeverestengineering How are you saving and loading your work

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.