diff options
author | Nathan-SR <144961007+Nathan-SR@users.noreply.github.com> | 2024-05-08 04:14:00 -0400 |
---|---|---|
committer | Nathan-SR <144961007+Nathan-SR@users.noreply.github.com> | 2024-05-08 04:14:00 -0400 |
commit | 920b4ec7a51e88769940b619c1b90b230b353ccb (patch) | |
tree | c31a3d7e476e1fd5f31bc575d1fddf2336dd72b0 /src | |
parent | 1a8a370f67c3076d1b47c3bd8c3929d65badcfeb (diff) |
equation parser working in dash; need to fix auto-updating
Diffstat (limited to 'src')
-rw-r--r-- | src/client/views/collections/collectionSchema/CollectionSchemaView.tsx | 50 | ||||
-rw-r--r-- | src/fields/SchemaHeaderField.ts | 1 |
2 files changed, 39 insertions, 12 deletions
diff --git a/src/client/views/collections/collectionSchema/CollectionSchemaView.tsx b/src/client/views/collections/collectionSchema/CollectionSchemaView.tsx index de38d0c56..d84dd33ff 100644 --- a/src/client/views/collections/collectionSchema/CollectionSchemaView.tsx +++ b/src/client/views/collections/collectionSchema/CollectionSchemaView.tsx @@ -273,7 +273,7 @@ export class CollectionSchemaView extends CollectionSubView() { @undoBatch changeColumnKey = (index: number, newKey: string, defaultVal?: any) => { if (!this.documentKeys.includes(newKey)) { - this.addNewKey(newKey, defaultVal, false); + this.addNewKey(newKey, defaultVal); } const currKeys = this.columnKeys.slice(); // copy the column key array first, then change it. @@ -284,7 +284,7 @@ export class CollectionSchemaView extends CollectionSubView() { @undoBatch addColumn = (key: string, defaultVal?: any) => { if (!this.documentKeys.includes(key)) { - this.addNewKey(key, defaultVal, false); + this.addNewKey(key, defaultVal); } const newColWidth = this.tableWidth / (this.storedColumnWidths.length + 1); @@ -299,11 +299,11 @@ export class CollectionSchemaView extends CollectionSubView() { }; @action - addNewKey = (key: string, defaultVal: any, isEquation: boolean) => { - if (isEquation) { + addNewKey = (key: string, defaultVal: any) => { + if (this._newFieldType == ColumnType.Equation) { this.childDocs.forEach(doc => { - const eq = this.parseEquation(key); - doc[DocData][key] = this.parsedEquationResult(eq); + const eq = this.parseEquation(defaultVal, doc); + doc[DocData][key] = this.parsedEquationResult(eq, doc); this.setupAutoUpdate(eq, doc); }); } else { @@ -316,19 +316,19 @@ export class CollectionSchemaView extends CollectionSubView() { @action setupAutoUpdate = (equation: string, doc: Doc) => { return autorun(() => { - const result = this.parsedEquationResult(equation); + const result = this.parsedEquationResult(equation, doc); doc[DocData][equation] = result; }); } - parseEquation = (eq: string): string => { + parseEquation = (eq: string, doc: Doc): string => { const variablePattern = /[a-z]+/gi; - return eq.replace(variablePattern, (match) => `doc[DocData]['${match}']`); + return eq.replace(variablePattern, (match) => `doc.${match}`); } - parsedEquationResult = (eq: string): number => { - const result = new Function('return ' + eq).call(this); - return result; + parsedEquationResult = (eq: string, doc: any): number => { + const func = new Function('doc', 'return ' + eq); + return func(doc); } @undoBatch @@ -691,6 +691,19 @@ export class CollectionSchemaView extends CollectionSubView() { })} /> ); + case ColumnType.Equation: + return ( + <input + type="text" + name="" + id="" + value={this._newFieldDefault ?? ''} + onPointerDown={e => e.stopPropagation()} + onChange={action((e: any) => { + this._newFieldDefault = e.target.value; + })} + /> + ); default: return undefined; } @@ -714,6 +727,7 @@ export class CollectionSchemaView extends CollectionSubView() { @action setKey = (key: string, defaultVal?: any) => { + console.log("called") if (this._makeNewColumn) { this.addColumn(key, defaultVal); } else { @@ -881,6 +895,18 @@ export class CollectionSchemaView extends CollectionSubView() { /> string </div> + <div className="schema-key-type-option"> + <input + type="radio" + name="newFieldType" + checked={this._newFieldType === ColumnType.Equation} + onChange={action(() => { + this._newFieldType = ColumnType.Equation; + this._newFieldDefault = ''; + })} + /> + equation + </div> <div className="schema-key-default-val">value: {this.fieldDefaultInput}</div> <div className="schema-key-warning">{this._newFieldWarning}</div> <div diff --git a/src/fields/SchemaHeaderField.ts b/src/fields/SchemaHeaderField.ts index 0a8dd1d9e..6fa94204a 100644 --- a/src/fields/SchemaHeaderField.ts +++ b/src/fields/SchemaHeaderField.ts @@ -12,6 +12,7 @@ export enum ColumnType { Image, RTF, Enumeration, + Equation, Any, } |