Skip to content

Commit 0c5ddd1

Browse files
authored
Merge pull request #5 from NomadRazor/fix-table-position
robust table positioning
2 parents ee8c6bc + 2a7b221 commit 0c5ddd1

File tree

2 files changed

+142
-18
lines changed

2 files changed

+142
-18
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ An Open Source alternative to dbdiagram.io, aiming to have the same basic featur
3434
+ 20. Fix key icon positioning
3535
+ 21. Show key icon for columns in composite PK
3636
+ 22. Fix table header color reshuffle when changing the code
37+
+ 23. Fix table positioning when changeing table ordering in code
3738

3839
## Architecture - Solution
3940

web/src/store/chart.js

+141-18
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ export const useChartStore = defineStore("chart", {
1212
tables: {},
1313
tablesColors:{},
1414
actualTables:{},
15+
tablesDict:{},
1516
refs: {},
1617
grid: {
1718
size: 100,
@@ -70,7 +71,12 @@ export const useChartStore = defineStore("chart", {
7071
return state.ctm;
7172
},
7273
getTables(){
73-
return this.tables;
74+
const k = Object.keys(this.tables)
75+
let tbls = {};
76+
for (const k in this.tables){
77+
tbls[k] = this.tables[k][Object.keys(this.tables[k])[0]]
78+
}
79+
return tbls;
7480
},
7581
getRefs(){
7682
return this.refs;
@@ -79,20 +85,71 @@ export const useChartStore = defineStore("chart", {
7985
return this.tableGroups;
8086
},
8187
getTable(state) {
82-
return (tableId) => {
83-
if (!(tableId in state.tables))
84-
state.tables[tableId] = {
85-
x: 0,
86-
y: 0,
87-
width: 220,
88-
height: 32
89-
};
90-
return state.tables[tableId];
88+
return (tableId,schema,tablename) => {
89+
90+
const tfn = `${schema}.${tablename}`;
91+
if (schema !== undefined && tablename !== undefined) {
92+
if (tableId in state.tables) {
93+
94+
if (Object.keys(state.tables[tableId]).includes('width')) {
95+
const c = state.tables[tableId];
96+
state.tables[tableId] = {};
97+
state.tables[tableId][tfn] = c
98+
99+
}
100+
if (!(tfn in state.tables[tableId])){
101+
let founded = false;
102+
for (const obj in state.tables){
103+
if (tfn in state.tables[Number(obj)] && obj != tableId){
104+
state.tables[tableId][tfn] = state.tables[Number(obj)][tfn]
105+
delete state.tables[Number(obj)][tfn]
106+
founded = true
107+
}
108+
}
109+
if (!founded) {
110+
111+
if (Object.keys(state.tables[tableId]).length > 0) {
112+
if (!(tfn in state.tables[tableId])){
113+
const k = Object.keys(state.tables[tableId])[0]
114+
state.tables[tableId][tfn] = state.tables[tableId][k]
115+
delete state.tables[tableId][k]
116+
}
117+
} else {
118+
119+
state.tables[tableId] = {};
120+
state.tables[tableId][tfn] = {
121+
x: 0,
122+
y: 0,
123+
width: 220,
124+
height: 32
125+
};
126+
}
127+
}
128+
}
129+
} else {
130+
state.tables[tableId] = {}
131+
state.tables[tableId][tfn] = {
132+
x: 0,
133+
y: 0,
134+
width: 220,
135+
height: 32
136+
};
137+
}
138+
}
139+
140+
if (schema === undefined || tablename === undefined) {
141+
142+
const k = Object.keys(state.tables[tableId])[0]
143+
return state.tables[tableId][k];
144+
} else {
145+
return state.tables[tableId][tfn]
146+
}
147+
91148
};
92149
},
93150
getTableColor(state) {
94151
return (tablename, tableId, schema) => {
95-
let tfn = `${schema}.${tablename}`;
152+
const tfn = `${schema}.${tablename}`;
96153

97154
if (tableId in state.tablesColors) {
98155

@@ -252,8 +309,60 @@ export const useChartStore = defineStore("chart", {
252309
datetime:null
253310
};
254311
},
312+
getTableV2(state,tableId,schema,tablename){
313+
const tfn = `${schema}.${tablename}`;
314+
if (schema !== undefined && tablename !== undefined) {
315+
if (tableId in state.tables) {
316+
317+
if (Object.keys(state.tables[tableId]).includes('width')) {
318+
const c = state.tables[tableId];
319+
state.tables[tableId] = {};
320+
state.tables[tableId][tfn] = c
321+
322+
}
323+
if (!(tfn in state.tables[tableId])){
324+
let founded = false;
325+
for (const obj in state.tables){
326+
if (tfn in state.tables[Number(obj)] && obj != tableId){
327+
state.tables[tableId][tfn] = state.tables[Number(obj)][tfn]
328+
delete state.tables[Number(obj)][tfn]
329+
founded = true
330+
}
331+
}
332+
if (!founded) {
333+
334+
if (Object.keys(state.tables[tableId]).length > 0) {
335+
if (!(tfn in state.tables[tableId])){
336+
const k = Object.keys(state.tables[tableId])[0]
337+
state.tables[tableId][tfn] = state.tables[tableId][k]
338+
delete state.tables[tableId][k]
339+
}
340+
} else {
341+
342+
state.tables[tableId] = {};
343+
state.tables[tableId][tfn] = {
344+
x: 0,
345+
y: 0,
346+
width: 220,
347+
height: 32
348+
};
349+
}
350+
}
351+
}
352+
} else {
353+
state.tables[tableId] = {}
354+
state.tables[tableId][tfn] = {
355+
x: 0,
356+
y: 0,
357+
width: 220,
358+
height: 32
359+
};
360+
}
361+
}
362+
},
255363
loadDatabase(database) {
256364
let tablesList = [];
365+
let dict = {};
257366
for (const schema of database.schemas){
258367
for(const tableGroup of schema.tableGroups)
259368
{
@@ -262,8 +371,12 @@ export const useChartStore = defineStore("chart", {
262371

263372
for(const table of schema.tables)
264373
{
265-
this.getTable(table.id);
374+
this.getTable(table.id,schema.name,table.name);
266375
this.getTableColor(table.name,table.id,schema.name)
376+
dict[table.id] = {
377+
schema: schema.name,
378+
name: table.name
379+
}
267380
}
268381
tablesList.push(...schema.tables.map((x)=> x.id));
269382

@@ -274,22 +387,30 @@ export const useChartStore = defineStore("chart", {
274387
console.log(schema);
275388
}
276389

277-
278390
this.$patch({
279-
actualTables:tablesList
391+
actualTables:tablesList,
392+
tablesDict:dict
280393
})
281394
this.loaded = true;
282395
},
283396
load(state) {
284-
console.log(Object.values(this.actualTables))
397+
console.log("state-> ",state,this.tablesDict)
285398
for (const tbl in state.tables) {
286399
if (!(Object.values(this.actualTables).includes(Number(tbl)))){
287-
delete state.tables[tbl]
288-
}
400+
delete state.tables[Number(tbl)]
401+
delete state.tablesColors[Number(tbl)]
402+
} else {
403+
if (Object.keys(state.tables[Number(tbl)]).includes('width')){
404+
this.getTableV2(state,tbl,this.tablesDict[Number(tbl)].schema,this.tablesDict[Number(tbl)].tablename)
405+
}
406+
}
289407
}
408+
console.log("state-> ",state,this.tablesDict)
290409
this.$reset();
291410
this.$patch({
292411
...state,
412+
actualTables:{},
413+
tablesDict:{},
293414
ctm: DOMMatrix.fromMatrix(state.ctm),
294415
inverseCtm: DOMMatrix.fromMatrix(state.inverseCtm).inverse()
295416
});
@@ -322,8 +443,10 @@ export const useChartStore = defineStore("chart", {
322443
});
323444
},
324445
updateTable(tableId, newTable) {
446+
const k = Object.keys(this.tables[tableId])[0];
447+
325448
this.$patch({
326-
tables:{[tableId]: newTable}
449+
tables:{[tableId]: {[k]: newTable}}
327450
});
328451
},
329452
updateRef(refId, newRef) {

0 commit comments

Comments
 (0)