anchor.js 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. import { Dom, FunctionExt } from '@antv/x6-common';
  2. import { Point } from '@antv/x6-geometry';
  3. import { ToolsView } from '../../view/tool';
  4. import * as Util from './util';
  5. class Anchor extends ToolsView.ToolItem {
  6. get type() {
  7. return this.options.type;
  8. }
  9. onRender() {
  10. Dom.addClass(this.container, this.prefixClassName(`edge-tool-${this.type}-anchor`));
  11. this.toggleArea(false);
  12. this.update();
  13. }
  14. update() {
  15. const type = this.type;
  16. const edgeView = this.cellView;
  17. const terminalView = edgeView.getTerminalView(type);
  18. if (terminalView) {
  19. this.updateAnchor();
  20. this.updateArea();
  21. this.container.style.display = '';
  22. }
  23. else {
  24. this.container.style.display = 'none';
  25. }
  26. return this;
  27. }
  28. updateAnchor() {
  29. const childNodes = this.childNodes;
  30. if (!childNodes) {
  31. return;
  32. }
  33. const anchorNode = childNodes.anchor;
  34. if (!anchorNode) {
  35. return;
  36. }
  37. const type = this.type;
  38. const edgeView = this.cellView;
  39. const options = this.options;
  40. const position = edgeView.getTerminalAnchor(type);
  41. const customAnchor = edgeView.cell.prop([type, 'anchor']);
  42. anchorNode.setAttribute('transform', `translate(${position.x}, ${position.y})`);
  43. const anchorAttrs = customAnchor
  44. ? options.customAnchorAttrs
  45. : options.defaultAnchorAttrs;
  46. if (anchorAttrs) {
  47. Object.keys(anchorAttrs).forEach((attrName) => {
  48. anchorNode.setAttribute(attrName, anchorAttrs[attrName]);
  49. });
  50. }
  51. }
  52. updateArea() {
  53. const childNodes = this.childNodes;
  54. if (!childNodes) {
  55. return;
  56. }
  57. const areaNode = childNodes.area;
  58. if (!areaNode) {
  59. return;
  60. }
  61. const type = this.type;
  62. const edgeView = this.cellView;
  63. const terminalView = edgeView.getTerminalView(type);
  64. if (terminalView) {
  65. const terminalCell = terminalView.cell;
  66. const magnet = edgeView.getTerminalMagnet(type);
  67. let padding = this.options.areaPadding || 0;
  68. if (!Number.isFinite(padding)) {
  69. padding = 0;
  70. }
  71. let bbox;
  72. let angle;
  73. let center;
  74. if (terminalView.isEdgeElement(magnet)) {
  75. bbox = terminalView.getBBox();
  76. angle = 0;
  77. center = bbox.getCenter();
  78. }
  79. else {
  80. bbox = terminalView.getUnrotatedBBoxOfElement(magnet);
  81. angle = terminalCell.getAngle();
  82. center = bbox.getCenter();
  83. if (angle) {
  84. center.rotate(-angle, terminalCell.getBBox().getCenter());
  85. }
  86. }
  87. bbox.inflate(padding);
  88. Dom.attr(areaNode, {
  89. x: -bbox.width / 2,
  90. y: -bbox.height / 2,
  91. width: bbox.width,
  92. height: bbox.height,
  93. transform: `translate(${center.x}, ${center.y}) rotate(${angle})`,
  94. });
  95. }
  96. }
  97. toggleArea(visible) {
  98. if (this.childNodes) {
  99. const elem = this.childNodes.area;
  100. if (elem) {
  101. elem.style.display = visible ? '' : 'none';
  102. }
  103. }
  104. }
  105. onMouseDown(evt) {
  106. if (this.guard(evt)) {
  107. return;
  108. }
  109. evt.stopPropagation();
  110. evt.preventDefault();
  111. this.graph.view.undelegateEvents();
  112. if (this.options.documentEvents) {
  113. this.delegateDocumentEvents(this.options.documentEvents);
  114. }
  115. this.focus();
  116. this.toggleArea(this.options.restrictArea);
  117. this.cell.startBatch('move-anchor', {
  118. ui: true,
  119. toolId: this.cid,
  120. });
  121. }
  122. resetAnchor(anchor) {
  123. const type = this.type;
  124. const cell = this.cell;
  125. if (anchor) {
  126. cell.prop([type, 'anchor'], anchor, {
  127. rewrite: true,
  128. ui: true,
  129. toolId: this.cid,
  130. });
  131. }
  132. else {
  133. cell.removeProp([type, 'anchor'], {
  134. ui: true,
  135. toolId: this.cid,
  136. });
  137. }
  138. }
  139. onMouseMove(evt) {
  140. const terminalType = this.type;
  141. const edgeView = this.cellView;
  142. const terminalView = edgeView.getTerminalView(terminalType);
  143. if (terminalView == null) {
  144. return;
  145. }
  146. const e = this.normalizeEvent(evt);
  147. const terminalCell = terminalView.cell;
  148. const terminalMagnet = edgeView.getTerminalMagnet(terminalType);
  149. let coords = this.graph.coord.clientToLocalPoint(e.clientX, e.clientY);
  150. const snapFn = this.options.snap;
  151. if (typeof snapFn === 'function') {
  152. const tmp = FunctionExt.call(snapFn, edgeView, coords, terminalView, terminalMagnet, terminalType, edgeView, this);
  153. coords = Point.create(tmp);
  154. }
  155. if (this.options.restrictArea) {
  156. if (terminalView.isEdgeElement(terminalMagnet)) {
  157. const pointAtConnection = terminalView.getClosestPoint(coords);
  158. if (pointAtConnection) {
  159. coords = pointAtConnection;
  160. }
  161. }
  162. else {
  163. const bbox = terminalView.getUnrotatedBBoxOfElement(terminalMagnet);
  164. const angle = terminalCell.getAngle();
  165. const origin = terminalCell.getBBox().getCenter();
  166. const rotatedCoords = coords.clone().rotate(angle, origin);
  167. if (!bbox.containsPoint(rotatedCoords)) {
  168. coords = bbox
  169. .getNearestPointToPoint(rotatedCoords)
  170. .rotate(-angle, origin);
  171. }
  172. }
  173. }
  174. let anchor;
  175. const anchorFn = this.options.anchor;
  176. if (typeof anchorFn === 'function') {
  177. anchor = FunctionExt.call(anchorFn, edgeView, coords, terminalView, terminalMagnet, terminalType, edgeView, this);
  178. }
  179. this.resetAnchor(anchor);
  180. this.update();
  181. }
  182. // eslint-disable-next-line @typescript-eslint/no-unused-vars
  183. onMouseUp(evt) {
  184. this.graph.view.delegateEvents();
  185. this.undelegateDocumentEvents();
  186. this.blur();
  187. this.toggleArea(false);
  188. const edgeView = this.cellView;
  189. if (this.options.removeRedundancies) {
  190. edgeView.removeRedundantLinearVertices({ ui: true, toolId: this.cid });
  191. }
  192. this.cell.stopBatch('move-anchor', { ui: true, toolId: this.cid });
  193. }
  194. onDblClick() {
  195. const anchor = this.options.resetAnchor;
  196. if (anchor) {
  197. this.resetAnchor(anchor === true ? undefined : anchor);
  198. }
  199. this.update();
  200. }
  201. }
  202. (function (Anchor) {
  203. Anchor.config({
  204. tagName: 'g',
  205. markup: [
  206. {
  207. tagName: 'circle',
  208. selector: 'anchor',
  209. attrs: {
  210. cursor: 'pointer',
  211. },
  212. },
  213. {
  214. tagName: 'rect',
  215. selector: 'area',
  216. attrs: {
  217. 'pointer-events': 'none',
  218. fill: 'none',
  219. stroke: '#33334F',
  220. 'stroke-dasharray': '2,4',
  221. rx: 5,
  222. ry: 5,
  223. },
  224. },
  225. ],
  226. events: {
  227. mousedown: 'onMouseDown',
  228. touchstart: 'onMouseDown',
  229. dblclick: 'onDblClick',
  230. },
  231. documentEvents: {
  232. mousemove: 'onMouseMove',
  233. touchmove: 'onMouseMove',
  234. mouseup: 'onMouseUp',
  235. touchend: 'onMouseUp',
  236. touchcancel: 'onMouseUp',
  237. },
  238. customAnchorAttrs: {
  239. 'stroke-width': 4,
  240. stroke: '#33334F',
  241. fill: '#FFFFFF',
  242. r: 5,
  243. },
  244. defaultAnchorAttrs: {
  245. 'stroke-width': 2,
  246. stroke: '#FFFFFF',
  247. fill: '#33334F',
  248. r: 6,
  249. },
  250. areaPadding: 6,
  251. snapRadius: 10,
  252. resetAnchor: true,
  253. restrictArea: true,
  254. removeRedundancies: true,
  255. anchor: Util.getAnchor,
  256. snap(pos, terminalView, terminalMagnet, terminalType, edgeView, toolView) {
  257. const snapRadius = toolView.options.snapRadius || 0;
  258. const isSource = terminalType === 'source';
  259. const refIndex = isSource ? 0 : -1;
  260. const ref = this.cell.getVertexAt(refIndex) ||
  261. this.getTerminalAnchor(isSource ? 'target' : 'source');
  262. if (ref) {
  263. if (Math.abs(ref.x - pos.x) < snapRadius)
  264. pos.x = ref.x;
  265. if (Math.abs(ref.y - pos.y) < snapRadius)
  266. pos.y = ref.y;
  267. }
  268. return pos;
  269. },
  270. });
  271. })(Anchor || (Anchor = {}));
  272. export const SourceAnchor = Anchor.define({
  273. name: 'source-anchor',
  274. type: 'source',
  275. });
  276. export const TargetAnchor = Anchor.define({
  277. name: 'target-anchor',
  278. type: 'target',
  279. });
  280. //# sourceMappingURL=anchor.js.map