import * as React from "react"; import * as ReactDOM from "react-dom"; import {observer} from "mobx-react/index"; import {DropTarget} from "react-dnd"; import DropTargetCollector = __ReactDnd.DropTargetCollector; import DropTargetMonitor = __ReactDnd.DropTargetMonitor; import DropTargetConnector = __ReactDnd.DropTargetConnector; import DragableLine from "./HorizonDragableLine"; import LeftPaddingBlock from "./LeftMarginBlock"; import RightPaddingBlock from "./RightMarginBlock"; import {DND_TYPES_HORIZON_RULER_LINE} from "../../../dnd/constants"; import {Report as ReportModel} from "../../../items/Report"; import {DesignerHost} from "../../DesignerHost"; import UIModel from "../../UIModel"; import DNDManager from "../../../dnd/DndManager"; import {RULE_WIDTH} from "./consts"; import {ReportRootDesigner} from "../../designer/ReportRootDesigner"; import {Unit, UnitType} from "../../../drawing/Unit"; function getStyles(props, context) { const reportDesigner = DesignerHost.instance().designReport; const rulerWidth = reportDesigner.viewPortSize.width; return { root: { left: -reportDesigner.viewPortScrollLeft, width: rulerWidth, height: RULE_WIDTH, backgroundColor: "rgba(255, 255, 255, 1)" } }; } interface Props { connectDropTarget: Function; itemTypes: string | string[]; } @observer class HorizonRuler extends React.Component { public uiModel: UIModel; private dndManager: DNDManager; private _host: DesignerHost; constructor(props: Props, context: any){ super(props, context); this._host = DesignerHost.instance(); this.uiModel = this._host.uiModel; this.dndManager = DNDManager.instance(); } render(): JSX.Element { // console.log("render horizonRuler"); const styles = getStyles.call(this, this.props, this.context); return this.props.connectDropTarget(
{this.renderTickMark()} {this.renderDragableLines()}
); } /** * 渲染标尺的拖动线 * @returns {any} */ renderDragableLines(): JSX.Element { // console.log("render marker"); const reportModel = this._host.designReport.report; return (
); } /** * 渲染刻度线 * @returns {any} */ renderTickMark(): JSX.Element { const reportDesigner: ReportRootDesigner = DesignerHost.instance().getDesigner(this._host.designReport.report); const paperWidth = reportDesigner.viewPortSize.width; const widthPx = paperWidth; const widthMm = Unit.mm(paperWidth).value; const svgStyle = { width : `${widthPx}px`, height: `20px`, }; let lines = []; let texts = []; let i = 0; for (let x = 0; (widthMm - x) > 0; x += 1, i++) { const h = (i % 10 > 0) ? (i % 5 > 0 ? "80%" : "60%") : "40%"; const xp = x*3.779527559 * reportDesigner.zoom; const line = ; lines.push(line); if(h === "40%"){ const tick = i/10; // tick===0?0:(x-(tick>9?8:4)) const text = 9?8:4))} y="10" >{tick} texts.push(text); } } return (
{lines} {texts}
); } } const dropTarget = { drop(props, monitor, component) { if (monitor.didDrop()) { // If you want, you can check whether some nested // target already handled drop return; } component.dndManager.horizonRulerDrop(props, monitor, component); // return component.reportModel.designSite.dragDropManager.drop(props, monitor, component); }, hover(props, monitor, component): void { /*// You can access the coordinates if you need them const clientOffset: {x: number, y: number} = monitor.getClientOffset(); const clientRect: ClientRect = ReactDOM.findDOMNode(component).getBoundingClientRect(); // You can check whether we"re over a nested drop target const isJustOverThisOne = monitor.isOver({shallow: true}); // You will receive hover() even for items for which canDrop() is false const canDrop = monitor.canDrop();*/ component.dndManager.horizonRulerHover(props, monitor, component); // component.reportModel.designSite.dragDropManager.hover(props, monitor, component); } }; const collect: DropTargetCollector = (connect, monitor) => ({ connectDropTarget: connect.dropTarget(), // You can ask the monitor about the current drag state: isOver: monitor.isOver(), isOverCurrent: monitor.isOver({shallow: true}), canDrop: monitor.canDrop(), }); export default DropTarget(DND_TYPES_HORIZON_RULER_LINE, dropTarget, collect)(HorizonRuler);