Custom Component Integration
Question
Hello!
I want to integrate a custom component with GrapesJS, using Angular CLI 7, but it doesn’t render.
My component is built on top of another open-source component. When I want to include it in any Angular project, I would just import the dependencies/libraries in the typescript files and the component itself (e.g.: <h2> <custom-comp> </custom-comp> </h2>) in the HTML.
In order to use it within GrapesJS, I initially created a Block Manager, where I attached my custom component:
var editor = grapesjs.init({...});
var blockManager = editor.BlockManager;
blockManager.add(‘custom-comp’, {
label: ‘Custom block',
content: '<div> <custom-comp> </custom-comp> </div>',
});
But this approach seem to work only for GrapesJS components, so I tried to follow this implementation
https://github.com/artf/grapesjs-mjml .
Would it suffice if I use only:
var type = ‘custom-comp’;
blockManager.addType(type, {
isComponent(el) {
if(el.tagName == type.toUpperCase())
{return {type, content:el.innerHTML} }
}
}).
I don’t quite understand whether model() and view() are compulsory to define and their usage. Would my component still render without including these? (like it is suggested here: https://github.com/artf/grapesjs-mjml/blob/master/src/components/Text.js)Answers (3)
Ok, but what will you do with the output (Angular components) of the editor? I mean, how you will use it at the end? Anyway, I see two different approaches to this kind of situation:
-
Create a custom render for each component (the exact thing/concept I've made with MJML). This is the most accurate approach because I should see in the canvas the exact component of the framework (eg. obviously if the renderer uses the same "compiler", in your case the Angular framework). Probably not the easiest approach as you have to grasp a good knowledge about both frameworks (eg. in your case Angular and GrapesJS) but for sure the best in the long term, once the renderer is implemented new components could be integrated without any hassle
-
You can create a kind of placeholder component, when the result will be always an HTML of your Angular components but the view is totally up to you to build. Example:
editor.DomComponents.addType('InputGroup', {
model: {
defaults: {
// The tag name that will be used in the final code
tagName: 'InputGroup',
attributes: { customProp: 'val1' },
// You would use traits to customize custom properties
traits: [
{
type: 'select',
name: 'customProp',
options: [
{ value: 'val1', name: 'Value 1' },
{ value: 'val2', name: 'Value 2' },
]
}
]
}
},
view: {
// eg. You can customize the tag in the canvas
// By default, the view will use the same tag of the model
tagName: 'div',
onRender() {
// What the user see in the canvas is totally up to you
// it can be a simple image as a placeholder or
// you can make it as much close to the original markup
const attrs = this.model.getAttributes();
this.el.innerHTML = `<div>
My custom HTML for this component<br>
My custom prop: ${attrs['customProp']}
</div>`;
}
}
});
This is the easiest but as you can see you have to setup a new view for each component.
@adelliinaa let's start from a simple question because I don't understand, what kind of editor are you trying to create? A builder for Angular components?!?
I want to create a form builder similar to the one which you created with the MJML components, but using Angular CLI instead of JS. And yes, the customed components which I want to integrate within GrapesJS are built in Angular as well.
Related Questions and Answers
Continue research with similar issue discussions.
Issue #3939
BUG: Disabled drag and drop in preview
GrapesJS version[X] I confirm to use the latest version of GrapesJSWhat browser are you using? v95Reproducible demo link https://codesandbo...
Issue #2972
FEAT: Disable scripts on canvas
Hello, first of all thanks for a great tool. On my project i hit the problem where I need to disable scripts inside the editor. I found 2 s...
Issue #386
Extend Pannel/Command examples/documentation with opening a new view
Hi Artur, First of all grapesjs is awsome! At the moment i am trying to puzzle together how to create a pannel with a button that opens a n...
Issue #2383
Custom component not pulling through style on first add.
Fiddle replicating the issue: https://jsfiddle.net/gd8mo3e4/1/ I have a custom component for creating a gallery of pdf documents. There's a...
Paid Plugins That Match This Issue
Curated by issue keywords and label relevance to help you ship faster.
Loading paid plugin recommendations...
Browse Plugin Categories
Jump directly to plugin category pages on the marketplace.