@difizen/ai-flow
Version:
Scalable, out-of-the-box, agent-oriented flow
95 lines • 5.04 kB
JavaScript
import { create } from 'zustand';
import { useFlowStore } from "./flowStore";
export var useHelpLine = create(function (set, get) {
return {
setHelpLineHorizontal: function setHelpLineHorizontal(helpLineHorizontal) {
set({
helpLineHorizontal: helpLineHorizontal
});
},
setHelpLineVertical: function setHelpLineVertical(helpLineVertical) {
set({
helpLineVertical: helpLineVertical
});
},
helpLineThreshold: 30,
setHelpLineThreshold: function setHelpLineThreshold(helpLineThreshold) {
set({
helpLineThreshold: helpLineThreshold
});
},
showHorizontalHelpLine: function showHorizontalHelpLine(node) {
var _useFlowStore$getStat = useFlowStore.getState(),
nodes = _useFlowStore$getStat.nodes;
var showHorizontalHelpLineNodes = nodes.filter(function (n) {
if (n.id === node.id) return false;
var nY = Math.ceil(n.position.y);
var nodeY = Math.ceil(node.position.y);
if (Math.abs(nY - nodeY) < get().helpLineThreshold) return true;
return false;
}).sort(function (a, b) {
return a.position.x - b.position.x;
});
var showHorizontalHelpLineNodesLength = showHorizontalHelpLineNodes.length;
if (showHorizontalHelpLineNodesLength > 0) {
var _last$measured$width, _last$measured;
var first = showHorizontalHelpLineNodes[0];
var last = showHorizontalHelpLineNodes[showHorizontalHelpLineNodesLength - 1];
var helpLine = {
top: first.position.y,
left: first.position.x,
width: last.position.x + ((_last$measured$width = (_last$measured = last.measured) === null || _last$measured === void 0 ? void 0 : _last$measured.width) !== null && _last$measured$width !== void 0 ? _last$measured$width : 0) - first.position.x
};
if (node.position.x < first.position.x) {
var _last$measured$width2, _last$measured2;
helpLine.left = node.position.x;
helpLine.width = last.position.x + ((_last$measured$width2 = (_last$measured2 = last.measured) === null || _last$measured2 === void 0 ? void 0 : _last$measured2.width) !== null && _last$measured$width2 !== void 0 ? _last$measured$width2 : 0) - node.position.x;
}
if (node.position.x > last.position.x) {
var _node$measured$width, _node$measured;
helpLine.width = node.position.x + ((_node$measured$width = (_node$measured = node.measured) === null || _node$measured === void 0 ? void 0 : _node$measured.width) !== null && _node$measured$width !== void 0 ? _node$measured$width : 0) - first.position.x;
}
get().setHelpLineHorizontal(helpLine);
return showHorizontalHelpLineNodes;
} else {
get().setHelpLineHorizontal();
return [];
}
},
showVerticalHelpLine: function showVerticalHelpLine(node) {
var _useFlowStore$getStat2 = useFlowStore.getState(),
nodes = _useFlowStore$getStat2.nodes;
var showVerticalHelpLineNodes = nodes.filter(function (n) {
if (n.id === node.id) return false;
var nX = Math.ceil(n.position.x);
var nodeX = Math.ceil(node.position.x);
if (Math.abs(nX - nodeX) < get().helpLineThreshold) return true;
return false;
}).sort(function (a, b) {
return a.position.y - b.position.y;
});
var showVerticalHelpLineNodesLength = showVerticalHelpLineNodes.length;
if (showVerticalHelpLineNodesLength > 0) {
var _last$measured$height, _last$measured3, _node$measured$height, _node$measured2;
var first = showVerticalHelpLineNodes[0];
var last = showVerticalHelpLineNodes[showVerticalHelpLineNodesLength - 1];
var helpLine = {
top: first.position.y,
left: first.position.x,
height: last.position.y + ((_last$measured$height = (_last$measured3 = last.measured) === null || _last$measured3 === void 0 ? void 0 : _last$measured3.height) !== null && _last$measured$height !== void 0 ? _last$measured$height : 0) - first.position.y
};
if (node.position.y < first.position.y) {
var _last$measured$height2, _last$measured4;
helpLine.top = node.position.y;
helpLine.height = last.position.y + ((_last$measured$height2 = (_last$measured4 = last.measured) === null || _last$measured4 === void 0 ? void 0 : _last$measured4.height) !== null && _last$measured$height2 !== void 0 ? _last$measured$height2 : 0) - node.position.y;
}
if (node.position.y > last.position.y) helpLine.height = node.position.y + ((_node$measured$height = (_node$measured2 = node.measured) === null || _node$measured2 === void 0 ? void 0 : _node$measured2.height) !== null && _node$measured$height !== void 0 ? _node$measured$height : 0) - first.position.y;
get().setHelpLineVertical(helpLine);
return showVerticalHelpLineNodes;
} else {
get().setHelpLineVertical();
return [];
}
}
};
});