Control.js 18 KB

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