-
Notifications
You must be signed in to change notification settings - Fork 42
fix: Fix overlap issue when creating multiple tables #22
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
fix: Fix overlap issue when creating multiple tables #22
Conversation
@HassanBahati is attempting to deploy a commit to the Md Azizul Hakim's projects Team on Vercel. A member of the Team first needs to authorize it. |
@HassanBahati Thanks, and Great job! This is indeed an issue, and your PR looks good, just some things to confirm before I approve this PR
Again, thank you for your contribution. I really appreciate it |
src/lib/utils.ts
Outdated
} | ||
} | ||
|
||
// Fallback: return a position far from existing nodes |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If the viewport area is crowded, will it default to overlap?
I believe it will never default to overlap; the first attempt it tries the default position (near viewport center or right-click location). There is spiral search which ensures that if crowded, spirals outward in a golden angle pattern (up to 20 attempts). And finally calls a fallback where if still crowded, places the table far from existing nodes with proper spacing
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Considering user experience, if they are dealing with a large, crowded diagram, overlapping might not be a major issue since they would need to locate the table anyway. After 20 attempts, we could create the table at its current position. Overlapping seems acceptable, as the user can simply drag it to a new spot.
@HassanBahati what do you think?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well, from UX perspective, it is easy for a user to think the previously created tables disappear, or are removed once you create new ones due to the over lap. Here is a case study, a user creates over 20 tables, but they all keep overlapping, what would they think happened to the prior ones created? Would they assume they are ontop of each other? Would they think only the last created table exists?
here is an example in which i created 20 tables but the ui seems like i only have the last created table. But, in actual sense all 20 tables exist under the 20th.
You have a valid point on the user simply dragging it to a new spot...but doesn't assume that the user knows about the overlapping behavior? Does that mean new users have to first learn about the overlap behavior probably by reading some documentation?

There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@HassanBahati Let’s say the viewport is crowded, and there’s no space for a new table. When a user creates a new table, the table might find an empty spot elsewhere(outside of the viewport), but the user won’t notice it at first glance. That’s my concern. Your solution of avoiding overlap is actually good, and I’m happy to merge it. I just have this one case where I’m trying to find a solution: if there’s no space left in the viewport, can we allow overlap(inside the viewport)? That’s my question.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Desktop.2025.09.23.-.19.31.02.01.mp4
here is a demo video with my concern
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
oh i now get you, its a concern about what happens when there isnt any more space left in the viewpoint.
i agree with you that if there’s no space left in the viewport, we can allow overlap(inside the viewport)....in that case, can adjust to keep the algorithm aware of current viewpoint size, and work with that until full, after which can over lap to the last created.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@HassanBahati Since we’re in agreement, let’s proceed with updating your algorithm, and then I’ll merge it. Thanks for your time and effort!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Desktop.2025.09.23.-.23.15.00.02.mp4
@HassanBahati I can see the overlapping code, but it doesn't seem to be applying correctly. Let me know if you can check it out; I'm testing it in preview mode.
The latest updates on your projects. Learn more about Vercel for GitHub.
|
x: maxX + spacing * 2, | ||
y: maxY + spacing * 2, | ||
}; | ||
// If all else fails, overlap on the last added table |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
added a final case that allows overlap to the last added table
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unfortunately, it's still not working as expected; the tables continue to be created outside the viewport when there's no available space. Perhaps you should consider passing an updated viewport boundary, which might help.
preferredPosition: { x: number; y: number }, | ||
nodeWidth: number = 288, | ||
nodeHeight: number = 100, | ||
spacing: number = 50 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe we have to pass view port boundary here as well
|
||
if (lastAddedNode && lastAddedNode.position) { | ||
return { | ||
x: lastAddedNode.position.x + 20, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we use const for this magic number 20?
// try positions in a grid around the preferred position | ||
const gridSize = nodeWidth + spacing; | ||
|
||
for (let distance = 1; distance <= 10; distance++) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
const MAX_SEARCH_DISTANCE = 10;
Can we use a constant for this distance magic number?
@HassanBahati Hi, any updates from your side? I’m working on #11 and will need this feature. If I don’t hear back from you, I’ll close the PR and use your logic as a base for my fix. Thanks! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @AHS12 , sorry been caught up late. Will resolve on here by tomorrow. Thanks!
Description
When creating multiple tables in the diagram editor, new tables would overlap with existing ones because they were all positioned at the same fixed coordinates relative to the viewport center.

Brief summary of changes and motivation.
Implemented a smart positioning algorithm that automatically finds non-overlapping positions for new tables:
findNonOverlappingPosition
that checks if the default position overlaps with existing nodes and uses a spiral positioning algorithm to find alternative non-overlapping positions. Falls back to placing tables far from existing nodes if needed.Type of Change
Testing
Test Environment:
Checklist
Additional Notes
Any extra context or screenshots.