AlexeyBoiko / DgrmJS

Dgrm.net - flowchart editor. Works on desktop, phone and tablet. Has no dependency. Pure JavaScript.
https://dgrm.net
Apache License 2.0
906 stars 83 forks source link

trigger event each time a shape is clicked #15

Closed tbo47 closed 2 years ago

tbo47 commented 2 years ago

I have a read only mode where when user click on a shape it should trigger an action. So the select event is not enough. I suggest we add this event too.

AlexeyBoiko commented 2 years ago

Thanks for suggest.

We cant add every needed event to core diagram functionality: Currently you need click event on a shape. Tomorrow I will need clickOnConnector event. Then I will want to draw a slider inside the shape.

Use decorators to add events and functionality to shapes:

const diagram = svgDiagramCreate(
    svg,
    // ShapeDecoratorFuctory
    function(shape, param) {
        // the way to add custom logic inside shapes - decorators
        return new ClickDecorator(shape, param.createParams.props)
            .on('click', del);

        // or access svg dom element directly
        shape.svgEl.addEventListener('click', ...);
        return shape;
    });

Decorator example - svg-shape-texteditor-decorator.js Other comment about decorators.

CLose PR if the solution suits you.

tbo47 commented 2 years ago

oh, perfect, thanks!