Issue attaching event to new plugin in Anglar
@sukheja-varun attach any event to the new plugin inside your plugin, you must first define a custom type component: then define a new block which is using our previously defined custom component type: for the modal, we define the below functions: the `createContent will generate the internal html for your modal. the...
Read full answer below ↓Question
Hi all, I was trying to add a new plugin to grapejs within my Angular project. Requirement: New plugin with name as Product. When the user drops the Product Plugin in HTML, it should open a modal where the user can type some attributes and an auto-complete search where the user can search multiple products and submit of modal I have to inject those products in HTML.
I checked the docs and found below example
function myPlugin(editor){
editor.BlockManager.add('my-first-block', {
label: 'Simple block',
content: '<div class="my-block">This is a simple block</div>',
});
}
But when I tried to work on this I was unable to :
- attach any event to the new plugin
- call API
- inject custom HTML
Answers (1)
@sukheja-varun
attach any event to the new plugin
inside your plugin, you must first define a custom type component:
editor.DomComponents.addType('my-first-type', {
model: {
defaults: {
components: '<div class="my-block">This is a simple block</div>',
}
}
})
then define a new block which is using our previously defined custom component type:
editor.BlockManager.add('my-first-block', {
label: 'Simple block',
content: {
type: 'my-first-type'
}
});
for the modal, we define the below functions:
const createContent = () => {
const content = document.createElement('div');
content.style = 'position: relative';
content.innerHTML =
`
<form onsubmit="">
<div >
<input type="text" id="query" placeholder="search..." name="query" style="
margin-bottom:10px;
font-size: 1rem;
padding: 10px 20px;
border-radius: 3px;
border: none;
">
</div>
</form>
<button style="
background-color: blue;
color:white;
font-size: 1rem;
border-radius: 5px;
border: none;
padding: 10px 20px;
cursor: pointer
">
Submit
</botton>
`
return content
}
the createContent will generate the internal html for your modal.
const applyChanges = () => {
axios.post("http://example.com", {
/*post body*/
})
}
the applyChanges is the click handler for the submit button inside the modal. and finally:
const showModal = () => {
const modal = editor.Modal
const content = createContent()
const btn = content.children.length === 2 && content.children[1];
btn.addEventListener('click', applyChanges)
modal.open({
title: "My Dialog!",
content: content
})
}
now that we have our component available as a block and we have a way to show our modal, we need to define a listener for the drop event inside our plugin:
editor.on("canvas:drop", (dt, m) => {
if (m && m.attributes && m.attributes.type == 'my-first-type') {
/*our component is dropped, show the modal:*/
showModal()
}
})
call API
as you can see inside applyChanges you can use whatever networking library you want, but there is no built-in networking library available inside grapesjs.
inject custom HTML
for injecting content inside your component, you can use either append or components. cheers!
Related Questions and Answers
Continue research with similar issue discussions.
Issue #1224
[Bug] Media query and classes rendering issue
Hi, I was trying to make a responsive layout but I discovered that the device switcher does not work consistently. The issue can be replica...
Issue #2850
[QUESTION] Add Dropdown to block using Traits
Hi @artf , I hope that you are well, I was wondering whether you could help me with some simple issue that I am having (I have recently sta...
Issue #2317
Need a feature to integrate modal form
[FEATURE REQUESTS] I have to add block which should a open a modal with form fields. Based on the form data selected I have to generate a h...
Issue #5148
none of the official GrapesJS plugins works with the new index.d.ts
GrapesJS version [X] I confirm to use the latest version of GrapesJS What browser are you using? all and none, the bug is during the build...
Paid Plugins That Match This Issue
Curated by issue keywords and label relevance to help you ship faster.
Loading paid plugin recommendations...
Check the open-source GrapesJS plugins on GitHub or run a quick search in our free catalog.
Browse free plugins →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.
Tutorial
GrapesJS Inline RTE Plugins Update: CKEditor 5 v0.1.4 and Froala Inline Text Editor
CKEditor 5 Inline for GrapesJS v0.1.4 fixes Studio SDK toolbar clipping, iframe injection and link balloon bugs. Compare with Froala Inline — both $69.
Tutorial
GJSDocs: Say Goodbye to Manual Document Work
Invoices, contracts, proposals, reports — all generated automatically. GJSDocs turns your template into a live document in seconds.
Tutorial
10 Mistakes When Building a Visual Editor (And How to Avoid Them)
Visual editors are powerful tools — they enable non-technical users to create websites, emails, landing pages, and applications without writing code
Browse Plugin Categories
Jump directly to plugin category pages on the marketplace.