QUESTION: How should I receive custom trait modifications in the script?
Question
Hi, @artf, this is a great framework, thank you very much.
I am now using it to create components of the mobile BUI framework, and I ran into a problem, I added a custom trait textarea, how can I make this When the data changes, it is triggered in the output script.
editor.TraitManager.addType('textarea', {
createInput({
trait,
component
}) {
// Here we can decide to use properties from the trait
const el=document.createElement('div');
el.innerHTML = `
<textarea class="buiv-textarea" name="${options[0].name}" rows="${options[0].rows}">${options[0].value}</textarea>
`;
return el;
},
onEvent({
elInput,
component,
event
}) {
// The data change will reach this callback, you can operate the component, but how can we trigger the parameters received in the script?
},
onUpdate({
elInput,
component,
trait
}) {
},
});
// use trait textarea
editor.DomComponents.addType("slide", {
model: defaultModel.extend({
defaults: {
...defaultModel.prototype.defaults,
name: "slide",
content: `<div class="bui-slide bui-slide-skin01"></div>`,
datas: '[{image:""}]',
traits: [{
type: 'textarea',
name: 'datas',
label: "data",
changeProp: 1,
}],
script() {
let parentId=this.id;
// first time it is [{image:""}], I want to listen 'datas' change trigger here.
console.log('{[ datas ]}')
},
},
}
}),
view: defaultView.extend({
init() {
const props=['loop', 'fullscreen', 'height', 'direction', 'visibleNum', 'scrollNum', 'datas', 'autoplay'];
const reactTo = props.map(prop => `change:${prop}`).join(' ');
this.listenTo(this.model, reactTo, this.render);
}
}),
});
Answers (3)
You'll need to add the trait to script-props, you can read more here https://grapesjs.com/docs/modules/Components-js.html#passing-properties-to-scripts
Thanks @Ju99ernaut, If it is some built-in features, when modifying the script can receive the value of the parameter, I am currently experiencing problems: custom feature textarea, can trigger changes such as dom, but the exported script can not receive the value of this parameter, even if the script-props are set: "datas".
You can try forcing the script to update:
editor.DomComponents.addType('slide', {
model: {
// ...
init() {
this.on('change:datas', () => this.trigger('change:script'));
},
},
// ...
});
Related Questions and Answers
Continue research with similar issue discussions.
Issue #1679
How to render component in canvas using trait.
Hi Artf ,Thanks for your great tool,I have created test case for my problem https://js.do/code/271771 I have created custom tool sidebar an...
Issue #3196
Question: Build grapesjs with npm
Hi @artf, Thank you very much for this great framework. I want to contribute to grapesjs. However, I am stupid about using webpack and babe...
Issue #2134
[QUESTIONS] create trait like video trait
how do I make a trait like a video trait, I have a provider type, the other trait type depends on the provider I choose, can it be demonstr...
Issue #3587
QUESTION: How can I create grapesjs service for generating HTML from stored components?
Hello. Is there possibility to create grapesjs instance, without generating whole editor? I had created custom components with async data 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.