| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150 |
- import { HtmlNode, HtmlNodeModel, h } from '@logicflow/core';
- class SqlNode extends HtmlNode {
- /**
- * 1.1.7版本后支持在view中重写锚点形状。
- * 重写锚点新增
- */
- getAnchorShape(anchorData) {
- const { x, y, type } = anchorData;
- return h('rect', {
- x: x - 5,
- y: y - 5,
- width: 10,
- height: 10,
- className: `custom-anchor ${type === 'left' ? 'incomming-anchor' : 'outgoing-anchor'
- }`,
- });
- }
- setHtml(rootEl) {
- rootEl.innerHTML = '';
- const {
- properties: { fields, tableName },
- } = this.props.model;
- rootEl.setAttribute('class', 'table-container');
- const container = document.createElement('div');
- container.className = `table-node table-color-${Math.ceil(
- Math.random() * 4,
- )}`;
- const tableNameElement = document.createElement('div');
- tableNameElement.innerText = tableName;
- tableNameElement.className = 'table-name';
- container.appendChild(tableNameElement);
- const fragment = document.createDocumentFragment();
- for (let i = 0; i < fields.length; i++) {
- const item = fields[i];
- const itemElement = document.createElement('div');
- itemElement.className = 'table-feild';
- const itemKey = document.createElement('span');
- itemKey.innerText = item.key;
- const itemType = document.createElement('span');
- itemType.innerText = item.type;
- itemType.className = 'feild-type';
- itemElement.appendChild(itemKey);
- itemElement.appendChild(itemType);
- fragment.appendChild(itemElement);
- }
- container.appendChild(fragment);
- rootEl.appendChild(container);
- }
- }
- class SqlNodeModel extends HtmlNodeModel {
- /**
- * 给model自定义添加字段方法
- */
- addField(item) {
- this.properties.fields.unshift(item);
- this.setAttributes();
- // 为了保持节点顶部位置不变,在节点变化后,对节点进行一个位移,位移距离为添加高度的一半。
- this.move(0, 24 / 2);
- // 更新节点连接边的path
- this.incoming.edges.forEach((egde) => {
- // 调用自定义的更新方案
- egde.updatePathByAnchor();
- });
- this.outgoing.edges.forEach((edge) => {
- // 调用自定义的更新方案
- edge.updatePathByAnchor();
- });
- }
- getOutlineStyle() {
- const style = super.getOutlineStyle();
- style.stroke = 'none';
- style.hover.stroke = 'none';
- return style;
- }
- // 如果不用修改锚地形状,可以重写颜色相关样式
- getAnchorStyle(anchorInfo) {
- const style = super.getAnchorStyle();
- if (anchorInfo.type === 'left') {
- style.fill = 'red';
- style.hover.fill = 'transparent';
- style.hover.stroke = 'transpanrent';
- style.className = 'lf-hide-default';
- } else {
- style.fill = 'green';
- }
- return style;
- }
- setAttributes() {
- this.width = 200;
- const {
- properties: { fields },
- } = this;
- this.height = 60 + fields.length * 24;
- const circleOnlyAsTarget = {
- message: '只允许从右边的锚点连出',
- validate: (sourceNode, targetNode, sourceAnchor) => {
- return sourceAnchor.type === 'right';
- },
- };
- this.sourceRules.push(circleOnlyAsTarget);
- this.targetRules.push({
- message: '只允许连接左边的锚点',
- validate: (sourceNode, targetNode, sourceAnchor, targetAnchor) => {
- return targetAnchor.type === 'left';
- },
- });
- }
- getDefaultAnchor() {
- const {
- id,
- x,
- y,
- width,
- height,
- isHovered,
- isSelected,
- properties: { fields, isConnection },
- } = this;
- const anchors = [];
- fields.forEach((feild, index) => {
- // 如果是连出,就不显示左边的锚点
- if (isConnection || !(isHovered || isSelected)) {
- anchors.push({
- x: x - width / 2 + 10,
- y: y - height / 2 + 60 + index * 24,
- id: `${id}_${feild.key}_left`,
- edgeAddable: false,
- type: 'left',
- });
- }
- if (!isConnection) {
- anchors.push({
- x: x + width / 2 - 10,
- y: y - height / 2 + 60 + index * 24,
- id: `${id}_${feild.key}_right`,
- type: 'right',
- });
- }
- });
- return anchors;
- }
- }
- export default {
- type: 'sql-node',
- model: SqlNodeModel,
- view: SqlNode,
- };
|