From 8147fd7d80746114a2245237e4dab2c89e2d49a2 Mon Sep 17 00:00:00 2001
From: Naafiyan Ahmed <naafiyan@gmail.com>
Date: Mon, 2 May 2022 16:27:49 -0400
Subject: working on getting subgroups to work

---
 src/client/views/InkTranscription.tsx | 87 ++++++++++++++++++++++++++++++++---
 1 file changed, 80 insertions(+), 7 deletions(-)

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

diff --git a/src/client/views/InkTranscription.tsx b/src/client/views/InkTranscription.tsx
index b1f807bdf..f7260c5b4 100644
--- a/src/client/views/InkTranscription.tsx
+++ b/src/client/views/InkTranscription.tsx
@@ -1,13 +1,14 @@
 import * as iink from 'iink-js';
 import { action, observable } from 'mobx';
 import * as React from 'react';
-import { DataSym, Doc, DocListCast } from '../../fields/Doc';
+import { DataSym, Doc, DocListCast, HeightSym, WidthSym } from '../../fields/Doc';
 import { InkData, InkField } from "../../fields/InkField";
 import { Cast, DateCast, NumCast } from '../../fields/Types';
 import { DocumentType } from "../documents/DocumentTypes";
 import './InkTranscription.scss';
 import { aggregateBounds, Utils } from '../../Utils';
 import { timesSeries } from 'async';
+import { CollectionFreeFormView } from './collections/collectionFreeForm';
 
 export class InkTranscription extends React.Component {
     static Instance: InkTranscription;
@@ -106,9 +107,12 @@ export class InkTranscription extends React.Component {
         return this._textRef = r;
     }
 
-    transcribeInk = (groupDoc: Doc | undefined, containingLayout: Doc, inkDocs: Doc[], math: boolean) => {
+    transcribeInk = (groupDoc: Doc | undefined, containingLayout: Doc, inkDocs: Doc[], math: boolean, ffView?: CollectionFreeFormView) => {
         if (!groupDoc) return;
         this.inkDocs = inkDocs;
+        if (ffView) {
+            this.ffView = ffView;
+        }
 
         const validInks = inkDocs.filter(s => s.type === DocumentType.INK);
 
@@ -165,6 +169,69 @@ export class InkTranscription extends React.Component {
         return max - min;
     }
 
+    subgroupsTranscriptions = (wordInkDocMap: Map<string, Doc[]>) => {
+         // TODO: nda - will probably want to go through ffView unprocessed docs and then see if any of the inksToGroup docs are in it and only use those
+        // loop through the words in wordInkDocMap
+        // for each word, get the inkDocs
+
+        // iterate through the keys of wordInkDocMap
+        wordInkDocMap.forEach((inkDocs: Doc[], word: string) => {
+            const selected = wordInkDocMap.get(word);
+            if (!selected) {
+                return;
+            }
+             // loop through selected an get the bound
+            const bounds: { x: number, y: number, width?: number, height?: number }[] = []
+
+            selected.map(action(d => {
+                const x = NumCast(d.x);
+                const y = NumCast(d.y);
+                const width = d[WidthSym]();
+                const height = d[HeightSym]();
+                bounds.push({ x, y, width, height });
+            }))
+
+            const aggregBounds = aggregateBounds(bounds, 0, 0);
+            const marqViewRef = this.ffView._marqueeViewRef.current;
+
+            // set the vals for bounds in marqueeView
+            if (marqViewRef) {
+                marqViewRef._downX = aggregBounds.x;
+                marqViewRef._downY = aggregBounds.y;
+                marqViewRef._lastX = aggregBounds.r;
+                marqViewRef._lastY = aggregBounds.b;
+            }
+
+            selected.map(action(d => {
+                const dx = NumCast(d.x);
+                const dy = NumCast(d.y);
+                delete d.x;
+                delete d.y;
+                delete d.activeFrame;
+                delete d._timecodeToShow;  // bcz: this should be automatic somehow.. along with any other properties that were logically associated with the original collection
+                delete d._timecodeToHide;  // bcz: this should be automatic somehow.. along with any other properties that were logically associated with the original collection
+                // calculate pos based on bounds
+                if (marqViewRef?.Bounds) {
+                    d.x = dx - marqViewRef.Bounds.left - marqViewRef.Bounds.width / 2;
+                    d.y = dy - marqViewRef.Bounds.top - marqViewRef.Bounds.height / 2;
+                }
+                return d;
+            }));
+            this.ffView.props.removeDocument?.(selected);
+            // TODO: nda - this is the code to actually get a new grouped collection
+            const newCollection = marqViewRef?.getCollection(selected, undefined, [], true);
+            if (newCollection) {
+                newCollection.height = newCollection[HeightSym]();
+                newCollection.width = newCollection[WidthSym]();
+                newCollection.title = word;
+            }
+
+
+            // nda - bug: when deleting a stroke before leaving writing mode, delete the stroke from unprocessed ink docs
+            newCollection && this.ffView.props.addDocument?.(newCollection);
+        });
+    }
+
     exportInk = (e: any, ref: any) => {
         const exports = e.detail.exports;
         console.log(e);
@@ -186,10 +253,12 @@ export class InkTranscription extends React.Component {
                     // map timestamp to strokes
                     const timestampWord = new Map<number, string>();
                     this.lastJiix.words.map((word: any) => {
-                        word.items.map((i: any) => {
-                            const ms = Date.parse(i.timestamp);
-                            timestampWord.set(ms, word.label);
-                        })
+                        if (word.items) {
+                            word.items.forEach((i: {id: string, timestamp: string, X: Array<number>, Y: Array<number>, F: Array<number>}) => {
+                                const ms = Date.parse(i.timestamp);
+                                timestampWord.set(ms, word.label);
+                            })
+                        }
                     })
 
                     const wordInkDocMap = new Map<string, Doc[]>();
@@ -210,8 +279,12 @@ export class InkTranscription extends React.Component {
                                 const newEntry = [inkDoc];
                                 wordInkDocMap.set(word, newEntry);
                             }
-                            
                         });
+                        // do the fun marq stuff
+                        // generate subgroups
+
+                        console.log(wordInkDocMap);
+                        this.subgroupsTranscriptions(wordInkDocMap);
 
                     }
                     // we can iterate through docs and for each doc index into the map by timestamp
-- 
cgit v1.2.3-70-g09d2

