Issue #2640💬 AnsweredOpened March 12, 2020by Ace06112 reactions

Adding extra attributes with Stylemanager to change selected block's attribute

快速解答by pouyamiralayi1

@Ace0611 you can use traits for both controlling props (id, title & etc) and attributes. Cheers!

Read full answer below ↓

Question

Thanks for this awesome plugin. I am integrating it with my current project. Have one issue with it. Here is my code, I have added a new sector with 3 input fields, I want to change the selected block's attribute(id, readOnly) with my value in those input fields. for instance: I want to change id of the selected block with my functionality. How can i achieve this?

editor.StyleManager.addSector( "mySector",

代码片段TEXT
{
      name: "Element Attributes",
      open: true,
      properties: [{ name: "id" }, { name: "name" }, { name: "readOnly" }]
      // buildProps: ['id', 'readOnly'],
    },
    { at: 0 }
  );

and to change the attribute of selected component:

editor.on("block:drag:stop", function(droppedComponent) {    
    droppedComponent.addAttributes({
      id: "customId"(it should take it from the input)
      // onclick: `jsFunc('#${randomId}')`
    });
  });

Answers (3)

pouyamiralayiMarch 12, 2020

@Ace0611 you can use traits for both controlling props (id, title & etc) and attributes. Cheers!

pouyamiralayiMarch 13, 2020

@Ace0611

Will using id by traits override that?

yes! id is just a regular prop like others. And notice that grapesjs already provides you with default traits for id & title. Cheers!

Ace0611March 13, 2020

@pouyamiralayi : Thanks for helping me out. One doubt though, Every block that is being dragged, generates a dynamic id. Will using id by traits override that? My apologies, I am travelling right now, so cant do it myself at the moment. I will try it as soon as possible.

Related Questions and Answers

Continue research with similar issue discussions.

Paid Plugins That Match This Issue

Curated by issue keywords and label relevance to help you ship faster.

View all plugins

Loading paid plugin recommendations...

Free option

Check the open-source GrapesJS plugins on GitHub or run a quick search in our free catalog.

Browse free plugins →
Premium option

Premium plugins ship with support, regular updates, and production-ready features — save days of integration work.

Browse premium plugins →

Related tutorials

In-depth guides on the same topic.

All tutorials →

Browse Plugin Categories

Jump directly to plugin category pages on the marketplace.