GrapesJS / grapesjs

Free and Open source Web Builder Framework. Next generation tool for building templates without coding
https://grapesjs.com
BSD 3-Clause "New" or "Revised" License
22.36k stars 4.05k forks source link

How to trigger the 'Import Template (modal)' on page load #4273

Closed boomshakar closed 2 years ago

boomshakar commented 2 years ago

Hello, Please i'm in need of the logic on how to the import template modal on initialization of the page... I'm using the newsletter preset plugin What i'm trying to achieve?: Make the user upload their code once the page has been loaded. Below are the logics I've tried,

` editor.on('load',(model, argument )=> {

    //open modAL
    const modal = edit.Modal;
    const cmdd = edit.Commands;

    //filter using component name
    if (model.getName() === 'Import template') {  //not working
      //show modal
      modal.open();
    }

    edit.on('modal:', (a, b, c) => {  //not working
      console.log({ a });
      console.log({ b });
      console.log({ c });
    });

    cmdd.run('modal:import-template', (a, b, c) => {  //not working
      console.log({ a });
      console.log({ b });
      console.log({ c });
    });

}`

Originally posted by @boomshakar in https://github.com/artf/grapesjs/discussions/4272

boomshakar commented 2 years ago

Hi @artf, I was able to get what I want by triggering click() on the button itself,

`

    const modal = edit.Modal;
    const query = new URLSearchParams(location.search);
    const uploadQuery = query.get('key');
    if (uploadQuery === 'upload-code') {
      const importClick = document.querySelector('.gjs-pn-btn.fa.fa-download');
      importClick.click();
      edit.on('modal', (e) => {
        if (e.open && e.title.textContent === 'Import template') {
          modal.setTitle('<div>Import your Template [inline-html/css]</div>');
        }
      });
    }

`

If there's a better workaround, i'd be glad If you share with me the knowledge. Thanks.

artf commented 2 years ago

@boomshakar please don't convert discussions to issues

boomshakar commented 2 years ago

@boomshakar please don't convert discussions to issues

Noted. Thanks