Skip to content
This repository has been archived by the owner on Dec 16, 2020. It is now read-only.

error when registering custom shape #24

Closed
SebGo opened this issue Jul 9, 2020 · 3 comments · Fixed by #25
Closed

error when registering custom shape #24

SebGo opened this issue Jul 9, 2020 · 3 comments · Fixed by #25

Comments

@SebGo
Copy link

SebGo commented Jul 9, 2020

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
Copy link
Contributor

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
Copy link
Contributor

tbouffard commented Jul 9, 2020

@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
Copy link
Owner

hungtcs commented Jul 9, 2020

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

tbouffard added a commit to tbouffard/mxgraph-type-definitions that referenced this issue Jul 9, 2020
Previous signature prevented to pass the shape constructor as the 2nd parameter.

Closes hungtcs#24
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants