Create one more key in JSON
Question
{ "type": "text", "status": "hovered", "content": "Insert your text here", "attributes": { "id": "ixx2" }, "activeOnRender": 0,
> "open": false
> }
In JSON I want add new key with object. Example **data** key this will have custom Traits. I want to add new object from where data can be fetch and populate the output. Can I do this? Below is sample object how I wanted. Can any one help me on this how can I achieve this.
`{
"type": "text",
"status": "hovered",
"content": "Insert your text here",
"attributes": {
"id": "ixx2"
},
"data":{
"url":"https://something.com",
"refresh_rate":"10"
},
"activeOnRender": 0,
"open": false
}`Answers (3)
Sorry I misunderstood your problem, so my previous comment was not relevant.
To achieve your desire, you need create your own trait type (docs) and component type (docs).
There is an easier way but it can not save all your custom data as an object to the data key.
The JSON result would be like this:
{
"type": "text",
"status": "hovered",
"content": "Insert your text here",
...
// custom data
"url": "https://something.com",
"refresh_rate": 10
}
Sample code:
const defaultType = editor.DomComponents.getType('default');
editor.DomComponents.addType('form, {
model: defaultType.model.extend({
defaults: {
...defaultModel.prototype.defaults,
droppable: ':not(form)',
draggable: ':not(form)',
traits: [
{
type: 'text',
label: 'API Url',
name: 'url',
changeProp: true, // if `true` the value will be stored as model's property, otherwise it will be stored as html attribute
},
{
type: 'text',
label: 'Form Action',
name: 'action',
changeProp: true,
}
}
},
{
isComponent(el) {
if(el.tagName == 'FORM'){
return {type: 'form'};
}
},
}),
view: defaultModel.view
})
You can look at grapesjs-plugin-form to have more idea.
I'm not sure if I get your problem.
But if you set any custom attributes to component model (except some preserve keys like components, styles, ...)
e.g: component.set('customTraits', yourCustomTraits);. It would be in JSON data.
editor.getWrapper().find('form').forEach(
component => component.set('customAttr', {
type: 'text',
name: 'api-link',
label: 'API Link',
placeholder: 'Enter API LINK',
})
)
I tried like this, but it is not working.
{
"type": "text",
"status": "hovered",
"content": "Insert your text here",
"attributes": {
"id": "ixx2"
},
"data": {
"url": "https://something.com",
"refresh_rate": "10"
},
"activeOnRender": 0,
"open": false
}
I want to add data key and object in JSON, when user select form they can enter API url. Can you please check am I doing correctly?
Related Questions and Answers
Continue research with similar issue discussions.
Issue #2360
[Question] How to add a new property type(integer) to StyleManager?
Hi, I am trying to create a custom StyleManager property with 'integer' type. My ultimate goal is to 're-define' how certain units are disp...
Issue #2808
[QUESTION] Custom type component in production environment
Hi, I have a question, when I create a custom type component from the editor, what would be the next step to show that component in my appl...
Issue #3331
Custom Asset Manager
Hi @artf I followed the documentation to make new type of asset manager here ( https://codepen.io/abhi_punk81/pen/MWbWzaq) I've created new...
Issue #1965
Custom content type
@artf Instead of using plugins js files, can we create our own content using html as I have done below. Is it a correct way to do it? Sorry...
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.