Browse Source

some handling for ColumnKind = Script

ncannasse 7 years ago
parent
commit
8efa74c94a
3 changed files with 48 additions and 1 deletions
  1. 14 0
      bin/cdb.css
  2. 14 0
      bin/cdb.less
  3. 20 1
      hide/comp/cdb/Cell.hx

+ 14 - 0
bin/cdb.css

@@ -143,6 +143,20 @@
   font-weight: normal;
   font-size: 75%;
 }
+.cdb .cdb-sheet td.t_script {
+  font-family: Courier;
+  color: #CCC;
+}
+.cdb .cdb-sheet td.t_script input,
+.cdb .cdb-sheet td.t_script textarea {
+  font-family: Courier;
+}
+.cdb .cdb-sheet td.t_script .kwd {
+  color: #88F;
+}
+.cdb .cdb-sheet td.t_script .str {
+  color: #C66;
+}
 .cdb .cdb-sheet .tile {
   display: inline-block;
   vertical-align: middle;

+ 14 - 0
bin/cdb.less

@@ -163,6 +163,20 @@
 			font-size: 75%;
 		}
 
+		td.t_script {
+			font-family : Courier;
+			input, textarea {
+				font-family : Courier;
+			}
+			color : #CCC;
+			.kwd {
+				color : #88F;
+			}
+			.str {
+				color : #C66;
+			}
+		}
+
 		.tile {
 			display: inline-block;
 			vertical-align: middle;

+ 20 - 1
hide/comp/cdb/Cell.hx

@@ -21,6 +21,7 @@ class Cell extends Component {
 		this.column = column;
 		@:privateAccess line.cells.push(this);
 		root.addClass("t_" + typeNames[column.type.getIndex()]);
+		if( column.kind == Script ) root.addClass("t_script");
 		refresh();
 	}
 
@@ -64,6 +65,8 @@ class Cell extends Component {
 			}
 		case TId:
 			v == "" ? '<span class="error">#MISSING</span>' : (editor.base.getSheet(sheet.name).index.get(v).obj == obj ? v : '<span class="error">#DUP($v)</span>');
+		case TString if( c.kind == Script ):
+			v == "" ? "&nbsp;" : colorizeScript(v);
 		case TString, TLayer(_):
 			v == "" ? "&nbsp;" : StringTools.htmlEscape(v).split("\n").join("<br/>");
 		case TRef(sname):
@@ -173,6 +176,18 @@ class Cell extends Component {
 		}
 	}
 
+	function colorizeScript( code : String ) {
+		code = StringTools.htmlEscape(code);
+		code = code.split("\n").join("<br/>");
+		// strings
+		code = ~/("[^"]*")/g.replace(code,'<span class="str">$1</span>');
+		code = ~/('[^']*')/g.replace(code,'<span class="str">$1</span>');
+		// keywords
+		for( k in ["for","if","var","this","while","else","do","break","continue","switch","function","return","new","throw","try","catch","case","default"] )
+			code = code.split(k).join('<span class="kwd">$k</span>');
+		return code;
+	}
+
 	function tileHtml( v : cdb.Types.TilePos, ?isInline ) {
 		var path = ide.getPath(v.file);
 		if( !editor.quickExists(path) ) {
@@ -198,7 +213,7 @@ class Cell extends Component {
 			var textSpan = element.wrapInner("<span>").find("span");
 			var textHeight = textSpan.height();
 			var textWidth = textSpan.width();
-			var longText = textHeight > 25;
+			var longText = textHeight > 25 || column.kind == Script;
 			element.empty();
 			element.addClass("edit");
 			var i = new Element(longText ? "<textarea>" : "<input>").appendTo(element);
@@ -240,6 +255,8 @@ class Cell extends Component {
 				e.stopPropagation();
 			});
 			i.keyup(function(_) try {
+				if( column.kind == Script )
+					try new hscript.Parser().parseString(i.val()) catch( e : Dynamic ) throw hscript.Printer.errorToString(e);
 				editor.base.parseValue(column.type, i.val());
 				setErrorMessage(null);
 			} catch( e : Dynamic ) {
@@ -452,6 +469,8 @@ class Cell extends Component {
 			// creates or remove a #DUP : need to refresh the whole table
 			if( prevTarget != null || (prevObj != null && (prevObj.obj != obj || table.sheet.index.get(prevValue) != null)) )
 				table.refresh();
+		case TString if( column.kind == Script ):
+			setValue(StringTools.trim(newValue));
 		default:
 			setValue(newValue);
 		}