Skip to content

Conversation

HassanBahati
Copy link

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.
image

Brief summary of changes and motivation.
Implemented a smart positioning algorithm that automatically finds non-overlapping positions for new tables:

  • Added utility function 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.
image

Type of Change

  • Bug fix
  • New feature
  • Breaking change
  • Documentation update

Testing

  • [] Tests added/updated
  • All tests pass locally
  • Tested on multiple browsers (Chrome, Firefox, Safari, Edge)

Test Environment:

  • Browser:
  • OS:

Checklist

  • Code follows project style
  • Self-reviewed code
  • Updated documentation
  • Backward compatible (or breaking change documented)
  • No new warnings/errors

Additional Notes

Any extra context or screenshots.

Copy link

vercel bot commented Sep 22, 2025

@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 HassanBahati changed the title fix: Fix table overlap issue when creating multiple tables fix: Fix overlap issue when creating multiple tables Sep 22, 2025
@AHS12
Copy link
Owner

AHS12 commented Sep 23, 2025

@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

  1. I've created Issue [Bug]: Creating a new table form dialog/menu/shortcut overlap with each other and not showing in editor #23 to track this.
  2. We have another feature allowing users to create a table from the right-click context menu. It uses a separate function, but please ensure your change doesn't impact that feature.
  3. Since users can work on any part of the diagram, can you confirm that new tables are being created within the viewport area?
  4. If the viewport area is crowded, will it default to overlap?

Again, thank you for your contribution. I really appreciate it

src/lib/utils.ts Outdated
}
}

// Fallback: return a position far from existing nodes
Copy link
Author

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

Copy link
Owner

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?

Copy link
Author

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?

image

Copy link
Owner

@AHS12 AHS12 Sep 23, 2025

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.

Copy link
Owner

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

Copy link
Author

@HassanBahati HassanBahati Sep 23, 2025

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.

Copy link
Owner

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!

Copy link
Owner

@AHS12 AHS12 Sep 23, 2025

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.

Copy link

vercel bot commented Sep 23, 2025

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Preview Comments Updated (UTC)
thoth-blueprint Ready Ready Preview Comment Sep 23, 2025 5:09pm

x: maxX + spacing * 2,
y: maxY + spacing * 2,
};
// If all else fails, overlap on the last added table
Copy link
Author

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

Copy link
Owner

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
Copy link
Owner

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,
Copy link
Owner

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++) {
Copy link
Owner

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?

@AHS12
Copy link
Owner

AHS12 commented Oct 5, 2025

@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!

Copy link
Author

@HassanBahati HassanBahati left a 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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants