Bladeren bron

more cdb integration: properties display, goto reference, keyboard shortcuts

Nicolas Cannasse 7 jaren geleden
bovenliggende
commit
1094364916
5 gewijzigde bestanden met toevoegingen van 186 en 40 verwijderingen
  1. 3 0
      bin/defaultProps.json
  2. 13 3
      hide/comp/cdb/Cursor.hx
  3. 43 17
      hide/comp/cdb/Editor.hx
  4. 124 10
      hide/comp/cdb/SubTable.hx
  5. 3 10
      hide/comp/cdb/Table.hx

+ 3 - 0
bin/defaultProps.json

@@ -21,6 +21,9 @@
 	// cdb keys
 
 	"key.cdb.showReferences" : "F3",
+	"key.cdb.gotoReference" : "F4",
+	"key.cdb.toggleList" : "Enter",
+	"key.cdb.closeList" : "Escape",
 
 	// cdb config
 

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

@@ -26,11 +26,21 @@ class Cursor {
 		if( update ) this.update();
 	}
 
+	public function setDefault(line, column) {
+		set(editor.tables[0], column, line);
+	}
+
 	public function getLine() {
 		if( table == null ) return null;
 		return table.lines[y];
 	}
 
+	public function getCell() {
+		var line = getLine();
+		if( line == null ) return null;
+		return line.cells[x];
+	}
+
 	public function move( dx : Int, dy : Int, shift : Bool, ctrl : Bool ) {
 		if( table == null )
 			return;
@@ -46,7 +56,7 @@ class Cursor {
 			y--;
 		if( dx > 0 && x < table.sheet.columns.length - 1 )
 			x++;
-		if( dy > 0 && y < table.sheet.lines.length - 1 )
+		if( dy > 0 && y < table.lines.length - 1 )
 			y++;
 		select = null;
 		update();
@@ -63,8 +73,8 @@ class Cursor {
 			y = 0;
 			select = null;
 		}
-		if( y >= table.sheet.lines.length ) {
-			y = table.sheet.lines.length - 1;
+		if( y >= table.lines.length ) {
+			y = table.lines.length - 1;
 			select = null;
 		}
 		var max = table.sheet.props.isProps ? 1 : table.sheet.columns.length;

+ 43 - 17
hide/comp/cdb/Editor.hx

@@ -32,6 +32,29 @@ class Editor extends Component {
 		keys.register("cdb.showReferences", showReferences);
 		keys.register("undo", function() if( undo.undo() ) { refresh(); save(); });
 		keys.register("redo", function() if( undo.redo() ) { refresh(); save(); });
+		keys.register("cdb.toggleList", function() {
+			var c = cursor.getCell();
+			if( c != null )
+				switch( c.column.type ) {
+				case TProperties, TList: c.root.click(); // toggle
+				default:
+				}
+		});
+		keys.register("cdb.closeList", function() {
+			var c = cursor.getCell();
+			if( c != null ) {
+				var sub = Std.instance(c.table, SubTable);
+				if( sub != null ) {
+					sub.cell.root.click();
+					return;
+				}
+			}
+			if( cursor.select != null ) {
+				cursor.select = null;
+				cursor.update();
+			}
+		});
+		keys.register("cdb.gotoReference", gotoReference);
 		base = sheet.base;
 		cursor = new Cursor(this);
 		refresh();
@@ -61,7 +84,6 @@ class Editor extends Component {
 	}
 
 	function searchFilter( filter : String ) {
-
 		if( filter == "" ) filter = null;
 		if( filter != null ) filter = filter.toLowerCase();
 
@@ -108,9 +130,28 @@ class Editor extends Component {
 		// todo : port from old cdb
 	}
 
+	function gotoReference() {
+		var c = cursor.getCell();
+		if( c == null || c.value == null ) return;
+		switch( c.column.type ) {
+		case TRef(s):
+			var sd = base.getSheet(s);
+			if( sd == null ) return;
+			var k = sd.index.get(c.value);
+			if( k == null ) return;
+			var index = sd.lines.indexOf(k.obj);
+			if( index >= 0 ) openReference(sd, index, 0);
+		default:
+		}
+	}
+
+	function openReference( s : cdb.Sheet, line : Int, column : Int ) {
+		ide.open("hide.view.CdbTable", { path : s.name }, function(view) @:privateAccess Std.instance(view,hide.view.CdbTable).editor.cursor.setDefault(line,column));
+	}
+
 	function refresh() {
 
-		root.html('');
+		root.empty();
 		root.addClass('cdb');
 
 		searchBox = new Element("<div>").addClass("searchBox").appendTo(root);
@@ -193,21 +234,6 @@ class Editor extends Component {
 	public function popupLine( line : Line ) {
 	}
 
-	public function makeSubSheet( cell : Cell ) {
-		var sheet = cell.table.sheet;
-		var c = cell.column;
-		var index = cell.line.index;
-		var key = sheet.getPath() + "@" + c.name + ":" + index;
-		var psheet = sheet.getSub(c);
-		return new cdb.Sheet(base,{
-			columns : psheet.columns, // SHARE
-			props : psheet.props, // SHARE
-			name : psheet.name, // same
-			lines : cell.value, // ref
-			separators : [], // none
-		},key, { sheet : sheet, column : cell.columnIndex, line : index });
-	}
-
 	public function close() {
 		for( t in tables.copy() )
 			t.dispose();

+ 124 - 10
hide/comp/cdb/SubTable.hx

@@ -6,20 +6,17 @@ class SubTable extends Table {
 	var slider : Element;
 	public var cell : Cell;
 
-	public function new(editor, sheet:cdb.Sheet, cell:Cell) {
+	public function new(editor, cell:Cell) {
 
+		this.editor = editor;
 		this.cell = cell;
-		if( sheet.lines == null ) {
-			@:privateAccess sheet.sheet.lines = [];
-			Reflect.setField(cell.line.obj, cell.column.name, sheet.lines);
-			// do not save for now
-		}
 
+		var sheet = makeSubSheet();
 		var line = cell.line;
 		if( line.subTable != null ) throw "assert";
 		line.subTable = this;
 
-		insertedTR = new Element("<tr>").addClass("list");
+		insertedTR = new Element("<tr>").addClass(cell.column.type == TProperties ? "props" : "list");
 		new Element("<td>").appendTo(insertedTR);
 		var group = new Element("<td>").attr("colspan", "" + cell.table.sheet.columns.length).appendTo(insertedTR);
 		slider = new Element("<div>").appendTo(group);
@@ -33,6 +30,41 @@ class SubTable extends Table {
 		super(editor, sheet, root);
 	}
 
+	public function makeSubSheet() {
+		var sheet = cell.table.sheet;
+		var c = cell.column;
+		var index = cell.line.index;
+		var key = sheet.getPath() + "@" + c.name + ":" + index;
+		var psheet = sheet.getSub(c);
+		var lines = switch( cell.column.type ) {
+		case TList:
+			var value = cell.value;
+			if( value == null ) {
+				value = [];
+				Reflect.setField(cell.line.obj, cell.column.name, sheet.lines);
+				// do not save for now
+			}
+			value;
+		case TProperties:
+			var value = cell.value;
+			if( value == null ) {
+				value = {};
+				Reflect.setField(cell.line.obj, cell.column.name, sheet.lines);
+				// do not save for now
+			}
+			[for( c in sheet.columns ) cell.value];
+		default:
+			throw "assert";
+		}
+		return new cdb.Sheet(editor.base,{
+			columns : psheet.columns, // SHARE
+			props : psheet.props, // SHARE
+			name : psheet.name, // same
+			lines : lines, // ref
+			separators : [], // none
+		},key, { sheet : sheet, column : cell.columnIndex, line : index });
+	}
+
 	public function show() {
 		slider.slideDown(100);
 	}
@@ -48,11 +80,93 @@ class SubTable extends Table {
 	override function dispose() {
 		super.dispose();
 		insertedTR.remove();
-		if( cell.column.opt && sheet.lines.length == 0 ) {
-			Reflect.deleteField(cell.line.obj, cell.column.name);
-			editor.save();
+		if( cell.column.opt ) {
+			var isEmpty = switch( cell.column.type ) {
+			case TList: sheet.lines.length == 0;
+			case TProperties: Reflect.fields(sheet.lines[0]).length == 0;
+			default: false;
+			}
+			if( isEmpty ) {
+				Reflect.deleteField(cell.line.obj, cell.column.name);
+				editor.save();
+			}
 		}
 		cell.refresh();
 	}
 
+	override function refresh() {
+		switch( cell.column.type ) {
+		case TProperties:
+			refreshProperties();
+		default:
+			super.refresh();
+		}
+	}
+
+	function refreshProperties() {
+		root.empty();
+
+		lines = [];
+
+		var available = [];
+		var props = sheet.lines[0];
+		for( c in sheet.columns ) {
+			if( c.opt && !Reflect.hasField(props,c.name) ) {
+				available.push(c);
+				continue;
+			}
+
+			var v = Reflect.field(props, c.name);
+			var l = new Element("<tr>").appendTo(root);
+			var th = new Element("<th>").text(c.name).appendTo(l);
+			var td = new Element("<td>").addClass("c").appendTo(l);
+
+			var line = new Line(this, lines.length, l);
+			var cell = new Cell(td, line, c);
+			lines.push(line);
+
+			td.click(function(e) {
+				editor.cursor.clickCell(cell, e.shiftKey);
+				e.stopPropagation();
+			});
+
+			th.mousedown(function(e) {
+				if( e.which == 3 ) {
+					editor.popupColumn(this, c);
+					editor.cursor.clickCell(cell, false);
+					e.preventDefault();
+					return;
+				}
+			});
+		}
+
+		/*
+		var end = J("<tr>").appendTo(content);
+		end = J("<td>").attr("colspan", "2").appendTo(end);
+		var sel = J("<select>").appendTo(end);
+		J("<option>").attr("value", "").text("--- Choose ---").appendTo(sel);
+		for( c in available )
+			J("<option>").attr("value",c.name).text(c.name).appendTo(sel);
+		J("<option>").attr("value","new").text("New property...").appendTo(sel);
+		sel.change(function(e) {
+			e.stopPropagation();
+			var v = sel.val();
+			if( v == "" )
+				return;
+			sel.val("");
+			if( v == "new" ) {
+				newColumn(sheet.name);
+				return;
+			}
+			for( c in available )
+				if( c.name == v ) {
+					Reflect.setField(props, c.name, base.getDefault(c, true));
+					save();
+					refresh();
+					return;
+				}
+		});*/
+
+	}
+
 }

+ 3 - 10
hide/comp/cdb/Table.hx

@@ -49,8 +49,6 @@ class Table extends Component {
 
 
 		var colCount = sheet.columns.length;
-		var todo = [];
-		var inTodo = false;
 		for( cindex in 0...sheet.columns.length ) {
 			var c = sheet.columns[cindex];
 			var col = J("<th>");
@@ -82,7 +80,7 @@ class Table extends Component {
 				});
 
 				switch( c.type ) {
-				case TList:
+				case TList, TProperties:
 					var key = sheet.getPath() + "@" + c.name + ":" + index;
 					cell.root.click(function(e) {
 						e.stopPropagation();
@@ -122,7 +120,7 @@ class Table extends Component {
 						if( titles.length == 0 ) titles = null;
 						sheet.props.separatorTitles = titles;
 						editor.undo.change(Field(sheet.props,"separatorTitles",old));
-						editor.save(); // no undo on separator rename
+						editor.save();
 
 					}).keypress(function(e) {
 						e.stopPropagation();
@@ -144,10 +142,6 @@ class Table extends Component {
 			});
 			root.append(l);
 		}
-
-		inTodo = true;
-		for( t in todo ) t();
-		inTodo = false;
 	}
 
 	function toggleList( cell : Cell ) {
@@ -158,8 +152,7 @@ class Table extends Component {
 			line.subTable = null;
 			if( cur.cell == cell ) return; // toggle
 		}
-		var sheet = editor.makeSubSheet(cell);
-		var sub = new SubTable(editor, sheet, cell);
+		var sub = new SubTable(editor, cell);
 		sub.show();
 		editor.cursor.set(sub);
 	}