@@ -1081,93 +1081,61 @@ const mergeArrays = async (a: any[], b: any[]) => {
10811081 return a ;
10821082}
10831083
1084- const mergeTwoCts = async ( ct : any , mergeCts : any ) => {
1085- const ctData : any = {
1086- ...ct ,
1087- title : mergeCts ?. title ,
1088- uid : mergeCts ?. uid ,
1089- options : {
1090- "singleton" : false ,
1091- }
1092- }
1093-
1094- for await ( const field of ctData ?. schema ?? [ ] ) {
1095- // Handle regular groups
1084+ function mergeSchemaFields ( sourceSchema : any [ ] , targetSchema : any [ ] ) {
1085+ for ( const field of sourceSchema ) {
10961086 if ( field ?. data_type === 'group' ) {
1097- const currentGroup = mergeCts ?. schema ?. find ( ( grp : any ) =>
1087+ const targetGroup = targetSchema ?. find ( ( grp : any ) =>
10981088 grp ?. uid === field ?. uid && grp ?. data_type === 'group'
10991089 ) ;
1100-
1101- if ( currentGroup ) {
1102- const group = [ ] ;
1103- for await ( const fieldGp of currentGroup ?. schema ?? [ ] ) {
1104- const fieldNst = field ?. schema ?. find ( ( fld : any ) =>
1105- fld ?. uid === fieldGp ?. uid && fld ?. data_type === fieldGp ?. data_type
1106- ) ;
1107- if ( fieldNst === undefined ) {
1108- group ?. push ( fieldGp ) ;
1109- }
1110- }
1111- field . schema = removeDuplicateFields ( [ ...field ?. schema ?? [ ] , ...group ] ) ;
1090+
1091+ if ( targetGroup ) {
1092+ const additional = ( targetGroup ?. schema ?? [ ] ) . filter ( ( tField : any ) =>
1093+ ! field ?. schema ?. find ( ( sField : any ) => sField ?. uid === tField ?. uid && sField ?. data_type === tField ?. data_type )
1094+ ) ;
1095+ field . schema = removeDuplicateFields ( [ ...field ?. schema ?? [ ] , ...additional ] ) ;
1096+ mergeSchemaFields ( field ?. schema , targetGroup ?. schema ?? [ ] ) ;
11121097 }
11131098 }
11141099
1115- // Handle modular blocks
11161100 if ( field ?. data_type === 'blocks' ) {
1117- const currentModularBlock = mergeCts ?. schema ?. find ( ( mb : any ) =>
1101+ const targetMB = targetSchema ?. find ( ( mb : any ) =>
11181102 mb ?. uid === field ?. uid && mb ?. data_type === 'blocks'
11191103 ) ;
1120-
1121- if ( currentModularBlock && currentModularBlock ?. blocks ) {
1122- // Iterate through each child block in the source
1104+
1105+ if ( targetMB ?. blocks ) {
11231106 for ( const sourceBlock of field ?. blocks ?? [ ] ) {
1124- // Find matching child block in target by UID
1125- const targetBlock = currentModularBlock ?. blocks ?. find ( ( tb : any ) =>
1126- tb ?. uid === sourceBlock ?. uid
1127- ) ;
1128-
1129- if ( targetBlock && targetBlock ?. schema ) {
1130- // Merge the schemas of matching child blocks
1131- const additionalFields = [ ] ;
1132-
1133- for ( const targetField of targetBlock ?. schema ?? [ ] ) {
1134- // Check if this field already exists in source block
1135- const existsInSource = sourceBlock ?. schema ?. find ( ( sf : any ) =>
1136- sf ?. uid === targetField ?. uid && sf ?. data_type === targetField ?. data_type
1137- ) ;
1138-
1139- if ( ! existsInSource ) {
1140- additionalFields . push ( targetField ) ;
1141- }
1142- }
1143-
1144- // Merge source and target fields, removing duplicates
1145- sourceBlock . schema = removeDuplicateFields ( [
1146- ...sourceBlock ?. schema ?? [ ] ,
1147- ...additionalFields
1148- ] ) ;
1149- }
1150- }
1151-
1152- // Add any child blocks from target that don't exist in source
1153- const additionalBlocks = [ ] ;
1154- for ( const targetBlock of currentModularBlock ?. blocks ?? [ ] ) {
1155- const existsInSource = field ?. blocks ?. find ( ( sb : any ) =>
1156- sb ?. uid === targetBlock ?. uid
1157- ) ;
1158-
1159- if ( ! existsInSource ) {
1160- additionalBlocks . push ( targetBlock ) ;
1107+ const targetBlock = targetMB ?. blocks ?. find ( ( tb : any ) => tb ?. uid === sourceBlock ?. uid ) ;
1108+
1109+ if ( targetBlock ?. schema ) {
1110+ const additional = ( targetBlock ?. schema ?? [ ] ) ?. filter ( ( tField : any ) =>
1111+ ! sourceBlock ?. schema ?. find ( ( sField : any ) => sField ?. uid === tField ?. uid && sField ?. data_type === tField ?. data_type )
1112+ ) ;
1113+ sourceBlock . schema = removeDuplicateFields ( [ ...sourceBlock ?. schema ?? [ ] , ...additional ] ) ;
1114+ mergeSchemaFields ( sourceBlock . schema , targetBlock . schema ?? [ ] ) ;
11611115 }
11621116 }
1163-
1164- field . blocks = removeDuplicateFields ( [
1165- ... field ?. blocks ?? [ ] ,
1166- ... additionalBlocks
1167- ] ) ;
1117+
1118+ const additionalBlocks = ( targetMB ?. blocks ?? [ ] ) . filter ( ( tb : any ) =>
1119+ ! field ?. blocks ?. find ( ( sb : any ) => sb ?. uid === tb ?. uid )
1120+ ) ;
1121+ field . blocks = removeDuplicateFields ( [ ... field ?. blocks ?? [ ] , ... additionalBlocks ] ) ;
11681122 }
11691123 }
11701124 }
1125+ }
1126+
1127+ const mergeTwoCts = async ( ct : any , mergeCts : any ) => {
1128+ const ctData : any = {
1129+ ...ct ,
1130+ title : mergeCts ?. title ,
1131+ uid : mergeCts ?. uid ,
1132+ options : {
1133+ "singleton" : false ,
1134+ }
1135+ }
1136+
1137+ mergeSchemaFields ( ctData ?. schema ?? [ ] , mergeCts ?. schema ?? [ ] ) ;
1138+
11711139 ctData . schema = await mergeArrays ( ctData ?. schema , mergeCts ?. schema ) ?? [ ] ;
11721140
11731141 return ctData ;
0 commit comments