UNPKG

plot-plan-designer

Version:

Design Editor Tools with React.js + ant.design + fabric.js

93 lines (92 loc) 4.36 kB
import React, { Component } from 'react'; import { Space } from 'antd'; import PropTypes from 'prop-types'; import i18n from 'i18next'; import CommonButton from '../common/CommonButton'; class WorkflowToolbar extends Component { constructor() { super(...arguments); this.state = { interactionMode: 'selection', }; this.handlers = { selection: () => { this.props.canvasRef.handler.interactionHandler.selection(); this.setState({ interactionMode: 'selection' }); }, grab: () => { this.props.canvasRef.handler.interactionHandler.grab(); this.setState({ interactionMode: 'grab' }); }, }; this.events = { keydown: (e) => { if (this.props.canvasRef.canvas.wrapperEl !== document.activeElement) { return false; } if (e.keyCode === 81) { this.handlers.selection(); } else if (e.keyCode === 87) { this.handlers.grab(); } }, }; this.waitForCanvasRender = (canvas) => { setTimeout(() => { if (canvas) { this.attachEventListener(canvas); return; } const { canvasRef } = this.props; this.waitForCanvasRender(canvasRef); }, 5); }; this.attachEventListener = (canvasRef) => { canvasRef.canvas.wrapperEl.addEventListener('keydown', this.events.keydown, false); }; this.detachEventListener = (canvasRef) => { canvasRef.canvas.wrapperEl.removeEventListener('keydown', this.events.keydown); }; } componentDidMount() { const { canvasRef } = this.props; this.waitForCanvasRender(canvasRef); } componentWillUnmount() { const { canvasRef } = this.props; this.detachEventListener(canvasRef); } render() { const { canvasRef, zoomRatio } = this.props; const { interactionMode } = this.state; const { selection, grab } = this.handlers; const zoomValue = parseInt((zoomRatio * 100).toFixed(2), 10); return (React.createElement(React.Fragment, null, React.createElement("div", { className: "rde-editor-toolbar-interaction" }, React.createElement(Space.Compact, null, React.createElement(CommonButton, { type: interactionMode === 'selection' ? 'primary' : 'default', style: { borderBottomLeftRadius: '8px', borderTopLeftRadius: '8px' }, onClick: () => { selection(); }, icon: "mouse-pointer", tooltipTitle: i18n.t('action.selection') }), React.createElement(CommonButton, { type: interactionMode === 'grab' ? 'primary' : 'default', style: { borderBottomRightRadius: '8px', borderTopRightRadius: '8px' }, onClick: () => { grab(); }, tooltipTitle: i18n.t('action.grab'), icon: "hand-rock" }))), React.createElement("div", { className: "rde-editor-toolbar-zoom" }, React.createElement(Space.Compact, null, React.createElement(CommonButton, { style: { borderBottomLeftRadius: '8px', borderTopLeftRadius: '8px' }, onClick: () => { canvasRef.handler.zoomHandler.zoomIn(); }, icon: "search-plus", tooltipTitle: i18n.t('action.zoom-in') }), React.createElement(CommonButton, { onClick: () => { canvasRef.handler.zoomHandler.zoomOneToOne(); }, tooltipTitle: i18n.t('action.one-to-one') }, `${zoomValue}%`), React.createElement(CommonButton, { style: { borderBottomRightRadius: '8px', borderTopRightRadius: '8px' }, onClick: () => { canvasRef.handler.zoomHandler.zoomOut(); }, icon: "search-minus", tooltipTitle: i18n.t('action.zoom-out') }))))); } } WorkflowToolbar.propTypes = { canvasRef: PropTypes.any, selectedItem: PropTypes.object, zoomRatio: PropTypes.number, }; export default WorkflowToolbar;