import * as d3 from 'd3'; import * as Mousetrap from 'mousetrap'; import GEditor from './geditor'; import GraphView, { ILayout } from './graph-view'; import NodeView, { INode } from './node-view'; import { d3Selection, cache, ICoord } from './util'; import { DRAGGING_NODE_CONFIG_KEY, SCALE_RANGE, EVENTS } from './constants'; export interface InitialSettings { dom: Element; geditor: GEditor; layout: ILayout; } export default class CanvasView { private dom: Element; public $el: d3Selection; private graphView: GraphView; private geditor: GEditor; private zoomHandler: d3.ZoomBehavior; constructor (settings: InitialSettings) { const { dom, geditor, layout } = settings; this.dom = dom; this.geditor = geditor; this.$el = d3.select(dom).append('svg') .attr('class', 'x-canvas') .attr('width', '100%') .attr('height', '100%') .attr('tabindex', 1); this.createZoomHandler(layout); this.initEvents(); } private initEvents () { this.$el .on('mouseenter', () => this.createNodeByDrag()) .on('contextmenu', () => this.onContextMenu()); /** * @TODO this.$el.on('click') 绑定的 click 事件不生效,暂时未找到原因,使用原生事件 API 代替 */ (this.$el.node() as SVGElement).addEventListener('click', () => this.onClick(), false); const mousetrap = new Mousetrap(this.$el.node() as SVGElement); mousetrap.bind('backspace', () => { this.graphView.removeSelections(); return false; }); mousetrap.bind('del', () => { this.graphView.removeSelections(); return false; }); /** * User should listen COPY event and save the selections for pasting later */ mousetrap.bind(['ctrl+c', 'command+c'], () => { const selections = this.graphView.getSelections(); this.geditor.emit(EVENTS.COPY_SHORTCUT, selections); }); /** * User should listen PASTE event, and call `graphView.createByCopying(selections)` * to implement the copy/paste function. * selections.nodes and selections.edges should be array of node/edge config, * rather than the array of node/edge id from COPY events. */ mousetrap.bind(['ctrl+v', 'command+v'], () => { this.geditor.emit(EVENTS.PASTE_SHORTCUT); }); this.$el.call(this.zoomHandler) .on('dblclick.zoom', null); // 禁用双击缩放 } private createZoomHandler (layout: ILayout) { this.zoomHandler = d3.zoom() .scaleExtent([SCALE_RANGE.MIN, SCALE_RANGE.MAX]) .on('zoom', () => this.onZoom()); this.resetZoomIdentity(layout.graph); } public resetZoomIdentity (options: ILayout['graph']) { const currTransform = this.graphView && this.graphView.getTransform(); const translate = options.translate || currTransform.translate; const scale = options.scale || currTransform.scale; const zoomIdentity = d3.zoomIdentity .translate(translate[0], translate[1]) .scale(scale); this.$el.call(this.zoomHandler.transform, zoomIdentity); } protected createNodeByDrag () { const nodeConfig = cache.get(DRAGGING_NODE_CONFIG_KEY); if (!nodeConfig) return; const translate = getTranslateByMouseCoord(this.$el, this.graphView); const nodeView = this.graphView.createNode(nodeConfig, translate); nodeView.manuallyTriggerDragEvent(d3.event); cache.remove(DRAGGING_NODE_CONFIG_KEY); } protected onZoom () { const transform = d3.event.transform; this.geditor.emit(EVENTS.CANVAS_ZOOMED, { translate: [transform.x, transform.y], scale: transform.k, }); if (this.graphView) { this.graphView.updateTransform({ translate: [transform.x, transform.y], scale: transform.k, }); } } protected onContextMenu () { d3.event.preventDefault(); this.geditor.emit(EVENTS.CANVAS_CONTEXTMENU, d3.event); } protected onClick () { this.geditor.emit(EVENTS.CANVAS_CLICKED, d3.event); } public setGraphView (graphView: GraphView) { this.graphView = graphView; } } export function initDraggable(dom: Element, nodeConfig: INode) { const drag = d3.drag() .on('start', () => { cache.set(DRAGGING_NODE_CONFIG_KEY, nodeConfig); }) .on('end', () => { cache.remove(DRAGGING_NODE_CONFIG_KEY); }); d3.select(dom).call(drag); } function getTranslateByMouseCoord($canvas: d3Selection, graphView: GraphView): [number, number] { const clientY = d3.event.clientY; const { translate, scale } = graphView.getTransform(); const [tx, ty] = translate; const canvasClientTop = ($canvas.node() as Element).getBoundingClientRect().top; const x = -1 * tx / scale; const y = (clientY - canvasClientTop - ty) / scale; return [x, y]; }