arrowhead.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. var __rest = (this && this.__rest) || function (s, e) {
  2. var t = {};
  3. for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
  4. t[p] = s[p];
  5. if (s != null && typeof Object.getOwnPropertySymbols === "function")
  6. for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
  7. if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
  8. t[p[i]] = s[p[i]];
  9. }
  10. return t;
  11. };
  12. import { Dom } from '@antv/x6-common';
  13. import { Point } from '@antv/x6-geometry';
  14. import { ToolsView } from '../../view/tool';
  15. class Arrowhead extends ToolsView.ToolItem {
  16. get type() {
  17. return this.options.type;
  18. }
  19. get ratio() {
  20. return this.options.ratio;
  21. }
  22. init() {
  23. if (this.options.attrs) {
  24. const _a = this.options.attrs, { class: className } = _a, attrs = __rest(_a, ["class"]);
  25. this.setAttrs(attrs, this.container);
  26. if (className) {
  27. Dom.addClass(this.container, className);
  28. }
  29. }
  30. }
  31. onRender() {
  32. Dom.addClass(this.container, this.prefixClassName(`edge-tool-${this.type}-arrowhead`));
  33. this.update();
  34. }
  35. update() {
  36. const ratio = this.ratio;
  37. const edgeView = this.cellView;
  38. const tangent = edgeView.getTangentAtRatio(ratio);
  39. const position = tangent ? tangent.start : edgeView.getPointAtRatio(ratio);
  40. const angle = (tangent && tangent.vector().vectorAngle(new Point(1, 0))) || 0;
  41. if (!position) {
  42. return this;
  43. }
  44. const matrix = Dom.createSVGMatrix()
  45. .translate(position.x, position.y)
  46. .rotate(angle);
  47. Dom.transform(this.container, matrix, { absolute: true });
  48. return this;
  49. }
  50. onMouseDown(evt) {
  51. if (this.guard(evt)) {
  52. return;
  53. }
  54. evt.stopPropagation();
  55. evt.preventDefault();
  56. const edgeView = this.cellView;
  57. if (edgeView.can('arrowheadMovable')) {
  58. edgeView.cell.startBatch('move-arrowhead', {
  59. ui: true,
  60. toolId: this.cid,
  61. });
  62. const coords = this.graph.snapToGrid(evt.clientX, evt.clientY);
  63. const data = edgeView.prepareArrowheadDragging(this.type, {
  64. x: coords.x,
  65. y: coords.y,
  66. options: Object.assign(Object.assign({}, this.options), { toolId: this.cid }),
  67. });
  68. this.cellView.setEventData(evt, data);
  69. this.delegateDocumentEvents(this.options.documentEvents, evt.data);
  70. edgeView.graph.view.undelegateEvents();
  71. this.container.style.pointerEvents = 'none';
  72. }
  73. this.focus();
  74. }
  75. onMouseMove(evt) {
  76. const e = this.normalizeEvent(evt);
  77. const coords = this.graph.snapToGrid(e.clientX, e.clientY);
  78. this.cellView.onMouseMove(e, coords.x, coords.y);
  79. this.update();
  80. }
  81. onMouseUp(evt) {
  82. this.undelegateDocumentEvents();
  83. const e = this.normalizeEvent(evt);
  84. const edgeView = this.cellView;
  85. const coords = this.graph.snapToGrid(e.clientX, e.clientY);
  86. edgeView.onMouseUp(e, coords.x, coords.y);
  87. this.graph.view.delegateEvents();
  88. this.blur();
  89. this.container.style.pointerEvents = '';
  90. edgeView.cell.stopBatch('move-arrowhead', {
  91. ui: true,
  92. toolId: this.cid,
  93. });
  94. }
  95. }
  96. (function (Arrowhead) {
  97. Arrowhead.config({
  98. tagName: 'path',
  99. isSVGElement: true,
  100. events: {
  101. mousedown: 'onMouseDown',
  102. touchstart: 'onMouseDown',
  103. },
  104. documentEvents: {
  105. mousemove: 'onMouseMove',
  106. touchmove: 'onMouseMove',
  107. mouseup: 'onMouseUp',
  108. touchend: 'onMouseUp',
  109. touchcancel: 'onMouseUp',
  110. },
  111. });
  112. })(Arrowhead || (Arrowhead = {}));
  113. export const SourceArrowhead = Arrowhead.define({
  114. name: 'source-arrowhead',
  115. type: 'source',
  116. ratio: 0,
  117. attrs: {
  118. d: 'M 10 -8 -10 0 10 8 Z',
  119. fill: '#333',
  120. stroke: '#fff',
  121. 'stroke-width': 2,
  122. cursor: 'move',
  123. },
  124. });
  125. export const TargetArrowhead = Arrowhead.define({
  126. name: 'target-arrowhead',
  127. type: 'target',
  128. ratio: 1,
  129. attrs: {
  130. d: 'M -10 -8 10 0 -10 8 Z',
  131. fill: '#333',
  132. stroke: '#fff',
  133. 'stroke-width': 2,
  134. cursor: 'move',
  135. },
  136. });
  137. //# sourceMappingURL=arrowhead.js.map