@@ -165,9 +165,9 @@ export default function ControlPanel({
165165 } else if ( a . element === ObjectType . RELATIONSHIP ) {
166166 deleteRelationship ( a . data . relationship . id , false ) ;
167167 } else if ( a . element === ObjectType . TYPE ) {
168- deleteType ( types . length - 1 , false ) ;
168+ deleteType ( a . data . type . id , false ) ;
169169 } else if ( a . element === ObjectType . ENUM ) {
170- deleteEnum ( enums . length - 1 , false ) ;
170+ deleteEnum ( a . data . enum . id , false ) ;
171171 }
172172 setRedoStack ( ( prev ) => [ ...prev , a ] ) ;
173173 } else if ( a . action === Action . MOVE ) {
@@ -199,9 +199,9 @@ export default function ControlPanel({
199199 } else if ( a . element === ObjectType . AREA ) {
200200 addArea ( a . data , false ) ;
201201 } else if ( a . element === ObjectType . TYPE ) {
202- addType ( { id : a . id , ... a . data } , false ) ;
202+ addType ( a . data , false ) ;
203203 } else if ( a . element === ObjectType . ENUM ) {
204- addEnum ( { id : a . id , ... a . data } , false ) ;
204+ addEnum ( a . data , false ) ;
205205 }
206206 setRedoStack ( ( prev ) => [ ...prev , a ] ) ;
207207 } else if ( a . action === Action . EDIT ) {
@@ -258,9 +258,12 @@ export default function ControlPanel({
258258 updateRelationship ( a . rid , a . undo ) ;
259259 } else if ( a . element === ObjectType . TYPE ) {
260260 if ( a . component === "field_add" ) {
261+ const type = types . find ( ( t , i ) =>
262+ typeof a . tid === "number" ? i === a . tid : t . id === a . tid ,
263+ ) ;
261264 updateType ( a . tid , {
262- fields : types [ a . tid ] . fields . filter (
263- ( _ , i ) => i !== types [ a . tid ] . fields . length - 1 ,
265+ fields : type . fields . filter ( ( f , i ) =>
266+ f . id ? f . id !== a . data . field . id : i !== type . fields . length - 1 ,
264267 ) ,
265268 } ) ;
266269 }
@@ -334,9 +337,9 @@ export default function ControlPanel({
334337 } else if ( a . element === ObjectType . RELATIONSHIP ) {
335338 addRelationship ( a . data , false ) ;
336339 } else if ( a . element === ObjectType . TYPE ) {
337- addType ( null , false ) ;
340+ addType ( a . data , false ) ;
338341 } else if ( a . element === ObjectType . ENUM ) {
339- addEnum ( null , false ) ;
342+ addEnum ( a . data , false ) ;
340343 }
341344 setUndoStack ( ( prev ) => [ ...prev , a ] ) ;
342345 } else if ( a . action === Action . MOVE ) {
@@ -367,9 +370,9 @@ export default function ControlPanel({
367370 } else if ( a . element === ObjectType . AREA ) {
368371 deleteArea ( a . data . id , false ) ;
369372 } else if ( a . element === ObjectType . TYPE ) {
370- deleteType ( a . id , false ) ;
373+ deleteType ( a . data . type . id , false ) ;
371374 } else if ( a . element === ObjectType . ENUM ) {
372- deleteEnum ( a . id , false ) ;
375+ deleteEnum ( a . data . enum . id , false ) ;
373376 }
374377 setUndoStack ( ( prev ) => [ ...prev , a ] ) ;
375378 } else if ( a . action === Action . EDIT ) {
@@ -436,14 +439,11 @@ export default function ControlPanel({
436439 updateRelationship ( a . rid , a . redo ) ;
437440 } else if ( a . element === ObjectType . TYPE ) {
438441 if ( a . component === "field_add" ) {
442+ const type = types . find ( ( t , i ) =>
443+ typeof a . tid === "number" ? i === a . tid : t . id === a . tid ,
444+ ) ;
439445 updateType ( a . tid , {
440- fields : [
441- ...types [ a . tid ] . fields ,
442- {
443- name : "" ,
444- type : "" ,
445- } ,
446- ] ,
446+ fields : [ ...type . fields , a . data . field ] ,
447447 } ) ;
448448 } else if ( a . component === "field" ) {
449449 updateType ( a . tid , {
@@ -711,7 +711,6 @@ export default function ControlPanel({
711711 return ;
712712 }
713713 const v = new Validator ( ) ;
714- console . log ( obj ) ;
715714 if ( v . validate ( obj , tableSchema ) . valid ) {
716715 addTable ( {
717716 table : {
@@ -782,11 +781,24 @@ export default function ControlPanel({
782781 setUndoStack ( [ ] ) ;
783782 setRedoStack ( [ ] ) ;
784783 if ( databases [ database ] . hasTypes ) {
785- setTypes ( diagram . types ?? [ ] ) ;
786- }
787- if ( databases [ database ] . hasEnums ) {
788- setEnums ( diagram . enums ?? [ ] ) ;
784+ setTypes (
785+ diagram . types . map ( ( t ) =>
786+ t . id
787+ ? t
788+ : {
789+ ...t ,
790+ id : nanoid ( ) ,
791+ fields : t . fields . map ( ( f ) =>
792+ f . id ? f : { ...f , id : nanoid ( ) } ,
793+ ) ,
794+ } ,
795+ ) ,
796+ ) ;
789797 }
798+ setEnums (
799+ diagram . enums . map ( ( e ) => ( ! e . id ? { ...e , id : nanoid ( ) } : e ) ) ??
800+ [ ] ,
801+ ) ;
790802 window . name = `d ${ diagram . id } ` ;
791803 } else {
792804 window . name = "" ;
0 commit comments