NodeSql.ts 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. import { HtmlNode, HtmlNodeModel, h } from '@logicflow/core';
  2. class SqlNode extends HtmlNode {
  3. /**
  4. * 1.1.7版本后支持在view中重写锚点形状。
  5. * 重写锚点新增
  6. */
  7. getAnchorShape(anchorData) {
  8. const { x, y, type } = anchorData;
  9. return h('rect', {
  10. x: x - 5,
  11. y: y - 5,
  12. width: 10,
  13. height: 10,
  14. className: `custom-anchor ${type === 'left' ? 'incomming-anchor' : 'outgoing-anchor'
  15. }`,
  16. });
  17. }
  18. setHtml(rootEl) {
  19. rootEl.innerHTML = '';
  20. const {
  21. properties: { fields, tableName },
  22. } = this.props.model;
  23. rootEl.setAttribute('class', 'table-container');
  24. const container = document.createElement('div');
  25. container.className = `table-node table-color-${Math.ceil(
  26. Math.random() * 4,
  27. )}`;
  28. const tableNameElement = document.createElement('div');
  29. tableNameElement.innerText = tableName;
  30. tableNameElement.className = 'table-name';
  31. container.appendChild(tableNameElement);
  32. const fragment = document.createDocumentFragment();
  33. for (let i = 0; i < fields.length; i++) {
  34. const item = fields[i];
  35. const itemElement = document.createElement('div');
  36. itemElement.className = 'table-feild';
  37. const itemKey = document.createElement('span');
  38. itemKey.innerText = item.key;
  39. const itemType = document.createElement('span');
  40. itemType.innerText = item.type;
  41. itemType.className = 'feild-type';
  42. itemElement.appendChild(itemKey);
  43. itemElement.appendChild(itemType);
  44. fragment.appendChild(itemElement);
  45. }
  46. container.appendChild(fragment);
  47. rootEl.appendChild(container);
  48. }
  49. }
  50. class SqlNodeModel extends HtmlNodeModel {
  51. /**
  52. * 给model自定义添加字段方法
  53. */
  54. addField(item) {
  55. this.properties.fields.unshift(item);
  56. this.setAttributes();
  57. // 为了保持节点顶部位置不变,在节点变化后,对节点进行一个位移,位移距离为添加高度的一半。
  58. this.move(0, 24 / 2);
  59. // 更新节点连接边的path
  60. this.incoming.edges.forEach((egde) => {
  61. // 调用自定义的更新方案
  62. egde.updatePathByAnchor();
  63. });
  64. this.outgoing.edges.forEach((edge) => {
  65. // 调用自定义的更新方案
  66. edge.updatePathByAnchor();
  67. });
  68. }
  69. getOutlineStyle() {
  70. const style = super.getOutlineStyle();
  71. style.stroke = 'none';
  72. style.hover.stroke = 'none';
  73. return style;
  74. }
  75. // 如果不用修改锚地形状,可以重写颜色相关样式
  76. getAnchorStyle(anchorInfo) {
  77. const style = super.getAnchorStyle();
  78. if (anchorInfo.type === 'left') {
  79. style.fill = 'red';
  80. style.hover.fill = 'transparent';
  81. style.hover.stroke = 'transpanrent';
  82. style.className = 'lf-hide-default';
  83. } else {
  84. style.fill = 'green';
  85. }
  86. return style;
  87. }
  88. setAttributes() {
  89. this.width = 200;
  90. const {
  91. properties: { fields },
  92. } = this;
  93. this.height = 60 + fields.length * 24;
  94. const circleOnlyAsTarget = {
  95. message: '只允许从右边的锚点连出',
  96. validate: (sourceNode, targetNode, sourceAnchor) => {
  97. return sourceAnchor.type === 'right';
  98. },
  99. };
  100. this.sourceRules.push(circleOnlyAsTarget);
  101. this.targetRules.push({
  102. message: '只允许连接左边的锚点',
  103. validate: (sourceNode, targetNode, sourceAnchor, targetAnchor) => {
  104. return targetAnchor.type === 'left';
  105. },
  106. });
  107. }
  108. getDefaultAnchor() {
  109. const {
  110. id,
  111. x,
  112. y,
  113. width,
  114. height,
  115. isHovered,
  116. isSelected,
  117. properties: { fields, isConnection },
  118. } = this;
  119. const anchors = [];
  120. fields.forEach((feild, index) => {
  121. // 如果是连出,就不显示左边的锚点
  122. if (isConnection || !(isHovered || isSelected)) {
  123. anchors.push({
  124. x: x - width / 2 + 10,
  125. y: y - height / 2 + 60 + index * 24,
  126. id: `${id}_${feild.key}_left`,
  127. edgeAddable: false,
  128. type: 'left',
  129. });
  130. }
  131. if (!isConnection) {
  132. anchors.push({
  133. x: x + width / 2 - 10,
  134. y: y - height / 2 + 60 + index * 24,
  135. id: `${id}_${feild.key}_right`,
  136. type: 'right',
  137. });
  138. }
  139. });
  140. return anchors;
  141. }
  142. }
  143. export default {
  144. type: 'sql-node',
  145. model: SqlNodeModel,
  146. view: SqlNode,
  147. };