Issue #424💬 AnsweredOpened October 16, 2017by aryeharmon0 reactions

Custom competent <template> missing inner html when creating a block with it

快速解答by artf

Hey @aryeharmon I just used this in the demo (try it by yourself in console) and importing <div><template><div>SOME TEXT</div></template></div> works just as expected.

Read full answer below ↓

Question

you gave me a fix in #244 but this is no longer working

Answers (3)

artfOctober 16, 2017

Hey @aryeharmon I just used this in the demo (try it by yourself in console)

var domc = editor.DomComponents;
var defaultModel = domc.getType('default').model;
var defaultView = domc.getType('default').view;
domc.addType('template', {
  model: defaultModel.extend({}, {
    isComponent: function(el) {
      if (el.tagName == 'TEMPLATE') {
      console.log(el, el.innerHTML);
        // as template.children will return an empty array the traverser
        // will skip all nodes, therefore attach them as a string (they 
        // will be processed later)
        return {type: 'template', components: el.innerHTML}
      }
    }
  }),
  view: defaultView.extend({
    // '<template>' can't be shown so in canvas use another tag
    tagName: 'div'
  }),
});

and importing <div><template><div>SOME TEXT</div></template></div> works just as expected.

aryeharmonOctober 18, 2017

OK so i see what the issue is, apparently when i add:

var domc = editor.DomComponents;
var defaultModel = domc.getType('default').model;
var defaultView = domc.getType('default').view;
domc.addType('template', {
  model: defaultModel.extend({}, {
    isComponent: function(el) {
      if (el.tagName == 'TEMPLATE') {
      console.log(el, el.innerHTML);
        // as template.children will return an empty array the traverser
        // will skip all nodes, therefore attach them as a string (they 
        // will be processed later)
        return {type: 'template', components: el.innerHTML}
      }
    }
  }),
  view: defaultView.extend({
    // '<template>' can't be shown so in canvas use another tag
    tagName: 'div'
  }),
});

to html the existing elements on page do not take effect. only newly added dom elements. for example i already have a template tag on page when load, i will not get the properties. but if i add the template after it will work.

aryeharmonOctober 18, 2017

btw, the editor.render(); is after the register of domc.addType

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 →

Browse Plugin Categories

Jump directly to plugin category pages on the marketplace.