aboutsummaryrefslogtreecommitdiff
path: root/src/client/views/nodes/ImageBox.tsx
blob: e0ef8f4230bf7fb1beb2afadb3ec4e302fecdd16 (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
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
import { Tooltip } from '@mui/material';
import axios from 'axios';
import { Colors, Button, Type } from '@dash/components';
import { action, computed, IReactionDisposer, makeObservable, observable, ObservableMap, reaction } from 'mobx';
import { observer } from 'mobx-react';
import { extname } from 'path';
import * as React from 'react';
import ReactLoading from 'react-loading';
import { ClientUtils, DashColor, returnEmptyString, returnFalse, returnOne, returnZero, setupMoveUpEvents, UpdateIcon } from '../../../ClientUtils';
import { Doc, DocListCast, Opt } from '../../../fields/Doc';
import { DocData } from '../../../fields/DocSymbols';
import { Id } from '../../../fields/FieldSymbols';
import { InkTool } from '../../../fields/InkField';
import { ObjectField } from '../../../fields/ObjectField';
import { Cast, ImageCast, NumCast, RTFCast, StrCast } from '../../../fields/Types';
import { ImageField } from '../../../fields/URLField';
import { TraceMobx } from '../../../fields/util';
import { emptyFunction } from '../../../Utils';
import { Docs } from '../../documents/Documents';
import { DocumentType } from '../../documents/DocumentTypes';
import { DocUtils, FollowLinkScript } from '../../documents/DocUtils';
import { Networking } from '../../Network';
import { DragManager } from '../../util/DragManager';
import { SnappingManager } from '../../util/SnappingManager';
import { undoable, undoBatch } from '../../util/UndoManager';
import { CollectionFreeFormView } from '../collections/collectionFreeForm/CollectionFreeFormView';
import { ContextMenu } from '../ContextMenu';
import { ContextMenuProps } from '../ContextMenuItem';
import { ViewBoxAnnotatableComponent } from '../DocComponent';
import { MarqueeAnnotator } from '../MarqueeAnnotator';
import { OverlayView } from '../OverlayView';
import { AnchorMenu } from '../pdf/AnchorMenu';
import { PinDocView, PinProps } from '../PinFuncs';
import { StickerPalette } from '../smartdraw/StickerPalette';
import { StyleProp } from '../StyleProp';
import { DocumentView } from './DocumentView';
import { FieldView, FieldViewProps } from './FieldView';
import { FocusViewOptions } from './FocusViewOptions';
import './ImageBox.scss';
import { OpenWhere } from './OpenWhere';
import { Upload } from '../../../server/SharedMediaTypes';
import { SmartDrawHandler } from '../smartdraw/SmartDrawHandler';
import { SettingsManager } from '../../util/SettingsManager';
import { AiOutlineSend } from 'react-icons/ai';
import { FireflyImageData } from '../smartdraw/FireflyConstants';

export class ImageEditorData {
    // eslint-disable-next-line no-use-before-define
    private static _instance: ImageEditorData;
    private static get imageData() { return (ImageEditorData._instance ?? new ImageEditorData()).imageData; } // prettier-ignore
    @observable imageData: { rootDoc: Doc | undefined; open: boolean; source: string; addDoc: Opt<(doc: Doc | Doc[], annotationKey?: string) => boolean> } = observable({ rootDoc: undefined, open: false, source: '', addDoc: undefined });
    @action private static set = (open: boolean, rootDoc: Doc | undefined, source: string, addDoc: Opt<(doc: Doc | Doc[], annotationKey?: string) => boolean>) => {
        this._instance.imageData = { open, rootDoc, source, addDoc };
    };

    constructor() {
        makeObservable(this);
        ImageEditorData._instance = this;
    }

    public static get Open()                     { return ImageEditorData.imageData.open;  } // prettier-ignore
    public static set Open(open: boolean)        { ImageEditorData.set(open, this.imageData.rootDoc, this.imageData.source, this.imageData.addDoc); } // prettier-ignore
    public static get Source()                   { return ImageEditorData.imageData.source; } // prettier-ignore
    public static set Source(source: string)     { ImageEditorData.set(this.imageData.open, this.imageData.rootDoc, source, this.imageData.addDoc); } // prettier-ignore
    public static get RootDoc()                  { return ImageEditorData.imageData.rootDoc; } // prettier-ignore
    public static set RootDoc(rootDoc: Opt<Doc>) { ImageEditorData.set(this.imageData.open, rootDoc, this.imageData.source, this.imageData.addDoc); } // prettier-ignore
    public static get AddDoc()                   { return ImageEditorData.imageData.addDoc; } // prettier-ignore
    public static set AddDoc(addDoc: Opt<(doc: Doc | Doc[], annotationKey?: string) => boolean>) { ImageEditorData.set(this.imageData.open, this.imageData.rootDoc, this.imageData.source, addDoc); } // prettier-ignore
}

const API_URL = 'https://api.unsplash.com/search/photos';
@observer
export class ImageBox extends ViewBoxAnnotatableComponent<FieldViewProps>() {
    public static LayoutString(fieldKey: string) {
        return FieldView.LayoutString(ImageBox, fieldKey);
    }
    _ffref = React.createRef<CollectionFreeFormView>();
    private _ignoreScroll = false;
    private _forcedScroll = false;
    private _dropDisposer?: DragManager.DragDropDisposer;
    private _disposers: { [name: string]: IReactionDisposer } = {};
    private _getAnchor: (savedAnnotations: Opt<ObservableMap<number, HTMLDivElement[]>>, addAsAnnotation: boolean) => Opt<Doc> = () => undefined;
    private _overlayIconRef = React.createRef<HTMLDivElement>();
    private _mainCont: React.RefObject<HTMLDivElement> = React.createRef();
    private _annotationLayer: React.RefObject<HTMLDivElement> = React.createRef();
    imageRef: HTMLImageElement | null = null; // <video> ref
    marqueeref = React.createRef<MarqueeAnnotator>();
    @observable Loading = false; // bcz: this should be migrated into StylProviderQuiz since it's not fundamental to the imageBox

    @observable private _searchInput = '';
    @observable private _savedAnnotations = new ObservableMap<number, (HTMLDivElement & { marqueeing?: boolean })[]>();
    @observable private _curSuffix = '';
    @observable private _error = '';
    @observable private _isHovering = false; // flag to switch between primary and alternate images on hover

    constructor(props: FieldViewProps) {
        super(props);
        makeObservable(this);
        this._props.setContentViewBox?.(this);
    }

    protected createDropTarget = (ele: HTMLDivElement) => {
        this._dropDisposer?.();
        ele && (this._dropDisposer = DragManager.MakeDropTarget(ele, this.drop.bind(this), this.Document));
    };
    getAnchor = (addAsAnnotation: boolean, pinProps?: PinProps) => {
        const visibleAnchor = this._getAnchor?.(this._savedAnnotations, true); // use marquee anchor, otherwise, save zoom/pan as anchor
        const anchor =
            visibleAnchor ??
            Docs.Create.ConfigDocument({
                title: 'ImgAnchor:' + this.Document.title,
                config_panX: NumCast(this.layoutDoc._freeform_panX),
                config_panY: NumCast(this.layoutDoc._freeform_panY),
                config_viewScale: Cast(this.layoutDoc._freeform_scale, 'number', null),
                annotationOn: this.Document,
            });
        if (anchor) {
            if (!addAsAnnotation) anchor.backgroundColor = 'transparent';
            addAsAnnotation && this.addDocument(anchor);
            PinDocView(anchor, { pinDocLayout: pinProps?.pinDocLayout, pinData: { ...(pinProps?.pinData ?? {}), pannable: !visibleAnchor } }, this.Document);
            return anchor;
        }
        return this.Document;
    };

    componentDidMount() {
        this._disposers.sizer = reaction(
            () => ({
                forceFull: this._props.renderDepth < 1 || this.layoutDoc._showFullRes,
                scrSize: (this.ScreenToLocalBoxXf().inverse().transformDirection(this.nativeSize.nativeWidth, this.nativeSize.nativeHeight)[0] / this.nativeSize.nativeWidth) * NumCast(this.layoutDoc._freeform_scale, 1),
                selected: this._props.isSelected(),
            }),
            ({ forceFull, scrSize, selected }) => {
                this._curSuffix = selected ? '_o' : this.fieldKey === 'icon' ? '_m' : forceFull ? '_o' : scrSize < 0.25 ? '_s' : scrSize < 0.5 ? '_m' : scrSize < 0.8 ? '_l' : '_o';
            },
            { fireImmediately: true, delay: 1000 }
        );
        const { layoutDoc } = this;
        this._disposers.path = reaction(
            () => ({ nativeSize: this.nativeSize, width: NumCast(this.layoutDoc._width) }),
            ({ nativeSize, width }) => {
                if (layoutDoc === this.layoutDoc || !this.layoutDoc._height) {
                    this.layoutDoc._height = (width * nativeSize.nativeHeight) / nativeSize.nativeWidth;
                }
            },
            { fireImmediately: true }
        );
        this._disposers.scroll = reaction(
            () => this.layoutDoc.layout_scrollTop,
            sTop => {
                this._forcedScroll = true;
                !this._ignoreScroll && this._mainCont.current && (this._mainCont.current.scrollTop = NumCast(sTop));
                this._mainCont.current?.scrollTo({ top: NumCast(sTop) });
                this._forcedScroll = false;
            },
            { fireImmediately: true }
        );
    }

    componentWillUnmount() {
        Object.values(this._disposers).forEach(disposer => disposer?.());
    }

    /**
     * Find images from the unsplash api to add to flashcards.
     */
    fetchImages = async () => {
        try {
            const { data } = await axios.get(`${API_URL}?query=${this._searchInput}&page=1&per_page=${1}&client_id=${process.env.VITE_API_KEY}`);
            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-',
            });
            this._props.addDocument?.(imageSnapshot);
        } catch (error) {
            console.log(error);
        }
    };

    handleSelection = async (selection: string) => {
        this._searchInput = selection;
    };

    drop = undoable((e: Event, de: DragManager.DropEvent) => {
        if (de.complete.docDragData) {
            let added: boolean | undefined;
            const targetIsBullseye = (ele: HTMLElement): boolean => {
                if (!ele) return false;
                if (ele === this._overlayIconRef.current) return true;
                return targetIsBullseye(ele.parentElement as HTMLElement);
            };
            if (de.metaKey || targetIsBullseye(e.target as HTMLElement)) {
                added = de.complete.docDragData.droppedDocuments.reduce((last: boolean, drop: Doc) => {
                    this.layoutDoc[this.fieldKey + '_usePath'] = 'alternate:hover';
                    return last && Doc.AddDocToList(this.dataDoc, this.fieldKey + '_alternates', drop);
                }, true);
            } else if (de.altKey || !this.dataDoc[this.fieldKey]) {
                const layoutDoc = de.complete.docDragData?.draggedDocuments[0];
                const targetField = Doc.LayoutFieldKey(layoutDoc);
                const targetDoc = layoutDoc[DocData];
                if (targetDoc[targetField] instanceof ImageField) {
                    added = true;
                    this.dataDoc[this.fieldKey] = ObjectField.MakeCopy(targetDoc[targetField] as ImageField);
                    Doc.SetNativeWidth(this.dataDoc, Doc.NativeWidth(targetDoc), this.fieldKey);
                    Doc.SetNativeHeight(this.dataDoc, Doc.NativeHeight(targetDoc), this.fieldKey);
                }
            }
            added === false && e.preventDefault();
            added !== undefined && e.stopPropagation();
            return added;
        }
        return false;
    }, 'image drop');

    @undoBatch
    resolution = () => {
        this.layoutDoc._showFullRes = !this.layoutDoc._showFullRes;
    };

    @undoBatch
    setNativeSize = action(() => {
        const oldnativeWidth = NumCast(this.dataDoc[this.fieldKey + '_nativeWidth']);
        const nscale = NumCast(this._props.PanelWidth()) * NumCast(this.layoutDoc._freeform_scale, 1);
        const nw = nscale / oldnativeWidth;
        this.dataDoc[this.fieldKey + '_nativeHeight'] = NumCast(this.dataDoc[this.fieldKey + '_nativeHeight']) * nw;
        this.dataDoc[this.fieldKey + '_nativeWidth'] = NumCast(this.dataDoc[this.fieldKey + '_nativeWidth']) * nw;
        this.dataDoc._freeform_panX = nw * NumCast(this.dataDoc._freeform_panX);
        this.dataDoc._freeform_panY = nw * NumCast(this.dataDoc._freeform_panY);
        this.dataDoc._freeform_panX_max = this.dataDoc._freeform_panX_max ? nw * NumCast(this.dataDoc._freeform_panX_max) : undefined;
        this.dataDoc._freeform_panX_min = this.dataDoc._freeform_panX_min ? nw * NumCast(this.dataDoc._freeform_panX_min) : undefined;
        this.dataDoc._freeform_panY_max = this.dataDoc._freeform_panY_max ? nw * NumCast(this.dataDoc._freeform_panY_max) : undefined;
        this.dataDoc._freeform_panY_min = this.dataDoc._freeform_panY_min ? nw * NumCast(this.dataDoc._freeform_panY_min) : undefined;
        const newnativeWidth = NumCast(this.dataDoc[this.fieldKey + '_nativeWidth']);
        DocListCast(this.dataDoc[this.annotationKey]).forEach(doc => {
            doc.x = (NumCast(doc.x) / oldnativeWidth) * newnativeWidth;
            doc.y = (NumCast(doc.y) / oldnativeWidth) * newnativeWidth;
            if (!RTFCast(doc[Doc.LayoutFieldKey(doc)])) {
                doc.width = (NumCast(doc.width) / oldnativeWidth) * newnativeWidth;
                doc.height = (NumCast(doc.height) / oldnativeWidth) * newnativeWidth;
            }
        });
    });
    @undoBatch
    rotate = action(() => {
        const nw = NumCast(this.dataDoc[this.fieldKey + '_nativeWidth']);
        const nh = NumCast(this.dataDoc[this.fieldKey + '_nativeHeight']);
        const w = this.layoutDoc._width;
        const h = this.layoutDoc._height;
        this.dataDoc[this.fieldKey + '_rotation'] = (NumCast(this.dataDoc[this.fieldKey + '_rotation']) + 90) % 360;
        this.dataDoc[this.fieldKey + '_nativeWidth'] = nh;
        this.dataDoc[this.fieldKey + '_nativeHeight'] = nw;
        this.layoutDoc._width = h;
        this.layoutDoc._height = w;
    });

    crop = (region: Doc | undefined, addCrop?: boolean) => {
        if (!region) return undefined;
        const cropping = Doc.MakeCopy(region, true);
        const regionData = region[DocData];
        regionData.lockedPosition = true;
        regionData.title = 'region:' + this.Document.title;
        regionData.followLinkToggle = true;
        this.addDocument(region);
        const anchx = NumCast(cropping.x);
        const anchy = NumCast(cropping.y);
        const anchw = NumCast(cropping._width);
        const anchh = NumCast(cropping._height);
        const viewScale = NumCast(this.dataDoc[this.fieldKey + '_nativeHeight']) / anchh;
        cropping.title = 'crop: ' + this.Document.title;
        cropping.x = NumCast(this.Document.x) + NumCast(this.layoutDoc._width);
        cropping.y = NumCast(this.Document.y);
        cropping._width = anchw * (this._props.NativeDimScaling?.() || 1);
        cropping._height = anchh * (this._props.NativeDimScaling?.() || 1);
        cropping.onClick = undefined;
        const croppingProto = cropping[DocData];
        croppingProto.annotationOn = undefined;
        croppingProto.isDataDoc = true;
        croppingProto.backgroundColor = undefined;
        croppingProto.proto = Cast(this.Document.proto, Doc, null)?.proto; // set proto of cropping's data doc to be IMAGE_PROTO
        croppingProto.type = DocumentType.IMG;
        croppingProto.layout = ImageBox.LayoutString('data');
        croppingProto.data = ObjectField.MakeCopy(this.dataDoc[this.fieldKey] as ObjectField);
        croppingProto.data_nativeWidth = anchw;
        croppingProto.data_nativeHeight = anchh;
        croppingProto.freeform_scale = viewScale;
        croppingProto.freeform_panX = anchx / viewScale;
        croppingProto.freeform_panY = anchy / viewScale;
        croppingProto.freeform_scale_min = viewScale;
        croppingProto.freeform_panX_min = anchx / viewScale;
        croppingProto.freeform_panX_max = anchw / viewScale;
        croppingProto.freeform_panY_min = anchy / viewScale;
        croppingProto.freeform_panY_max = anchh / viewScale;
        if (addCrop) {
            DocUtils.MakeLink(region, cropping, { link_relationship: 'cropped image' });
            cropping.x = NumCast(this.Document.x) + NumCast(this.layoutDoc._width);
            cropping.y = NumCast(this.Document.y);
            this._props.addDocTab(cropping, OpenWhere.inParent);
        }
        DocumentView.addViewRenderedCb(cropping, dv => setTimeout(() => (dv.ComponentView as ImageBox).setNativeSize(), 200));
        this._props.bringToFront?.(cropping);
        return cropping;
    };

    specificContextMenu = (): void => {
        const field = Cast(this.dataDoc[this.fieldKey], ImageField);
        if (field) {
            const funcs: ContextMenuProps[] = [];
            funcs.push({ description: 'Rotate Clockwise 90', event: this.rotate, icon: 'redo-alt' });
            funcs.push({ description: `Show ${this.layoutDoc._showFullRes ? 'Dynamic Res' : 'Full Res'}`, event: this.resolution, icon: 'expand' });
            funcs.push({ description: 'Set Native Pixel Size', event: this.setNativeSize, icon: 'expand-arrows-alt' });
            funcs.push({
                description: 'GetImageText',
                event: () => {
                    Networking.PostToServer('/queryFireflyImageText', {
                        file: (file => {
                            const ext = extname(file);
                            return file.replace(ext, (this._error ? '_o' : this._curSuffix) + ext);
                        })(ImageCast(this.Document[Doc.LayoutFieldKey(this.Document)])?.url.href),
                    }).then(text => alert(text));
                },
                icon: 'expand-arrows-alt',
            });
            funcs.push({
                description: 'Expand Image',
                event: () => {
                    Networking.PostToServer('/expandImage', {
                        prompt: 'sunny skies',
                        file: (file => {
                            const ext = extname(file);
                            return file.replace(ext, (this._error ? '_o' : this._curSuffix) + ext);
                        })(ImageCast(this.Document[Doc.LayoutFieldKey(this.Document)])?.url.href),
                    }).then((info: Upload.ImageInformation) => {
                        const img = Docs.Create.ImageDocument(info.accessPaths.agnostic.client, { title: 'expand:' + this.Document.title });
                        DocUtils.assignImageInfo(info, img);
                        this._props.addDocTab(img, OpenWhere.addRight);
                    });
                },
                icon: 'expand-arrows-alt',
            });
            funcs.push({ description: 'Copy path', event: () => ClientUtils.CopyText(this.choosePath(field.url)), icon: 'copy' });
            funcs.push({
                description: 'Open Image Editor',
                event: action(() => {
                    ImageEditorData.Open = true;
                    ImageEditorData.Source = this.choosePath(field.url);
                    ImageEditorData.AddDoc = this._props.addDocument;
                    ImageEditorData.RootDoc = this.Document;
                }),
                icon: 'pencil-alt',
            });
            this.layoutDoc.ai &&
                funcs.push({
                    description: 'Regenerate AI Image',
                    event: action(e => {
                        !SmartDrawHandler.Instance.ShowRegenerate ? SmartDrawHandler.Instance.displayRegenerate(e?.x || 0, e?.y || 0) : SmartDrawHandler.Instance.hideRegenerate();
                    }),
                    icon: 'pen-to-square',
                });
            funcs.push({
                description: this.Document.savedAsSticker ? 'Sticker Saved!' : 'Save to Stickers',
                event: action(undoable(async () => await StickerPalette.addToPalette(this.Document), 'save to palette')),
                icon: this.Document.savedAsSticker ? 'clipboard-check' : 'file-arrow-down',
            });
            ContextMenu.Instance?.addItem({ description: 'Options...', subitems: funcs, icon: 'asterisk' });
        }
    };

    // updateIcon = () => new Promise<void>(res => res());
    updateIcon = (usePanelDimensions?: boolean) => {
        const contentDiv = this._mainCont.current;
        return !contentDiv
            ? new Promise<void>(res => res())
            : UpdateIcon(
                  this.layoutDoc[Id] + '_icon_' + new Date().getTime(),
                  contentDiv,
                  usePanelDimensions ? this._props.PanelWidth() : NumCast(this.layoutDoc._width),
                  usePanelDimensions ? this._props.PanelHeight() : NumCast(this.layoutDoc._height),
                  this._props.PanelWidth(),
                  this._props.PanelHeight(),
                  0,
                  1,
                  false,
                  '',
                  (iconFile, nativeWidth, nativeHeight) => {
                      this.dataDoc.icon = new ImageField(iconFile);
                      this.dataDoc.icon_nativeWidth = nativeWidth;
                      this.dataDoc.icon_nativeHeight = nativeHeight;
                  }
              );
    };

    choosePath = (url: URL) => {
        if (!url?.href) return '';
        const lower = url.href.toLowerCase();
        if (url.protocol === 'data') return url.href;
        if (url.href.indexOf(window.location.origin) === -1 && url.href.indexOf('dashblobstore') === -1) return ClientUtils.CorsProxy(url.href);
        if (!/\.(png|jpg|jpeg|gif|webp)$/.test(lower) || lower.endsWith('/assets/unknown-file-icon-hi.png')) return `/assets/unknown-file-icon-hi.png`;

        const ext = extname(url.href);
        return url.href.replace(ext, (this._error ? '_o' : this._curSuffix) + ext);
    };
    getScrollHeight = () => (this._props.fitWidth?.(this.Document) !== false && NumCast(this.layoutDoc._freeform_scale, 1) === NumCast(this.dataDoc._freeform_scaleMin, 1) ? this.nativeSize.nativeHeight : undefined);

    @computed get nativeSize() {
        TraceMobx();
        if (this.paths.length && this.paths[0].includes('icon-hi')) return { nativeWidth: NumCast(this.layoutDoc._width), nativeHeight: NumCast(this.layoutDoc._height), nativeOrientation: 0 };
        const nativeWidth = NumCast(this.dataDoc[this.fieldKey + '_nativeWidth'], NumCast(this.layoutDoc[this.fieldKey + '_nativeWidth'], 500));
        const nativeHeight = NumCast(this.dataDoc[this.fieldKey + '_nativeHeight'], NumCast(this.layoutDoc[this.fieldKey + '_nativeHeight'], 500));
        const nativeOrientation = NumCast(this.dataDoc[this.fieldKey + '_nativeOrientation'], 1);
        return { nativeWidth, nativeHeight, nativeOrientation };
    }
    @computed get overlayImageIcon() {
        const usePath = this.layoutDoc[`_${this.fieldKey}_usePath`];
        return (
            <Tooltip
                title={
                    <div className="dash-tooltip">
                        toggle between
                        <span style={{ color: usePath === undefined ? 'black' : undefined }}>
                            <em> primary, </em>
                        </span>
                        <span style={{ color: usePath === 'alternate' ? 'black' : undefined }}>
                            <em>alternate, </em>
                        </span>
                        and show
                        <span style={{ color: usePath === 'alternate:hover' ? 'black' : undefined }}>
                            <em> alternate on hover</em>
                        </span>
                    </div>
                }>
                <div
                    className="imageBox-alternateDropTarget"
                    ref={this._overlayIconRef}
                    onPointerDown={e =>
                        setupMoveUpEvents(e.target, e, returnFalse, emptyFunction, () => {
                            this.layoutDoc[`_${this.fieldKey}_usePath`] = usePath === undefined ? 'alternate' : usePath === 'alternate' ? 'alternate:hover' : undefined;
                        })
                    }
                    style={{
                        display: (this._props.isContentActive() !== false && SnappingManager.CanEmbed) || this.dataDoc[this.fieldKey + '_alternates'] ? 'block' : 'none',
                        width: 'min(10%, 25px)',
                        height: 'min(10%, 25px)',
                        background: usePath === undefined ? 'white' : usePath === 'alternate' ? 'black' : 'gray',
                        color: usePath === undefined ? 'black' : 'white',
                    }}>
                    <FontAwesomeIcon icon="circle-half-stroke" size="lg" />
                </div>
            </Tooltip>
        );
    }

    @computed get paths() {
        const field = this.dataDoc[this.fieldKey] instanceof ImageField ? Cast(this.dataDoc[this.fieldKey], ImageField, null) : new ImageField(String(this.dataDoc[this.fieldKey])); // retrieve the primary image URL that is being rendered from the data doc
        const alts = DocListCast(this.dataDoc[this.fieldKey + '_alternates']); // retrieve alternate documents that may be rendered as alternate images
        const defaultUrl = new URL(ClientUtils.prepend('/assets/unknown-file-icon-hi.png'));
        const altpaths =
            alts
                ?.map(doc => (doc instanceof Doc ? (ImageCast(doc[Doc.LayoutFieldKey(doc)])?.url ?? defaultUrl) : defaultUrl))
                .filter(url => url)
                .map(url => this.choosePath(url)) ?? []; // acc  ess the primary layout data of the alternate documents
        const paths = field ? [this.choosePath(field.url), ...altpaths] : altpaths;
        return paths.length ? paths : [defaultUrl.href];
    }

    @computed get content() {
        TraceMobx();

        const backColor = DashColor((this._props.styleProvider?.(this.layoutDoc, this._props, StyleProp.BackgroundColor) as string) ?? Colors.WHITE);
        // allow use case where the image is transparent when the alpha value is to smallest possible value from UI (alpha = 1 out of 255)
        const backAlpha = backColor.alpha() < 0.015 && backColor.alpha() > 0 ? backColor.alpha() : 1;
        const srcpath = this.layoutDoc.hideImage ? '' : this.paths[0];
        const fadepath = this.layoutDoc.hideImage ? '' : this.paths.lastElement();
        const { nativeWidth, nativeHeight /* , nativeOrientation */ } = this.nativeSize;
        const rotation = NumCast(this.dataDoc[this.fieldKey + '_rotation']);
        const aspect = rotation % 180 ? nativeHeight / nativeWidth : 1;
        let transformOrigin = 'center center';
        let transform = `translate(0%, 0%) rotate(${rotation}deg) scale(${aspect})`;
        if (rotation === 90 || rotation === -270) {
            transformOrigin = 'top left';
            transform = `translate(100%, 0%) rotate(${rotation}deg) scale(${aspect})`;
        } else if (rotation === 180) {
            transform = `rotate(${rotation}deg) scale(${aspect})`;
        } else if (rotation === 270 || rotation === -90) {
            transformOrigin = 'right top';
            transform = `translate(-100%, 0%) rotate(${rotation}deg) scale(${aspect})`;
        }
        const usePath = this.layoutDoc[`_${this.fieldKey}_usePath`];

        return (
            <div
                className="imageBox-cont"
                onPointerEnter={action(() => {
                    this._isHovering = true;
                })}
                onPointerLeave={action(() => {
                    this._isHovering = false;
                })}
                key={this.layoutDoc[Id]}
                ref={this.createDropTarget}
                onPointerDown={this.marqueeDown}>
                <div className="imageBox-fader" style={{ opacity: backAlpha }}>
                    <img
                        alt=""
                        ref={action((r: HTMLImageElement | null) => (this.imageRef = r))}
                        key="paths"
                        src={srcpath}
                        style={{ transform, transformOrigin, objectFit: 'fill', height: '100%' }}
                        onError={action(e => {
                            this._error = e.toString();
                        })}
                        draggable={false}
                        width={nativeWidth}
                    />
                    {fadepath === srcpath ? null : (
                        <div className={`imageBox-fadeBlocker${(this._isHovering && usePath === 'alternate:hover') || usePath === 'alternate' ? '-hover' : ''}`} style={{ transition: StrCast(this.layoutDoc.viewTransition, 'opacity 1000ms') }}>
                            <img alt="" className="imageBox-fadeaway" key="fadeaway" src={fadepath} style={{ transform, transformOrigin }} draggable={false} width={nativeWidth} />
                        </div>
                    )}
                </div>
                {this.overlayImageIcon}
            </div>
        );
    }

    @observable private _regenInput = '';
    @observable private _canInteract = true;
    @observable private _regenerateLoading = false;
    @observable private _prevImgs: FireflyImageData[] = [];

    componentAIViewHistory = () => {
        return (
            <div className="imageBox-aiView-history">
                {this._prevImgs.map(img => (
                    <img
                        key={img.pathname}
                        className="imageBox-aiView-img"
                        src={img.href}
                        onClick={() => {
                            this.dataDoc[this.fieldKey] = new ImageField(img.pathname);
                            this.dataDoc.ai_firefly_prompt = img.prompt;
                            this.dataDoc.ai_firefly_seed = img.seed;
                        }}
                    />
                ))}
            </div>
        );
    };

    componentAIView = () => {
        const field = this.dataDoc[this.fieldKey] instanceof ImageField ? Cast(this.dataDoc[this.fieldKey], ImageField, null) : new ImageField(String(this.dataDoc[this.fieldKey]));
        const showRegenerate = this.Document[DocData].ai;
        return (
            <div className="imageBox-aiView">
                Edit Image with AI
                {showRegenerate && (
                    <div className="imageBox-aiView-regenerate-container">
                        <text className="imageBox-aiView-subtitle">Regenerate AI Image</text>
                        <div className="imageBox-aiView-regenerate">
                            <input
                                className="imageBox-aiView-input"
                                aria-label="Edit instructions input"
                                // className="smartdraw-input"
                                type="text"
                                value={this._regenInput}
                                onChange={action(e => this._canInteract && (this._regenInput = e.target.value))}
                                // onKeyDown={this.handleKeyPress}
                                placeholder="Prompt (Optional)"
                            />
                            <Button
                                text="Regenerate"
                                type={Type.SEC}
                                // style={{ alignSelf: 'flex-end' }}
                                icon={this._regenerateLoading ? <ReactLoading type="spin" color={SettingsManager.userVariantColor} width={16} height={20} /> : <AiOutlineSend />}
                                iconPlacement="right"
                                onClick={undoable(
                                    action(async () => {
                                        this._regenerateLoading = true;
                                        await SmartDrawHandler.Instance.regenerate([this.Document], undefined, undefined, this._regenInput, true).then(newImgs => {
                                            if (newImgs[0]) {
                                                const url = newImgs[0].pathname;
                                                const imgField = new ImageField(url);
                                                this._prevImgs.length === 0 &&
                                                    this._prevImgs.push({ prompt: StrCast(this.dataDoc.ai_firefly_prompt), seed: NumCast(this.dataDoc.ai_firefly_seed), href: this.paths.lastElement(), pathname: field.url.pathname });
                                                this.dataDoc[this.fieldKey] = imgField;
                                                this._prevImgs.unshift({ prompt: newImgs[0].prompt, seed: newImgs[0].seed, href: this.paths.lastElement(), pathname: url });
                                                this._regenerateLoading = false;
                                                this._regenInput = '';
                                            }
                                        });
                                    }),
                                    'regenerate image'
                                )}
                            />
                            <Button
                                // style={{ alignSelf: 'flex-end' }}
                                text="Get Variations"
                                type={Type.SEC}
                                // icon={this._isLoading && this._regenInput !== '' ? <ReactLoading type="spin" color={SettingsManager.userVariantColor} width={16} height={20} /> : <AiOutlineSend />}
                                iconPlacement="right"
                                // onClick={this.handleSendClick}
                            />
                        </div>
                    </div>
                )}
                <div className="imageBox-aiView-options-container">
                    {showRegenerate && <text className="imageBox-aiView-subtitle"> More Image Options </text>}
                    <div className="imageBox-aiView-options">
                        <Button
                            type={Type.TERT}
                            text="Get Text"
                            icon={<FontAwesomeIcon icon="font" />}
                            color={SettingsManager.userBackgroundColor}
                            iconPlacement="right"
                            onClick={() => {
                                Networking.PostToServer('/queryFireflyImageText', {
                                    file: (file => {
                                        const ext = extname(file);
                                        return file.replace(ext, (this._error ? '_o' : this._curSuffix) + ext);
                                    })(ImageCast(this.Document[Doc.LayoutFieldKey(this.Document)])?.url.href),
                                }).then(text => alert(text));
                            }}
                        />
                        <Button
                            type={Type.TERT}
                            text="Generative Fill"
                            icon={<FontAwesomeIcon icon="fill" />}
                            color={SettingsManager.userBackgroundColor}
                            // icon={this._isLoading && this._regenInput !== '' ? <ReactLoading type="spin" color={SettingsManager.userVariantColor} width={16} height={20} /> : <AiOutlineSend />}
                            iconPlacement="right"
                            onClick={action(() => {
                                ImageEditorData.Open = true;
                                ImageEditorData.Source = (field && this.choosePath(field.url)) || '';
                                ImageEditorData.AddDoc = this._props.addDocument;
                                ImageEditorData.RootDoc = this.Document;
                            })}
                        />
                        <Button
                            type={Type.TERT}
                            text="Expand"
                            icon={<FontAwesomeIcon icon="expand" />}
                            color={SettingsManager.userBackgroundColor}
                            // icon={this._isLoading && this._regenInput !== '' ? <ReactLoading type="spin" color={SettingsManager.userVariantColor} width={16} height={20} /> : <AiOutlineSend />}
                            iconPlacement="right"
                            onClick={() => {
                                Networking.PostToServer('/expandImage', {
                                    prompt: 'sunny skies',
                                    file: (file => {
                                        const ext = extname(file);
                                        return file.replace(ext, (this._error ? '_o' : this._curSuffix) + ext);
                                    })(ImageCast(this.Document[Doc.LayoutFieldKey(this.Document)])?.url.href),
                                }).then((info: Upload.ImageInformation) => {
                                    const img = Docs.Create.ImageDocument(info.accessPaths.agnostic.client, { title: 'expand:' + this.Document.title });
                                    DocUtils.assignImageInfo(info, img);
                                    this._props.addDocTab(img, OpenWhere.addRight);
                                });
                            }}
                        />
                    </div>
                </div>
            </div>
        );
    };

    @computed get annotationLayer() {
        TraceMobx();
        return <div className="imageBox-annotationLayer" style={{ height: this._props.PanelHeight() }} ref={this._annotationLayer} />;
    }
    screenToLocalTransform = () => this.ScreenToLocalBoxXf().translate(0, NumCast(this.layoutDoc._layout_scrollTop) * this.ScreenToLocalBoxXf().Scale);
    marqueeDown = (e: React.PointerEvent) => {
        if (!this.dataDoc[this.fieldKey]) {
            this.chooseImage();
        } else if (!e.altKey && e.button === 0 && NumCast(this.layoutDoc._freeform_scale, 1) <= NumCast(this.dataDoc.freeform_scaleMin, 1) && this._props.isContentActive() && Doc.ActiveTool !== InkTool.Ink) {
            setupMoveUpEvents(
                this,
                e,
                action(moveEv => {
                    MarqueeAnnotator.clearAnnotations(this._savedAnnotations);
                    this.marqueeref.current?.onInitiateSelection([moveEv.clientX, moveEv.clientY]);
                    return true;
                }),
                returnFalse,
                () => MarqueeAnnotator.clearAnnotations(this._savedAnnotations),
                false
            );
        }
    };
    @action
    finishMarquee = () => {
        this._getAnchor = AnchorMenu.Instance?.GetAnchor;
        this._props.styleProvider?.(this.Document, this._props, StyleProp.AnchorMenuItems);
        AnchorMenu.Instance.addToCollection = this._props.DocumentView?.()._props.addDocument;
        AnchorMenu.Instance.marqueeWidth = this.marqueeref.current?.Width ?? 0;
        AnchorMenu.Instance.marqueeHeight = this.marqueeref.current?.Height ?? 0;
        this.marqueeref.current?.onTerminateSelection();
        this._props.select(false);
    };
    focus = (anchor: Doc, options: FocusViewOptions) => (anchor.type === DocumentType.CONFIG ? undefined : this._ffref.current?.focus(anchor, options));

    renderedPixelDimensions = async () => {
        const { nativeWidth: width, nativeHeight: height } = await Networking.PostToServer('/inspectImage', { source: this.paths[0] });
        return { width, height };
    };

    savedAnnotations = () => this._savedAnnotations;
    render() {
        TraceMobx();
        const borderRad = this._props.styleProvider?.(this.layoutDoc, this._props, StyleProp.BorderRounding) as string;
        const borderRadius = borderRad?.includes('px') ? `${Number(borderRad.split('px')[0]) / (this._props.NativeDimScaling?.() || 1)}px` : borderRad;
        return (
            <div
                className="imageBox"
                onContextMenu={this.specificContextMenu}
                ref={this._mainCont}
                onScroll={action(() => {
                    if (!this._forcedScroll) {
                        if (this.layoutDoc._layout_scrollTop || this._mainCont.current?.scrollTop) {
                            this._ignoreScroll = true;
                            this.layoutDoc._layout_scrollTop = this._mainCont.current?.scrollTop;
                            this._ignoreScroll = false;
                        }
                    }
                })}
                style={{
                    width: this._props.PanelWidth() ? undefined : `100%`,
                    height: this._props.PanelHeight() ? undefined : `100%`,
                    pointerEvents: this.layoutDoc._lockedPosition ? 'none' : undefined,
                    borderRadius,
                    overflow: this.layoutDoc.layout_fitWidth || this._props.fitWidth?.(this.Document) ? 'auto' : 'hidden',
                }}>
                <CollectionFreeFormView
                    ref={this._ffref}
                    {...this._props}
                    setContentViewBox={emptyFunction}
                    NativeWidth={returnZero}
                    NativeHeight={returnZero}
                    renderDepth={this._props.renderDepth + 1}
                    fieldKey={this.annotationKey}
                    styleProvider={this._props.styleProvider}
                    isAnnotationOverlay
                    annotationLayerHostsContent
                    PanelWidth={this._props.PanelWidth}
                    PanelHeight={this._props.PanelHeight}
                    ScreenToLocalTransform={this.screenToLocalTransform}
                    select={emptyFunction}
                    focus={this.focus}
                    getScrollHeight={this.getScrollHeight}
                    NativeDimScaling={returnOne}
                    isAnyChildContentActive={returnFalse}
                    whenChildContentsActiveChanged={this.whenChildContentsActiveChanged}
                    removeDocument={this.removeDocument}
                    moveDocument={this.moveDocument}
                    addDocument={this.addDocument}>
                    {this.content}
                </CollectionFreeFormView>
                {this.Loading ? (
                    <div className="loading-spinner" style={{ position: 'absolute' }}>
                        <ReactLoading type="spin" height={50} width={50} color={'blue'} />
                    </div>
                ) : null}
                {this.annotationLayer}
                {!this._mainCont.current || !this.DocumentView || !this._annotationLayer.current ? null : (
                    <MarqueeAnnotator
                        Document={this.Document}
                        ref={this.marqueeref}
                        scrollTop={0}
                        annotationLayerScrollTop={0}
                        scaling={returnOne}
                        annotationLayerScaling={this._props.NativeDimScaling}
                        screenTransform={this.DocumentView().screenToViewTransform}
                        docView={this.DocumentView}
                        addDocument={this.addDocument}
                        finishMarquee={this.finishMarquee}
                        savedAnnotations={this.savedAnnotations}
                        selectionText={returnEmptyString}
                        annotationLayer={this._annotationLayer.current}
                        marqueeContainer={this._mainCont.current}
                        highlightDragSrcColor=""
                        anchorMenuCrop={this.crop}
                        // anchorMenuFlashcard={() => this.getImageDesc()}
                    />
                )}
            </div>
        );
    }

    public chooseImage = () => {
        const input = document.createElement('input');
        input.type = 'file';
        input.multiple = true;
        input.accept = 'image/*';
        input.onchange = async () => {
            const file = input.files?.[0];
            if (file) {
                const disposer = OverlayView.ShowSpinner();
                const [{ result }] = await Networking.UploadFilesToServer({ file });
                if (result instanceof Error) {
                    alert('Error uploading files - possibly due to unsupported file types');
                } else {
                    this.dataDoc[this.fieldKey] = new ImageField(result.accessPaths.agnostic.client);
                    !(result instanceof Error) && DocUtils.assignImageInfo(result, this.dataDoc);
                }
                disposer();
            } else {
                console.log('No file selected');
            }
        };
        input.click();
    };
}

Docs.Prototypes.TemplateMap.set(DocumentType.IMG, {
    layout: { view: ImageBox, dataField: 'data' },
    options: { acl: '', freeform: '', systemIcon: 'BsFileEarmarkImageFill' },
});