Skip to content

Commit 9bc1dac

Browse files
committed
Improve the edges: add curve when nodes not aligned, improved visibility
1 parent f2ce0ac commit 9bc1dac

3 files changed

Lines changed: 43 additions & 19 deletions

File tree

src/config/cytoscape-style.js

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ const style = [
4646
style: {
4747
curveStyle: 'bezier',
4848
targetArrowShape: 'triangle',
49+
arrowScale: 1.2,
4950
},
5051
},
5152
{
@@ -54,7 +55,29 @@ const style = [
5455
width: 'data(style.thickness)',
5556
lineColor: 'data(style.backgroundColor)',
5657
targetArrowColor: 'data(style.backgroundColor)',
57-
curveStyle: 'segments',
58+
curveStyle: (ele) => {
59+
const source = ele.source();
60+
const target = ele.target();
61+
62+
// Get positions
63+
const p1 = source.position();
64+
const p2 = target.position();
65+
66+
// Calculate difference
67+
const dx = Math.abs(p1.x - p2.x);
68+
const dy = Math.abs(p1.y - p2.y);
69+
70+
// Define a threshold for what counts as "aligned"
71+
const threshold = 10;
72+
73+
// If aligned horizontally OR vertically, be straight
74+
if (dx < threshold || dy < threshold) {
75+
return 'straight';
76+
}
77+
78+
// use unbundled-bezier to respect bend points
79+
return 'unbundled-bezier';
80+
},
5881
segmentDistances: 'data(bendData.bendDistance)',
5982
segmentWeights: 'data(bendData.bendWeight)',
6083
edgeDistances: 'node-position',
@@ -67,13 +90,13 @@ const style = [
6790
label: 'data(label)',
6891
edgeTextRotation: 'autorotate',
6992
zIndex: 999,
93+
fontSize: 12,
7094
textBackgroundOpacity: 1,
71-
color: '#000',
95+
textBackgroundPadding: '3px',
96+
textBorderWidth: 0,
97+
color: '#333',
7298
textBackgroundColor: '#fff',
7399
textBackgroundShape: 'roundrectangle',
74-
textBorderColor: '#fff',
75-
textBorderWidth: 2,
76-
textBorderOpacity: 1,
77100
},
78101
},
79102
{

src/config/defaultStyles.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ const NodeStyle = {
99
};
1010

1111
const EdgeStyle = {
12-
thickness: 1,
13-
backgroundColor: null,
12+
thickness: 2,
13+
backgroundColor: '#555',
1414
shape: 'solid',
1515
};
1616

src/graph-builder/graph-core/1-core.js

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ class CoreGraph {
3939
}
4040
// if (cy) this.cy = cy;
4141
this.cy = cytoscape({ ...cyOptions, container: element });
42+
this.cy.on('position', 'node', () => {
43+
this.cy.edges().updateStyle();
44+
});
4245
this.id = id;
4346
this.projectName = projectName;
4447
this.authorName = authorName;
@@ -81,50 +84,48 @@ class CoreGraph {
8184
this.cy.nodeEditing({
8285
resizeToContentCueEnabled: () => false,
8386
setWidth: (node, width) => {
84-
// HARD ENFORCEMENT: Snap width every frame during resize
85-
const snappedWidth = this.snapDimensionToGrid(width);
86-
node.data('style', { ...node.data('style'), width: snappedWidth });
87+
// Allow free resizing during drag - snapping will happen on resizeend
88+
node.data('style', { ...node.data('style'), width });
8789

8890
// Adjust position to maintain edge alignment based on resize handle
8991
const resizeType = node.scratch('resizeType');
9092
if (resizeType && (resizeType.includes('left') || resizeType.includes('right'))) {
9193
const currentPos = node.position();
9294
const initialPos = node.scratch('resizeInitialPos');
9395
const initialWidth = node.scratch('width');
94-
const widthDelta = snappedWidth - initialWidth;
96+
const widthDelta = width - initialWidth;
9597

9698
let newX = currentPos.x;
9799
if (resizeType.includes('left')) {
98100
newX = initialPos.x - widthDelta / 2;
99101
} else if (resizeType.includes('right')) {
100102
newX = initialPos.x + widthDelta / 2;
101103
}
102-
node.position({ x: this.snapToGrid(newX), y: currentPos.y });
104+
node.position({ x: newX, y: currentPos.y });
103105
}
104-
return snappedWidth;
106+
return width;
105107
},
106108
setHeight: (node, height) => {
107-
// HARD ENFORCEMENT: Snap height every frame during resize
108-
const snappedHeight = this.snapDimensionToGrid(height);
109-
node.data('style', { ...node.data('style'), height: snappedHeight });
109+
// Allow free resizing during drag - snapping will happen on resizeend
110+
node.data('style', { ...node.data('style'), height });
110111

111112
// Adjust position to maintain edge alignment based on resize handle
112113
const resizeType = node.scratch('resizeType');
113114
if (resizeType && (resizeType.includes('top') || resizeType.includes('bottom'))) {
114115
const currentPos = node.position();
115116
const initialPos = node.scratch('resizeInitialPos');
116117
const initialHeight = node.scratch('height');
117-
const heightDelta = snappedHeight - initialHeight;
118+
const heightDelta = height - initialHeight;
118119

119120
let newY = currentPos.y;
120121
if (resizeType.includes('top')) {
121122
newY = initialPos.y - heightDelta / 2;
122123
} else if (resizeType.includes('bottom')) {
123124
newY = initialPos.y + heightDelta / 2;
124125
}
125-
node.position({ x: currentPos.x, y: this.snapToGrid(newY) });
126+
node.position({ x: currentPos.x, y: newY });
126127
}
127-
return snappedHeight;
128+
return height;
128129
},
129130
isNoResizeMode(node) { return node.data('type') !== 'ordin'; },
130131
isNoControlsMode(node) { return node.data('type') !== 'ordin'; },

0 commit comments

Comments
 (0)