UNPKG

plot-plan-designer

Version:

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

94 lines (93 loc) 4.1 kB
import React, { forwardRef, useEffect, useRef, useState } from 'react'; import PropTypes from 'prop-types'; import { Modal, Form, Input } from 'antd'; import i18n from 'i18next'; import Canvas from '../../canvas/Canvas'; import StyleProperty from '../properties/StyleProperty'; const StyleModal = forwardRef(({ visible, style = {}, onOk, onCancel, validateTitle, onChange }) => { const [form] = Form.useForm(); const containerRef = useRef(null); const canvasRef = useRef(null); const [dimensions, setDimensions] = useState({ width: 150, height: 150 }); const defaultStyle = { fill: 'rgba(0, 0, 0, 1)', opacity: 1, stroke: 'rgba(255, 255, 255, 0)', strokeWidth: 1, }; useEffect(() => { const waitForContainerRender = (container) => { if (!container) { setTimeout(() => waitForContainerRender(containerRef.current), 5); return; } const width = container.clientWidth; const height = container.clientHeight; setDimensions({ width, height }); const option = { type: 'i-text', text: '\uf3c5', fontFamily: 'Font Awesome 5 Free', fontWeight: 900, fontSize: 60, width: 30, height: 30, editable: false, name: 'New marker', tooltip: { enabled: false, }, left: 200, top: 50, id: 'styles', }; if (canvasRef.current) { canvasRef.current.handler.add(option); } }; if (visible) { waitForContainerRender(containerRef.current); } }, [visible]); useEffect(() => { const currentStyle = Object.assign(Object.assign({}, defaultStyle), style); delete currentStyle.strokeDashArray; const waitForCanvasRender = (canvas) => { if (!canvas) { setTimeout(() => waitForCanvasRender(canvasRef.current), 5); return; } Object.keys(currentStyle).forEach((key) => { canvas.handlers.setById('styles', key, currentStyle[key]); }); }; if (visible) { waitForCanvasRender(canvasRef.current); form.resetFields(); } }, [style, visible, form]); return (React.createElement(Modal, { open: visible, onOk: onOk, onCancel: onCancel, title: i18n.t('common.title') }, React.createElement(Form, { form: form, layout: "vertical" }, React.createElement(Form.Item, { label: i18n.t('common.title'), required: true, hasFeedback: true, help: validateTitle === null || validateTitle === void 0 ? void 0 : validateTitle.help, validateStatus: validateTitle === null || validateTitle === void 0 ? void 0 : validateTitle.validateStatus }, React.createElement(Input, { value: style === null || style === void 0 ? void 0 : style.title, onChange: (e) => { const updatedTitle = e.target.value; onChange === null || onChange === void 0 ? void 0 : onChange(null, { title: updatedTitle }, Object.assign(Object.assign({}, style), { title: updatedTitle })); } })), StyleProperty.render(canvasRef.current, form, style)), React.createElement("div", { ref: containerRef }, React.createElement(Canvas, { ref: canvasRef, editable: false, canvasOption: { width: dimensions.width, height: dimensions.height, backgroundColor: '#f3f3f3', }, workareaOption: { backgroundColor: 'transparent' } })))); }); StyleModal.displayName = 'StyleModal'; StyleModal.propTypes = { visible: PropTypes.bool, style: PropTypes.object, onOk: PropTypes.func, onCancel: PropTypes.func, validateTitle: PropTypes.object, onChange: PropTypes.func, }; export default StyleModal;