Browse Source

Revert "Prevent duplicate id on paste in cdb (#222)"

This reverts commit 5ba591fd640af75994264e507de02b7ae2647de0.
Clement Espeute 3 years ago
parent
commit
23bd0e0dd9
3 changed files with 75 additions and 91 deletions
  1. 43 9
      hide/comp/cdb/Cell.hx
  2. 32 51
      hide/comp/cdb/Editor.hx
  3. 0 31
      hide/comp/cdb/Table.hx

+ 43 - 9
hide/comp/cdb/Cell.hx

@@ -268,9 +268,43 @@ 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 = table.getScope().concat(localScope);
+		var scope = getScope().concat(localScope);
 		if( scope.length < targetDepth )
 			scope.push({ s : currentSheet, obj : obj });
 		while( scope.length >= targetDepth )
@@ -296,7 +330,7 @@ class Cell extends Component {
 			if( v == "" )
 				'<span class="error">#MISSING</span>';
 			else {
-				var id = c.scope != null ? Table.makeId(scope,c.scope,v) : v;
+				var id = c.scope != null ? 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
@@ -308,7 +342,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 ? Table.makeId(refScope(s,sheet,obj,scope),s.idCol.scope,v) : v);
+				var i = s.index.get(s.idCol.scope != null ? 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:
@@ -717,7 +751,7 @@ class Cell extends Component {
 				}
 				if( isLocal ) {
 					var scope = refScope(sdat,table.getRealSheet(),line.obj,[]);
-					var prefix = Table.makeId(scope, sdat.idCol.scope, null)+":";
+					var prefix = 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,
@@ -775,7 +809,7 @@ class Cell extends Component {
 				var elts;
 				if( isLocal ) {
 					var scope = refScope(sdat,table.getRealSheet(),line.obj,[]);
-					var prefix = Table.makeId(scope, sdat.idCol.scope, null)+":";
+					var prefix = 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 }];
@@ -1049,8 +1083,8 @@ class Cell extends Component {
 	}
 
 	public function isUniqueID(id : String, ignoreSelf:Bool = false) {
-		var scope = table.getScope();
-		var idWithScope : String = if (column.scope != null) Table.makeId(scope, column.scope, id) else id;
+		var scope = getScope();
+		var idWithScope : String = if (column.scope != null) makeId(scope, column.scope, id) else id;
 		return editor.isUniqueID(table.getRealSheet(), if(ignoreSelf) line.obj else {}, idWithScope);
 	}
 
@@ -1070,7 +1104,7 @@ class Cell extends Component {
 			var prevValue = value;
 			var realSheet = table.getRealSheet();
 			var isLocal = realSheet.idCol.scope != null;
-			var parentID = isLocal ? Table.makeId([],realSheet.idCol.scope,null) : null;
+			var parentID = isLocal ? 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 ?
@@ -1084,7 +1118,7 @@ class Cell extends Component {
 					var m = new Map();
 					m.set(value, newValue);
 					if( isLocal ) {
-						var scope = table.getScope();
+						var scope = getScope();
 						var parent = scope[scope.length - realSheet.idCol.scope];
 						editor.base.updateLocalRefs(realSheet, m, parent.obj, parent.s);
 					} else

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

@@ -420,8 +420,6 @@ 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();
@@ -437,8 +435,6 @@ 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]);
 					}
@@ -474,10 +470,6 @@ 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
@@ -536,7 +528,6 @@ class Editor extends Component {
 						continue;
 
 					setValue(obj1, obj2, c1, c2);
-
 					if( c2.type == TList || c2.type == TProperties )
 						fullRefresh = true;
 					if( !fullRefresh )
@@ -1221,46 +1212,6 @@ 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(), {}, newId));
-
-			return newId;
-		}
-		return "";
-	}
-
 	public function duplicateLine( table : Table, index = 0 ) {
 		if( !table.canInsert() || table.displayMode != Table )
 			return;
@@ -1276,8 +1227,38 @@ class Editor extends Component {
 				} else {
 					// Increment the number at the end of the id if there is one
 
-					var newId = getNewUniqueId(val, cursor.table, c);
-					if (newId != null) {
+					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));
 						Reflect.setField(obj, c.name, newId);
 					}
 				}

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

@@ -558,37 +558,6 @@ 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 });
-		}
-		return scope;
-	}
-
-	public static 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 ) {
-				break;
-			}
-			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 = [];