import * as d3 from 'd3'; import * as EventAPI from 'events'; import * as dagre from 'dagre'; import CanvasView, { initDraggable } from './canvas-view'; import GraphView, { InitialSettings as GraphViewSettings, IGraph, ILayout } from './graph-view'; import NodeView, { InitialSettings as NodeViewSettings, INode } from './node-view'; import EdgeView, { InitialSettings as EdgeViewSettings, IEdge } from './edge-view'; import SlotView, { InitialSettings as SlotViewSettings, ISlot } from './slot-view'; import plugins from './plugin'; import { GenericConstructor, d3Selection, cache, ICoord } from './util'; import { GEDITOR_INSTANCE_KEY, GEDITOR_PLUGIN_INSTANCE_KEY, DAG_AUTO_LAYOUT_CONF, NODE_UI } from './constants'; import './static/index.less'; export interface InitialSettings { /** 挂载 geditor 的 dom 节点 */ dom: Element; /** 图数据结构 */ graph?: IGraph; /** 图布局信息 */ layout?: ILayout; /** 图是否可编辑 */ editable?: boolean; /** * methods 是提供给 geditor 的二次开发者和其面向的上层业务之间通信的方式 * 比如: * dag-editor 的算子被点击以后需要展示代表运行结果的圆圈组, * 圆圈组的颜色取决于算子的运行结果是否存在。 * dag-editor 可以在 methods 中定义名为 `isOutputExists` 的方法, * dag-editor 的使用者必须实现该方法,以便 dag-editor 可以调用该方法来获知输出是否存在, * 从而决定如何绘制圆圈组。 */ methods?: { [key: string]: any; }; } export default class GEditor extends EventAPI { private layout = {}; public canvasView: CanvasView; public graphView: GraphView; public methods: InitialSettings['methods']; constructor(settings: InitialSettings) { super(); if (!settings.dom) { throw new Error('settings.dom is required to mount the geditor'); } const { graph, dom, editable, methods } = settings; if ((dom as any)[GEDITOR_INSTANCE_KEY] instanceof GEditor) { throw new Error(`GEditor has initialized already.`); } this.initPlugin(); let { layout } = settings; if (!this.validateLayout(graph, layout)) { layout = this.autoLayout(graph); } this.canvasView = new CanvasView({ dom, layout, geditor: this }); this.graphView = new GraphView({ graph, layout, editable, $canvas: this.canvasView.$el, geditor: this, }); this.canvasView.setGraphView(this.graphView); Object.defineProperty(dom, GEDITOR_INSTANCE_KEY, { get: () => this, }); this.methods = methods || {}; } // @TODO 添加一个检查 graph 合法性的方法 private validateLayout(graph: IGraph, layout: ILayout) { if (!layout || !layout.graph || !layout.nodes) { return false; } const nodeIds = Object.keys(layout.nodes); if (nodeIds.length !== Object.keys(graph.nodes).length) { console.error( `Detected unmatch of the nodes amount between the layout.nodes and graph.nodes,\ auto layouting will be applied.`, ); return false; } const inexistsNodeId = nodeIds.find(nodeId => !graph.nodes[nodeId]); if (inexistsNodeId) { console.error( `Detected node[id=${inexistsNodeId}] of layout.nodes does not exits in graph.nodes,\ auto layouting will be applied.`, ); return false; } return true; } protected autoLayout(graph: IGraph) { const layout: ILayout = { graph: { translate: [0, 0], scale: 1, }, nodes: { }, }; const g = new dagre.graphlib.Graph(); g.setGraph(DAG_AUTO_LAYOUT_CONF); g.setDefaultEdgeLabel(() => ({})); const nodes = graph.nodes || {}; const edges = graph.edges || {}; Object.keys(nodes).forEach((nodeId) => { g.setNode(nodeId, { width: NODE_UI.WIDTH, height: NODE_UI.HEIGHT, }); }); Object.values(edges).forEach((edge) => { g.setEdge(edge.srcNodeId, edge.destNodeId); }); dagre.layout(g); g.nodes().forEach((nodeId) => { const position = g.node(nodeId); if (position) { // 结果多出来一个 undefined layout.nodes[nodeId] = [position.x, position.y]; } }); return layout; } public destroy () { this.graphView.destroy(); this.removeAllListeners(); } public isActive () { const svg = this.canvasView.$el.node() as SVGElement; return svg.getBoundingClientRect().width > 0; } /** * nodeView 工厂方法 */ public getNodeViewClass(nodeConfig: INode): GenericConstructor { return NodeView; } /** * edgeView 工厂方法 */ public getEdgeViewClass(edgeConfig: IEdge): GenericConstructor { return EdgeView; } /** * slotView 工厂方法 */ public getSlotViewClass(slotConfig: ISlot): GenericConstructor { return SlotView; } /** * slot 是否可连接,默认全部可连 * 不同的 editor 请根据各自的业务逻辑实现该方法 */ public connectable(srcSlot: SlotView, destSlot: SlotView, graphView: GraphView): boolean { return true; } public initPlugin () { const containers = Array.from(document.querySelectorAll('[geditor-plugin]')); containers.forEach((dom) => { const name = dom.getAttribute('geditor-plugin'); if (!plugins[name]) { console.error(`Unkown plugin "${name}"`); return; } if (!(dom as any)[GEDITOR_PLUGIN_INSTANCE_KEY]) { const instance = new plugins[name]({ dom, geditor: this, }); Object.defineProperty(dom, GEDITOR_PLUGIN_INSTANCE_KEY, { get: () => instance, }); } }); } static getInstanceByDom(dom: Element | string): (GEditor | null) { const el = dom instanceof Element ? dom : document.querySelector(dom); if (el) { return (el as any)[GEDITOR_INSTANCE_KEY]; } return null; } static draggable(dom: Element, nodeConfig: INode) { initDraggable(dom, nodeConfig); } static destroy (dom: Element | string) { const instance = this.getInstanceByDom(dom); if (instance) { instance.destroy(); } } }