From 2d7037dd5664700cda04b1fb0c6c77fe6f49d66c Mon Sep 17 00:00:00 2001
From: Nathan-SR <144961007+Nathan-SR@users.noreply.github.com>
Date: Thu, 9 May 2024 23:33:17 -0400
Subject: goodbye old parser ;( hello new parser :)

---
 .../collectionSchema/CollectionSchemaView.tsx      | 75 +++++++++++-----------
 1 file changed, 39 insertions(+), 36 deletions(-)

(limited to 'src/client/views/collections/collectionSchema/CollectionSchemaView.tsx')

diff --git a/src/client/views/collections/collectionSchema/CollectionSchemaView.tsx b/src/client/views/collections/collectionSchema/CollectionSchemaView.tsx
index 19d170c81..e493b2e5d 100644
--- a/src/client/views/collections/collectionSchema/CollectionSchemaView.tsx
+++ b/src/client/views/collections/collectionSchema/CollectionSchemaView.tsx
@@ -6,7 +6,7 @@ import { observer } from 'mobx-react';
 import * as React from 'react';
 import { returnEmptyDoclist, returnEmptyString, returnFalse, returnIgnore, returnNever, returnTrue, setupMoveUpEvents, smoothScroll } from '../../../../ClientUtils';
 import { emptyFunction } from '../../../../Utils';
-import { Doc, DocListCast, Field, FieldType, NumListCast, Opt, StrListCast } from '../../../../fields/Doc';
+import { Doc, DocListCast, Field, FieldType, IdToDoc, NumListCast, Opt, StrListCast } from '../../../../fields/Doc';
 import { DocData } from '../../../../fields/DocSymbols';
 import { Id } from '../../../../fields/FieldSymbols';
 import { List } from '../../../../fields/List';
@@ -288,7 +288,6 @@ export class CollectionSchemaView extends CollectionSubView() {
             this.addNewKey(key, defaultVal);
         }
         
-        this.modifyCellTags(true);
         const newColWidth = this.tableWidth / (this.storedColumnWidths.length + 1);
         const currWidths = this.storedColumnWidths.slice();
         currWidths.splice(0, 0, newColWidth);
@@ -302,19 +301,24 @@ export class CollectionSchemaView extends CollectionSubView() {
 
     @action
     addNewKey = (key: string, defaultVal: any) => {
-        if (this._newFieldType == ColumnType.Equation) {
-            this.childDocs.forEach(doc => {
-                const parsedEquation = this.parseEquation(defaultVal);
-                const val = computed(() => { return this.parsedEquationResult(parsedEquation, doc);})
-                doc[DocData][key] = val.get();
-                // doc[DocData][key] = this.parsedEquationResult(eq, doc);
-                // this.setupAutoUpdate(eq, doc);
-            });
-        } else {
-            this.childDocs.forEach(doc => {
-                doc[DocData][key] = defaultVal;
-            });
+        this.childDocs.forEach(doc => {
+            doc[DocData][key] = defaultVal;
+        });
+    }
+
+    cleanupComputedField = (field: string) => {
+        const idPattern = /idToDoc\((.*?)\)/g;
+        let modField = field.slice();
+        let matches;
+        let results = new Map<string, string>();
+        while ((matches = idPattern.exec(field)) !== null) {
+            results.set(matches[0], matches[1].replace(/"/g, '')); 
         }
+        console.log(results);
+        results.forEach((id, funcId) => {
+            modField = modField.replace(funcId, 'd' + (this.rowIndex(IdToDoc(id)) + 1).toString());
+        })
+        return modField;
     }
 
     // @action
@@ -325,30 +329,30 @@ export class CollectionSchemaView extends CollectionSubView() {
     //     });
     // }
 
-    parseEquation = (eq: string): [string, Map<string, Doc>] => {
-        const variablePattern = /[a-z]+/gi;
-        const tagRefPattern = /{([^}]*)}/g;
-        const docTagRefPairs = new Map<string, Doc>();
-        let modifiedEq = eq;
-        let match;
+    // parseEquation = (eq: string): [string, Map<string, Doc>] => {
+    //     const variablePattern = /[a-z]+/gi;
+    //     const tagRefPattern = /{([^}]*)}/g;
+    //     const docTagRefPairs = new Map<string, Doc>();
+    //     let modifiedEq = eq;
+    //     let match;
     
-        while (match = tagRefPattern.exec(eq)) {
-            const ref = match[1]; 
-            const [doc, key] = this.getCellInfoFromTag(ref);
-            docTagRefPairs.set(ref, doc);
-            const replacement = `docTagRefPairs.get('${ref}').${key}`;
-            modifiedEq = modifiedEq.replace(new RegExp(`{${ref}}`, 'g'), replacement);
-        }
+    //     while (match = tagRefPattern.exec(eq)) {
+    //         const ref = match[1]; 
+    //         const [doc, key] = this.getCellInfoFromTag(ref);
+    //         docTagRefPairs.set(ref, doc);
+    //         const replacement = `docTagRefPairs.get('${ref}').${key}`;
+    //         modifiedEq = modifiedEq.replace(new RegExp(`{${ref}}`, 'g'), replacement);
+    //     }
     
-        //modifiedEq = modifiedEq.replace(variablePattern, (match) => `doc.${match}`);
+    //     modifiedEq = modifiedEq.replace(variablePattern, (match) => `doc.${match}`);
     
-        return [modifiedEq, docTagRefPairs];
-    }
+    //     return [modifiedEq, docTagRefPairs];
+    // }
     
-    parsedEquationResult = (eq: [string, Map<string, Doc>], doc: any): number => {
-        const func = new Function('doc', 'docTagRefPairs', 'return ' + eq[0]);
-        return func(doc, eq[1]);
-    }
+    // parsedEquationResult = (eq: [string, Map<string, Doc>], doc: any): number => {
+    //     const func = new Function('doc', 'docTagRefPairs', 'return ' + eq[0]);
+    //     return func(doc, eq[1]);
+    // }
 
     @undoBatch
     removeColumn = (index: number) => {
@@ -547,7 +551,6 @@ export class CollectionSchemaView extends CollectionSubView() {
     selectCell = (doc: Doc, col: number, shiftKey: boolean, ctrlKey: boolean) => {
         this.populateCellTags();
         console.log(this.getCellTag(doc, col));
-        console.log(this.getCellInfoFromTag("C1"));
         if (!shiftKey && !ctrlKey) this.clearSelection();
         !this._selectedCells && (this._selectedCells = []);
         !shiftKey && this._selectedCells && this._selectedCells.push(doc);
@@ -602,7 +605,7 @@ export class CollectionSchemaView extends CollectionSubView() {
 
         const colTag = tag.replace(/[^A-Z]/g, '');
         const col = lettersToNumber(colTag);
-        const key: string = this.columnKeys[col];
+        const key: string = this.columnKeys[col - 1];
 
         const rowNum = Number(tag.replace(/[^\d]/g, ''));
         const doc = this.childDocs[rowNum - 1];
-- 
cgit v1.2.3-70-g09d2

