2
0
Nicolas Cannasse 6 жил өмнө
parent
commit
0f9bab662a

+ 4 - 0
bin/cdb.css

@@ -213,6 +213,10 @@
   border: 2px solid white;
   outline: 1px solid black;
 }
+.cdb .cdb-sheet td.t_flags {
+  position: relative;
+  overflow: visible;
+}
 .cdb .cdb-sheet .flagValues {
   position: absolute;
   background-color: #333;

+ 5 - 0
bin/cdb.less

@@ -237,6 +237,11 @@
 			}
 		}
 
+		td.t_flags {
+			position: relative;
+			overflow: visible;
+		}
+
 		.flagValues {
 			position : absolute;
 			background-color : #333;

+ 8 - 0
hide/Config.hx

@@ -90,6 +90,14 @@ class Config {
 		return defaultVal;
 	}
 
+	public function set( key : String, val : Dynamic ) {
+		if( val == null )
+			Reflect.deleteField(source, key);
+		else
+			Reflect.setField(source, key, val);
+		save();
+	}
+
 	public static function loadForProject( projectPath : String, resourcePath : String ) {
 		var hidePath = Ide.inst.appPath;
 

+ 4 - 0
hide/Ide.hx

@@ -655,6 +655,10 @@ class Ide {
 		var value = ask("Sheet name");
 		if( value == "" || value == null ) return null;
 		var s = database.createSheet(value, index);
+		if( s == null ) {
+			error("Name already exists");
+			return null;
+		}
 		saveDatabase();
 		databaseApi.editor.refresh();
 		return s;

+ 3 - 0
hide/comp/cdb/Cell.hx

@@ -347,6 +347,7 @@ class Cell extends Component {
 				var val = s.val();
 				if( val == "~" ) val = null;
 				setValue(val);
+				sel2.close();
 				closeEdit();
 			});
 			s.on("select2:close", function(_) closeEdit());
@@ -363,11 +364,13 @@ class Cell extends Component {
 			(untyped s.select2)(props);
 			(untyped s.select2)("val", currentValue == null ? "" : currentValue);
 			(untyped s.select2)("open");
+			var sel2 = s.data("select2");
 
 			s.change(function(e) {
 				var val = Std.parseInt(s.val());
 				if( val < 0 ) val = null;
 				setValue(val);
+				sel2.close();
 				closeEdit();
 			});
 			s.keydown(function(e) {

+ 3 - 0
hide/comp/cdb/Cursor.hx

@@ -50,10 +50,13 @@ class Cursor {
 	}
 
 	public function save() {
+		if( table == null ) return null;
 		return { sheet : table.sheet, x : x, y : y, select : select == null ? null : { x : select.x, y : select.y} };
 	}
 
 	public function load( s ) {
+		if( s == null )
+			return false;
 		var table = null;
 		for( t in editor.tables )
 			if( t.sheet == s.sheet ) {

+ 41 - 2
hide/comp/cdb/Editor.hx

@@ -181,7 +181,46 @@ class Editor extends Component {
 	function onPaste() {
 		var text = ide.getClipboard();
 		if( clipboard == null || text != clipboard.text ) {
-			// TODO : edit and copy text
+			if( cursor.x < 0 || cursor.y < 0 ) return;
+			var x1 = cursor.x;
+			var y1 = cursor.y;
+			var x2 = cursor.select == null ? x1 : cursor.select.x;
+			var y2 = cursor.select == null ? y1 : cursor.select.y;
+			if( x1 > x2 ) {
+				var tmp = x1;
+				x1 = x2;
+				x2 = tmp;
+			}
+			if( y1 > y2 ) {
+				var tmp = y1;
+				y1 = y2;
+				y2 = tmp;
+			}
+			beginChanges();
+			for( x in x1...x2+1 ) {
+				var col = sheet.columns[x];
+				var value : Dynamic = null;
+				switch( col.type ) {
+				case TId:
+					if( ~/^[A-Za-z0-9_]+$/.match(text) ) value = text;
+				case TString:
+					value = text;
+				case TInt:
+					value = Std.parseInt(text);
+				case TFloat:
+					value = Std.parseFloat(text);
+					if( Math.isNaN(value) ) value = null;
+				default:
+				}
+				if( value == null ) continue;
+				for( y in y1...y2+1 ) {
+					var obj = sheet.lines[y];
+					Reflect.setField(obj, col.name, value);
+				}
+			}
+			endChanges();
+			sheet.sync();
+			refreshAll();
 			return;
 		}
 		beginChanges();
@@ -672,7 +711,7 @@ class Editor extends Component {
 		if( onChange == null ) onChange = function() {}
 		var index = base.sheets.indexOf(sheet);
 		new hide.comp.ContextMenu([
-			{ label : "Add Sheet", click : function() { beginChanges(); ide.createDBSheet(index+1); endChanges(); onChange(); } },
+			{ label : "Add Sheet", click : function() { beginChanges(); var db = ide.createDBSheet(index+1); endChanges(); if( db != null ) onChange(); } },
 			{ label : "Move Left", click : function() { beginChanges(); base.moveSheet(sheet,-1); endChanges(); onChange(); } },
 			{ label : "Move Right", click : function() { beginChanges(); base.moveSheet(sheet,1); endChanges(); onChange(); } },
 			{ label : "Rename", click : function() {

+ 3 - 1
hide/comp/cdb/SubTable.hx

@@ -67,7 +67,9 @@ class SubTable extends Table {
 				Reflect.setField(cell.line.obj, cell.column.name, value);
 				// do not save for now
 			}
-			[for( f in psheet.columns ) value];
+			var lines = [for( f in psheet.columns ) value];
+			if( lines.length == 0 ) lines.push(value);
+			lines;
 		default:
 			throw "assert";
 		}

+ 2 - 2
hide/comp/cdb/Table.hx

@@ -44,8 +44,8 @@ class Table extends Component {
 		}
 	}
 
-	public function cloneTableHead() {
-		var target = J('.head');
+	function cloneTableHead() {
+		var target = element.find('thead').first().find('.head');
 		if (target.length == 0) {
 			trace("Fail clone table head");
 			return;

+ 25 - 4
hide/view/CdbTable.hx

@@ -5,10 +5,18 @@ class CdbTable extends hide.ui.View<{ path : String }> {
 	var sheets : Array<cdb.Sheet>;
 	var tabContents : Array<Element>;
 	var editor : hide.comp.cdb.Editor;
+	var currentSheet : Int = 0;
 
 	public function new( ?state ) {
 		super(state);
 		syncSheets();
+		var name = this.config.get("cdb.currentSheet");
+		if( name != null )
+			for( s in sheets )
+				if( s.name == name ) {
+					currentSheet = sheets.indexOf(s);
+					break;
+				}
 	}
 
 	function syncSheets() {
@@ -32,9 +40,14 @@ class CdbTable extends hide.ui.View<{ path : String }> {
 		if( editor != null )
 			editor.remove();
 		editor = new hide.comp.cdb.Editor(sheets[index],config,ide.databaseApi,tabContents[index]);
-		editor.focus();
-		editor.onFocus = activate;
+		haxe.Timer.delay(function() {
+			// delay
+			editor.focus();
+			editor.onFocus = activate;
+		},0);
 		undo = ide.databaseApi.undo;
+		currentSheet = index;
+		ide.currentConfig.set("cdb.currentSheet", sheets[index].name);
 	}
 
 	override function onDisplay() {
@@ -64,11 +77,19 @@ class CdbTable extends hide.ui.View<{ path : String }> {
 			tabs.onTabChange = setEditor;
 			tabs.onTabRightClick = function(index) {
 				syncSheets();
-				editor.popupSheet(sheets[index], function() { syncSheets(); rebuild(); });
+				var sheetCount = sheets.length;
+				editor.popupSheet(sheets[index], function() {
+					syncSheets();
+					if( sheets.length > sheetCount )
+						currentSheet = index + 1;
+					else if( sheets.length < sheetCount )
+						currentSheet = index == 0 ? 0 : index - 1;
+					rebuild();
+				});
 			};
 		}
 		if( sheets.length > 0 )
-			setEditor(0);
+			tabs.currentTab = tabContents[currentSheet].parent();
 
 		watch(@:privateAccess ide.databaseFile, () -> {
 			syncSheets();