From bc3b9900c390be9053fa7b32280c940bd4eb4129 Mon Sep 17 00:00:00 2001
From: Zachary Zhang <zacharyzhang7@gmail.com>
Date: Sat, 31 Aug 2024 00:00:36 -0400
Subject: commit

---
 src/client/views/GestureOverlay.tsx | 81 ++++++++++++++++++++++++++++---------
 1 file changed, 61 insertions(+), 20 deletions(-)

(limited to 'src/client/views/GestureOverlay.tsx')

diff --git a/src/client/views/GestureOverlay.tsx b/src/client/views/GestureOverlay.tsx
index abb8445dc..ec94102d7 100644
--- a/src/client/views/GestureOverlay.tsx
+++ b/src/client/views/GestureOverlay.tsx
@@ -7,6 +7,7 @@ import { emptyFunction } from '../../Utils';
 import { Doc, Opt } from '../../fields/Doc';
 import { InkData, InkField, InkTool } from '../../fields/InkField';
 import { NumCast } from '../../fields/Types';
+import { Docs } from '../documents/Documents';
 import {
     ActiveArrowEnd,
     ActiveArrowScale,
@@ -33,6 +34,8 @@ import { ObservableReactComponent } from './ObservableReactComponent';
 import { ActiveFillColor, DocumentView } from './nodes/DocumentView';
 import { CollectionFreeFormView } from './collections/collectionFreeForm';
 import { InkingStroke } from './InkingStroke';
+import { NullLiteral } from 'typescript';
+import { isNull } from 'lodash';
 export enum ToolglassTools {
     InkToText = 'inktotext',
     IgnoreGesture = 'ignoregesture',
@@ -138,7 +141,7 @@ export class GestureOverlay extends ObservableReactComponent<React.PropsWithChil
         let cuspBooleanArray: boolean[] = [];
         let docsToDelete: Doc[] = [];
         const childDocs = (ffView?.ComponentView as CollectionFreeFormView).childDocs;
-        childDocs.map(doc => DocumentView.getDocumentView(doc, DocumentView.getDocumentView(doc))).filter(inkView => inkView?.ComponentView instanceof InkingStroke);
+        childDocs.filter(doc => doc.type === 'ink').map(doc => DocumentView.getDocumentView(doc, DocumentView.getDocumentView(doc)));
         if ((ffView?.ComponentView as CollectionFreeFormView).childDocs) {
             if (cuspArray.length > 4) {
                 console.log('there are enough cusps');
@@ -294,16 +297,17 @@ export class GestureOverlay extends ObservableReactComponent<React.PropsWithChil
                             break;
                         case Gestures.RightAngle:
                             this.convertToText();
+                            this._points = [];
                             console.log('RightAngle');
+                            return;
                         default:
                     }
                 }
-
-                // if no gesture (or if the gesture was unsuccessful), "dry" the stroke into an ink document
                 if (this.isScribble(points)) {
                     this._points = [];
                     return;
                 }
+                // if no gesture (or if the gesture was unsuccessful), "dry" the stroke into an ink document
                 if (!actionPerformed) {
                     const newPoints = this._points.reduce((p, pts) => {
                         p.push([pts.X, pts.Y]);
@@ -327,7 +331,6 @@ export class GestureOverlay extends ObservableReactComponent<React.PropsWithChil
                     this._points.length = 0;
                     this._points.push(...controlPoints);
                     this.dispatchGesture(Gestures.Stroke);
-                    const ffView = DocumentView.allViews().find(view => view.ComponentView instanceof CollectionFreeFormView);
                 }
             }
         }
@@ -335,23 +338,61 @@ export class GestureOverlay extends ObservableReactComponent<React.PropsWithChil
     };
     convertToText() {
         const ffView = DocumentView.allViews().find(view => view.ComponentView instanceof CollectionFreeFormView);
-        const collectionDocs = (ffView?.ComponentView as CollectionFreeFormView).childDocs.filter(doc => doc.type === 'collection').filter(doc => doc.transcription);
-        (ffView?.ComponentView as CollectionFreeFormView).childDocs.forEach(doc => {
-            console.log(doc.x);
-            console.log(doc.y);
-            console.log(doc.width);
-            console.log(doc.height);
-            if (typeof doc.width === 'number' && typeof doc.height === 'number' && typeof doc.x === 'number' && typeof doc.y === 'number') {
-                const rect1 = { minX: doc.x, maxX: doc.x + doc.width, minY: doc.y, maxY: doc.y + doc.height };
-                console.log(rect1);
-                console.log(this.getExtremeCoordinates(this._points));
-                const points = this._points.map(p => ({ X: p.X, Y: p.Y }));
-                console.log(points);
-                if (this.isRectangleOverlap(rect1, this.getExtremeCoordinates(this._points))) {
-                    console.log('somethings in');
+        let docsToBeConverted: Doc[] = [];
+        let minX = 999999999;
+        let maxX = -999999999;
+        let minY = 999999999;
+        let maxY = -999999999;
+        (ffView?.ComponentView as CollectionFreeFormView).childDocs
+            .filter(doc => doc.type === 'collection')
+            .forEach(doc => {
+                if (typeof doc.width === 'number' && typeof doc.height === 'number' && typeof doc.x === 'number' && typeof doc.y === 'number') {
+                    const bounds = DocumentView.getDocumentView(doc)?.getBounds;
+                    console.log(DocumentView.getDocumentView(doc));
+                    console.log(bounds);
+                    if (bounds) {
+                        const rect1 = { minX: bounds.left, maxX: bounds.right, minY: bounds.top, maxY: bounds.bottom };
+                        console.log(rect1);
+                        console.log(this.getExtremeCoordinates(this._points));
+                        const points = this._points.map(p => ({ X: p.X, Y: p.Y }));
+                        console.log(points);
+                        if (this.isRectangleOverlap(rect1, this.getExtremeCoordinates(this._points))) {
+                            if (doc.x < minX) {
+                                minX = doc.x;
+                            }
+                            if (doc.x > maxX) {
+                                maxX = doc.x;
+                            }
+                            if (doc.y < minY) {
+                                minY = doc.y;
+                            }
+                            if (doc.y + doc.height > maxY) {
+                                maxY = doc.y + doc.height;
+                            }
+                            const newDoc = Docs.Create.TextDocument(doc.transcription as string, { title: '', x: doc.x as number, y: minY });
+                            newDoc.height = doc.height;
+                            newDoc.width = doc.width;
+                            if (ffView?.ComponentView?.addDocument && ffView?.ComponentView?.removeDocument) {
+                                ffView.ComponentView.addDocument(newDoc);
+                                ffView.ComponentView.removeDocument(doc);
+                            }
+                            //docsToBeConverted.push(doc);
+                        }
+                    }
                 }
-            }
-        });
+            });
+        // const collectionDoc = Docs.Create.FreeformDocument(docsToBeConverted, { title: '', x: minX as number, y: maxY as number });
+        // collectionDoc.height = 50;
+        // collectionDoc.width = 50;
+        // if (ffView?.ComponentView?.addDocument) {
+        //     ffView.ComponentView.addDocument(collectionDoc);
+        // }
+        // DocumentView.getDocumentView(collectionDoc)?.ComponentView?.updateIcon;
+        // const newDoc = Docs.Create.TextDocument(doc.transcription as string, { title: '', x: doc.x as number, y: maxY });
+        // newDoc.height = 50;
+        // if (ffView?.ComponentView?.addDocument) {
+        //     ffView.ComponentView.addDocument(newDoc);
+        // }
     }
     getNumberOfCusps(points: any) {
         let arrayOfPoints: any[] = [];
-- 
cgit v1.2.3-70-g09d2

