plot-plan-designer
Version:
Design Editor Tools with React.js + ant.design + fabric.js
94 lines (93 loc) • 4.07 kB
JavaScript
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 AnimationProperty from '../properties/AnimationProperty';
const AnimationModal = forwardRef(({ visible, animation, onOk, onCancel, validateTitle, onChange }) => {
const [form] = Form.useForm();
const containerRef = useRef(null);
const canvasRef = useRef(null);
const [dimensions, setDimensions] = useState({ width: 150, height: 150 });
useEffect(() => {
if (!visible && canvasRef.current) {
canvasRef.current.handler.animationHandler.stop('animations');
}
form.resetFields();
}, [visible, animation, form]);
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: 'animations',
fill: 'rgba(0, 0, 0, 1)',
stroke: 'rgba(255, 255, 255, 0)',
};
if (canvasRef.current) {
canvasRef.current.handler.add(option);
}
};
if (visible) {
waitForContainerRender(containerRef.current);
}
}, [visible]);
useEffect(() => {
const waitForCanvasRender = (canvas, anim) => {
if (!canvas) {
setTimeout(() => waitForCanvasRender(canvasRef.current, anim), 5);
return;
}
canvas.handler.setById('animations', 'animation', anim);
};
if (visible) {
waitForCanvasRender(canvasRef.current, animation);
}
}, [animation, visible]);
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.help, validateStatus: validateTitle.validateStatus },
React.createElement(Input, { value: animation === null || animation === void 0 ? void 0 : animation.title, onChange: (e) => {
const updatedTitle = e.target.value;
onChange === null || onChange === void 0 ? void 0 : onChange(null, { animation: { title: updatedTitle } }, { animation: Object.assign(Object.assign({}, animation), { title: updatedTitle }) });
} })),
AnimationProperty.render(canvasRef.current, form, {
animation,
id: 'animations',
})),
React.createElement("div", { ref: containerRef },
React.createElement(Canvas, { ref: canvasRef, editable: false, canvasOption: {
width: dimensions.width,
height: dimensions.height,
backgroundColor: '#f3f3f3',
}, workareaOption: { backgroundColor: 'transparent' } }))));
});
AnimationModal.displayName = 'AnimationModal';
AnimationModal.propTypes = {
visible: PropTypes.bool,
animation: PropTypes.object,
onOk: PropTypes.func,
onCancel: PropTypes.func,
validateTitle: PropTypes.object,
onChange: PropTypes.func,
};
export default AnimationModal;