Issue #2297Opened September 27, 2019by umerrinayat2 reactions

[Question] Add script using drag and drop that will add elements dynamically

Question

Hey. Thank you for this great project. I'm using it in one of my project and need some help.

1- I have a block element with script in it. This script add a form dynamically inside a iframe.

blockManager.add('custom-script', {
	label: 'email-custom-script',
	category: 'Forms',
	content: `
		<div>
			<script type="text/javascript" src="https://app.getresponse.com/view_webform_v2.js?u=zqQFS&webforms_id=SGRbz"></script>
		<div>`,
	attributes: {
	title: 'Get Response Form',
	class: 'fa fa-square'
	}
})

When I drag and drop the script in anywhere on the page. Grapesjs ignore the position and render the form at the end of the page. Screenshot from 2019-09-27 12-32-54

But when I export the HTML and CSS from grapesjs the position is correct where it should be. Why this is happening. Screenshot from 2019-09-27 12-39-42

2- When I enable "allowScripts: 1," in grapesjs init function. I can add scripts but can't drag and drop (use) my custom block elements

let blockManager = editor.BlockManager;
	let snippets = await this.getSectionSnippets();
	snippets.map((item) => {
	blockManager.add(item.name, {
		label: item.name,
		category: 'Sections',
		content: `<html><head><script></script><style>${item.css}</style></head><body>${item.html}</body></html>`,
		attributes: {
			title: item.name,
			class: 'fa fa-square'
		}
	})
});```


If a disable "allowScript" then I can use my custom block element but can't add scripts. 

Answers (3)

umerrinayatOctober 3, 20192 reactions

Finally it is solved @pouyamiralayi Thank you for helping me out. After using your solution. It insert the script at correct position.

pouyamiralayiSeptember 27, 20190 reactions

Hi! did you try putting your script in the script tag? this way it will be executed at the end and probably will respect the order you desire:

editor.BlockManager.add('form-block', {
    script: function(){/* your-js */}
}

cheers.

artfSeptember 28, 20190 reactions

When I drag and drop the script in anywhere on the page. Grapesjs ignore the position and render the form at the end of the page.

To be honest I don't know, it's all depended by the third-party library (in your case GetResponse) and how it appends content once loaded

When I enable "allowScripts: 1," in grapesjs init function. I can add scripts but can't drag and drop (use) my custom block elements

First of all, there is no need to use html, head, body tags in your blocks. You're dropping HTML snippets inside a body of already defined document... I highly suggest you read about Components as you'll get much more control than simple blocks

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

Browse Plugin Categories

Jump directly to plugin category pages on the marketplace.