File size: 2,708 Bytes
420bd16 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 | export const getNodeType = (content) => {
if (content.includes('{"') && content.includes('"}')) return 'decision';
if (content.includes('["') && content.includes('"]')) return 'process';
if (content.includes('<"') && content.includes('">')) return 'input';
if (content.includes('|"') && content.includes('"|')) return 'user';
if (content.includes('(("') && content.includes('"))')) return 'root';
if (content.includes('("') && content.includes('")')) return 'output';
return 'default';
};
export const extractIcon = (line) => {
if (!line.includes('::icon')) return '';
const iconMatch = line.match(/::icon\((.*?)\)/);
return iconMatch ? iconMatch[1].replace('fafa', 'fa fa') : '';
};
export const processChartContent = (content, nodeShapes, expandedNodes) => {
if (!content) return { nodes: [], edges: [] };
const lines = content.split('\n').filter(line => line.trim() && line.trim() !== 'mindmap');
const nodes = [];
const edges = [];
let currentNodeId = 0;
let lastNodeAtLevel = {};
// First pass: count nodes at each level
const nodesPerLevel = {};
lines.forEach(line => {
const level = (line.match(/^[\t\s]*/)[0].length / 2) || 0;
nodesPerLevel[level] = (nodesPerLevel[level] || 0) + 1;
});
// Track nodes added at each level
const nodesAddedAtLevel = {};
lines.forEach(line => {
const indentMatch = line.match(/^[\t\s]*/);
const level = indentMatch ? indentMatch[0].length / 2 : 0;
const contentMatch = line.match(/(\(\(".*?"\)\))|(\[".*?"\])|(\{".*?"\})|(<".*?">)|(\|".*?"\|)|(\(".*?"\))/);
if (!contentMatch) return;
const content = contentMatch[0];
const nodeType = getNodeType(content);
const icon = extractIcon(line);
nodesAddedAtLevel[level] = (nodesAddedAtLevel[level] || 0);
const position = calculateNodePosition(
level,
nodesAddedAtLevel[level],
nodesPerLevel[level]
);
const nodeId = `node-${currentNodeId}`;
const node = {
id: nodeId,
type: 'custom',
data: {
label: content.replace(/[[\](){}|<>"]/g, ''),
icon,
level,
type: nodeType
},
position,
style: getNodeStyle(
nodeType,
nodeShapes[nodeId] || SHAPES.RECTANGLE,
level === 0,
expandedNodes.has(nodeId)
),
};
nodes.push(node);
if (level > 0 && lastNodeAtLevel[level - 1] !== undefined) {
edges.push({
id: `edge-${currentNodeId}`,
source: lastNodeAtLevel[level - 1],
target: node.id,
...edgeDefaults,
});
}
lastNodeAtLevel[level] = node.id;
nodesAddedAtLevel[level]++;
currentNodeId++;
});
return { nodes, edges };
}; |