react-diagrams icon indicating copy to clipboard operation
react-diagrams copied to clipboard

TypeError: path.getTotalLength is not a function

Open mathiasbaldissera opened this issue 7 years ago • 2 comments

Hello,

I'm trying to make a new link and such labels to it, but i'm facing a problem. I had copy the DefaultLinkWidged code and made few changes (added markers to it, only), but, when I add a Label to it, I get this error: image But, with the library's DefaultLinkWidget, it's work perfectly. Can someone help me?

mathiasbaldissera avatar Aug 08 '19 15:08 mathiasbaldissera

The ref won't be correct if you wrapped your link with a g tag like in the examples. This is in my factory:

  public generateLinkSegment(
    model: ObjectDefinitionRelationshipModel,
    widget: DefaultLinkWidget,
    selected: boolean,
    path: string
  ) {
    return (
      <g>
        <MyLink
          widget={widget}
          selected={selected}
          model={model}
          path={path}
        />
      </g>
    );
  }

I ended up just extending the default link widget and overriding the generateLink function to set the proper ref. If you look at the Bottom clone element I just push the correct Path dom element into the refsPaths. The rest was copy and pasted from the DefaultLinkWidget generateLink function.

class LinkWidget extends DefaultLinkWidget {
  public generateLink(path: string, extraProps: any, id: string | number): JSX.Element {
    const { diagramEngine, link } = this.props;

    const Bottom = React.cloneElement(
      (diagramEngine.getFactoryForLink(link) as DefaultLinkFactory).generateLinkSegment(
        link,
        this,
        this.state.selected || link.isSelected(),
        path
      ),
      {
        ref: (ref: any) => ref && this.refPaths.push(ref.firstChild)
      }
    );

    const Top = React.cloneElement(Bottom, {
      ...extraProps,
      strokeLinecap: "round",
      onMouseLeave: () => {
        this.setState({ selected: false });
      },
      onMouseEnter: () => {
        this.setState({ selected: true });
      },
      ref: null,
      "data-linkid": link.getID(),
      strokeOpacity: this.state.selected ? 0.1 : 0,
      strokeWidth: 20,
      onContextMenu: () => {
        if (!diagramEngine.isModelLocked(this.props.link)) {
          // event.preventDefault();
          link.remove();
        }
      }
    });

    return (
      <g key={"link-" + id}>
        {Bottom}
        {Top}
      </g>
    );
  }
}

Then again in your factory you want to clone the extended widget.

  public generateReactWidget(diagramEngine: DiagramEngine, link: DefaultLinkModel): JSX.Element {
    return React.createElement(LinkWidget, {
      link,
      diagramEngine
    });
  }

WilliamStephens avatar Aug 08 '19 15:08 WilliamStephens

Thank you very much. With your explanation, and changing just 1 line, it worked fine now. ref: (ref:SVGGElement) => ref && this.refPaths.push(ref.firstChild as SVGPathElement)

mathiasbaldissera avatar Aug 08 '19 16:08 mathiasbaldissera