aboutsummaryrefslogtreecommitdiff
path: root/src/client/util/DocumentManager.ts
blob: a3c7429b9e0b65b8501d28fb247580190b55f20e (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
import { action, computed, observable } from 'mobx';
import { Doc, DocListCastAsync } from '../../new_fields/Doc';
import { Id } from '../../new_fields/FieldSymbols';
import { Cast, NumCast } from '../../new_fields/Types';
import { CollectionDockingView } from '../views/collections/CollectionDockingView';
import { CollectionPDFView } from '../views/collections/CollectionPDFView';
import { CollectionVideoView } from '../views/collections/CollectionVideoView';
import { CollectionView } from '../views/collections/CollectionView';
import { DocumentView } from '../views/nodes/DocumentView';
import { LinkManager } from './LinkManager';
import { undoBatch, UndoManager } from './UndoManager';
import { Scripting } from './Scripting';
import { List } from '../../new_fields/List';


export class DocumentManager {

    //global holds all of the nodes (regardless of which collection they're in)
    @observable
    public DocumentViews: DocumentView[] = [];

    // singleton instance
    private static _instance: DocumentManager;

    // create one and only one instance of NodeManager
    public static get Instance(): DocumentManager {
        return this._instance || (this._instance = new this());
    }

    //private constructor so no other class can create a nodemanager
    private constructor() {
        // this.DocumentViews = new Array<DocumentView>();
    }

    //gets all views
    public getDocumentViewsById(id: string) {
        let toReturn: DocumentView[] = [];
        DocumentManager.Instance.DocumentViews.map(view => {
            if (view.props.Document[Id] === id) {
                toReturn.push(view);
            }
        });
        if (toReturn.length === 0) {
            DocumentManager.Instance.DocumentViews.map(view => {
                let doc = view.props.Document.proto;
                if (doc && doc[Id]) {
                    if (doc[Id] === id) { toReturn.push(view); }
                }
            });
        }
        return toReturn;
    }

    public getAllDocumentViews(doc: Doc) {
        return this.getDocumentViewsById(doc[Id]);
    }

    public getDocumentViewById(id: string, preferredCollection?: CollectionView | CollectionPDFView | CollectionVideoView): DocumentView | null {

        let toReturn: DocumentView | null = null;
        let passes = preferredCollection ? [preferredCollection, undefined] : [undefined];

        for (let pass of passes) {
            DocumentManager.Instance.DocumentViews.map(view => {
                if (view.props.Document[Id] === id && (!pass || view.props.ContainingCollectionView === preferredCollection)) {
                    toReturn = view;
                    return;
                }
            });
            if (!toReturn) {
                DocumentManager.Instance.DocumentViews.map(view => {
                    let doc = view.props.Document.proto;
                    if (doc && doc[Id] === id && (!pass || view.props.ContainingCollectionView === preferredCollection)) {
                        toReturn = view;
                    }
                });
            }
        }

        return toReturn;
    }

    public getDocumentView(toFind: Doc, preferredCollection?: CollectionView | CollectionPDFView | CollectionVideoView): DocumentView | null {
        return this.getDocumentViewById(toFind[Id], preferredCollection);
    }

    public getDocumentViews(toFind: Doc): DocumentView[] {

        let toReturn: DocumentView[] = [];

        //gets document view that is in a freeform canvas collection
        DocumentManager.Instance.DocumentViews.map(view => {
            let doc = view.props.Document;

            if (doc === toFind) {
                toReturn.push(view);
            } else {
                if (Doc.AreProtosEqual(doc, toFind)) {
                    toReturn.push(view);
                }
            }
        });

        return toReturn;
    }

    @computed
    public get LinkedDocumentViews() {
        let pairs = DocumentManager.Instance.DocumentViews.filter(dv => dv.isSelected() || Doc.IsBrushed(dv.props.Document)).reduce((pairs, dv) => {
            let linksList = LinkManager.Instance.getAllRelatedLinks(dv.props.Document);
            pairs.push(...linksList.reduce((pairs, link) => {
                if (link) {
                    let linkToDoc = LinkManager.Instance.getOppositeAnchor(link, dv.props.Document);
                    if (linkToDoc) {
                        DocumentManager.Instance.getDocumentViews(linkToDoc).map(docView1 => {
                            pairs.push({ a: dv, b: docView1, l: link });
                        });
                    }
                }
                return pairs;
            }, [] as { a: DocumentView, b: DocumentView, l: Doc }[]));
            // }
            return pairs;
        }, [] as { a: DocumentView, b: DocumentView, l: Doc }[]);

        return pairs;
    }


    @undoBatch
    public jumpToDocument = async (docDelegate: Doc, willZoom: boolean, forceDockFunc: boolean = false, dockFunc?: (doc: Doc) => void, linkPage?: number, docContext?: Doc): Promise<void> => {
        let doc = Doc.GetProto(docDelegate);
        const contextDoc = await Cast(doc.annotationOn, Doc);
        if (contextDoc) {
            const page = NumCast(doc.page, linkPage || 0);
            const curPage = NumCast(contextDoc.curPage, page);
            if (page !== curPage) contextDoc.curPage = page;
        }

        let docView: DocumentView | null;
        // using forceDockFunc as a flag for splitting linked to doc to the right...can change later if needed
        if (!forceDockFunc && (docView = DocumentManager.Instance.getDocumentView(doc))) {
            Doc.BrushDoc(docView.props.Document);
            if (linkPage !== undefined) docView.props.Document.curPage = linkPage;
            UndoManager.RunInBatch(() => docView!.props.focus(docView!.props.Document, willZoom), "focus");
        } else {
            if (!contextDoc) {
                let docs = docContext ? await DocListCastAsync(docContext.data) : undefined;
                let found = false;
                // bcz: this just searches within the context for the target -- perhaps it should recursively search through all children?
                docs && docs.map(d => found = found || Doc.AreProtosEqual(d, docDelegate));
                if (docContext && found) {
                    let targetContextView: DocumentView | null;

                    if (!forceDockFunc && docContext && (targetContextView = DocumentManager.Instance.getDocumentView(docContext))) {
                        docContext.panTransformType = "Ease";
                        targetContextView.props.focus(docDelegate, willZoom);
                    } else {
                        (dockFunc || CollectionDockingView.AddRightSplit)(docContext, undefined);
                        setTimeout(() => {
                            let dv = DocumentManager.Instance.getDocumentView(docContext);
                            dv && this.jumpToDocument(docDelegate, willZoom, forceDockFunc,
                                doc => dv!.props.focus(dv!.props.Document, true, 1, () => dv!.props.addDocTab(doc, undefined, "inPlace")),
                                linkPage);
                        }, 1050);
                    }
                } else {
                    const actualDoc = Doc.MakeAlias(docDelegate);
                    Doc.BrushDoc(actualDoc);
                    if (linkPage !== undefined) actualDoc.curPage = linkPage;
                    (dockFunc || CollectionDockingView.AddRightSplit)(actualDoc, undefined);
                }
            } else {
                let contextView: DocumentView | null;
                Doc.BrushDoc(docDelegate);
                if (!forceDockFunc && (contextView = DocumentManager.Instance.getDocumentView(contextDoc))) {
                    contextDoc.panTransformType = "Ease";
                    contextView.props.focus(docDelegate, willZoom);
                } else {
                    (dockFunc || CollectionDockingView.AddRightSplit)(contextDoc, undefined);
                    setTimeout(() => {
                        this.jumpToDocument(docDelegate, willZoom, forceDockFunc, dockFunc, linkPage);
                    }, 10);
                }
            }
        }
    }

    @action
    zoomIntoScale = (docDelegate: Doc, scale: number) => {
        let doc = Doc.GetProto(docDelegate);

        let docView: DocumentView | null;
        docView = DocumentManager.Instance.getDocumentView(doc);
        if (docView) {
            docView.props.zoomToScale(scale);
        }
    }

    getScaleOfDocView = (docDelegate: Doc) => {
        let doc = Doc.GetProto(docDelegate);

        let docView: DocumentView | null;
        docView = DocumentManager.Instance.getDocumentView(doc);
        if (docView) {
            return docView.props.getScale();
        } else {
            return 1;
        }
    }

    @action
    animateBetweenPoint = (scrpt: number[], expandedDocs: Doc[] | undefined): void => {
        expandedDocs && expandedDocs.map(expDoc => {
            if (expDoc.isMinimized || expDoc.isAnimating === "min") { // MAXIMIZE DOC
                if (expDoc.isMinimized) {  // docs are never actaully at the minimized location.  so when we unminimize one, we have to set our overrides to make it look like it was at the minimize location
                    expDoc.isMinimized = false;
                    expDoc.animateToPos = new List<number>([...scrpt, 0]);
                    expDoc.animateToDimensions = new List<number>([0, 0]);
                }
                setTimeout(() => {
                    expDoc.isAnimating = "max";
                    expDoc.animateToPos = new List<number>([0, 0, 1]);
                    expDoc.animateToDimensions = new List<number>([NumCast(expDoc.width), NumCast(expDoc.height)]);
                    setTimeout(() => expDoc.isAnimating === "max" && (expDoc.isAnimating = expDoc.animateToPos = expDoc.animateToDimensions = undefined), 600);
                }, 0);
            } else {  // MINIMIZE DOC
                expDoc.isAnimating = "min";
                expDoc.animateToPos = new List<number>([...scrpt, 0]);
                expDoc.animateToDimensions = new List<number>([0, 0]);
                setTimeout(() => {
                    if (expDoc.isAnimating === "min") {
                        expDoc.isMinimized = true;
                        expDoc.isAnimating = expDoc.animateToPos = expDoc.animateToDimensions = undefined;
                    }
                }, 600);
            }
        });
    }
}
Scripting.addGlobal(function focus(doc: any) { DocumentManager.Instance.getDocumentViews(Doc.GetProto(doc)).map(view => view.props.focus(doc, true)); });