aboutsummaryrefslogtreecommitdiff
path: root/src/client/util/LinkManager.ts
blob: 6a2ac6bbbb47f9c96eff6a86f010353743e643c2 (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
import { action, makeObservable, observable, observe } from 'mobx';
import { computedFn } from 'mobx-utils';
import * as rp from 'request-promise';
import { ClientUtils } from '../../ClientUtils';
import { Doc, DocListCast, DocListCastAsync, FieldType, Opt } from '../../fields/Doc';
import { DirectLinks, DocData } from '../../fields/DocSymbols';
import { FieldLoader } from '../../fields/FieldLoader';
import { Id } from '../../fields/FieldSymbols';
import { List } from '../../fields/List';
import { ProxyField } from '../../fields/Proxy';
import { Cast, DocCast, PromiseValue, StrCast } from '../../fields/Types';
import { DocServer } from '../DocServer';
import { DocumentType } from '../documents/DocumentTypes';
import { ScriptingGlobals } from './ScriptingGlobals';
/*
 * link doc:
 * - link_anchor_1: doc
 * - link_anchor_2: doc
 *
 * group doc:
 * - type: string representing the group type/name/category
 * - metadata: doc representing the metadata kvps
 *
 * metadata doc:
 * - user defined kvps
 */
export class LinkManager {
    // eslint-disable-next-line no-use-before-define
    @observable static _instance: LinkManager;
    @observable.shallow userLinkDBs: Doc[] = [];
    @observable public currentLink: Opt<Doc> = undefined;
    @observable public currentLinkAnchor: Opt<Doc> = undefined;
    public static get Instance() {
        return LinkManager._instance;
    }

    public static Links(doc: Doc | undefined) {
        return doc ? LinkManager.Instance.getAllRelatedLinks(doc) : [];
    }
    public addLinkDB = async (linkDb: any) => {
        await Promise.all(
            ((await DocListCastAsync(linkDb.data)) ?? []).map(link =>
                // makes sure link anchors are loaded to avoid incremental updates to computedFns in LinkManager
                [PromiseValue(link?.link_anchor_1), PromiseValue(link?.link_anchor_2)]
            )
        );
        this.userLinkDBs.push(linkDb);
    };
    public static AutoKeywords = 'keywords:Usages';
    constructor() {
        makeObservable(this);
        LinkManager._instance = this;
        Doc.AddLink = this.addLink;
        this.createlink_relationshipLists();
        // since this is an action, not a reaction, we get only one shot to add this link to the Anchor docs
        // Thus make sure all promised values are resolved from link -> link.proto -> link.link_anchor_[1,2] -> link.link_anchor_[1,2].proto
        // Then add the link to the anchor protos.
        const addLinkToDoc = (lprom: Doc) =>
            PromiseValue(lprom).then((link: Opt<Doc>) =>
                PromiseValue(link?.proto as Doc).then((lproto: Opt<Doc>) =>
                    Promise.all([lproto?.link_anchor_1 as Doc, lproto?.link_anchor_2 as Doc].map(PromiseValue)).then((lAnchs: Opt<Doc>[]) =>
                        Promise.all(lAnchs.map(lAnch => PromiseValue(lAnch?.proto as Doc))).then((lAnchProtos: Opt<Doc>[]) =>
                            Promise.all(lAnchProtos.map(lAnchProto => PromiseValue(lAnchProto?.proto as Doc))).then(
                                link &&
                                    action(() => {
                                        Doc.AddDocToList(Doc.UserDoc(), 'links', link);
                                        lAnchs[0]?.[DocData][DirectLinks].add(link);
                                        lAnchs[1]?.[DocData][DirectLinks].add(link);
                                    })
                            )
                        )
                    )
                )
            );

        const remLinkFromDoc = (lprom: Doc) =>
            PromiseValue(lprom).then((link: Opt<Doc>) =>
                PromiseValue(link?.proto as Doc).then((lproto: Opt<Doc>) =>
                    Promise.all([lproto?.link_anchor_1 as Doc, lproto?.link_anchor_2 as Doc].map(PromiseValue)).then((lAnchs: Opt<Doc>[]) =>
                        Promise.all(lAnchs.map(lAnch => PromiseValue(lAnch?.proto as Doc))).then((lAnchProtos: Opt<Doc>[]) =>
                            Promise.all(lAnchProtos.map(lAnchProto => PromiseValue(lAnchProto?.proto as Doc))).then(
                                action(() => {
                                    link && lAnchs[0] && lAnchs[0][DocData][DirectLinks].delete(link);
                                    link && lAnchs[1] && lAnchs[1][DocData][DirectLinks].delete(link);
                                })
                            )
                        )
                    )
                )
            );

        const watchUserLinkDB = (userLinkDBDoc: Doc) => {
            const toRealField = (field: FieldType) => (field instanceof ProxyField ? field.value : field); // see List.ts.  data structure is not a simple list of Docs, but a list of ProxyField/Fields
            if (userLinkDBDoc.data) {
                observe(
                    userLinkDBDoc.data,
                    change => {
                        // observe pushes/splices on a user link DB 'data' field (should only happen for local changes)
                        switch (change.type as any) {
                            case 'splice':
                                (change as any).added.forEach((link: any) => addLinkToDoc(toRealField(link)));
                                (change as any).removed.forEach((link: any) => remLinkFromDoc(toRealField(link)));
                                break;
                            case 'update': // let oldValue = change.oldValue;
                            default:
                        }
                    },
                    true
                );
                observe(
                    userLinkDBDoc,
                    'data', // obsever when a new array of links is assigned as the link DB 'data' field (should happen whenever a remote user adds/removes a link)
                    change => {
                        switch (change.type as any) {
                            case 'update':
                                Promise.all([...((change.oldValue as any as Doc[]) || []), ...((change.newValue as any as Doc[]) || [])]).then(doclist => {
                                    const oldDocs = doclist.slice(0, ((change.oldValue as any as Doc[]) || []).length);
                                    const newDocs = doclist.slice(((change.oldValue as any as Doc[]) || []).length, doclist.length);

                                    const added = newDocs?.filter(link => !(oldDocs || []).includes(link));
                                    const removed = oldDocs?.filter(link => !(newDocs || []).includes(link));
                                    added?.forEach((link: any) => addLinkToDoc(toRealField(link)));
                                    removed?.forEach((link: any) => remLinkFromDoc(toRealField(link)));
                                });
                                break;
                            default:
                        }
                    },
                    true
                );
            }
        };
        observe(
            this.userLinkDBs,
            change => {
                switch (change.type as any) {
                    case 'splice':
                        (change as any).added.forEach(watchUserLinkDB);
                        break;
                    case 'update': // let oldValue = change.oldValue;
                    default:
                }
            },
            true
        );
        FieldLoader.ServerLoadStatus.message = 'links';
        this.addLinkDB(Doc.LinkDBDoc());
    }

    public createlink_relationshipLists = () => {
        // create new lists for link relations and their associated colors if the lists don't already exist
        !Doc.UserDoc().link_relationshipList && (Doc.UserDoc().link_relationshipList = new List<string>());
        !Doc.UserDoc().link_ColorList && (Doc.UserDoc().link_ColorList = new List<string>());
        !Doc.UserDoc().link_relationshipSizes && (Doc.UserDoc().link_relationshipSizes = new List<number>());
    };

    public addLink(linkDoc: Doc, checkExists = false) {
        Doc.AddDocToList(Doc.UserDoc(), 'links', linkDoc);
        if (!checkExists || !DocListCast(Doc.LinkDBDoc().data).includes(linkDoc)) {
            Doc.AddDocToList(Doc.LinkDBDoc(), 'data', linkDoc);
            // eslint-disable-next-line no-use-before-define
            setTimeout(UPDATE_SERVER_CACHE, 100);
        }
    }
    public deleteLink(linkDoc: Doc) {
        const ret = Doc.RemoveDocFromList(Doc.LinkDBDoc(), 'data', linkDoc);
        linkDoc[DocData].link_anchor_1 = linkDoc[DocData].link_anchor_2 = undefined;
        return ret;
    }
    public deleteAllLinksOnAnchor(anchor: Doc) {
        LinkManager.Instance.relatedLinker(anchor).forEach(LinkManager.Instance.deleteLink);
    }

    public getAllRelatedLinks(anchor: Doc) {
        return this.relatedLinker(anchor);
    } // finds all links that contain the given anchor
    public getAllDirectLinks(anchor?: Doc): Doc[] {
        return anchor ? Array.from(anchor[DocData][DirectLinks]) : [];
    } // finds all links that contain the given anchor

    computedRelatedLinks = (anchor: Doc, processed: Doc[]): Doc[] => {
        if (Doc.IsSystem(anchor)) return [];
        if (!anchor || anchor instanceof Promise || Doc.GetProto(anchor) instanceof Promise) {
            console.log('WAITING FOR DOC/PROTO IN LINKMANAGER');
            return [];
        }

        const dirLinks = Array.from(anchor[DocData][DirectLinks]).filter(l => Doc.GetProto(anchor) === anchor[DocData] || ['1', '2'].includes(LinkManager.anchorIndex(l, anchor) as any));
        const anchorRoot = DocCast(anchor.rootDocument, anchor); // template Doc fields store annotations on the topmost root of a template (not on themselves since the template layout items are only for layout)
        const annos = DocListCast(anchorRoot[Doc.LayoutFieldKey(anchor) + '_annotations']);
        return Array.from(
            annos.reduce((set, anno) => {
                if (!processed.includes(anno)) {
                    processed.push(anno);
                    this.computedRelatedLinks(anno, processed).forEach(link => set.add(link));
                }
                return set;
            }, new Set<Doc>(dirLinks))
        );
    };

    relatedLinker = computedFn((anchor: Doc): Doc[] => this.computedRelatedLinks(anchor, [anchor]), true);

    // returns map of group type to anchor's links in that group type
    public getRelatedGroupedLinks(anchor: Doc): Map<string, Array<Doc>> {
        const anchorGroups = new Map<string, Array<Doc>>();
        this.relatedLinker(anchor).forEach(link => {
            if (link.link_relationship && link.link_relationship !== '-ungrouped-') {
                const relation = StrCast(link.link_relationship);
                const anchorRelation: string = relation.indexOf(':') !== -1 ? relation.split(':')[Doc.AreProtosEqual(Cast(link.link_anchor_1, Doc, null), anchor) ? 0 : 1] : relation;
                const group = anchorGroups.get(anchorRelation);
                anchorGroups.set(anchorRelation, group ? [...group, link] : [link]);
            } else {
                // if link is in no groups then put it in default group
                const group = anchorGroups.get('*');
                anchorGroups.set('*', group ? [...group, link] : [link]);
            }
        });
        return anchorGroups;
    }

    // finds the opposite anchor of a given anchor in a link
    public static getOppositeAnchor(linkDoc: Doc, anchor: Doc): Doc | undefined {
        const id = LinkManager.anchorIndex(linkDoc, anchor);
        const a1 = DocCast(linkDoc.link_anchor_1);
        const a2 = DocCast(linkDoc.link_anchor_2);
        return id === '1' ? a2 : id === '2' ? a1 : id === '0' ? linkDoc : undefined;
        // if (Doc.AreProtosEqual(DocCast(anchor.annotationOn, anchor), DocCast(a1?.annotationOn, a1))) return a2;
        // if (Doc.AreProtosEqual(DocCast(anchor.annotationOn, anchor), DocCast(a2?.annotationOn, a2))) return a1;
        // if (Doc.AreProtosEqual(anchor, linkDoc)) return linkDoc;
    }
    public static anchorIndex(linkDoc: Doc, anchor: Doc) {
        const a1 = DocCast(linkDoc.link_anchor_1);
        const a2 = DocCast(linkDoc.link_anchor_2);
        if (linkDoc.link_matchEmbeddings) {
            return [a2, a2?.annotationOn].includes(anchor) ? '2' : '1';
        }
        if (Doc.AreProtosEqual(DocCast(anchor?.annotationOn, anchor), DocCast(a1?.annotationOn, a1))) return '1';
        if (Doc.AreProtosEqual(DocCast(anchor?.annotationOn, anchor), DocCast(a2?.annotationOn, a2))) return '2';
        if (Doc.AreProtosEqual(anchor, linkDoc)) return '0';
        return undefined;
    }
}

let cacheDocumentIds = ''; // ; separate string of all documents ids in the user's working set (cached on the server)
export function UPDATE_SERVER_CACHE() {
    const prototypes = Object.values(DocumentType)
        .filter(type => type !== DocumentType.NONE)
        .map(type => DocServer._cache[type + 'Proto'])
        .filter(doc => doc instanceof Doc)
        .map(doc => doc as Doc);
    const references = new Set<Doc>(prototypes);
    Doc.FindReferences(Doc.UserDoc(), references, undefined);
    DocListCast(DocCast(Doc.UserDoc().myLinkDatabase).data).forEach(link => {
        if (!references.has(DocCast(link.link_anchor_1)) && !references.has(DocCast(link.link_anchor_2))) {
            Doc.RemoveDocFromList(DocCast(Doc.UserDoc().myLinkDatabase), 'data', link);
            Doc.AddDocToList(Doc.MyRecentlyClosed, undefined, link);
        }
    });
    LinkManager.Instance.userLinkDBs.forEach(linkDb => Doc.FindReferences(linkDb, references, undefined));
    const filtered = Array.from(references);

    const newCacheUpdate = filtered.map(doc => doc[Id]).join(';');
    if (newCacheUpdate === cacheDocumentIds) return;
    cacheDocumentIds = newCacheUpdate;

    // print out cached docs
    Doc.MyDockedBtns.linearView_IsOpen && console.log('Set cached docs = ');
    const isFiltered = filtered.filter(doc => !Doc.IsSystem(doc));
    const strings = isFiltered.map(doc => StrCast(doc.title) + ' ' + (Doc.IsDataProto(doc) ? '(data)' : '(embedding)'));
    Doc.MyDockedBtns.linearView_IsOpen && strings.sort().forEach((str, i) => console.log(i.toString() + ' ' + str));

    rp.post(ClientUtils.prepend('/setCacheDocumentIds'), {
        body: {
            cacheDocumentIds,
        },
        json: true,
    });
}

ScriptingGlobals.add(
    // eslint-disable-next-line prefer-arrow-callback
    function links(doc: any) {
        return new List(LinkManager.Links(doc));
    },
    'returns all the links to the document or its annotations',
    '(doc: any)'
);