import * as React from "react"; import {autobind} from "core-decorators"; import {observer} from "mobx-react/index"; import {Layer, Rect, Stage, Group, Line} from "react-konva"; import {DesignerHost} from "../DesignerHost"; import {SnapGridLayer} from "../layer/SnapGridLayer"; import {ItemsLayer} from "../layer/ItemsLayer"; import EditingLayer from "./EditingLayer"; import {Report} from "../../items/Report"; import {BehaviorService} from "../behavior/BehaviorService"; import {PointF, Unit} from "../../drawing/Unit"; import {IAdonerWindowControl} from "../../interfaces/Interfaces"; import {DragEventArgs} from "../DragEventArgs"; import {action} from "mobx/lib/mobx"; import {Cursor} from "../../drawing/Cursor"; import {QueryContinueDragEventArgs} from "../behavior/QueryContinueDragEventArgs"; import {DragAction, MouseButtons} from "../behavior/Enums"; import DNDManager from "../../dnd/DndManager"; import {HitMoveEvent} from "../../common/HitMoveEvent"; import FocusStates from '../../../reporting/common/FocusStates'; import {KeyStates} from "../../common/KeyStates"; const Konva = require("konva"); @observer export class AdornerWindow extends React.Component implements IAdonerWindowControl{ private _stage: any private _host: DesignerHost; private _behaviorService: BehaviorService; private _isDragging:boolean; private _dragDataObject:any; private _eventListeners:any; private _mouseEvt:MouseEvent; private _keyDownEvt: any; private _keyMoveStartTimer: number; private dndManager = DNDManager.instance(); constructor(props:any, context:any) { super(props, context); this._host = DesignerHost.instance(); this._behaviorService = this._host.getService(BehaviorService) as BehaviorService; this._behaviorService.adornerWindow = this; this.dealWithKonva(); } dealWithKonva(){ Konva.Stage.prototype._bindContentEvents = function(){ console.log('bind content event called'); } } setAppropriateCursor(cursor:Cursor):void { if (cursor) { this._stage.container().style.cursor = cursor.name; } else { this._stage.container().style.cursor = ""; } } getMousePointToStage():PointF { const mousePos = this.getPointerPositionForStage(); let pt = new PointF(-100000, -100000); if (mousePos) { pt = new PointF(mousePos.x, mousePos.y); } return pt; } getStagePosition():Object { const bounds = (this.refs["stage"] as any).domNode.getBoundingClientRect(); const pt = new PointF(bounds.left, bounds.top); return { position: pt, width: bounds.width, height: bounds.height }; } getPointerPositionForStage(){ const evt = this._mouseEvt; const point = {x:evt.pageX, y:evt.pageY}; const stageRect = this._stage.content.getBoundingClientRect(); const stagePoint = {x:stageRect.left, y:stageRect.top}; const position = this.dndManager.getMousePointToStage(point, stagePoint); return position; } @autobind onMouseEnter(evt: any) { } @autobind onMouseDown(evt: any) { this._mouseEvt = evt; const mousePos = this.getMousePointToStage(); if (!mousePos) { return; } if (this._isDragging) { return; } const pt = new PointF(mousePos.x, mousePos.y); this._behaviorService.propagateHitTest(pt); this._behaviorService.onMouseDown(new HitMoveEvent(evt), pt); } @autobind onDoubleClick(evt: any){ this._mouseEvt = evt; const mousePos = this.getMousePointToStage(); if (!mousePos) { return; } const pt = new PointF(mousePos.x, mousePos.y); this._behaviorService.propagateHitTest(pt); this._behaviorService.onDoubleClick(evt,pt); } @autobind onMouseMove(evt: any) { if(this._keyMoveStartTimer) return; this._mouseEvt = evt; if (this._isDragging) { this.onDragOver(new DragEventArgs(this._dragDataObject)); } else { const pt = this.getMousePointToStage(); this._behaviorService.propagateHitTest(pt); this._behaviorService.onMouseMove(new HitMoveEvent(evt), pt); } } @autobind onMouseUp(evt: any) { this._mouseEvt = evt; //关闭对设计区外部鼠标松开的监听 //window.removeEventListener('mouseup', this.onMouseUp); if (this._isDragging) { this.onDragDrop(new DragEventArgs(this._dragDataObject)); } else { const pt = this.getMousePointToStage(); this._behaviorService.propagateHitTest(pt); this._behaviorService.onMouseUp(); } } @autobind onMouseLeave(evt: any) { if(this._keyMoveStartTimer) return; this._mouseEvt = evt; if (this._isDragging) { this.onDragLeave(); } else if (! this._behaviorService.isDragging) { this._behaviorService.propagateHitTest(new PointF(-1, -1)); } } @autobind onMouseOver(evt: any) { if(this._keyMoveStartTimer) return; this._mouseEvt = evt; if (this._isDragging) { this.onDragOver(new DragEventArgs(this._dragDataObject)); } else { const pt = this.getMousePointToStage(); this._behaviorService.propagateHitTest(pt); this._behaviorService.onMouseOver(pt); } } doDragDrop(data:any):void { this._isDragging = true; this._dragDataObject = data; this.onDragEnter(new DragEventArgs(data)); } @action onDragEnter(args:DragEventArgs):void { const pt = this.getMousePointToStage(); this._behaviorService.validDragArgs = args; this._behaviorService.propagateHitTest(pt); } @action onDragLeave():void { this._behaviorService.validDragArgs = null; this._behaviorService.onDragLeave(null); } @action onDragDrop(args:DragEventArgs):void { this._isDragging = false; this._dragDataObject = null; const success = this._behaviorService.onDragDrop(args); this.onQueryContinueDrag(new QueryContinueDragEventArgs(DragAction.Drop)); if (success) { DesignerHost.instance().historyService.snap(); } else{ this._behaviorService.adorners.forEach(adorner => adorner.clear()); DesignerHost.instance().historyService.stayLastPoint(); } } @action onDragOver(args:DragEventArgs):void { const pt = this.getMousePointToStage(); this._behaviorService.propagateHitTest(pt); this._behaviorService.onDragOver(args, pt); this.onGiveFeedback(); this.onQueryContinueDrag(new QueryContinueDragEventArgs(DragAction.Continue)); } @action onGiveFeedback() { this.fire("giveFeedback", null); } @action onQueryContinueDrag(args: QueryContinueDragEventArgs) { this.fire("queryContinueDrag", args); } @autobind onKeyUp(evt: KeyboardEvent) { if(!FocusStates.isFocusIn(this.refs["report"]) ){ return; } KeyStates.preventDefaultEvent(evt); this._keyMoveStartTimer = null; if (this._isDragging) { this.onDragDrop(new DragEventArgs(this._dragDataObject)); this._behaviorService.propagateHitTest(this._keyDownEvt); this._behaviorService.onMouseUp(); } } @action doKeyMove(evt: HitMoveEvent, pt: PointF) { // 要记录下当前的位置 this._keyDownEvt = pt; this._behaviorService.propagateHitTest(pt); if (this._isDragging) { this._behaviorService.onDragOver(new DragEventArgs(this._dragDataObject), pt); this.onGiveFeedback(); this.onQueryContinueDrag(new QueryContinueDragEventArgs(DragAction.Continue)); } else { this._behaviorService.onMouseMove(evt, pt); } } @autobind onKeyDown(evt: KeyboardEvent) { // 找到要移动的item const curTimer = new Date().getTime(); if(!FocusStates.isFocusIn(this.refs["report"]) ){ return; } KeyStates.preventDefaultEvent(evt); if(!this._keyMoveStartTimer || (this._keyMoveStartTimer && (curTimer - this._keyMoveStartTimer) >= 90)) { this._keyMoveStartTimer = new Date().getTime(); const obj = {x:0,y:0,button: MouseButtons.Left, moveStep:true}; this._behaviorService.onKeyDown(new HitMoveEvent(obj)); } } fire(name:string, evt:any):void { if (!this._eventListeners) { return; } let events:Array = this._eventListeners[name]; if (! events) { return; } events.forEach(event => event(evt)); } addEventListener(name:string, callback:Function):void { if (!this._eventListeners) { this._eventListeners = {}; } let events:Array = this._eventListeners[name]; if (!events) { events = []; this._eventListeners[name] = events; } events.push(callback); } removeEventListener(name:string, callback:Function):void { if (!this._eventListeners) { return; } let events:Array = this._eventListeners[name]; if (! events) { return; } this._eventListeners[name] = events.filter(event => event !== callback); } componentDidMount():void { this._stage = (this.refs["stage"] as any).getStage(); // hack 手段, 防止 Stage 对 mouse event 执行 preventDefault this._stage.content.removeEventListener('mousedown', this._stage['_mousedown']); // 捕获 window 的 mouse move event. window.addEventListener('mousemove', this.onMouseMove); // 捕获 window 的mouse up event. window.addEventListener('mouseup', this.onMouseUp); window.addEventListener('keyup', this.onKeyUp); window.addEventListener('keydown', this.onKeyDown); this._host.designReport.report.on(Report.OnResizeChangeEventName, ()=> { console.log('onresize report'); this.forceUpdate(); }) } componentWillUnmount():void { window.removeEventListener('mousemove', this.onMouseMove); window.removeEventListener('mouseup', this.onMouseUp); this._host.designReport.report.dispose(); this._behaviorService.adornerWindow = null; this._eventListeners = null; } getLayer(name: string): any{ return this.refs[name]; } render():JSX.Element { const designReport = DesignerHost.instance().designReport; const stageWidth = designReport.viewPortSize.width; const stageHeight = designReport.viewPortSize.height; const stageProps = { width: stageWidth, height: stageHeight, listening: false }; return (
{/**/}
) } }