@@ -156,6 +156,67 @@ class Database {
156156 return addSheet (s , index < 0 ? null : index + 1 );
157157 }
158158
159+ function unlinkStructRef (parent : Sheet , c : Column ): Void {
160+ var srcSheetName = c .structRef ;
161+ var destSheetName = parent .name + " @" + c .name ;
162+
163+ function copyRec (srcName : String , destName : String , parent : Sheet , c : Column ): Void {
164+ var srcSheet = getSheet (srcName );
165+ if (srcSheet == null )
166+ return ;
167+
168+ var s : cdb. Data . SheetData = {
169+ name : destName ,
170+ props : {hide : true },
171+ separators : [],
172+ lines : [],
173+ columns : [],
174+ };
175+ if (c .type == TProperties || c .type == TPolymorph )
176+ s .props .isProps = true ;
177+
178+ for (srcCol in srcSheet .columns ) {
179+ var nc : cdb. Data . Column = {
180+ name : srcCol .name ,
181+ type : srcCol .type ,
182+ typeStr : srcCol .typeStr ,
183+ };
184+ if (srcCol .opt != null ) nc .opt = srcCol .opt ;
185+ if (srcCol .display != null ) nc .display = srcCol .display ;
186+ if (srcCol .kind != null ) nc .kind = srcCol .kind ;
187+ if (srcCol .scope != null ) nc .scope = srcCol .scope ;
188+ if (srcCol .documentation != null ) nc .documentation = srcCol .documentation ;
189+ if (srcCol .editor != null ) nc .editor = srcCol .editor ;
190+ if (srcCol .defaultValue != null ) nc .defaultValue = srcCol .defaultValue ;
191+ if (srcCol .shared != null ) nc .shared = srcCol .shared ;
192+ if (srcCol .structRef != null ) nc .structRef = srcCol .structRef ;
193+ s .columns .push (nc );
194+ }
195+
196+ // Find insertion point (after parent, accounting for other subs)
197+ var index = data .sheets .indexOf (Lambda .find (data .sheets , function (sh ) return sh .name == parent .name ));
198+ for (c2 in parent .columns ) {
199+ if (c == c2 )
200+ break ;
201+ if (c2 .type .match (TProperties | TList | TPolymorph )) {
202+ var sub = parent .getSub (c2 );
203+ index = data .sheets .indexOf (@:privateAccess sub .sheet );
204+ }
205+ }
206+ var destSheet = addSheet (s , index < 0 ? null : index + 1 );
207+
208+ for (col in s .columns ) {
209+ if ((col .type == TList || col .type == TProperties || col .type == TPolymorph ) && col .structRef == null ) {
210+ var nestedSrcName = srcName + " @" + col .name ;
211+ var nestedDestName = destName + " @" + col .name ;
212+ copyRec (nestedSrcName , nestedDestName , destSheet , col );
213+ }
214+ }
215+ }
216+
217+ copyRec (srcSheetName , destSheetName , parent , c );
218+ }
219+
159220 public function sync () {
160221 smap = new Map ();
161222 for ( s in sheets )
@@ -396,8 +457,14 @@ class Database {
396457 if ( old .structRef != c .structRef ) {
397458 if ( old .type == TList || old .type == TProperties || old .type == TPolymorph ) {
398459 if ( old .structRef != null && c .structRef == null ) {
460+ var refSheet = sheet .base .getSheet (old .structRef );
461+ if ( refSheet != null ) {
462+ var needProps = c .type == TProperties || c .type == TPolymorph ;
463+ if ( refSheet .props .isProps != needProps )
464+ return " Cannot unlink: type mismatch" ;
465+ }
466+ sheet .base .unlinkStructRef (sheet , old );
399467 old .structRef = null ;
400- sheet .base .createSubSheet (sheet , old );
401468 }
402469 else if ( old .structRef == null && c .structRef != null ) {
403470 var subSheet = sheet .base .getSheet (sheet .name + " @" + old .name );
0 commit comments