index.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. var SelectionSelect = /** @class */ (function () {
  2. function SelectionSelect(_a) {
  3. var _this = this;
  4. var lf = _a.lf;
  5. this.__disabled = false;
  6. this.isDefaultStopMoveGraph = false;
  7. this.isWholeNode = true;
  8. this.isWholeEdge = true;
  9. this.__draw = function (ev) {
  10. var _a = _this.lf.getPointByClient(ev.clientX, ev.clientY).domOverlayPosition, x1 = _a.x, y1 = _a.y;
  11. _this.endPoint = { x: x1, y: y1 };
  12. var _b = _this.startPoint, x = _b.x, y = _b.y;
  13. var style = _this.wrapper.style;
  14. var left = x;
  15. var top = y;
  16. var width = x1 - x;
  17. var height = y1 - y;
  18. if (x1 < x) {
  19. left = x1;
  20. width = x - x1;
  21. }
  22. if (y1 < y) {
  23. top = y1;
  24. height = y - y1;
  25. }
  26. style.left = left + "px";
  27. style.top = top + "px";
  28. style.width = width + "px";
  29. style.height = height + "px";
  30. };
  31. this.__drawOff = function () {
  32. document.removeEventListener('mousemove', _this.__draw);
  33. document.removeEventListener('mouseup', _this.__drawOff);
  34. _this.wrapper.oncontextmenu = null;
  35. _this.__domContainer.removeChild(_this.wrapper);
  36. var _a = _this.startPoint, x = _a.x, y = _a.y;
  37. var _b = _this.endPoint, x1 = _b.x, y1 = _b.y;
  38. // 选区太小的情况就忽略
  39. if (Math.abs(x1 - x) < 10 && Math.abs(y1 - y) < 10) {
  40. return;
  41. }
  42. var lt = [Math.min(x, x1), Math.min(y, y1)];
  43. var rt = [Math.max(x, x1), Math.max(y, y1)];
  44. var elements = _this.lf.graphModel.getAreaElement(lt, rt, _this.isWholeEdge, _this.isWholeNode, true);
  45. var group = _this.lf.graphModel.group;
  46. elements.forEach(function (element) {
  47. // 如果节点属于分组,则不不选中节点
  48. if (!group || !group.getNodeGroup(element.id)) {
  49. _this.lf.selectElementById(element.id, true);
  50. }
  51. });
  52. _this.lf.emit('selection:selected', elements);
  53. };
  54. this.lf = lf;
  55. // 初始化isDefaultStopMoveGraph取值
  56. var stopMoveGraph = lf.getEditConfig().stopMoveGraph;
  57. this.isDefaultStopMoveGraph = stopMoveGraph;
  58. lf.openSelectionSelect = function () {
  59. _this.openSelectionSelect();
  60. };
  61. lf.closeSelectionSelect = function () {
  62. _this.closeSelectionSelect();
  63. };
  64. }
  65. SelectionSelect.prototype.render = function (lf, domContainer) {
  66. var _this = this;
  67. this.__domContainer = domContainer;
  68. lf.on('blank:mousedown', function (_a) {
  69. var e = _a.e;
  70. var config = lf.getEditConfig();
  71. // 鼠标控制滚动移动画布的时候,不能选区。
  72. if (!config.stopMoveGraph || _this.__disabled) {
  73. return;
  74. }
  75. // 禁用右键框选,修复可能导致画布出现多个框选框不消失的问题,见https://github.com/didi/LogicFlow/issues/985
  76. var isRightClick = e.button === 2;
  77. if (isRightClick) {
  78. return;
  79. }
  80. var _b = lf.getPointByClient(e.clientX, e.clientY).domOverlayPosition, x = _b.x, y = _b.y;
  81. _this.startPoint = { x: x, y: y };
  82. _this.endPoint = { x: x, y: y };
  83. var wrapper = document.createElement('div');
  84. wrapper.className = 'lf-selection-select';
  85. wrapper.oncontextmenu = function prevent(ev) {
  86. ev.preventDefault();
  87. };
  88. wrapper.style.top = _this.startPoint.y + "px";
  89. wrapper.style.left = _this.startPoint.x + "px";
  90. domContainer.appendChild(wrapper);
  91. _this.wrapper = wrapper;
  92. document.addEventListener('mousemove', _this.__draw);
  93. document.addEventListener('mouseup', _this.__drawOff);
  94. });
  95. };
  96. /**
  97. * 设置选中的灵敏度
  98. * @param isWholeEdge 是否要边的起点终点都在选区范围才算选中。默认true
  99. * @param isWholeNode 是否要节点的全部点都在选区范围才算选中。默认true
  100. */
  101. SelectionSelect.prototype.setSelectionSense = function (isWholeEdge, isWholeNode) {
  102. if (isWholeEdge === void 0) { isWholeEdge = true; }
  103. if (isWholeNode === void 0) { isWholeNode = true; }
  104. this.isWholeEdge = isWholeEdge;
  105. this.isWholeNode = isWholeNode;
  106. };
  107. /**
  108. * 开启选区
  109. */
  110. SelectionSelect.prototype.openSelectionSelect = function () {
  111. var stopMoveGraph = this.lf.getEditConfig().stopMoveGraph;
  112. if (!stopMoveGraph) {
  113. this.isDefaultStopMoveGraph = false;
  114. this.lf.updateEditConfig({
  115. stopMoveGraph: true,
  116. });
  117. }
  118. this.open();
  119. };
  120. /**
  121. * 关闭选区
  122. */
  123. SelectionSelect.prototype.closeSelectionSelect = function () {
  124. if (!this.isDefaultStopMoveGraph) {
  125. this.lf.updateEditConfig({
  126. stopMoveGraph: false,
  127. });
  128. }
  129. this.close();
  130. };
  131. SelectionSelect.prototype.open = function () {
  132. this.__disabled = false;
  133. };
  134. SelectionSelect.prototype.close = function () {
  135. this.__disabled = true;
  136. };
  137. SelectionSelect.pluginName = 'selectionSelect';
  138. return SelectionSelect;
  139. }());
  140. export { SelectionSelect };