aboutsummaryrefslogtreecommitdiff
path: root/src/client/views/nodes/ComparisonBox.tsx
blob: 58af3ad52578aba6dbfbc958bc54e46eab9936f4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
import { MathJax, MathJaxContext } from 'better-react-mathjax';
import { Tooltip } from '@mui/material';
import { action, computed, makeObservable, observable, reaction } from 'mobx';
import { observer } from 'mobx-react';
import * as React from 'react';
import { returnFalse, returnNone, returnTrue, setupMoveUpEvents } from '../../../ClientUtils';
import { emptyFunction } from '../../../Utils';
import { Doc, Opt } from '../../../fields/Doc';
import { DocData } from '../../../fields/DocSymbols';
import { RichTextField } from '../../../fields/RichTextField';
import { DocCast, NumCast, RTFCast, StrCast, toList } from '../../../fields/Types';
import { GPTCallType, gptAPICall, gptImageLabel } from '../../apis/gpt/GPT';
import '../pdf/GPTPopup/GPTPopup.scss';
import { DocUtils } from '../../documents/DocUtils';
import { DocumentType } from '../../documents/DocumentTypes';
import { Docs } from '../../documents/Documents';
import { DragManager } from '../../util/DragManager';
import { dropActionType } from '../../util/DropActionTypes';
import { undoable } from '../../util/UndoManager';
import { ViewBoxAnnotatableComponent } from '../DocComponent';
import { PinDocView, PinProps } from '../PinFuncs';
import { StyleProp } from '../StyleProp';
import './ComparisonBox.scss';
import { DocumentView } from './DocumentView';
import { FieldView, FieldViewProps } from './FieldView';
import { FormattedTextBox } from './formattedText/FormattedTextBox';
import ReactLoading from 'react-loading';
import { ContextMenu } from '../ContextMenu';
import { ContextMenuProps } from '../ContextMenuItem';
import { tickStep } from 'd3';
import { CollectionCarouselView } from '../collections/CollectionCarouselView';
import { FollowLinkScript } from '../../documents/DocUtils';
import { schema } from '../nodes/formattedText/schema_rts';
import { Id } from '../../../fields/FieldSymbols';
import axios from 'axios';
import ReactMarkdown from 'react-markdown';
import { WebField, nullAudio } from '../../../fields/URLField';

const API_URL = 'https://api.unsplash.com/search/photos';

@observer
export class ComparisonBox extends ViewBoxAnnotatableComponent<FieldViewProps>() {
    public static LayoutString(fieldKey: string) {
        return FieldView.LayoutString(ComparisonBox, fieldKey);
    }
    private _disposers: (DragManager.DragDropDisposer | undefined)[] = [undefined, undefined];
    constructor(props: FieldViewProps) {
        super(props);
        makeObservable(this);
        this.setListening();
    }

    @observable private _inputValue = '';
    @observable private _outputValue = '';
    @observable private _loading = false;
    @observable private _isEmpty = false;
    @observable private _audio: Doc = Docs.Create.TextDocument('');
    @observable childActive = false;
    @observable _yRelativeToTop: boolean = true;
    @observable _animating = '';
    @observable mathJaxConfig = {
        loader: { load: ['input/asciimath'] },
    };
    @observable private _listening = false;
    @observable transcriptElement = '';
    @observable private frontSide = false;
    SpeechRecognition = window.SpeechRecognition || window.webkitSpeechRecognition;
    @observable recognition = new this.SpeechRecognition();
    _closeRef = React.createRef<HTMLDivElement>();

    get revealOp() {
        return this.layoutDoc[`_${this._props.fieldKey}_revealOp`];
    }
    get clipHeightKey() {
        return '_' + this._props.fieldKey + '_clipHeight';
    }

    get clipWidthKey() {
        return '_' + this._props.fieldKey + '_clipWidth';
    }

    @computed get clipWidth() {
        return NumCast(this.layoutDoc[this.clipWidthKey], 50);
    }

    @computed get clipHeight() {
        return NumCast(this.layoutDoc[this.clipHeightKey], 200);
    }

    @computed get overlayAlternateIcon() {
        return (
            <Tooltip title={<div className="dash-tooltip">flip</div>}>
                <div
                    className="formattedTextBox-alternateButton"
                    onPointerDown={e =>
                        setupMoveUpEvents(e.target, e, returnFalse, emptyFunction, () => {
                            if (!this.revealOp || this.revealOp === 'flip') {
                                this.flipFlashcard();
                            }
                        })
                    }
                    style={{
                        background: this.revealOp === 'hover' ? 'gray' : this.frontSide ? 'white' : 'black',
                        color: this.revealOp === 'hover' ? 'black' : this.frontSide ? 'black' : 'white',
                        display: 'inline-block',
                    }}>
                    <div key="alternate" className="formattedTextBox-flip">
                        <FontAwesomeIcon icon="turn-up" size="1x" />
                    </div>
                </div>
            </Tooltip>
        );
    }

    @computed get flashcardMenu() {
        return (
            <div>
                <Tooltip
                    title={this.frontSide ? <div className="dash-tooltip">Flip to front side to use GPT</div> : <div className="dash-tooltip">Ask GPT to create an answer on the back side of the flashcard based on your question on the front</div>}>
                    <div style={{ position: 'absolute', bottom: '3px', right: '50px', cursor: 'pointer' }} onPointerDown={e => (!this.frontSide ? this.findImageTags() : null)}>
                        <FontAwesomeIcon icon="lightbulb" size="xl" />
                    </div>
                </Tooltip>
                {DocCast(this.Document.embedContainer).type_collection === 'carousel' ? null : (
                    <div>
                        <Tooltip title={<div>Create a flashcard pile</div>}>
                            <div style={{ position: 'absolute', bottom: '3px', right: '74px', cursor: 'pointer' }} onPointerDown={e => this.createFlashcardPile([this.Document], false)}>
                                <FontAwesomeIcon icon="folder-plus" size="xl" />
                            </div>
                        </Tooltip>
                        <Tooltip title={<div className="dash-tooltip">Create new flashcard stack based on text</div>}>
                            <div style={{ position: 'absolute', bottom: '3px', right: '104px', cursor: 'pointer' }} onClick={e => this.gptFlashcardPile()}>
                                <FontAwesomeIcon icon="layer-group" size="xl" />
                            </div>
                        </Tooltip>
                    </div>
                )}
                <Tooltip title={<div className="dash-tooltip">Hover to reveal</div>}>
                    <div style={{ position: 'absolute', bottom: '3px', right: '25px', cursor: 'pointer' }} onClick={e => this.handleHover()}>
                        <FontAwesomeIcon color={this.revealOp === 'hover' ? 'blue' : 'black'} icon="hand-point-up" size="xl" />
                    </div>
                </Tooltip>
            </div>
        );
    }

    @action handleHover = () => {
        if (this.revealOp === 'hover') {
            this.layoutDoc[`_${this._props.fieldKey}_revealOp`] = 'flip';
            this.Document.forceActive = false;
        } else {
            this.layoutDoc[`_${this._props.fieldKey}_revealOp`] = 'hover';
            this.Document.forceActive = true;
        }
    };

    @action handleInputChange = (e: React.ChangeEvent<HTMLTextAreaElement>) => {
        this._inputValue = e.target.value;
        console.log(this._inputValue);
    };

    @action activateContent = () => {
        this.childActive = true;
    };

    @action handleRenderClick = () => {
        this.frontSide = !this.frontSide;
    };

    @action handleRenderGPTClick = async () => {
        console.log('Phonetic transcription: ' + DocCast(this.Document.audio).phoneticTranscription);
        const phonTrans = DocCast(this.Document.audio).phoneticTranscription;
        if (phonTrans) {
            this._inputValue = StrCast(phonTrans);
            console.log('INPUT:' + this._inputValue);
            this.askGPTPhonemes(this._inputValue);
        } else if (this._inputValue) this.askGPT(GPTCallType.QUIZ);
        this.frontSide = false;
        this._outputValue = '';
    };

    @action
    private onPointerMove = ({ movementX }: PointerEvent) => {
        const width = movementX * this.ScreenToLocalBoxXf().Scale + (this.clipWidth / 100) * this._props.PanelWidth();
        if (width && width > 5 && width < this._props.PanelWidth()) {
            this.layoutDoc[this.clipWidthKey] = (width * 100) / this._props.PanelWidth();
        }
        return false;
    };

    componentDidMount() {
        this._props.setContentViewBox?.(this);
        reaction(
            () => this._props.isSelected(), // when this reaction should update
            selected => !selected && (this.childActive = false) // what it should update to
        );
    }

    protected createDropTarget = (ele: HTMLDivElement | null, fieldKey: string, disposerId: number) => {
        this._disposers[disposerId]?.();
        if (ele) {
            this._disposers[disposerId] = DragManager.MakeDropTarget(ele, (e, dropEvent) => this.internalDrop(e, dropEvent, fieldKey), this.layoutDoc);
        }
    };

    private internalDrop = undoable((e: Event, dropEvent: DragManager.DropEvent, fieldKey: string) => {
        if (dropEvent.complete.docDragData) {
            const { droppedDocuments } = dropEvent.complete.docDragData;
            const added = dropEvent.complete.docDragData.moveDocument?.(droppedDocuments, this.Document, (doc: Doc | Doc[]) => this.addDoc(toList(doc).lastElement(), fieldKey));
            Doc.SetContainer(droppedDocuments.lastElement(), this.dataDoc);
            !added && e.preventDefault();
            e.stopPropagation(); // prevent parent Doc from registering new position so that it snaps back into place
            // this.childActive = false;
            return added;
        }
        return undefined;
    }, 'internal drop');

    getAnchor = (addAsAnnotation: boolean, pinProps?: PinProps) => {
        const anchor = Docs.Create.ConfigDocument({
            title: 'CompareAnchor:' + this.Document.title,
            // set presentation timing properties for restoring view
            presentation_transition: 1000,
            annotationOn: this.Document,
        });
        if (anchor) {
            if (!addAsAnnotation) anchor.backgroundColor = 'transparent';
            /* addAsAnnotation && */ this.addDocument(anchor);
            PinDocView(anchor, { pinDocLayout: pinProps?.pinDocLayout, pinData: { ...(pinProps?.pinData ?? {}), clippable: true } }, this.Document);
            return anchor;
        }
        return this.Document;
    };

    clearDoc = undoable((fieldKey: string) => {
        this.dataDoc[fieldKey] = undefined;
        this._isEmpty = true;
    }, 'clear doc');

    moveDoc = (doc: Doc, addDocument: (document: Doc | Doc[]) => boolean, which: string) => this.remDoc(doc, which) && addDocument(doc);
    addDoc = (doc: Doc, which: string) => {
        if (this.dataDoc[which] && !this._isEmpty) return false;
        this.dataDoc[which] = doc;
        return true;
    };
    remDoc = (doc: Doc, which: string) => {
        if (this.dataDoc[which] === doc) {
            this._isEmpty = true;
            // this.dataDoc[which] = 'empty';
            console.log('HEREEEE');
            this.dataDoc[which] = undefined;
            return true;
        }
        console.log('FALSE');
        return false;
    };

    closeDown = (e: React.PointerEvent, which: string) => {
        setupMoveUpEvents(
            this,
            e,
            moveEv => {
                const de = new DragManager.DocumentDragData([DocCast(this.dataDoc[which])], dropActionType.move);
                de.moveDocument = (doc: Doc | Doc[], targetCollection: Doc | undefined, addDocument: (doc: Doc | Doc[]) => boolean): boolean => addDocument(doc);
                de.canEmbed = true;
                DragManager.StartDocumentDrag([this._closeRef.current!], de, moveEv.clientX, moveEv.clientY);
                return true;
            },
            emptyFunction,
            () => this.clearDoc(which)
        );
    };
    docStyleProvider = (doc: Opt<Doc>, props: Opt<FieldViewProps>, property: string) => {
        switch (property) {
            case StyleProp.PointerEvents: return 'none';
            default:                      return this._props.styleProvider?.(doc, props, property);
        } // prettier-ignore
    };
    moveDoc1 = (docs: Doc | Doc[], targetCol: Doc | undefined, addDoc: (doc: Doc | Doc[]) => boolean) => toList(docs).reduce((res, doc: Doc) => res && this.moveDoc(doc, addDoc, this.fieldKey + '_1'), true);
    moveDoc2 = (docs: Doc | Doc[], targetCol: Doc | undefined, addDoc: (doc: Doc | Doc[]) => boolean) => toList(docs).reduce((res, doc: Doc) => res && this.moveDoc(doc, addDoc, this.fieldKey + '_2'), true);
    remDoc1 = (docs: Doc | Doc[]) => toList(docs).reduce((res, doc) => res && this.remDoc(doc, this.fieldKey + '_1'), true);
    remDoc2 = (docs: Doc | Doc[]) => toList(docs).reduce((res, doc) => res && this.remDoc(doc, this.fieldKey + '_2'), true);

    private registerSliding = (e: React.PointerEvent<HTMLDivElement>, targetWidth: number) => {
        if (e.button !== 2) {
            setupMoveUpEvents(
                this,
                e,
                this.onPointerMove,
                emptyFunction,
                action((moveEv, doubleTap) => {
                    if (doubleTap) {
                        this.childActive = true;
                        if (!this.dataDoc[this.fieldKey + '_1'] && !this.dataDoc[this.fieldKey]) this.dataDoc[this.fieldKey + '_1'] = DocUtils.copyDragFactory(Doc.UserDoc().emptyNote as Doc);
                        if (!this.dataDoc[this.fieldKey + '_2'] && !this.dataDoc[this.fieldKey + '_alternate']) this.dataDoc[this.fieldKey + '_2'] = DocUtils.copyDragFactory(Doc.UserDoc().emptyNote as Doc);
                    }
                }),
                false,
                undefined,
                action(() => {
                    if (this.childActive) return;
                    this._animating = 'all 200ms';
                    // on click, animate slider movement to the targetWidth
                    this.layoutDoc[this.clipWidthKey] = (targetWidth * 100) / this._props.PanelWidth();
                    // this.layoutDoc[this.clipHeightKey] = (targetWidth * 100) / this._props.PanelHeight();

                    setTimeout(
                        action(() => {
                            this._animating = '';
                        }),
                        200
                    );
                })
            );
        }
    };

    setListening = () => {
        const SpeechRecognition = window.SpeechRecognition || window.webkitSpeechRecognition;
        if (SpeechRecognition) {
            this.recognition.continuous = true;
            this.recognition.interimResults = true;
            this.recognition.lang = 'en-US';
            this.recognition.onresult = this.handleResult.bind(this);
        }
        ContextMenu.Instance.setLangIndex(0);
    };

    startListening = () => {
        this.recognition.start();
        this._listening = true;
    };

    stopListening = () => {
        this.recognition.stop();
        this._listening = false;
    };

    setLanguage = (language: string, ind: number) => {
        this.recognition.lang = language;
        ContextMenu.Instance.setLangIndex(ind);
    };

    convertAbr = () => {
        switch (this.recognition.lang) {
            case 'en-US': return 'English'; //prettier-ignore
            case 'es-ES': return 'Spanish'; //prettier-ignore
            case 'fr-FR': return 'French'; //prettier-ignore
            case 'it-IT': return 'Italian'; //prettier-ignore
            case 'zh-CH': return 'Mandarin Chinese'; //prettier-ignore
            case 'ja': return 'Japanese'; //prettier-ignore
            default: return 'Korean'; //prettier-ignore
        }
    };

    openContextMenu = (x: number, y: number, evalu: boolean) => {
        ContextMenu.Instance.clearItems();
        ContextMenu.Instance.addItem({ description: 'English', event: e => this.setLanguage('en-US', 0), icon: 'question' }); //prettier-ignore
        ContextMenu.Instance.addItem({ description: 'Spanish', event: e => this.setLanguage('es-ES', 1 ), icon: 'question'}); //prettier-ignore
        ContextMenu.Instance.addItem({ description: 'French', event: e => this.setLanguage('fr-FR', 2), icon: 'question' }); //prettier-ignore
        ContextMenu.Instance.addItem({ description: 'Italian', event: e => this.setLanguage('it-IT', 3), icon: 'question' }); //prettier-ignore
        if (!evalu) ContextMenu.Instance.addItem({ description: 'Mandarin Chinese', event: e => this.setLanguage('zh-CH', 4), icon: 'question' }); //prettier-ignore
        ContextMenu.Instance.addItem({ description: 'Japanese', event: e => this.setLanguage('ja', 5), icon: 'question' }); //prettier-ignore
        ContextMenu.Instance.addItem({ description: 'Korean', event: e => this.setLanguage('ko', 6), icon: 'question' }); //prettier-ignore
        ContextMenu.Instance.displayMenu(x, y);
    };

    evaluatePronunciation = () => {
        const newAudio = Docs.Create.AudioDocument(nullAudio, { _width: 200, _height: 100 });
        this.Document.audio = newAudio[DocData];
        this._props.DocumentView?.()._props.addDocument?.(newAudio);
    };

    pushInfo = async () => {
        const audio = {
            file: this._audio.url,
        };
        const response = await axios.post('http://localhost:105/recognize/', audio, {
            headers: {
                'Content-Type': 'application/json',
            },
        });
        this.Document.phoneticTranscription = response.data['transcription'];
        console.log('RESPONSE: ' + response.data['transcription']);
    };

    createFlashcardPile(collectionArr: Doc[], gpt: boolean) {
        const newCol = Docs.Create.CarouselDocument(collectionArr, {
            _width: NumCast(this.layoutDoc['_' + this._props.fieldKey + '_width'], 250) + 50,
            _height: NumCast(this.layoutDoc['_' + this._props.fieldKey + '_width'], 200) + 50,
            _layout_fitWidth: false,
            _layout_autoHeight: true,
        });
        newCol['x'] = this.layoutDoc['x'];
        newCol['y'] = NumCast(this.layoutDoc['y']) + 50;
        newCol.type_collection = 'carousel';

        if (gpt) {
            this._props.DocumentView?.()._props.addDocument?.(newCol);
            this._props.removeDocument?.(this.Document);
        } else {
            this._props.addDocument?.(newCol);
            this._props.removeDocument?.(this.Document);
            this.Document.embedContainer = newCol;
        }
    }

    gptFlashcardPile = async () => {
        var text = await this.askGPT(GPTCallType.STACK);
        var senArr = text?.split('Question: ');
        var collectionArr: Doc[] = [];
        for (let i = 1; i < senArr?.length!; i++) {
            const newDoc = Docs.Create.ComparisonDocument(senArr![i], { _layout_isFlashcard: true, _width: 300, _height: 300 });

            if (StrCast(senArr![i]).includes('Keyword: ')) {
                const question = StrCast(senArr![i]).split('Keyword: ');
                const img = await this.fetchImages(question[1]);
                const textSide1 = question[0].includes('Answer: ') ? question[0].split('Answer: ')[0] : question[0];
                const textDoc1 = Docs.Create.TextDocument(question[0]);
                const rtfiel = new RichTextField(
                    JSON.stringify({
                        doc: {
                            type: 'doc',
                            content: [
                                {
                                    type: 'paragraph',
                                    attrs: { align: null, color: null, id: null, indent: null, inset: null, lineSpacing: null, paddingBottom: null, paddingTop: null },
                                    content: [
                                        { type: 'text', text: question[0].includes('Answer: ') ? question[0].split('Answer: ')[0] : question[0] },
                                        { type: 'dashDoc', attrs: { width: '200px', height: '200px', title: 'dashDoc', float: 'unset', hidden: false, docId: img![Id] } },
                                    ],
                                },
                            ],
                        },
                        selection: { type: 'text', anchor: 2, head: 2 },
                    }),
                    textSide1
                );

                textDoc1[DocData].text = rtfiel;
                DocCast(newDoc)[DocData][this.fieldKey + '_1'] = textDoc1;
                DocCast(newDoc)[DocData][this.fieldKey + '_0'] = Docs.Create.TextDocument(question[0].includes('Answer: ') ? question[0].split('Answer: ')[1] : question[1]);
            }
            collectionArr.push(newDoc);
        }
        this.createFlashcardPile(collectionArr, true);
    };

    askGPT = async (callType: GPTCallType): Promise<string | undefined> => {
        const questionText = 'Question: ' + StrCast(RTFCast(DocCast(this.dataDoc[this.fieldKey + '_1']).text)?.Text);
        const rubricText = ' Rubric: ' + StrCast(RTFCast(DocCast(this.dataDoc[this.fieldKey + '_0']).text)?.Text);
        const queryText = questionText + ' UserAnswer: ' + this._inputValue + '. ' + rubricText;
        this._loading = true;
        const doc = DocCast(this.dataDoc[this.props.fieldKey + '_0']);
        if (callType == GPTCallType.CHATCARD) {
            if (StrCast(RTFCast(DocCast(this.dataDoc[this.fieldKey + '_1']).text)?.Text) === '') {
                this._loading = false;
                return;
            }
            this.flipFlashcard();
        }
        try {
            console.log(queryText);
            const res = await gptAPICall(callType == GPTCallType.QUIZ ? queryText : questionText, callType);
            if (!res) {
                console.error('GPT call failed');
                return;
            }
            if (callType == GPTCallType.CHATCARD) {
                DocCast(this.dataDoc[this.props.fieldKey + '_0'])[DocData].text = res;
            } else if (callType == GPTCallType.QUIZ) {
                console.log(this._inputValue);
                this._outputValue = res.replace(/UserAnswer/g, "user's answer").replace(/Rubric/g, 'rubric');
            } else if (callType === GPTCallType.FLASHCARD) {
                this._loading = false;
                return res;
            } else if (callType === GPTCallType.STACK) {
            }
            this._loading = false;
            return res;
        } catch (err) {
            console.error('GPT call failed');
        }
        this._loading = false;
    };
    layoutWidth = () => NumCast(this.layoutDoc.width, 200);
    layoutHeight = () => NumCast(this.layoutDoc.height, 200);

    findImageTags = async () => {
        const c = this.DocumentView?.().ContentDiv!.getElementsByTagName('img');
        if (c?.length === 0) await this.askGPT(GPTCallType.CHATCARD);
        if (c) {
            this._loading = true;
            for (let i of c) {
                console.log(i);
                if (i.className !== 'ProseMirror-separator') await this.getImageDesc(i.src);
            }
            this._loading = false;
        }
    };

    askGPTPhonemes = async (phonemes: string) => {
        const sentence = StrCast(RTFCast(DocCast(this.dataDoc[this.fieldKey + '_1']).text)?.Text);
        const phon = 'w ʌ ɪ z j ɔː ɹ n e ɪ m ';
        const phon2 = 'h ʌ ɛ r j ʌ t ʌ d eɪ';
        const phon6 = 'huː ɑɹ juː tədeɪ';
        const phon3 = 'ʃ eɪ oʊ s i ʃ oʊ z b aɪ ð ə s iː ʃ oʊ';
        const phon4 = 'kamo estas hɔi';
        const phon5 = 'la s e n a l';
        const promptEng =
            'Consider all possible phonetic transcriptions of the intended sentence "' +
            sentence +
            '" that is standard in American speech without showing the user. Compare each word in the following phonemes with those phonetic transcriptions without displaying anything to the user: "' +
            phon6 +
            '". Steps to do this: Align the words with each word in the intended sentence by combining the phonemes to get a pronunciation that resembles the word in order. Do not describe phonetic corrections with the phonetic alphabet - describe it by providing other examples of how it should sound. Note if a word or sound missing, including missing vowels and consonants. If there is an additional word that does not match with the provided sentence, say so. For each word, if any letters mismatch and would sound weird in American speech and they are not allophones of the same phoneme and they are far away from each on the ipa vowel chat and that pronunciation is not normal for the meaning of the word, note this difference and explain how it is supposed to sound. Only note the difference if they are not allophones of the same phoneme and if they are far away on the vowel chart. The goal is to be understood, not sound like a native speaker. Just so you know, "i" sounds like "ee" as in "bee", not "ih" as an "lick". Interpret "ɹ" as the same as "r". Interpret "ʌ" as the same as "ə". If "ɚ", "ɔː", and "ɔ" are options for pronunciation, do not choose "ɚ". Ignore differences with colons. Ignore redundant letters and words and sounds and the splitting of words; do not mention this since there could be repeated words in the sentence. Provide a response like this: "Lets work on improving the pronunciation of "coffee." You said "ceeffee," which is close, but we need to adjust the vowel sound. In American English, "coffee" is pronounced /ˈkɔːfi/, with a long "aw" sound. Try saying "kah-fee." Your intonation is good, but try putting a bit more stress on "like" in the sentence "I would like a coffee with milk." This will make your speech sound more natural. Keep practicing, and lets try saying the whole sentence again!"';
        const promptSpa =
            'Consider all possible phonetic transcriptions of the intended sentence "' +
            'como estás hoy' +
            '" that is standard in Spanish speech without showing the user. Compare each word in the following phonemes with those phonetic transcriptions without displaying anything to the user: "' +
            phon4 +
            '". Steps to do this: Align the words with each word in the intended sentence by combining the phonemes to get a pronunciation that resembles the word in order. Do not describe phonetic corrections with the phonetic alphabet - describe it by providing other examples of how it should sound. Note if a word or sound missing, including missing vowels and consonants. If there is an additional word that does not match with the provided sentence, say so. For each word, if any letters mismatch and would sound weird in Spanish speech and they are not allophones of the same phoneme and they are far away from each on the ipa vowel chat and that pronunciation is not normal for the meaning of the word, note this difference and explain how it is supposed to sound.  Only note the difference if they are not allophones of the same phoneme and if they are far away on the vowel chart; say good job if it would be understood by a native Spanish speaker. Just so you know, "i" sounds like "ee" as in "bee", not "ih" as an "lick". Interpret "ɹ" as the same as "r". Interpret "ʌ" as the same as "ə". Do not make "θ" and "f" interchangable. Do not make "n" and "ɲ" interchangable. Do not make "e" and "i" interchangable. If "ɚ", "ɔː", and "ɔ" are options for pronunciation, do not choose "ɚ". Ignore differences with colons. Ignore redundant letters and words and sounds and the splitting of words; do not mention this since there could be repeated words in the sentence. Identify "ɔi" sounds like "oy". Ignore accents and do not say anything to the user about this.';
        const promptAll =
            'Consider all possible phonetic transcriptions of the intended sentence "' +
            sentence +
            '" that is standard in ' +
            this.convertAbr() +
            ' speech without showing the user. Compare each word in the following phonemes with those phonetic transcriptions without displaying anything to the user: "' +
            phonemes +
            '". Steps to do this: Align the words with each word in the intended sentence by combining the phonemes to get a pronunciation that resembles the word in order. Do not describe phonetic corrections with the phonetic alphabet - describe it by providing other examples of how it should sound. Note if a word or sound missing, including missing vowels and consonants. If there is an additional word that does not match with the provided sentence, say so. For each word, if any letters mismatch and would sound weird in ' +
            this.convertAbr() +
            ' speech and they are not allophones of the same phoneme and they are far away from each on the ipa vowel chat and that pronunciation is not normal for the meaning of the word, note this difference and explain how it is supposed to sound. Just so you know, "i" sounds like "ee" as in "bee", not "ih" as an "lick". Interpret "ɹ" as the same as "r". Interpret "ʌ" as the same as "ə". Do not make "θ" and "f" interchangable. Do not make "n" and "ɲ" interchangable. Do not make "e" and "i" interchangable. If "ɚ", "ɔː", and "ɔ" are options for pronunciation, do not choose "ɚ". Ignore differences with colons. Ignore redundant letters and words and sounds and the splitting of words; do not mention this since there could be repeated words in the sentence. Provide a response like this: "Lets work on improving the pronunciation of "coffee." You said "cawffee," which is close, but we need to adjust the vowel sound. In American English, "coffee" is pronounced /ˈkɔːfi/, with a long "aw" sound. Try saying "kah-fee." Your intonation is good, but try putting a bit more stress on "like" in the sentence "I would like a coffee with milk." This will make your speech sound more natural. Keep practicing, and lets try saying the whole sentence again!"';

        switch (this.recognition.lang) {
            case 'en-US':
                this._outputValue = await gptAPICall(promptEng, GPTCallType.PRONUNCIATION);
                break;
            case 'es-ES':
                this._outputValue = await gptAPICall(promptSpa, GPTCallType.PRONUNCIATION);
                break;
            default:
                this._outputValue = await gptAPICall(promptAll, GPTCallType.PRONUNCIATION);
                break;
        }
    };

    handleResult = (e: SpeechRecognitionEvent) => {
        let interimTranscript = '';
        let finalTranscript = '';
        for (let i = e.resultIndex; i < e.results.length; i++) {
            const transcript = e.results[i][0].transcript;
            if (e.results[i].isFinal) {
                finalTranscript += transcript;
            } else {
                interimTranscript += transcript;
            }
        }
        this._inputValue += finalTranscript;
    };

    fetchImages = async (selection: string) => {
        try {
            const { data } = await axios.get(`${API_URL}?query=${selection}&page=1&per_page=${1}&client_id=Q4zruu6k6lum2kExiGhLNBJIgXDxD6NNj0SRHH_XXU0`);
            console.log(data.results);
            const imageSnapshot = Docs.Create.ImageDocument(data.results[0].urls.small, {
                _nativeWidth: Doc.NativeWidth(this.layoutDoc),
                _nativeHeight: Doc.NativeHeight(this.layoutDoc),
                x: NumCast(this.layoutDoc.x),
                y: NumCast(this.layoutDoc.y),
                onClick: FollowLinkScript(),
                _width: 150,
                _height: 150,
                title: '--snapshot' + NumCast(this.layoutDoc._layout_currentTimecode) + ' image-',
            });
            imageSnapshot['x'] = this.layoutDoc['x'];
            imageSnapshot['y'] = this.layoutDoc['y'];
            return imageSnapshot;
        } catch (error) {
            console.log(error);
        }
    };

    static imageUrlToBase64 = async (imageUrl: string): Promise<string> => {
        try {
            const response = await fetch(imageUrl);
            const blob = await response.blob();

            return new Promise((resolve, reject) => {
                const reader = new FileReader();
                reader.readAsDataURL(blob);
                reader.onloadend = () => resolve(reader.result as string);
                reader.onerror = error => reject(error);
            });
        } catch (error) {
            console.error('Error:', error);
            throw error;
        }
    };

    getImageDesc = async (u: string) => {
        try {
            const hrefBase64 = await ComparisonBox.imageUrlToBase64(u);
            const response = await gptImageLabel(hrefBase64, 'Answer the following question as a short flashcard response. Do not include a label.' + (this.dataDoc.text as RichTextField)?.Text);

            DocCast(this.dataDoc[this.props.fieldKey + '_0'])[DocData].text = response;
        } catch (error) {
            console.log('Error');
        }
    };

    @action
    flipFlashcard = () => {
        this.frontSide = !this.frontSide;
    };

    hoverFlip = (side: boolean) => {
        if (this.revealOp === 'hover') this.layoutDoc[`_${this._props.fieldKey}_usePath`] = side;
    };

    testForTextFields = (whichSlot: string) => {
        const slotData = Doc.Get(this.dataDoc, whichSlot, true);
        const slotHasText = slotData instanceof RichTextField || typeof slotData === 'string';
        const subjectText = RTFCast(this.Document[this.fieldKey])?.Text.trim();
        const altText = RTFCast(this.Document[this.fieldKey + '_alternate'])?.Text.trim();
        const layoutTemplateString =
            slotHasText ? FormattedTextBox.LayoutString(whichSlot):
            whichSlot.endsWith('1') ? (subjectText !== undefined ? FormattedTextBox.LayoutString(this.fieldKey) : undefined) :
                                       altText !== undefined     ? FormattedTextBox.LayoutString(this.fieldKey + '_alternate'): undefined; // prettier-ignore

        // A bit hacky to try out the concept of using GPT to fill in flashcards
        //   If the second slot doesn't have anything in it, but the fieldKey slot has text (e.g., this.text is a string)
        // and the fieldKey + "_alternate" has text that includes a GPT query (indicated by (( && )) ) that is parameterized (optionally) by the fieldKey text (this) or other metadata (this.<field>).
        // eg., this.text_alternate is
        //     "((Provide a one sentence definition for (this) that doesn't use any word in (this.excludeWords) ))"
        //     where (this) is replaced by the text in the fieldKey slot abd this.excludeWords is repalced by the conetnts of the excludeWords field
        // The GPT call will put the "answer"  in the second slot of the comparison (eg., text_2)
        if (whichSlot.endsWith('2') && !layoutTemplateString?.includes(whichSlot)) {
            const queryText = altText?.replace('(this)', subjectText); // TODO: this should be done in Doc.setField but it doesn't know about the fieldKey ...
            if (queryText?.match(/\(\(.*\)\)/)) {
                Doc.SetField(this.Document, whichSlot, ':=' + queryText, false); // make the second slot be a computed field on the data doc that calls ChatGpt
            }
        }
        return layoutTemplateString;
    };

    render() {
        const clearButton = (which: string) => (
            <Tooltip title={<div className="dash-tooltip">remove</div>}>
                <div
                    // style={{ position: 'relative', top: '0px', left: '10px' }}
                    ref={this._closeRef}
                    className={`clear-button ${which}`}
                    onPointerDown={e => this.closeDown(e, which)} // prevent triggering slider movement in registerSliding
                >
                    <FontAwesomeIcon className={`clear-button ${which}`} icon="times" size="xs" />
                </div>
            </Tooltip>
        );
        const displayDoc = (whichSlot: string) => {
            const whichDoc = DocCast(this.dataDoc[whichSlot]);
            const targetDoc = DocCast(whichDoc?.annotationOn, whichDoc);
            const layoutString = targetDoc ? '' : this.testForTextFields(whichSlot);
            // whichDoc['backgroundColor'] = this.layoutDoc['backgroundColor'];

            return targetDoc || layoutString ? (
                // <MathJaxContext config={this.mathJaxConfig}>
                //     <MathJax>
                <>
                    <DocumentView
                        // eslint-disable-next-line react/jsx-props-no-spreading
                        {...this._props}
                        ignoreUsePath={layoutString ? true : undefined}
                        renderDepth={this.props.renderDepth + 1}
                        LayoutTemplateString={layoutString}
                        Document={layoutString ? this.Document : targetDoc}
                        containerViewPath={this.DocumentView?.().docViewPath}
                        moveDocument={whichSlot.endsWith('1') ? this.moveDoc1 : this.moveDoc2}
                        removeDocument={whichSlot.endsWith('1') ? this.remDoc1 : this.remDoc2}
                        NativeWidth={this.layoutWidth}
                        NativeHeight={this.layoutHeight}
                        isContentActive={() => this.childActive}
                        isDocumentActive={returnFalse}
                        dontSelect={returnTrue}
                        whenChildContentsActiveChanged={this.whenChildContentsActiveChanged}
                        styleProvider={this.childActive ? this._props.styleProvider : this.docStyleProvider}
                        hideLinkButton
                        pointerEvents={this.childActive ? undefined : returnNone}
                    />
                    {/* </MathJax> */}
                    {/* <div style={{ position: 'absolute', top: '-5px', left: '2px' }}>{layoutString ? null : clearButton(whichSlot)}</div> */}
                    {/* </MathJaxContext> // placeholder image if doc is missingleft: `${NumCast(this.layoutDoc.width, 200) - 33}px` */}
                </>
            ) : (
                <div className="placeholder">
                    <FontAwesomeIcon className="upload-icon" icon="cloud-upload-alt" size="lg" />
                </div>
            );
        };
        const displayBox = (which: string, index: number, cover: number) => (
            <div
                className={`${index === 0 ? 'before' : 'after'}Box-cont`}
                key={which}
                style={{ width: this._props.PanelWidth() }}
                onPointerDown={e => {
                    this.registerSliding(e, cover);
                    this.activateContent();
                }}
                ref={ele => this.createDropTarget(ele, which, index)}>
                {!this._isEmpty ? displayDoc(which) : null}
                {/* {this.dataDoc[this.fieldKey + '_0'] !== 'empty' ? displayDoc(which) : null} */}
            </div>
        );

        if (this.Document._layout_isFlashcard) {
            const side = this.frontSide ? 1 : 0;

            // add text box to each side when comparison box is first created
            // (!this.dataDoc[this.fieldKey + '_0'] && this.dataDoc[this._props.fieldKey + '_0'] !== 'empty')
            if (!this.dataDoc[this.fieldKey + '_0'] && !this._isEmpty) {
                const dataSplit = StrCast(this.dataDoc.data).includes('Keyword: ') ? StrCast(this.dataDoc.data).split('Keyword: ') : StrCast(this.dataDoc.data).split('Answer: ');
                const newDoc = Docs.Create.TextDocument(dataSplit[1]);
                this.addDoc(newDoc, this.fieldKey + '_0');
            }

            if (!this.dataDoc[this.fieldKey + '_1'] && !this._isEmpty) {
                const dataSplit = StrCast(this.dataDoc.data).includes('Keyword: ') ? StrCast(this.dataDoc.data).split('Keyword: ') : StrCast(this.dataDoc.data).split('Answer: ');
                const newDoc = Docs.Create.TextDocument(dataSplit[0]);
                this.addDoc(newDoc, this.fieldKey + '_1');
            }

            if (DocCast(this.Document.embedContainer) && DocCast(this.Document.embedContainer).practiceMode === 'quiz') {
                const text = StrCast(RTFCast(DocCast(this.dataDoc[this.fieldKey + '_1']).text)?.Text);
                return (
                    <div className={`comparisonBox${this._props.isContentActive() ? '-interactive' : ''}`} style={{ display: 'flex', flexDirection: 'column' }}>
                        <p style={{ color: 'white', padding: 10 }}>{text}</p>
                        <p style={{ display: text === '' ? 'flex' : 'none', color: 'white', marginLeft: '10px' }}>Return to all flashcards and add text to both sides. </p>
                        <div className="input-box">
                            <textarea
                                value={this.frontSide ? this._outputValue : this._inputValue}
                                onChange={this.handleInputChange}
                                onScroll={e => {
                                    e.stopPropagation();
                                    e.preventDefault();
                                }}
                                placeholder={!this.layoutDoc[`_${this._props.fieldKey}_usePath`] ? 'Enter a response for GPT to evaluate.' : ''}
                                readOnly={this.frontSide}></textarea>

                            {this._loading ? (
                                <div className="loading-spinner" style={{ position: 'absolute' }}>
                                    <ReactLoading type="spin" height={30} width={30} color={'blue'} />
                                </div>
                            ) : null}
                        </div>
                        <div>
                            <div className="submit-button" style={{ overflow: 'hidden', display: 'flex', width: '100%' }}>
                                <div
                                    className="submit-buttonschema-header-button"
                                    onPointerDown={e => this.openContextMenu(e.clientX, e.clientY, false)}
                                    style={{ position: 'absolute', top: '5px', left: '11px', zIndex: '100', width: '5px', height: '5px', cursor: 'pointer' }}>
                                    <FontAwesomeIcon color={'white'} icon="caret-down" />
                                </div>
                                <button className="submit-buttonrecord" onClick={this._listening ? this.stopListening : this.startListening} style={{ background: this._listening ? 'lightgray' : '', borderRadius: '2px' }}>
                                    {<FontAwesomeIcon icon="microphone" size="lg" />}
                                </button>
                                <div
                                    className="submit-buttonschema-header-button"
                                    onPointerDown={e => this.openContextMenu(e.clientX, e.clientY, true)}
                                    style={{ position: 'absolute', top: '5px', left: '50px', zIndex: '100', width: '5px', height: '5px', cursor: 'pointer' }}>
                                    <FontAwesomeIcon color={'white'} icon="caret-down" />
                                </div>
                                <button
                                    className="submit-buttonpronunciation"
                                    onClick={this.evaluatePronunciation}
                                    style={{ display: 'inline-flex', alignItems: 'center', background: this._listening ? 'lightgray' : '', borderRadius: '2px', width: '100%' }}>
                                    Evaluate Pronunciation
                                </button>

                                {this.frontSide ? (
                                    <button className="submit-buttonsubmit" type="button" onClick={this.handleRenderGPTClick} style={{ borderRadius: '2px', marginBottom: '3px', width: '100%' }}>
                                        Submit
                                    </button>
                                ) : (
                                    <button className="submit-buttonsubmit" type="button" onClick={this.handleRenderClick} style={{ display: 'inline-flex', alignItems: 'center', borderRadius: '2px', marginBottom: '3px', width: '100%' }}>
                                        Redo the Question
                                    </button>
                                )}
                            </div>
                        </div>
                    </div>
                );
            }

            // render a normal flashcard when not a QuizCard
            return (
                <div
                    className={`comparisonBox${this._props.isContentActive() ? '-interactive' : ''}`} /* change className to easily disable/enable pointer events in CSS */
                    style={{ display: 'flex', flexDirection: 'column', overflow: 'hidden' }}
                    onMouseEnter={() => {
                        this.hoverFlip(false);
                    }}
                    onMouseLeave={() => {
                        this.hoverFlip(true);
                    }}>
                    {displayBox(`${this.fieldKey}_${side === 0 ? 1 : 0}`, side, this._props.PanelWidth() - 3)}
                    {this._loading ? (
                        <div className="loading-spinner" style={{ position: 'absolute' }}>
                            <ReactLoading type="spin" height={30} width={30} color={'blue'} />
                        </div>
                    ) : null}
                    {this._props.isContentActive() ? this.flashcardMenu : null}
                    {this.overlayAlternateIcon}
                </div>
            );
        }
        // render a comparison box that compares items side by side
        return (
            <div className={`comparisonBox${this._props.isContentActive() ? '-interactive' : ''}` /* change className to easily disable/enable pointer events in CSS */}>
                {displayBox(`${this.fieldKey}_2`, 1, this._props.PanelWidth() - 3)}
                <div className="clip-div" style={{ width: this.clipWidth + '%', transition: this._animating, background: StrCast(this.layoutDoc._backgroundColor, 'gray') }}>
                    {displayBox(`${this.fieldKey}_1`, 0, 0)}
                </div>

                <div
                    className="slide-bar"
                    style={{
                        left: `calc(${this.clipWidth + '%'} - 0.5px)`,
                        cursor: this.clipWidth < 5 ? 'e-resize' : this.clipWidth / 100 > (this._props.PanelWidth() - 5) / this._props.PanelWidth() ? 'w-resize' : undefined,
                    }}
                    onPointerDown={e => !this.childActive && this.registerSliding(e, this._props.PanelWidth() / 2)} /* if clicked, return slide-bar to center */
                >
                    <div className="slide-handle" />
                </div>
            </div>
        );
    }
}

Docs.Prototypes.TemplateMap.set(DocumentType.COMPARISON, {
    layout: { view: ComparisonBox, dataField: 'data' },
    options: {
        acl: '',
        backgroundColor: 'gray',
        dropAction: dropActionType.move,
        waitForDoubleClickToClick: 'always',
        _layout_reflowHorizontal: true,
        _layout_reflowVertical: true,
        _layout_nativeDimEditable: true,
        systemIcon: 'BsLayoutSplit',
    },
});