hungtcs / mxgraph-type-definitions

mxgraph type definitions
https://www.npmjs.com/package/mxgraph-type-definitions
Apache License 2.0
20 stars 8 forks source link

error when registering custom shape #24

Closed SebGo closed 4 years ago

SebGo commented 4 years ago

Hi,

Thank you for the work you do in this repo.

I'm trying to implement a custom shape by extending mxActor.

export class MyShape extends mxActor {
  constructor(bounds: mxRectangle, fill: string, stroke: string, strokewidth?: number ) {
    super(bounds, fill, stroke, strokewidth);
  }

  public redrawPath(path, x, y, w, h) {
    const w25: number = w * 0.25;
    const h50: number = h / 2;
    path.moveTo(0, 0);
    path.lineTo(w, 0);
    path.lineTo(w, h);
    path.lineTo(0, h);
    path.lineTo(w25, h50);
    path.lineTo(0, 0);
    path.close();
  }
}

When registering the Shape I get following error: Argument of type 'typeof MyShape' is not assignable to parameter of type 'typeof mxShape'.

mxCellRenderer.registerShape(
    "MyShape",
    MyShape
);

I think a solution could be to change registerShape() in mxCellRenderer.d.ts from static registerShape(key: string, shape: typeof mxShape): void; to static registerShape(key: string, shape: new (...args: any) => mxShape): void;

tbouffard commented 4 years ago

I agree with @SebGo I have the same isse, the shape constructor is expected by the registerShape function. When I used the old https://github.com/lgleim/mxgraph-typings, I can do mxCellRenderer.registerShape("MyShape", MyShape); and this perfectly works at runtime. For a real example using lgleim types, see https://github.com/process-analytics/bpmn-visualization-js/blob/v0.1.5/src/component/mxgraph/ShapeConfigurator.ts#L37

tbouffard commented 4 years ago

@hungtcs I am preparing a Pull Request for this.

About the constructor parameters types, I will use ...args: any as suggested in the issue. mxGraph standard shapes use bounds: mxRectangle, fill: string, stroke: string, strokewidth: number. I guess that as mxGraph is in charge of instantiating the mxShape, subclassers cannot add extra parameters, but I don't want to introduce any limitations or integration issues for now. We can manage stricter types later

[UPDATE]: mxShape constructor is constructor(stencil: mxStencil), wich highly suggests to keep ...args: any in the registerShape signature

hungtcs commented 4 years ago

Thanks for your solution @SebGo , I have encountered the same problem these days, but I didn't find the perfect way.