@ichigo_san/graphing
Version:
A lightweight UML-style diagram editor built with React Flow and Tailwind CSS
167 lines (166 loc) • 5.62 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _react = _interopRequireWildcard(require("react"));
var _reactflow = require("reactflow");
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'
};
const CircleNode = _ref => {
let {
data,
id,
selected,
isConnectable
} = _ref;
const [isEditing, setIsEditing] = (0, _react.useState)(false);
const [label, setLabel] = (0, _react.useState)(data.label || 'Circle');
(0, _react.useEffect)(() => {
setLabel(data.label || 'Circle');
}, [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 shadow",
lineClassName: "border-2 border-dashed border-indigo-500 opacity-60",
nodeWidth: data.width,
nodeHeight: data.height
}), /*#__PURE__*/_react.default.createElement("div", {
style: {
background: data.color || '#C5E1A5',
border: "2px solid ".concat(data.borderColor || '#ddd'),
borderRadius: '50%',
width: '100%',
height: '100%',
display: 'flex',
alignItems: 'center',
justifyContent: 'center',
cursor: 'move',
transition: 'all 0.2s ease',
boxShadow: selected ? '0 0 0 2px #2196F3' : '0 2px 4px rgba(0,0,0,0.1)',
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",
position: _reactflow.Position.Top,
id: "top",
style: handleStyle,
isConnectable: isConnectable,
className: "circle-handle"
}), /*#__PURE__*/_react.default.createElement(_reactflow.Handle, {
type: "source",
position: _reactflow.Position.Right,
id: "right",
style: handleStyle,
isConnectable: isConnectable,
className: "circle-handle"
}), /*#__PURE__*/_react.default.createElement(_reactflow.Handle, {
type: "source",
position: _reactflow.Position.Bottom,
id: "bottom",
style: handleStyle,
isConnectable: isConnectable,
className: "circle-handle"
}), /*#__PURE__*/_react.default.createElement(_reactflow.Handle, {
type: "source",
position: _reactflow.Position.Left,
id: "left",
style: handleStyle,
isConnectable: isConnectable,
className: "circle-handle"
}), /*#__PURE__*/_react.default.createElement(_reactflow.Handle, {
type: "target",
position: _reactflow.Position.Top,
id: "top-target",
style: {
opacity: 0,
pointerEvents: 'none'
},
isConnectable: isConnectable
}), /*#__PURE__*/_react.default.createElement(_reactflow.Handle, {
type: "target",
position: _reactflow.Position.Right,
id: "right-target",
style: {
opacity: 0,
pointerEvents: 'none'
},
isConnectable: isConnectable
}), /*#__PURE__*/_react.default.createElement(_reactflow.Handle, {
type: "target",
position: _reactflow.Position.Bottom,
id: "bottom-target",
style: {
opacity: 0,
pointerEvents: 'none'
},
isConnectable: isConnectable
}), /*#__PURE__*/_react.default.createElement(_reactflow.Handle, {
type: "target",
position: _reactflow.Position.Left,
id: "left-target",
style: {
opacity: 0,
pointerEvents: 'none'
},
isConnectable: isConnectable
})));
};
var _default = exports.default = CircleNode;