UNPKG

@ichigo_san/graphing

Version:

A lightweight UML-style diagram editor built with React Flow and Tailwind CSS

192 lines (191 loc) 6.6 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = void 0; var _react = _interopRequireWildcard(require("react")); var _reactflow = require("reactflow"); var _gradientUtils = require("../utils/gradientUtils"); var _shadowUtils = require("../utils/shadowUtils"); var _transformUtils = require("../utils/transformUtils"); function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function (e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != typeof e && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (const t in e) "default" !== t && {}.hasOwnProperty.call(e, t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, t)) && (i.get || i.set) ? o(f, t, i) : f[t] = e[t]); return f; })(e, t); } const handleStyle = { background: '#3b82f6', border: '2px solid #fff', width: 8, height: 8, borderRadius: '50%', zIndex: 10, cursor: 'crosshair', transition: 'all 0.15s ease', opacity: 0.7 }; const DiamondNode = ({ data, id, selected, isConnectable }) => { const [isEditing, setIsEditing] = (0, _react.useState)(false); const [label, setLabel] = (0, _react.useState)(data.label || 'Diamond'); (0, _react.useEffect)(() => { setLabel(data.label || 'Diamond'); }, [data.label]); const inputRef = (0, _react.useRef)(null); const handleDoubleClick = (0, _react.useCallback)(e => { e.stopPropagation(); setIsEditing(true); }, []); const handleLabelSubmit = (0, _react.useCallback)(() => { setIsEditing(false); if (data.onLabelChange && label !== data.label) { data.onLabelChange(id, label); } }, [id, label, data]); const handleKeyDown = (0, _react.useCallback)(e => { if (e.key === 'Enter') { handleLabelSubmit(); } if (e.key === 'Escape') { setLabel(data.label); setIsEditing(false); } }, [handleLabelSubmit, data.label]); return /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, /*#__PURE__*/_react.default.createElement(_reactflow.NodeResizer, { isVisible: selected, minWidth: 80, minHeight: 80, handleClassName: "w-2.5 h-2.5 bg-gray-500 rounded-full border-2 border-white", lineClassName: "border-2 border-dashed border-indigo-500 opacity-60", nodeWidth: data.width, nodeHeight: data.height }), /*#__PURE__*/_react.default.createElement("div", { style: { transform: 'rotate(45deg)', background: (0, _gradientUtils.ensureBackwardCompatibility)(data.background || data.color || '#81D4FA'), border: `${data.borderWidth || 2}px ${data.borderStyle || 'solid'} ${data.borderColor || '#ddd'}`, opacity: data.opacity ?? 1, ...(0, _transformUtils.createTransformStyles)(data, 'diamond'), width: '100%', height: '100%', display: 'flex', alignItems: 'center', justifyContent: 'center', cursor: 'move', transition: 'all 0.2s ease', boxShadow: selected ? `${(0, _shadowUtils.ensureShadowCompatibility)(data.boxShadow || 'none')}, 0 0 0 2px #2196F3` : (0, _shadowUtils.ensureShadowCompatibility)(data.boxShadow || 'none'), boxSizing: 'border-box' } }, /*#__PURE__*/_react.default.createElement("div", { style: { transform: 'rotate(-45deg)', display: 'flex', flexDirection: 'column', alignItems: 'center', justifyContent: 'center', padding: '8px', width: '100%', height: '100%', boxSizing: 'border-box' }, onDoubleClick: handleDoubleClick }, data.icon && /*#__PURE__*/_react.default.createElement("span", { style: { fontSize: '14px' } }, data.icon), isEditing ? /*#__PURE__*/_react.default.createElement("input", { ref: inputRef, value: label, onChange: e => setLabel(e.target.value), onBlur: handleLabelSubmit, onKeyDown: handleKeyDown, autoFocus: true, style: { fontSize: '14px', border: 'none', borderBottom: '2px solid #2196F3', outline: 'none', background: 'transparent', padding: '4px', textAlign: 'center', width: '100%' } }) : /*#__PURE__*/_react.default.createElement("span", { style: { fontSize: '14px' } }, label)), /*#__PURE__*/_react.default.createElement(_reactflow.Handle, { type: "source", position: _reactflow.Position.Top, id: "top-source", style: handleStyle, isConnectable: isConnectable, className: "diamond-handle" }), /*#__PURE__*/_react.default.createElement(_reactflow.Handle, { type: "source", position: _reactflow.Position.Right, id: "right-source", style: handleStyle, isConnectable: isConnectable, className: "diamond-handle" }), /*#__PURE__*/_react.default.createElement(_reactflow.Handle, { type: "source", position: _reactflow.Position.Bottom, id: "bottom-source", style: handleStyle, isConnectable: isConnectable, className: "diamond-handle" }), /*#__PURE__*/_react.default.createElement(_reactflow.Handle, { type: "source", position: _reactflow.Position.Left, id: "left-source", style: handleStyle, isConnectable: isConnectable, className: "diamond-handle" }), /*#__PURE__*/_react.default.createElement(_reactflow.Handle, { type: "target", position: _reactflow.Position.Top, id: "top-target", style: { opacity: 0, pointerEvents: 'none', width: 8, height: 8 }, isConnectable: isConnectable }), /*#__PURE__*/_react.default.createElement(_reactflow.Handle, { type: "target", position: _reactflow.Position.Right, id: "right-target", style: { opacity: 0, pointerEvents: 'none', width: 8, height: 8 }, isConnectable: isConnectable }), /*#__PURE__*/_react.default.createElement(_reactflow.Handle, { type: "target", position: _reactflow.Position.Bottom, id: "bottom-target", style: { opacity: 0, pointerEvents: 'none', width: 8, height: 8 }, isConnectable: isConnectable }), /*#__PURE__*/_react.default.createElement(_reactflow.Handle, { type: "target", position: _reactflow.Position.Left, id: "left-target", style: { opacity: 0, pointerEvents: 'none', width: 8, height: 8 }, isConnectable: isConnectable }))); }; var _default = exports.default = DiamondNode;