Issue #1178💬 AnsweredOpened June 6, 2018by demodesign0 reactions

Want to replace wrapper div

快速解答by artf

No, currently there is no way to import a wrapper, but I'd be glad to accept a PR for this

Read full answer below ↓

Question

Hello artf,

I want to replace wrapper div, for example : When we create the newsletter template, wrapper div already exists there. On import template if wrapper div exists in import html then need to replace already exists wrapper with new one.

Is that possible? following is my code

var blockManager = editor.BlockManager;
var type = 'template';
bm.add(type, {
   label: 'Test',
   category: 'My Templates',
   content: {
    type: type,
    content:  '<div id="wrapper" style="box-sizing: border-box; background-image: url(http://www.test.net/source/MzE=/badlands-dawn-20180314012931.jpg); background-repeat: repeat; background-position: left top; background-size: cover; background-attachment: fixed;"><div class="div-box c5389 c6138" style="box-sizing: border-box; margin: 0 auto; max-width: 600px; padding: 5px 5px 5px 5px; width: 100%;"><div id="id29o6" style="box-sizing: border-box;"><div id="isee9e" style="box-sizing: border-box; padding: 0; margin: 0; vertical-align: top; display: inline-block; width: 100%;">
</div></div><h1 class="heading" id="iti4ul" style="box-sizing: border-box;">Insert title here</h1><p class="paragraph" id="ik4ktj" style="box-sizing: border-box;">Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua</p><h1 class="heading" id="irpqvn" style="box-sizing: border-box;">Insert title here</h1><p class="paragraph" id="ii5irl" style="box-sizing: border-box;">Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua</p><a class="c5561" id="iqlfky" style="box-sizing: border-box; font-size: 20px; padding: 10px 25px; background-color: #673AB7; color: #fff; text-align: center; border-radius: 4px; font-weight: 300; display: inline-block; margin: 10px;">Link Button Text</a></div></div>'
   }
  });

var dc = editor.DomComponents;
var defaultType = dc.getType('default');
var defaultModel = defaultType.model;

editor.DomComponents.addType(type, {   
   view: defaultType.view.extend({
    render: function () {
     defaultType.view.prototype.render.apply(this, arguments);    
     // Here you apply all your custom logic
     var content = this.model.attributes.content;
     var wrapperStyle = '';
     if (content) {
      var c = $(content),
              wrapperId = c.attr('id');
      if (wrapperId == 'wrapper') {
       wrapperStyle = c.attr('style');
       content = c[0].innerHTML;
      }
     }
     var ws = wrapperStyle.split(';');
     if (ws) {
      var wc = editor.DomComponents.getWrapper();
      var wstyles = {};
      for (var i = 0; i < ws.length; i++) {
       var st = ws[i].split(': ');
       st[0] = st[0].trim();
       if (st[0] != 'undefined' && st[0] != '') {
        wstyles[st[0]] = st[1];
       }
      }
      wc.set({
       style: wstyles
      });
     }     
     this.model.attributes.content = content;     
     return this;
    },
   }),
  });

After drag and drop, it not editable @artf

Answers (2)

artfJune 8, 2018

No, currently there is no way to import a wrapper, but I'd be glad to accept a PR for this

padcomSeptember 11, 2023

Any progress on this? Specifying the wrapper component is very useful when working with custom elements that need a context, such as FormKit that requires some data provide()d...

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.