Control.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  1. var __extends = (this && this.__extends) || (function () {
  2. var extendStatics = function (d, b) {
  3. extendStatics = Object.setPrototypeOf ||
  4. ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
  5. function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
  6. return extendStatics(d, b);
  7. };
  8. return function (d, b) {
  9. extendStatics(d, b);
  10. function __() { this.constructor = d; }
  11. d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  12. };
  13. })();
  14. var __assign = (this && this.__assign) || function () {
  15. __assign = Object.assign || function(t) {
  16. for (var s, i = 1, n = arguments.length; i < n; i++) {
  17. s = arguments[i];
  18. for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
  19. t[p] = s[p];
  20. }
  21. return t;
  22. };
  23. return __assign.apply(this, arguments);
  24. };
  25. var __read = (this && this.__read) || function (o, n) {
  26. var m = typeof Symbol === "function" && o[Symbol.iterator];
  27. if (!m) return o;
  28. var i = m.call(o), r, ar = [], e;
  29. try {
  30. while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
  31. }
  32. catch (error) { e = { error: error }; }
  33. finally {
  34. try {
  35. if (r && !r.done && (m = i["return"])) m.call(i);
  36. }
  37. finally { if (e) throw e.error; }
  38. }
  39. return ar;
  40. };
  41. import { h, Component } from 'preact';
  42. import { LogicFlowUtil } from '@logicflow/core';
  43. import Rect from '../BasicShape/Rect';
  44. import { getDiamondResizeEdgePoint, getEllipseResizeEdgePoint, getRectResizeEdgePoint, ModelType } from './Util';
  45. var StepDrag = LogicFlowUtil.StepDrag;
  46. var Control = /** @class */ (function (_super) {
  47. __extends(Control, _super);
  48. function Control(props) {
  49. var _this = _super.call(this) || this;
  50. // 更新中心点位置,更新文案位置
  51. _this.updatePosition = function (_a) {
  52. var deltaX = _a.deltaX, deltaY = _a.deltaY;
  53. var _b = _this.nodeModel, x = _b.x, y = _b.y;
  54. _this.nodeModel.x = x + deltaX / 2;
  55. _this.nodeModel.y = y + deltaY / 2;
  56. _this.nodeModel.moveText(deltaX / 2, deltaY / 2);
  57. };
  58. // 计算control拖动后,节点的宽高
  59. _this.getResize = function (_a) {
  60. var index = _a.index, deltaX = _a.deltaX, deltaY = _a.deltaY, width = _a.width, height = _a.height, PCTResizeInfo = _a.PCTResizeInfo, _b = _a.pct, pct = _b === void 0 ? 1 : _b;
  61. var resize = { width: width, height: height, deltaX: deltaX, deltaY: deltaY };
  62. if (PCTResizeInfo) {
  63. var sensitivity = 4; // 越低越灵敏
  64. var deltaScale = 0;
  65. var combineDelta = 0;
  66. switch (index) {
  67. case 0:
  68. combineDelta = (deltaX * -1 - deltaY) / sensitivity;
  69. break;
  70. case 1:
  71. combineDelta = (deltaX - deltaY) / sensitivity;
  72. break;
  73. case 2:
  74. combineDelta = (deltaX + deltaY) / sensitivity;
  75. break;
  76. case 3:
  77. combineDelta = (deltaX * -1 + deltaY) / sensitivity;
  78. break;
  79. default:
  80. break;
  81. }
  82. if (combineDelta !== 0) {
  83. deltaScale = Math.round((combineDelta / PCTResizeInfo.ResizeBasis.basisHeight)
  84. * 100000) / 1000;
  85. }
  86. PCTResizeInfo.ResizePCT.widthPCT = Math.max(Math.min(PCTResizeInfo.ResizePCT.widthPCT + deltaScale, PCTResizeInfo.ScaleLimit.maxScaleLimit), PCTResizeInfo.ScaleLimit.minScaleLimit);
  87. PCTResizeInfo.ResizePCT.hightPCT = Math.max(Math.min(PCTResizeInfo.ResizePCT.hightPCT + deltaScale, PCTResizeInfo.ScaleLimit.maxScaleLimit), PCTResizeInfo.ScaleLimit.minScaleLimit);
  88. var spcWidth = Math.round((PCTResizeInfo.ResizePCT.widthPCT
  89. * PCTResizeInfo.ResizeBasis.basisWidth) / 100);
  90. var spcHeight = Math.round((PCTResizeInfo.ResizePCT.hightPCT
  91. * PCTResizeInfo.ResizeBasis.basisHeight) / 100);
  92. switch (index) {
  93. case 0:
  94. deltaX = width - spcWidth;
  95. deltaY = height - spcHeight;
  96. break;
  97. case 1:
  98. deltaX = spcWidth - width;
  99. deltaY = height - spcHeight;
  100. break;
  101. case 2:
  102. deltaX = spcWidth - width;
  103. deltaY = spcHeight - height;
  104. break;
  105. case 3:
  106. deltaX = width - spcWidth;
  107. deltaY = spcHeight - height;
  108. break;
  109. default:
  110. break;
  111. }
  112. resize.width = spcWidth;
  113. resize.height = spcHeight;
  114. resize.deltaX = deltaX / pct;
  115. resize.deltaY = deltaY / pct;
  116. return resize;
  117. }
  118. switch (index) {
  119. case 0:
  120. resize.width = width - deltaX * pct;
  121. resize.height = height - deltaY * pct;
  122. break;
  123. case 1:
  124. resize.width = width + deltaX * pct;
  125. resize.height = height - deltaY * pct;
  126. break;
  127. case 2:
  128. resize.width = width + deltaX * pct;
  129. resize.height = height + deltaY * pct;
  130. break;
  131. case 3:
  132. resize.width = width - deltaX * pct;
  133. resize.height = height + deltaY * pct;
  134. break;
  135. default:
  136. break;
  137. }
  138. return resize;
  139. };
  140. // 矩形更新
  141. _this.updateRect = function (_a) {
  142. var deltaX = _a.deltaX, deltaY = _a.deltaY;
  143. var _b = _this.nodeModel, id = _b.id, x = _b.x, y = _b.y, width = _b.width, height = _b.height, radius = _b.radius, PCTResizeInfo = _b.PCTResizeInfo;
  144. // 更新中心点位置,更新文案位置
  145. var index = _this.index;
  146. var size = _this.getResize({
  147. index: index,
  148. deltaX: deltaX,
  149. deltaY: deltaY,
  150. width: width,
  151. height: height,
  152. PCTResizeInfo: PCTResizeInfo,
  153. pct: 1,
  154. });
  155. // 限制放大缩小的最大最小范围
  156. var _c = _this.nodeModel, minWidth = _c.minWidth, minHeight = _c.minHeight, maxWidth = _c.maxWidth, maxHeight = _c.maxHeight;
  157. if (size.width < minWidth
  158. || size.width > maxWidth
  159. || size.height < minHeight
  160. || size.height > maxHeight) {
  161. // 为了避免放到和缩小位置和鼠标不一致的问题
  162. _this.dragHandler.cancelDrag();
  163. return;
  164. }
  165. _this.updatePosition({ deltaX: size.deltaX, deltaY: size.deltaY });
  166. // 更新宽高
  167. _this.nodeModel.width = size.width;
  168. _this.nodeModel.height = size.height;
  169. _this.nodeModel.setProperties({
  170. nodeSize: {
  171. width: size.width,
  172. height: size.height,
  173. },
  174. });
  175. var edges = _this.getNodeEdges(id);
  176. var beforeNode = {
  177. x: x,
  178. y: y,
  179. width: width,
  180. height: height,
  181. radius: radius,
  182. };
  183. var afterNode = {
  184. x: _this.nodeModel.x,
  185. y: _this.nodeModel.y,
  186. width: _this.nodeModel.width,
  187. height: _this.nodeModel.height,
  188. radius: radius,
  189. };
  190. var params = {
  191. point: '',
  192. beforeNode: beforeNode,
  193. afterNode: afterNode,
  194. };
  195. // 更新边
  196. var afterPoint;
  197. edges.sourceEdges.forEach(function (item) {
  198. params.point = item.startPoint;
  199. afterPoint = getRectResizeEdgePoint(params);
  200. item.updateStartPoint(afterPoint);
  201. });
  202. edges.targetEdges.forEach(function (item) {
  203. params.point = item.endPoint;
  204. afterPoint = getRectResizeEdgePoint(params);
  205. item.updateEndPoint(afterPoint);
  206. });
  207. _this.eventEmit({ beforeNode: beforeNode, afterNode: afterNode });
  208. };
  209. // 椭圆更新
  210. _this.updateEllipse = function (_a) {
  211. var deltaX = _a.deltaX, deltaY = _a.deltaY;
  212. var _b = _this.nodeModel, id = _b.id, rx = _b.rx, ry = _b.ry, x = _b.x, y = _b.y, PCTResizeInfo = _b.PCTResizeInfo;
  213. var index = _this.index;
  214. var width = rx;
  215. var height = ry;
  216. var size = _this.getResize({
  217. index: index,
  218. deltaX: deltaX,
  219. deltaY: deltaY,
  220. width: width,
  221. height: height,
  222. PCTResizeInfo: PCTResizeInfo,
  223. pct: 1 / 2,
  224. });
  225. // 限制放大缩小的最大最小范围
  226. var _c = _this.nodeModel, minWidth = _c.minWidth, minHeight = _c.minHeight, maxWidth = _c.maxWidth, maxHeight = _c.maxHeight;
  227. if (size.width < (minWidth / 2)
  228. || size.width > (maxWidth / 2)
  229. || size.height < (minHeight / 2)
  230. || size.height > (maxHeight / 2)) {
  231. _this.dragHandler.cancelDrag();
  232. return;
  233. }
  234. // 更新中心点位置,更新文案位置
  235. _this.updatePosition({ deltaX: size.deltaX, deltaY: size.deltaY });
  236. // 更新rx ry,宽高为计算属性自动更新
  237. // @ts-ignore
  238. _this.nodeModel.rx = size.width;
  239. // @ts-ignore
  240. _this.nodeModel.ry = size.height;
  241. _this.nodeModel.setProperties({
  242. nodeSize: {
  243. rx: size.width,
  244. ry: size.height,
  245. },
  246. });
  247. var edges = _this.getNodeEdges(id);
  248. var beforeNode = { x: x, y: y };
  249. var afterNode = {
  250. rx: size.width,
  251. ry: size.height,
  252. x: _this.nodeModel.x,
  253. y: _this.nodeModel.y,
  254. };
  255. var params = {
  256. point: {},
  257. beforeNode: beforeNode,
  258. afterNode: afterNode,
  259. };
  260. // 更新边
  261. var afterPoint;
  262. edges.sourceEdges.forEach(function (item) {
  263. params.point = item.startPoint;
  264. afterPoint = getEllipseResizeEdgePoint(params);
  265. item.updateStartPoint(afterPoint);
  266. });
  267. edges.targetEdges.forEach(function (item) {
  268. params.point = item.endPoint;
  269. afterPoint = getEllipseResizeEdgePoint(params);
  270. item.updateEndPoint(afterPoint);
  271. });
  272. _this.eventEmit({ beforeNode: __assign(__assign({}, beforeNode), { rx: rx, ry: ry }), afterNode: afterNode });
  273. };
  274. // 菱形更新
  275. _this.updateDiamond = function (_a) {
  276. var deltaX = _a.deltaX, deltaY = _a.deltaY;
  277. var _b = _this.nodeModel, id = _b.id, rx = _b.rx, ry = _b.ry, x = _b.x, y = _b.y, PCTResizeInfo = _b.PCTResizeInfo;
  278. var index = _this.index;
  279. var width = rx;
  280. var height = ry;
  281. var size = _this.getResize({
  282. index: index,
  283. deltaX: deltaX,
  284. deltaY: deltaY,
  285. width: width,
  286. height: height,
  287. PCTResizeInfo: PCTResizeInfo,
  288. pct: 1 / 2,
  289. });
  290. // 限制放大缩小的最大最小范围
  291. var _c = _this.nodeModel, minWidth = _c.minWidth, minHeight = _c.minHeight, maxWidth = _c.maxWidth, maxHeight = _c.maxHeight;
  292. if (size.width < (minWidth / 2)
  293. || size.width > (maxWidth / 2)
  294. || size.height < (minHeight / 2)
  295. || size.height > (maxHeight / 2)) {
  296. _this.dragHandler.cancelDrag();
  297. return;
  298. }
  299. // 更新中心点位置,更新文案位置
  300. _this.updatePosition({ deltaX: size.deltaX, deltaY: size.deltaY });
  301. // 更新rx ry,宽高为计算属性自动更新
  302. // @ts-ignore
  303. _this.nodeModel.rx = size.width;
  304. // @ts-ignore
  305. _this.nodeModel.ry = size.height;
  306. _this.nodeModel.setProperties({
  307. nodeSize: {
  308. rx: size.width,
  309. ry: size.height,
  310. },
  311. });
  312. var beforeNode = { x: x, y: y, rx: rx, ry: ry };
  313. var afterNode = {
  314. rx: size.width,
  315. ry: size.height,
  316. x: _this.nodeModel.x,
  317. y: _this.nodeModel.y,
  318. };
  319. var params = {
  320. point: {},
  321. beforeNode: beforeNode,
  322. afterNode: afterNode,
  323. };
  324. // 更新边
  325. var afterPoint;
  326. var edges = _this.getNodeEdges(id);
  327. edges.sourceEdges.forEach(function (item) {
  328. params.point = item.startPoint;
  329. afterPoint = getDiamondResizeEdgePoint(params);
  330. item.updateStartPoint(afterPoint);
  331. });
  332. edges.targetEdges.forEach(function (item) {
  333. params.point = item.endPoint;
  334. afterPoint = getDiamondResizeEdgePoint(params);
  335. item.updateEndPoint(afterPoint);
  336. });
  337. _this.eventEmit({ beforeNode: beforeNode, afterNode: afterNode });
  338. };
  339. _this.eventEmit = function (_a) {
  340. var beforeNode = _a.beforeNode, afterNode = _a.afterNode;
  341. var _b = _this.nodeModel, id = _b.id, modelType = _b.modelType, type = _b.type;
  342. var oldNodeSize = __assign({ id: id, modelType: modelType, type: type }, beforeNode);
  343. var newNodeSize = __assign({ id: id, modelType: modelType, type: type }, afterNode);
  344. _this.graphModel.eventCenter.emit('node:resize', { oldNodeSize: oldNodeSize, newNodeSize: newNodeSize });
  345. };
  346. _this.onDragging = function (_a) {
  347. var _b;
  348. var deltaX = _a.deltaX, deltaY = _a.deltaY;
  349. var transformModel = _this.graphModel.transformModel;
  350. var modelType = _this.nodeModel.modelType;
  351. _b = __read(transformModel.fixDeltaXY(deltaX, deltaY), 2), deltaX = _b[0], deltaY = _b[1];
  352. // html和矩形的计算方式是一样的,共用一个方法
  353. if (modelType === ModelType.RECT_NODE || modelType === ModelType.HTML_NODE) {
  354. _this.updateRect({ deltaX: deltaX, deltaY: deltaY });
  355. // this.nodeModel.resize(deltaX, deltaY);
  356. }
  357. else if (modelType === ModelType.ELLIPSE_NODE) {
  358. _this.updateEllipse({ deltaX: deltaX, deltaY: deltaY });
  359. }
  360. else if (modelType === ModelType.DIAMOND_NODE) {
  361. _this.updateDiamond({ deltaX: deltaX, deltaY: deltaY });
  362. }
  363. };
  364. /**
  365. * 由于将拖拽放大缩小改成丝滑模式,这个时候需要在拖拽结束的时候,将节点的位置更新到grid上.
  366. */
  367. _this.onDragEnd = function () {
  368. var _a = _this.graphModel.gridSize, gridSize = _a === void 0 ? 1 : _a;
  369. var x = gridSize * Math.round(_this.nodeModel.x / gridSize);
  370. var y = gridSize * Math.round(_this.nodeModel.y / gridSize);
  371. _this.nodeModel.moveTo(x, y);
  372. };
  373. _this.index = props.index;
  374. _this.nodeModel = props.model;
  375. _this.graphModel = props.graphModel;
  376. // 为保证对齐线功能正常使用,step默认是网格grid的两倍,
  377. // 没有网格设置,默认为2,保证坐标是整数
  378. // let step = 2;
  379. // if (gridSize > 1) {
  380. // step = 2 * gridSize;
  381. // }
  382. // if (this.nodeModel.gridSize) {
  383. // step = 2 * this.nodeModel.gridSize;
  384. // }
  385. _this.state = {};
  386. _this.dragHandler = new StepDrag({
  387. onDragging: _this.onDragging,
  388. onDragEnd: _this.onDragEnd,
  389. step: 1,
  390. });
  391. return _this;
  392. }
  393. Control.prototype.getNodeEdges = function (nodeId) {
  394. var graphModel = this.graphModel;
  395. var edges = graphModel.edges;
  396. var sourceEdges = [];
  397. var targetEdges = [];
  398. for (var i = 0; i < edges.length; i++) {
  399. var edgeModel = edges[i];
  400. if (edgeModel.sourceNodeId === nodeId) {
  401. sourceEdges.push(edgeModel);
  402. }
  403. else if (edges[i].targetNodeId === nodeId) {
  404. targetEdges.push(edgeModel);
  405. }
  406. }
  407. return { sourceEdges: sourceEdges, targetEdges: targetEdges };
  408. };
  409. Control.prototype.render = function () {
  410. var _a = this.props, x = _a.x, y = _a.y, index = _a.index, model = _a.model;
  411. var style = model.getControlPointStyle();
  412. return (h("g", { className: "lf-resize-control-" + index },
  413. h(Rect, __assign({ className: "lf-node-control" }, { x: x, y: y }, style, { onMouseDown: this.dragHandler.handleMouseDown }))));
  414. };
  415. return Control;
  416. }(Component));
  417. export default Control;