diff --git a/src/context/AreasContext.jsx b/src/context/AreasContext.jsx index 2e18c1c22..bf7de9ff2 100644 --- a/src/context/AreasContext.jsx +++ b/src/context/AreasContext.jsx @@ -23,19 +23,27 @@ export default function AreasContextProvider({ children }) { } else { const width = 200; const height = 200; - setAreas((prev) => [ - ...prev, - { - id: prev.length, - name: `area_${prev.length}`, - x: transform.pan.x - width / 2, - y: transform.pan.y - height / 2, - width, - height, - color: defaultBlue, - locked: false, - }, - ]); + setAreas((prev) => { + let x = transform.pan.x - width / 2; + let y = transform.pan.y - height / 2; + while (prev.some((a) => a.x === x && a.y === y)) { + x += 20; + y += 20; + } + return [ + ...prev, + { + id: prev.length, + name: `area_${prev.length}`, + x, + y, + width, + height, + color: defaultBlue, + locked: false, + }, + ]; + }); } if (addToHistory) { setUndoStack((prev) => [ diff --git a/src/context/DiagramContext.jsx b/src/context/DiagramContext.jsx index e21edab0a..42fec876e 100644 --- a/src/context/DiagramContext.jsx +++ b/src/context/DiagramContext.jsx @@ -18,11 +18,17 @@ export default function DiagramContextProvider({ children }) { const addTable = (data, addToHistory = true) => { const id = nanoid(); + let x = transform.pan.x; + let y = transform.pan.y; + while (tables.some((t) => t.x === x && t.y === y)) { + x += 20; + y += 20; + } const newTable = { id, name: `table_${id}`, - x: transform.pan.x, - y: transform.pan.y, + x, + y, locked: false, fields: [ { diff --git a/src/context/NotesContext.jsx b/src/context/NotesContext.jsx index ce5bbae2e..48134ef58 100644 --- a/src/context/NotesContext.jsx +++ b/src/context/NotesContext.jsx @@ -27,20 +27,28 @@ export default function NotesContextProvider({ children }) { }); } else { const height = 88; - setNotes((prev) => [ - ...prev, - { - id: prev.length, - x: transform.pan.x, - y: transform.pan.y - height / 2, - title: `note_${prev.length}`, - content: "", - locked: false, - color: defaultNoteTheme, - height, - width: noteWidth, - }, - ]); + setNotes((prev) => { + let x = transform.pan.x; + let y = transform.pan.y - height / 2; + while (prev.some((n) => n.x === x && n.y === y)) { + x += 20; + y += 20; + } + return [ + ...prev, + { + id: prev.length, + x, + y, + title: `note_${prev.length}`, + content: "", + locked: false, + color: defaultNoteTheme, + height, + width: noteWidth, + }, + ]; + }); } if (addToHistory) { setUndoStack((prev) => [