grapesjs icon indicating copy to clipboard operation
grapesjs copied to clipboard

Traits values are not saved if I'm not typing on them

Open jvillena opened this issue 8 years ago • 21 comments

Hi @artf, I'm struggling with an issue when I'm trying to insert values in the Traits parameters. If I insert a new value without typing on it then the value is gone after the component is deselected. If you check the video attached maybe it's clearer.

http://recordit.co/m9Jgp2XGXr

I need to know if there is a way to update the model with the new value inserted on traits parameters.

jvillena avatar Jul 24 '18 12:07 jvillena

You are changing the title attribute of the element. so the value will not be updated. Can you post your trait creation code.

mcsekar12 avatar Jul 24 '18 14:07 mcsekar12

Hi @mcsekar12, it happens with all the elements not only with the title. I'm not creating a traits, I only included when the component is selected a special button to allow user to include variables in the inputs. So I would like to know why the event is not triggering as when I'm typing on it.

jvillena avatar Jul 25 '18 06:07 jvillena

@jvillena It is difficult to tell without seeing the code . Can you post the code where you are updating the input.

mcsekar12 avatar Jul 25 '18 07:07 mcsekar12

@mcsekar12 There is not special code. I'm only injecting the value in the input with JQuery like: $(element).val("test");

This action is typing "test" on the title input field but the traits model of the element is not triggering any event.

Do we have any way to update the component traits value by triggering an event?

jvillena avatar Jul 25 '18 08:07 jvillena

I saw that the traits input field when you type on it is updating the model attributes wit changeProp 1 and also in the changes attribute is including the new value. I would like to know how I can force to do it injecting value using jQuery or normal Javascript.

jvillena avatar Jul 25 '18 08:07 jvillena

Can someone support me, please? @artf

jvillena avatar Jul 25 '18 14:07 jvillena

If you do not really type on them, the JS listener is not triggered, so they are not saved. I tried everything some weeks ago, even trying to emulate human typing and press enter with code, but the traits were still not saved. You would need to know that kind of event are traits listening (I still do not know it) and call it manually with your own code.

jmrgz avatar Jul 25 '18 17:07 jmrgz

@jvillena it's up to you triggering events... $(element).val("test").trigger('change')

artf avatar Jul 25 '18 21:07 artf

@artf I did it several times and I tested triggering different events but the result is the same it's not updating the model with the value changed. I don't know if you are catching the events on the traits and stop the propagation if not I would like to know how to update for instance the title of a components without typing on the input field element. I have to buttons that opens a popup where you can select a variable that it will be inserted on the title input field. But if after that I trigger the event change as you mention above and deselected the element if I click again on the element then the title is empty.

So I'd like to know how to update the traits input fields belongs to any component or plugin that it was by 3 parties.

jvillena avatar Jul 26 '18 08:07 jvillena

@artf @xamdam777 I found a solution to update any traits input without typing on it. For instance in our project we have to buttons to add macro functions and also variables so I need to catch the value and inject on the trait parameter selected. To do that when the user click or select a component I need to inject some extra data parameter on the input field of each traits beforehand:


  editor.on('component:toggled', model => {
            // To inject buttons close to the input fields
             addATButtons();
}

In my function called "addATButtons" I inject this extra parameters that I need them to later on update the traits values:


var addATButtons = function() {
        // To initialize at input variables in trait
        // First we need to look into trait panels to find input text field
        $('.gjs-trt-trait').each(function () {

            var label = $(this).find(".gjs-label");

            $(this).find("input[type=text]").each(function () {
                if (!$(this).hasClass("js-input-at")) {
                        if (label.attr("title").toLowerCase() != "id"){
                            $(this).addClass("js-input-at");
                            var random_value =  Math.floor((1 + Math.random()) * 0x10000).toString(16).substring(1);
                            $(this).attr("name", random_value);
                            $(this).attr("data-attributes", label.attr("title").toLowerCase());

                        }


                }
            });




        });

  
    };

Finally when I click on the "Accept" button of my modal dialog I need to update the traits value on the model of the component dynamically:

const component =  editorRTE.DomComponents.getWrapper().find('.gjs-comp-selected')[0];
               const updateTraits = model => {
                   if (model && model.get('traits')){
                       var attr_list = {};
                       model.get('traits').models.forEach(traits => {

                           var name_attr = '[data-attributes="' + traits.attributes.name + '"]';
                           attr_list[traits.attributes.name] = $(name_attr).val();
                           //console.log(traits.attributes);


                       });

                       if (!jQuery.isEmptyObject(attr_list)){
                           component.setAttributes(attr_list);
                       }


                   }

               };


               updateTraits(component);

The event "change" was not updating the model so I have to work around with this solution.

jvillena avatar Jul 26 '18 11:07 jvillena

@jvillena thank you very much! I've been trying your code and it seems great.

Have you tried it with color picker traits? I found that color picker ones needed more work since when you do not manually type the color, not only the model, but the selected color in the picker won't update too.

Do you think that we could use this approach for updating style manager properties without typing? In my case, I have been trying everything to correct bugs like this one:

https://github.com/artf/grapesjs/issues/1319

And I think that automatically typing (correct) style manager values when they are wrong could be a solution.

jmrgz avatar Jul 26 '18 13:07 jmrgz

@xamdam777 I'll take a look later. Now I'm struggling with creating a new Trait select re-implementing on my side the select trait because I need to inject new attributes on components dynamically.

I'll keep you inform.

jvillena avatar Jul 26 '18 13:07 jvillena

@jvillena that sounds hard! good luck :) Thanks

jmrgz avatar Jul 26 '18 15:07 jmrgz

https://jsfiddle.net/szLp8h4n/286

artf avatar Jul 27 '18 19:07 artf

That fiddle will be very useful, thanks a lot @artf !

Do you think that we could use something like that to update style properties programmatically?

I would love to be able to update any value, like for example setting margin-top 10px to the selected component.

I can already do it, but the change will not appear on the style manager like your previous example, so it is not a solution.

Again thanks for your great work!

jmrgz avatar Jul 28 '18 00:07 jmrgz

Thanks @artf that solved my problem in a simple solution so then I don't run over all inputs to update only one.

Great jobs!

jvillena avatar Jul 29 '18 14:07 jvillena

@xamdam777 you can use component.addStyle({ 'margin-top': '10px' }) (or setStyle for a compete replace of the style).

artf avatar Aug 01 '18 21:08 artf

@artf thanks a lot for finding time to reply.

I have tried that before, but it did not work as expected.

Demo example:

  1. Select "Build your templates without coding" (class .lead-title)
  2. Apply this command in the console: editor.getSelected().setStyle({'margin-top': '75px'});
  3. You will see that the property is not changed in the style manager. Instead, it is added to a new #id selector, but the margin-top from the class .lead-title stays the same as before (150px).

That is the reason because I'm trying to find a way to programmatically trigger the user inputs on the style manager. That would "mimic" the user interaction and it should work fine.

Unfortunately, I have spent tons of hours trying to trigger every event that I can see in the code, but my jQuery inputs .val() on the style manager boxes are never triggered.

Thanks again!

jmrgz avatar Aug 02 '18 19:08 jmrgz

@xamdam777 you're right, in that case you should use editor.getSelectedToStyle() which will return the proper object, a Component if you don't have any class selector associated (the same you get with editor.getSelected()) or the CSS Rule object associated to the selected component by its classes

artf avatar Aug 08 '18 19:08 artf

Thats cool @artf

I think this is the solution I was looking for :+1:

This code will add or update the selected element CSS property. I changed to addStyle, since when using setStyle all the previous properties were deleted (complete css replacement, not by property).

var test = editor.getSelectedToStyle(); test.addStyle({'margin-top': '75px'});

A minor problem is that when you change the style using the previous code, you need to unselect & select the block again to see the new value in the style manager.

I have tried triggering editor.select(null); after applying the code, but it only works fine in the console. If I use it inside editor.on("component:selected", the editor seems to crash.

Thank you very much for your previous code, it helps a lot!!!

jmrgz avatar Aug 09 '18 00:08 jmrgz