aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/client/views/collections/collectionSchema/CollectionSchemaView.tsx50
-rw-r--r--src/fields/SchemaHeaderField.ts1
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,
}