button.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. "use strict";
  2. var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
  3. if (k2 === undefined) k2 = k;
  4. var desc = Object.getOwnPropertyDescriptor(m, k);
  5. if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
  6. desc = { enumerable: true, get: function() { return m[k]; } };
  7. }
  8. Object.defineProperty(o, k2, desc);
  9. }) : (function(o, m, k, k2) {
  10. if (k2 === undefined) k2 = k;
  11. o[k2] = m[k];
  12. }));
  13. var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
  14. Object.defineProperty(o, "default", { enumerable: true, value: v });
  15. }) : function(o, v) {
  16. o["default"] = v;
  17. });
  18. var __importStar = (this && this.__importStar) || function (mod) {
  19. if (mod && mod.__esModule) return mod;
  20. var result = {};
  21. if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
  22. __setModuleDefault(result, mod);
  23. return result;
  24. };
  25. Object.defineProperty(exports, "__esModule", { value: true });
  26. exports.Button = void 0;
  27. const x6_geometry_1 = require("@antv/x6-geometry");
  28. const x6_common_1 = require("@antv/x6-common");
  29. const tool_1 = require("../../view/tool");
  30. const Util = __importStar(require("./util"));
  31. class Button extends tool_1.ToolsView.ToolItem {
  32. onRender() {
  33. x6_common_1.Dom.addClass(this.container, this.prefixClassName('cell-tool-button'));
  34. this.update();
  35. }
  36. update() {
  37. this.updatePosition();
  38. return this;
  39. }
  40. updatePosition() {
  41. const view = this.cellView;
  42. const matrix = view.cell.isEdge()
  43. ? this.getEdgeMatrix()
  44. : this.getNodeMatrix();
  45. x6_common_1.Dom.transform(this.container, matrix, { absolute: true });
  46. }
  47. getNodeMatrix() {
  48. const view = this.cellView;
  49. const options = this.options;
  50. let { x = 0, y = 0 } = options;
  51. const { offset, useCellGeometry, rotate } = options;
  52. let bbox = Util.getViewBBox(view, useCellGeometry);
  53. const angle = view.cell.getAngle();
  54. if (!rotate) {
  55. bbox = bbox.bbox(angle);
  56. }
  57. let offsetX = 0;
  58. let offsetY = 0;
  59. if (typeof offset === 'number') {
  60. offsetX = offset;
  61. offsetY = offset;
  62. }
  63. else if (typeof offset === 'object') {
  64. offsetX = offset.x;
  65. offsetY = offset.y;
  66. }
  67. x = x6_common_1.NumberExt.normalizePercentage(x, bbox.width);
  68. y = x6_common_1.NumberExt.normalizePercentage(y, bbox.height);
  69. let matrix = x6_common_1.Dom.createSVGMatrix().translate(bbox.x + bbox.width / 2, bbox.y + bbox.height / 2);
  70. if (rotate) {
  71. matrix = matrix.rotate(angle);
  72. }
  73. matrix = matrix.translate(x + offsetX - bbox.width / 2, y + offsetY - bbox.height / 2);
  74. return matrix;
  75. }
  76. getEdgeMatrix() {
  77. const view = this.cellView;
  78. const options = this.options;
  79. const { offset = 0, distance = 0, rotate } = options;
  80. let tangent;
  81. let position;
  82. let angle;
  83. const d = x6_common_1.NumberExt.normalizePercentage(distance, 1);
  84. if (d >= 0 && d <= 1) {
  85. tangent = view.getTangentAtRatio(d);
  86. }
  87. else {
  88. tangent = view.getTangentAtLength(d);
  89. }
  90. if (tangent) {
  91. position = tangent.start;
  92. angle = tangent.vector().vectorAngle(new x6_geometry_1.Point(1, 0)) || 0;
  93. }
  94. else {
  95. position = view.getConnection().start;
  96. angle = 0;
  97. }
  98. let matrix = x6_common_1.Dom.createSVGMatrix()
  99. .translate(position.x, position.y)
  100. .rotate(angle);
  101. if (typeof offset === 'object') {
  102. matrix = matrix.translate(offset.x || 0, offset.y || 0);
  103. }
  104. else {
  105. matrix = matrix.translate(0, offset);
  106. }
  107. if (!rotate) {
  108. matrix = matrix.rotate(-angle);
  109. }
  110. return matrix;
  111. }
  112. onMouseDown(e) {
  113. if (this.guard(e)) {
  114. return;
  115. }
  116. e.stopPropagation();
  117. e.preventDefault();
  118. const onClick = this.options.onClick;
  119. if (typeof onClick === 'function') {
  120. x6_common_1.FunctionExt.call(onClick, this.cellView, {
  121. e,
  122. view: this.cellView,
  123. cell: this.cellView.cell,
  124. btn: this,
  125. });
  126. }
  127. }
  128. }
  129. exports.Button = Button;
  130. (function (Button) {
  131. Button.config({
  132. name: 'button',
  133. useCellGeometry: true,
  134. events: {
  135. mousedown: 'onMouseDown',
  136. touchstart: 'onMouseDown',
  137. },
  138. });
  139. })(Button = exports.Button || (exports.Button = {}));
  140. (function (Button) {
  141. Button.Remove = Button.define({
  142. name: 'button-remove',
  143. markup: [
  144. {
  145. tagName: 'circle',
  146. selector: 'button',
  147. attrs: {
  148. r: 7,
  149. fill: '#FF1D00',
  150. cursor: 'pointer',
  151. },
  152. },
  153. {
  154. tagName: 'path',
  155. selector: 'icon',
  156. attrs: {
  157. d: 'M -3 -3 3 3 M -3 3 3 -3',
  158. fill: 'none',
  159. stroke: '#FFFFFF',
  160. 'stroke-width': 2,
  161. 'pointer-events': 'none',
  162. },
  163. },
  164. ],
  165. distance: 60,
  166. offset: 0,
  167. useCellGeometry: true,
  168. onClick({ view, btn }) {
  169. btn.parent.remove();
  170. view.cell.remove({ ui: true, toolId: btn.cid });
  171. },
  172. });
  173. })(Button = exports.Button || (exports.Button = {}));
  174. //# sourceMappingURL=button.js.map