فهرست منبع

Fix copy paste id duplication

Clement Espeute 3 سال پیش
والد
کامیت
818667f742
3فایلهای تغییر یافته به همراه95 افزوده شده و 76 حذف شده
  1. 11 44
      hide/comp/cdb/Cell.hx
  2. 51 32
      hide/comp/cdb/Editor.hx
  3. 33 0
      hide/comp/cdb/Table.hx

+ 11 - 44
hide/comp/cdb/Cell.hx

@@ -268,43 +268,9 @@ class Cell extends Component {
 		return view == null || view.show == null || view.show.indexOf(column) >= 0;
 	}
 
-	var _cachedScope : Array<{ s : cdb.Sheet, obj : Dynamic }>;
-	function getScope() {
-		if( _cachedScope != null ) return _cachedScope;
-		var scope = [];
-		var line = line;
-		while( true ) {
-			var p = Std.downcast(line.table, SubTable);
-			if( p == null ) break;
-			line = p.cell.line;
-			scope.unshift({ s : line.table.getRealSheet(), obj : line.obj });
-		}
-		return _cachedScope = scope;
-	}
-
-	function makeId( scopes : Array<{ s : cdb.Sheet, obj : Dynamic }>, scope : Int, id : String ) {
-		var ids = [];
-		if( id != null ) ids.push(id);
-		var pos = scopes.length;
-		while( true ) {
-			pos -= scope;
-			if( pos < 0 ) {
-				scopes = getScope();
-				pos += scopes.length;
-			}
-			var s = scopes[pos];
-			var pid = Reflect.field(s.obj, s.s.idCol.name);
-			if( pid == null ) return "";
-			ids.unshift(pid);
-			scope = s.s.idCol.scope;
-			if( scope == null ) break;
-		}
-		return ids.join(":");
-	}
-
 	function refScope( targetSheet : cdb.Sheet, currentSheet : cdb.Sheet, obj : Dynamic, localScope : Array<{ s : cdb.Sheet, obj : Dynamic }> ) {
 		var targetDepth = targetSheet.name.split("@").length;
-		var scope = getScope().concat(localScope);
+		var scope = table.getScope().concat(localScope);
 		if( scope.length < targetDepth )
 			scope.push({ s : currentSheet, obj : obj });
 		while( scope.length >= targetDepth )
@@ -330,7 +296,7 @@ class Cell extends Component {
 			if( v == "" )
 				'<span class="error">#MISSING</span>';
 			else {
-				var id = c.scope != null ? makeId(scope,c.scope,v) : v;
+				var id = c.scope != null ? table.makeId(scope,c.scope,v) : v;
 				!sheet.duplicateIds.exists(id) ? v : '<span class="error">#DUP($v)</span>';
 			}
 		case TString if( c.kind == Script ):  // wrap content in div because td cannot have max-height
@@ -342,7 +308,7 @@ class Cell extends Component {
 				'<span class="error">#MISSING</span>';
 			else {
 				var s = editor.base.getSheet(sname);
-				var i = s.index.get(s.idCol.scope != null ? makeId(refScope(s,sheet,obj,scope),s.idCol.scope,v) : v);
+				var i = s.index.get(s.idCol.scope != null ? table.makeId(refScope(s,sheet,obj,scope),s.idCol.scope,v) : v);
 				i == null ? '<span class="error">#REF($v)</span>' : (i.ico == null ? "" : tileHtml(i.ico,true)+" ") + StringTools.htmlEscape(i.disp);
 			}
 		case TBool:
@@ -751,7 +717,7 @@ class Cell extends Component {
 				}
 				if( isLocal ) {
 					var scope = refScope(sdat,table.getRealSheet(),line.obj,[]);
-					var prefix = makeId(scope, sdat.idCol.scope, null)+":";
+					var prefix = table.makeId(scope, sdat.idCol.scope, null)+":";
 					elts = [ for( d in sdat.all ) if( StringTools.startsWith(d.id,prefix) ) {
 						id : d.id.split(":").pop(),
 						ico : d.ico,
@@ -809,7 +775,7 @@ class Cell extends Component {
 				var elts;
 				if( isLocal ) {
 					var scope = refScope(sdat,table.getRealSheet(),line.obj,[]);
-					var prefix = makeId(scope, sdat.idCol.scope, null)+":";
+					var prefix = table.makeId(scope, sdat.idCol.scope, null)+":";
 					elts = [for( d in sdat.all ) if( StringTools.startsWith(d.id,prefix) ) { id : d.id.split(":").pop(), ico : d.ico, text : d.disp }];
 				} else
 					elts = [for( d in sdat.all ) { id : d.id, ico : d.ico, text : d.disp }];
@@ -1083,8 +1049,8 @@ class Cell extends Component {
 	}
 
 	public function isUniqueID(id : String, ignoreSelf:Bool = false) {
-		var scope = getScope();
-		var idWithScope : String = if (column.scope != null) makeId(scope, column.scope, id) else id;
+		var scope = table.getScope();
+		var idWithScope : String = if (column.scope != null) table.makeId(scope, column.scope, id) else id;
 		return editor.isUniqueID(table.getRealSheet(), if(ignoreSelf) line.obj else {}, idWithScope);
 	}
 
@@ -1104,7 +1070,7 @@ class Cell extends Component {
 			var prevValue = value;
 			var realSheet = table.getRealSheet();
 			var isLocal = realSheet.idCol.scope != null;
-			var parentID = isLocal ? makeId([],realSheet.idCol.scope,null) : null;
+			var parentID = isLocal ? table.makeId([],realSheet.idCol.scope,null) : null;
 			// most likely our obj, unless there was a #DUP
 			var prevObj = value != null ? realSheet.index.get(isLocal ? parentID+":"+value : value) : null;
 			// have we already an obj mapped to the same id ?
@@ -1118,7 +1084,7 @@ class Cell extends Component {
 					var m = new Map();
 					m.set(value, newValue);
 					if( isLocal ) {
-						var scope = getScope();
+						var scope = table.getScope();
 						var parent = scope[scope.length - realSheet.idCol.scope];
 						editor.base.updateLocalRefs(realSheet, m, parent.obj, parent.s);
 					} else
@@ -1132,7 +1098,8 @@ class Cell extends Component {
 				// Refresh display of all ids in the column manually
 				var colId = table.sheet.columns.indexOf(column);
 				for (l in table.lines) {
-					l.cells[colId].refresh(false);	
+					if (l.cells[colId] != null)
+						l.cells[colId].refresh(false);	
 				}
 			}
 			/*

+ 51 - 32
hide/comp/cdb/Editor.hx

@@ -420,6 +420,8 @@ class Editor extends Component {
 				beginChanges();
 				var obj = line.obj;
 				formulas.removeFromValue(obj, col);
+				if (col.type == TId)
+					value = getNewUniqueId(value, cursor.table, col);
 				Reflect.setField(obj, col.name, value);
 			} else {
 				beginChanges();
@@ -435,6 +437,8 @@ class Editor extends Component {
 						if( value == null ) continue;
 						var obj = sheet.lines[y];
 						formulas.removeFromValue(obj, col);
+						if (col.type == TId)
+							value = getNewUniqueId(value, cursor.table, col);
 						Reflect.setField(obj, col.name, value);
 						toRefresh.push(allLines[y].cells[x]);
 					}
@@ -470,6 +474,10 @@ class Editor extends Component {
 			}
 			if( v == null && !destCol.opt )
 				v = base.getDefault(destCol, sheet);
+
+			if (destCol.type == TId) {
+				v = getNewUniqueId(v, cursor.table, destCol);
+			}
 			if( v == null )
 				Reflect.deleteField(destObj, destCol.name);
 			else
@@ -528,6 +536,7 @@ class Editor extends Component {
 						continue;
 
 					setValue(obj1, obj2, c1, c2);
+
 					if( c2.type == TList || c2.type == TProperties )
 						fullRefresh = true;
 					if( !fullRefresh )
@@ -1212,6 +1221,46 @@ class Editor extends Component {
 		table.refresh();
 	}
 
+	public function getNewUniqueId(originalId : String, table : Table, column : cdb.Data.Column) {
+		var str = originalId;
+		var currentValue : Null<Int> = null;
+		var strIdx : Int = 0;
+
+		// Find the number at the end of the string
+		while (strIdx < str.length) {
+			var substr = str.substr(str.length-1-strIdx);
+			var newValue = Std.parseInt(substr);
+			if (newValue != null)
+				currentValue = newValue;
+			else {
+				break;
+			}
+			strIdx += 1;
+		}
+
+		var scope = table.getScope();
+
+		if (currentValue != null) {
+			var newId : String;
+			var idWithScope : String;
+			do {
+				currentValue+=1;
+				var valStr = Std.string(currentValue);
+
+				// Pad with zeroes
+				for (i in 0...strIdx - valStr.length) {
+					valStr = "0" + valStr;
+				}
+				newId = str.substr(0, -strIdx) + valStr;
+				idWithScope = if(column.scope != null)  table.makeId(scope, column.scope, newId) else newId;
+			}
+			while (!isUniqueID(table.getRealSheet(), {}, idWithScope));
+
+			return newId;
+		}
+		return "";
+	}
+
 	public function duplicateLine( table : Table, index = 0 ) {
 		if( !table.canInsert() || table.displayMode != Table )
 			return;
@@ -1227,38 +1276,8 @@ class Editor extends Component {
 				} else {
 					// Increment the number at the end of the id if there is one
 
-					var str = (val:String);
-					var currentValue : Null<Int> = null;
-					var strIdx : Int = 0;
-					
-					// Find the number at the end of the string
-					while (strIdx < str.length) {
-						var substr = str.substr(str.length-1-strIdx);
-						var newValue = Std.parseInt(substr);
-						if (newValue != null)
-							currentValue = newValue;
-						else {
-							break;
-						}
-						strIdx += 1;
-					}
-					
-					if (currentValue != null) {
-						var cell : Cell = table.lines[index].cells[colId];
-
-						var newId : String;
-						var idWithScope : String;
-						do {
-							currentValue+=1;							
-							var valStr = Std.string(currentValue);
-
-							// Pad with zeroes
-							for (i in 0...strIdx - valStr.length) {
-								valStr = "0" + valStr;
-							}
-							newId = str.substr(0, -strIdx) + valStr;
-						}
-						while (!cell.isUniqueID(newId,true));
+					var newId = getNewUniqueId(val, table, c);
+					if (newId != null) {
 						Reflect.setField(obj, c.name, newId);
 					}
 				}

+ 33 - 0
hide/comp/cdb/Table.hx

@@ -558,6 +558,39 @@ class Table extends Component {
 		return { hidden : hidden, element : sep };
 	}
 
+	public function getScope() : Array<{ s : cdb.Sheet, obj : Dynamic }> {
+		var scope = [];
+		var table = this;
+		while( true ) {
+			var p = Std.downcast(table, SubTable);
+			if( p == null ) break;
+			var line = p.cell.line;
+			scope.unshift({ s : line.table.getRealSheet(), obj : line.obj });
+			table = table.parent;
+		}
+		return scope;
+	}
+
+	public function makeId(scopes : Array<{ s : cdb.Sheet, obj : Dynamic }>, scope : Int, id : String) : String {
+		var ids = [];
+		if( id != null ) ids.push(id);
+		var pos = scopes.length;
+		while( true ) {
+			pos -= scope;
+			if( pos < 0 ) {
+				scopes = getScope();
+				pos += scopes.length;
+			}
+			var s = scopes[pos];
+			var pid = Reflect.field(s.obj, s.s.idCol.name);
+			if( pid == null ) return "";
+			ids.unshift(pid);
+			scope = s.s.idCol.scope;
+			if( scope == null ) break;
+		}
+		return ids.join(":");
+	}
+
 	function refreshProperties() {
 		lines = [];