UNPKG

@ichigo_san/graphing

Version:

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

179 lines (178 loc) 6.49 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 sourceHandleStyle = { background: '#3b82f6', border: '2px solid #fff', width: 8, height: 8, borderRadius: '50%', zIndex: 10 }; const targetHandleStyle = { opacity: 0, pointerEvents: 'none', width: 8, height: 8 }; const overlayHandleStyle = { opacity: 0, border: 'none', width: '100%', height: '100%', left: 0, top: 0, transform: 'none', pointerEvents: 'none' }; const HexagonNode = ({ data, id, selected, isConnectable }) => { const [isEditing, setIsEditing] = (0, _react.useState)(false); const [label, setLabel] = (0, _react.useState)(data.label || 'Hexagon'); (0, _react.useEffect)(() => { setLabel(data.label || 'Hexagon'); }, [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: { clipPath: 'polygon(25% 0%, 75% 0%, 100% 50%, 75% 100%, 25% 100%, 0% 50%)', background: (0, _gradientUtils.ensureBackwardCompatibility)(data.background || data.color || '#FFCC80'), border: `${data.borderWidth || 2}px ${data.borderStyle || 'solid'} ${data.borderColor || '#ddd'}`, opacity: data.opacity ?? 1, ...(0, _transformUtils.createTransformStyles)(data, 'hexagon'), 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' }, 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: '80%' } }) : /*#__PURE__*/_react.default.createElement("span", { style: { fontSize: '14px' } }, label), /*#__PURE__*/_react.default.createElement(_reactflow.Handle, { type: "source", id: "hexagon-source", position: _reactflow.Position.Bottom, style: overlayHandleStyle }), /*#__PURE__*/_react.default.createElement(_reactflow.Handle, { type: "target", id: "hexagon-target", position: _reactflow.Position.Bottom, style: overlayHandleStyle }), /*#__PURE__*/_react.default.createElement(_reactflow.Handle, { className: "custom-handle", type: "source", id: "right-source", position: _reactflow.Position.Right, style: sourceHandleStyle }), /*#__PURE__*/_react.default.createElement(_reactflow.Handle, { className: "custom-handle", type: "target", id: "right-target", position: _reactflow.Position.Right, style: targetHandleStyle }), /*#__PURE__*/_react.default.createElement(_reactflow.Handle, { className: "custom-handle", type: "source", id: "left-source", position: _reactflow.Position.Left, style: sourceHandleStyle }), /*#__PURE__*/_react.default.createElement(_reactflow.Handle, { className: "custom-handle", type: "target", id: "left-target", position: _reactflow.Position.Left, style: targetHandleStyle }), /*#__PURE__*/_react.default.createElement(_reactflow.Handle, { className: "custom-handle", type: "source", id: "bottom-source", position: _reactflow.Position.Bottom, style: sourceHandleStyle }), /*#__PURE__*/_react.default.createElement(_reactflow.Handle, { className: "custom-handle", type: "target", id: "bottom-target", position: _reactflow.Position.Bottom, style: targetHandleStyle }), /*#__PURE__*/_react.default.createElement(_reactflow.Handle, { className: "custom-handle", type: "source", id: "top-source", position: _reactflow.Position.Top, style: sourceHandleStyle }), /*#__PURE__*/_react.default.createElement(_reactflow.Handle, { className: "custom-handle", type: "target", id: "top-target", position: _reactflow.Position.Top, style: targetHandleStyle }))); }; var _default = exports.default = HexagonNode;