customArrow.js 2.34 KB
/*
 * @Date: 2025-03-12 15:30:18
 * @LastEditors: hookehuyr hookehuyr@gmail.com
 * @LastEditTime: 2025-03-12 16:19:06
 * @FilePath: /logic-flow2/src/views/edge/customArrow.js
 * @Description: 文件描述
 */
import { PolylineEdge, PolylineEdgeModel, h } from '@logicflow/core';

class CustomEdgeModel extends PolylineEdgeModel {
  // customTextPosition = true;
  initEdgeData(data) {
    super.initEdgeData(data);
    this.customTextPosition = true;
    this.adjustEdgeMiddle = true;  // 允许调整边中间的点
  }
  setAttributes() {
    this.isAnimation = true;
  }
  // setHovered(isHovered) {
  //   super.setHovered(isHovered);
  //   this.isAnimation = isHovered;
  // }
  getEdgeAnimationStyle() {
    const style = super.getEdgeAnimationStyle();
    style.strokeDasharray = "5 5";
    style.strokeDashoffset = "100%";
    style.animationDuration = "10s";
    return style;
  }
}

class CustomArrow extends PolylineEdge {
  getAdjustPointShape(x, y) {
    return h("g", {}, [
      h("image", {
        x: x - 9,
        y: y - 9,
        width: 18,
        height: 18,
        cursor: "move",
        href:
          "data:image/svg+xml;base64,PCFET0NUWVBFIHN2ZyBQVUJMSUMgIi0vL1czQy8vRFREIFNWRyAxLjEvL0VOIiAiaHR0cDovL3d3dy53My5vcmcvR3JhcGhpY3MvU1ZHLzEuMS9EVEQvc3ZnMTEuZHRkIj48c3ZnIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgeG1sbnM6eGxpbms9Imh0dHA6Ly93d3cudzMub3JnLzE5OTkveGxpbmsiIHdpZHRoPSIyMnB4IiBoZWlnaHQ9IjIycHgiIHZlcnNpb249IjEuMSI+PGNpcmNsZSBjeD0iMTEiIGN5PSIxMSIgcj0iNyIgc3Ryb2tlPSIjZmZmIiBmaWxsPSIjMjliNmYyIi8+PGNpcmNsZSBjeD0iMTEiIGN5PSIxMSIgcj0iMyIgc3Ryb2tlPSIjZmZmIiBmaWxsPSJ0cmFuc3BhcmVudCIvPjwvc3ZnPg=="
      })
    ]);
  }

  getEndArrow() {
    const { model } = this.props;
    const {
      properties: { arrowType },
    } = model;

    const { stroke, strokeWidth } = model.getArrowStyle();
    const pathAttr = {
      stroke,
      strokeWidth,
    };

    if (arrowType === 'empty') {
      // 空心箭头
      return h('path', {
        ...pathAttr,
        fill: '#FFF',
        d: 'M 0 0  -20 -5 -30 0 -20 5 z',
      });
    } else if (arrowType === 'half') {
      // 半箭头
      return h('path', {
        ...pathAttr,
        d: 'M 0 0 -10 5',
      });
    }
    return h('path', {
      ...pathAttr,
      d: 'M 0 0 -10 -5 -10 5 z',
    });
  }
}

export default {
  type: 'custom-arrow',
  model: CustomEdgeModel,
  view: CustomArrow,
};