From 9419693f37ad53fcc0763a118d2cb865659a2ff4 Mon Sep 17 00:00:00 2001
From: bob <bcz@cs.brown.edu>
Date: Tue, 6 Aug 2019 14:08:59 -0400
Subject: multiple changes to PDF related code -- mostly clean up

---
 src/client/views/pdf/Annotation.tsx | 69 +++++++++++++++----------------------
 1 file changed, 27 insertions(+), 42 deletions(-)

(limited to 'src/client/views/pdf/Annotation.tsx')

diff --git a/src/client/views/pdf/Annotation.tsx b/src/client/views/pdf/Annotation.tsx
index a08ff5969..9d68a86b8 100644
--- a/src/client/views/pdf/Annotation.tsx
+++ b/src/client/views/pdf/Annotation.tsx
@@ -4,17 +4,20 @@ import { observer } from "mobx-react";
 import { Doc, DocListCast, HeightSym, WidthSym } from "../../../new_fields/Doc";
 import { Id } from "../../../new_fields/FieldSymbols";
 import { List } from "../../../new_fields/List";
-import { BoolCast, Cast, FieldValue, NumCast, StrCast } from "../../../new_fields/Types";
+import { Cast, FieldValue, NumCast, StrCast } from "../../../new_fields/Types";
 import { DocumentManager } from "../../util/DocumentManager";
 import { PresentationView } from "../presentationview/PresentationView";
 import PDFMenu from "./PDFMenu";
 import "./Annotation.scss";
-import { AnnotationTypes, scale, Viewer } from "./PDFViewer";
+import { AnnotationTypes, scale } from "./PDFViewer";
 
 interface IAnnotationProps {
     anno: Doc;
     index: number;
-    parent: Viewer;
+    ParentIndex: () => number;
+    fieldExtensionDoc: Doc;
+    scrollTo?: (n: number) => void;
+    addDocTab: (document: Doc, dataDoc: Doc | undefined, where: string) => void;
 }
 
 export default class Annotation extends React.Component<IAnnotationProps> {
@@ -23,10 +26,8 @@ export default class Annotation extends React.Component<IAnnotationProps> {
         let res = annotationDocs.map(a => {
             let type = NumCast(a.type);
             switch (type) {
-                // case AnnotationTypes.Pin:
-                //     return <PinAnnotation parent={this} document={a} x={NumCast(a.x)} y={NumCast(a.y)} width={a[WidthSym]()} height={a[HeightSym]()} key={a[Id]} />;
                 case AnnotationTypes.Region:
-                    return <RegionAnnotation parent={this.props.parent} document={a} index={this.props.index} x={NumCast(a.x)} y={NumCast(a.y)} width={a[WidthSym]()} height={a[HeightSym]()} key={a[Id]} />;
+                    return <RegionAnnotation addDocTab={this.props.addDocTab} document={a} fieldExtensionDoc={this.props.fieldExtensionDoc} scrollTo={this.props.scrollTo} ParentIndex={this.props.ParentIndex} index={this.props.index} x={NumCast(a.x)} y={NumCast(a.y)} width={a[WidthSym]()} height={a[HeightSym]()} key={a[Id]} />;
                 default:
                     return <div></div>;
             }
@@ -41,7 +42,10 @@ interface IRegionAnnotationProps {
     width: number;
     height: number;
     index: number;
-    parent: Viewer;
+    ParentIndex: () => number;
+    fieldExtensionDoc: Doc;
+    scrollTo?: (n: number) => void;
+    addDocTab: (document: Doc, dataDoc: Doc | undefined, where: string) => void;
     document: Doc;
 }
 
@@ -51,34 +55,18 @@ class RegionAnnotation extends React.Component<IRegionAnnotationProps> {
 
     private _reactionDisposer?: IReactionDisposer;
     private _scrollDisposer?: IReactionDisposer;
-    private _mainCont: React.RefObject<HTMLDivElement>;
-
-    constructor(props: IRegionAnnotationProps) {
-        super(props);
-
-        this._mainCont = React.createRef();
-    }
+    private _mainCont: React.RefObject<HTMLDivElement> = React.createRef();
 
     componentDidMount() {
         this._reactionDisposer = reaction(
-            () => BoolCast(this.props.document.delete),
-            () => {
-                if (BoolCast(this.props.document.delete)) {
-                    if (this._mainCont.current) {
-                        this._mainCont.current.style.display = "none";
-                    }
-                }
-            },
+            () => this.props.document.delete,
+            () => this.props.document.delete && this._mainCont.current && (this._mainCont.current.style.display = "none"),
             { fireImmediately: true }
         );
 
         this._scrollDisposer = reaction(
-            () => this.props.parent.Index,
-            () => {
-                if (this.props.parent.Index === this.props.index) {
-                    this.props.parent.scrollTo(this.props.y * scale);
-                }
-            }
+            () => this.props.ParentIndex(),
+            () => this.props.ParentIndex() === this.props.index && this.props.scrollTo && this.props.scrollTo(this.props.y * scale)
         );
     }
 
@@ -88,16 +76,15 @@ class RegionAnnotation extends React.Component<IRegionAnnotationProps> {
     }
 
     deleteAnnotation = () => {
-        let annotation = DocListCast(this.props.parent.props.parent.fieldExtensionDoc.annotations);
+        let annotation = DocListCast(this.props.fieldExtensionDoc.annotations);
         let group = FieldValue(Cast(this.props.document.group, Doc));
-        if (group && annotation.indexOf(group) !== -1) {
-            let newAnnotations = annotation.filter(a => a !== FieldValue(Cast(this.props.document.group, Doc)));
-            this.props.parent.props.parent.fieldExtensionDoc.annotations = new List<Doc>(newAnnotations);
-        }
-
         if (group) {
-            let groupAnnotations = DocListCast(group.annotations);
-            groupAnnotations.forEach(anno => anno.delete = true);
+            if (annotation.indexOf(group) !== -1) {
+                let newAnnotations = annotation.filter(a => a !== FieldValue(Cast(this.props.document.group, Doc)));
+                this.props.fieldExtensionDoc.annotations = new List<Doc>(newAnnotations);
+            }
+
+            DocListCast(group.annotations).forEach(anno => anno.delete = true);
         }
 
         PDFMenu.Instance.fadeOut(true);
@@ -105,9 +92,7 @@ class RegionAnnotation extends React.Component<IRegionAnnotationProps> {
 
     pinToPres = () => {
         let group = FieldValue(Cast(this.props.document.group, Doc));
-        if (group) {
-            PresentationView.Instance.PinDoc(group);
-        }
+        group && PresentationView.Instance.PinDoc(group);
     }
 
     @action
@@ -118,7 +103,7 @@ class RegionAnnotation extends React.Component<IRegionAnnotationProps> {
                 let context = await Cast(targetDoc.targetContext, Doc);
                 if (context) {
                     DocumentManager.Instance.jumpToDocument(targetDoc, false, false,
-                        ((doc) => this.props.parent.props.parent.props.addDocTab(targetDoc!, undefined, e.ctrlKey ? "onRight" : "inTab")),
+                        ((doc) => this.props.addDocTab(targetDoc!, undefined, e.ctrlKey ? "onRight" : "inTab")),
                         undefined, undefined);
                 }
             }
@@ -151,8 +136,8 @@ class RegionAnnotation extends React.Component<IRegionAnnotationProps> {
                     left: this.props.x * scale,
                     width: this.props.width * scale,
                     height: this.props.height * scale,
-                    backgroundColor: this.props.parent.Index === this.props.index ? "green" : StrCast(this.props.document.color)
-                }}></div>
+                    backgroundColor: this.props.ParentIndex() === this.props.index ? "green" : StrCast(this.props.document.color)
+                }} />
         );
     }
 }
\ No newline at end of file
-- 
cgit v1.2.3-70-g09d2

