[QUESTION] Reset trait on edit the content of a custom component
Question
Hi,
I have a custom component which content can be changed by switching trait select options, but also can be edited similar to the text component type when it is already rendered inside the canvas. Is there an easy way to reset the traits related attribute on edit of a component?
model: {
defaults: {
type: 'tag',
tagName: 'span',
droppable: 0,
editable: 1,
textable: 1,
traits: [{
type: 'select',
label: 'Custom type',
name: 'customType',
options: customTypeOptions,
}],
},
toHTML() {
return this.get('content');
},
view: {
events: {
focus: 'resetTrait',
},
reseTrait() {
}
}
Answers (3)
@artf, regarding the "Select" I couldn't manage to connect the change in the content and make it work on change of the trait in one flow and also to change it manually by editing it's text/content. The manual change wasn't syncing the trait, but I managed to make it work. A custom text component worked just fine.
As for the documentation from the last release, I am reading it right now and it looks very good, but I haven't seen it when I posted the question.
It may be only me, but I learn by example, so more examples and use cases will be always a plus.
I understand that the product is open and I appreciate your effort. I hope that I will gain more knowledge and will be able to contribute with something in near future.
Cheers.
You could attach an event listener to the content prop of the component, then in the callback fetch the trait or change the value prop of the trait, see example below taken from docs
// Let's update `options` of our `type` trait, defined in Input component
const component = editor.getSelected();
component.getTrait('type').set('options', [
{ id: 'opt1', name: 'New option 1'},
{ id: 'opt2', name: 'New option 2'},
]);
// or with multiple values
component.getTrait('type').set({
label: 'My type',
options: [...],
});
Thank you Zachary1748, but I can't use that in my case. Maybe I didn't explained it right. I have a custom made component. The components content can be changed in 3 ways:
- When dragged and droped into the canvas (you have a popup with options)
- From Settings (also via choosing between options)
- By editing its text inside the canvas
Anyway I think I fount a solution - I created a custom data type and binded the events to its change also, not only to the content changes. But I am still testing it.
Related Questions and Answers
Continue research with similar issue discussions.
Issue #2404
Question: how to observe children changes in component
Is there an easy way to subscribe/listen to changes in the components/children of my custom component type? Perhaps an event I can subscrib...
Issue #3019
[QUESTION] - Bind inner text/html (like div, p, h1) to Trait
Hi! I've created a custom trait, and I want to be allowed to change the content of the tags (in this case it's a H1 tag) based on the trait...
Issue #3541
[QUESTION] What is the idiomatic way to append and remove classes from components?
Hey there 👋 Love the editor! We're using traits to modify what classes a component has, but that feels a bit hacky as you have to redeclar...
Issue #486
Custom Component not editable and also after traits update canvas does not update
below is code i used for traits and custom section but after adding component via block, cannot edit text or trait update does not update c...
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.