aboutsummaryrefslogtreecommitdiff
path: root/src/client/views/nodes/LinkBox.tsx
blob: d31fadf77ab34b3b6e8f7e24dc6cd52daf621434 (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
/* eslint-disable @typescript-eslint/no-unused-vars */
import { action, computed, IReactionDisposer, makeObservable, observable, reaction } from 'mobx';
import { observer } from 'mobx-react';
import * as React from 'react';
import Xarrow from 'react-xarrows';
import { DashColor, lightOrDark, returnFalse } from '../../../ClientUtils';
import { FieldResult } from '../../../fields/Doc';
import { DocCss, DocData } from '../../../fields/DocSymbols';
import { Id } from '../../../fields/FieldSymbols';
import { List } from '../../../fields/List';
import { DocCast, NumCast, StrCast } from '../../../fields/Types';
import { TraceMobx } from '../../../fields/util';
import { emptyFunction } from '../../../Utils';
import { Docs } from '../../documents/Documents';
import { DocumentType } from '../../documents/DocumentTypes';
import { SnappingManager } from '../../util/SnappingManager';
import { ViewBoxBaseComponent } from '../DocComponent';
import { EditableView } from '../EditableView';
import { StyleProp } from '../StyleProp';
import { ComparisonBox } from './ComparisonBox';
import { DocumentView } from './DocumentView';
import { FieldView, FieldViewProps } from './FieldView';
import { RichTextMenu } from './formattedText/RichTextMenu';
import './LinkBox.scss';

@observer
export class LinkBox extends ViewBoxBaseComponent<FieldViewProps>() {
    public static LayoutString(fieldKey: string = 'link') {
        return FieldView.LayoutString(LinkBox, fieldKey);
    }
    _hackToSeeIfDeleted: NodeJS.Timeout | undefined;
    _disposers: { [name: string]: IReactionDisposer } = {};
    _divRef: HTMLDivElement | null = null;
    @observable _forceAnimate: number = 0; // forces xArrow to animate when a transition animation is detected on something that affects an anchor
    @observable _hide = false; // don't render if anchor is not visible since that breaks xAnchor

    constructor(props: FieldViewProps) {
        super(props);
        makeObservable(this);
    }
    @computed get anchor1() { return this.anchor(1); } // prettier-ignore
    @computed get anchor2() { return this.anchor(2); } // prettier-ignore

    anchor = (which: number) => {
        const anch = DocCast(this.dataDoc['link_anchor_' + which]);
        const anchor = anch?.layout_unrendered ? DocCast(anch.annotationOn) : anch;
        return DocumentView.getDocumentView(anchor, this.DocumentView?.().containerViewPath?.().lastElement());
    };
    componentWillUnmount() {
        this._hackToSeeIfDeleted && clearTimeout(this._hackToSeeIfDeleted);
        Object.keys(this._disposers).forEach(key => this._disposers[key]());
    }
    componentDidMount() {
        this._props.setContentViewBox?.(this);
        this._disposers.deleting = reaction(
            () => !this.anchor1 && !this.anchor2 && this.DocumentView?.() && (!DocumentView.LightboxDoc() || DocumentView.LightboxContains(this.DocumentView!())),
            empty => {
                if (empty) {
                    this._hackToSeeIfDeleted = setTimeout(() => {
                        !this.anchor1 && !this.anchor2 && this._props.removeDocument?.(this.Document);
                    }, 1000);
                }
            }
        );
        this._disposers.dragging = reaction(
            () => SnappingManager.IsDragging,
            () => setTimeout( action(() => {// need to wait for drag manager to set 'hidden' flag on dragged DOM elements
                const a = this.anchor1;
                const b = this.anchor2;
                let a1 = a && document.getElementById(a.ViewGuid);
                let a2 = b && document.getElementById(b.ViewGuid);
                // test whether the anchors themselves are hidden,...
                if (!a1 || !a2 || a?.ContentDiv?.hidden || b?.ContentDiv?.hidden) this._hide = true;
                else {
                    // .. or whether any of their DOM parents are hidden
                    for (; a1 && !a1.hidden; a1 = a1.parentElement);
                    for (; a2 && !a2.hidden; a2 = a2.parentElement);
                    this._hide = !!(a1 || a2);
                }
            })) // prettier-ignore
        );
    }
    /**
     * When an IconButton is clicked, it will receive focus.  However, we don't want that since we want or need that since we really want
     * to maintain focus in the label's editing div (and cursor position).  so this relies on IconButton's having a tabindex set to -1 so that
     * we can march up the tree from the 'relatedTarget' to determine if the loss of focus was caused by a fonticonbox.   If it is, we then
     * restore focus
     * @param e focusout event on the editing div
     */
    keepFocus = (e: FocusEvent) => {
        if (e.relatedTarget instanceof HTMLElement && e.relatedTarget.tabIndex === -1) {
            for (let ele: HTMLElement | null = e.relatedTarget; ele; ele = (ele as HTMLElement)?.parentElement) {
                if (['listItem-container', 'fonticonbox'].includes((ele as HTMLElement)?.className ?? '')) {
                    console.log('RESTORE :', document.activeElement, this._divRef);
                    this._divRef?.focus();
                    break;
                }
            }
        }
    };

    setRef = (r: HTMLDivElement | null) => (this._divRef = r);
    render() {
        TraceMobx();

        if (this._hide) return null;
        const a = this.anchor1;
        const b = this.anchor2;
        this._forceAnimate;
        const docView = this._props.docViewPath().lastElement();

        if (a && b) {
            // text selection bounds are not directly observable, so we have to
            // force an update when anything that could affect them changes (text edits causing reflow, scrolling)
            a.Document[a.LayoutFieldKey];
            b.Document[b.LayoutFieldKey];
            a.Document.layout_scrollTop;
            b.Document.layout_scrollTop;
            a.Document[DocCss];
            b.Document[DocCss];

            const axf = a.screenToViewTransform(); // these force re-render when a or b moves (so do NOT remove)
            const bxf = b.screenToViewTransform();
            const scale = docView?.screenToViewTransform().Scale ?? 1;
            const at = a.getBounds?.transition; // these force re-render when a or b change size and at the end of an animated transition
            const bt = b.getBounds?.transition; //    inquring getBounds() also causes text anchors to update whether or not they reflow (any size change triggers an invalidation)

            let foundParent = false;
            const getAnchor = (field: FieldResult): Element[] => {
                const docField = DocCast(field);
                const doc = docField?.layout_unrendered ? DocCast(docField.annotationOn, docField) : docField;
                if (!doc) return [];
                const ele = document.getElementById(DocumentView.UniquifyId(DocumentView.LightboxContains(this.DocumentView?.()), doc[Id]));
                if (ele?.className === 'linkBox-label') foundParent = true;
                if (ele?.getBoundingClientRect().width) return [ele];
                const eles = Array.from(document.getElementsByClassName(doc[Id])).filter(el => el?.getBoundingClientRect().width);
                const annoOn = DocCast(doc.annotationOn);
                if (eles.length || !annoOn) return eles;
                const pareles = getAnchor(annoOn);
                foundParent = !!pareles.length;
                return pareles;
            };
            //   if there's an element in the DOM with a classname containing a link anchor's id (eg a hypertext <a>),
            //   then that DOM element is a hyperlink source for the current anchor and we want to place our link box at it's top right
            //   otherwise, we just use the computed nearest point on the document boundary to the target Document
            const targetAhyperlinks = getAnchor(this.dataDoc.link_anchor_1);
            const targetBhyperlinks = getAnchor(this.dataDoc.link_anchor_2);

            const container = this.DocumentView?.().containerViewPath?.().lastElement()?.ContentDiv;
            const aid = targetAhyperlinks?.find(alink => container?.contains(alink))?.id ?? targetAhyperlinks?.lastElement()?.id;
            const bid = targetBhyperlinks?.find(blink => container?.contains(blink))?.id ?? targetBhyperlinks?.lastElement()?.id;
            if (!aid || !bid) {
                setTimeout(action(() => (this._forceAnimate += 0.01)));
                return null;
            }
            if (foundParent) {
                setTimeout(
                    action(() => (this._forceAnimate += 0.01)),
                    1
                );
            }
            if (at || bt) {
                setTimeout(action(() => (this._forceAnimate += 0.01))); // this forces an update during a transition animation
            }
            const highlight = this._props.styleProvider?.(this.layoutDoc, this._props, StyleProp.Highlighting) as { highlightStyle: string; highlightColor: string; highlightIndex: number; highlightStroke: boolean };
            const highlightColor = highlight?.highlightIndex ? highlight?.highlightColor : undefined;
            const color = this._props.styleProvider?.(this.layoutDoc, this._props, StyleProp.Color) as string;
            const fontFamily = this._props.styleProvider?.(this.layoutDoc, this._props, StyleProp.FontFamily) as string;
            const fontSize = this._props.styleProvider?.(this.layoutDoc, this._props, StyleProp.FontSize) as number;
            const fontColor = (c => (c !== 'transparent' ? c : undefined))(StrCast(this.layoutDoc.link_fontColor));
            const { stroke_markerScale: strokeMarkerScale, stroke_width: strokeRawWidth, stroke_startMarker: strokeStartMarker, stroke_endMarker: strokeEndMarker, stroke_dash: strokeDash } = this.Document;

            const strokeWidth = NumCast(strokeRawWidth, 1);
            const linkDesc = StrCast(this.dataDoc.link_description) || ' ';
            const labelText = linkDesc.substring(0, 50) + (linkDesc.length > 50 ? '...' : '');
            return (
                <>
                    {!highlightColor ? null : (
                        <Xarrow
                            divContainerStyle={{ transform: `scale(${scale})` }}
                            start={aid}
                            end={bid} //
                            strokeWidth={strokeWidth + Math.max(2, strokeWidth * 0.1)}
                            showHead={!!strokeStartMarker}
                            showTail={!!strokeEndMarker}
                            headSize={NumCast(strokeMarkerScale, 3)}
                            tailSize={NumCast(strokeMarkerScale, 3)}
                            tailShape={strokeEndMarker === 'dot' ? 'circle' : 'arrow1'}
                            headShape={strokeStartMarker === 'dot' ? 'circle' : 'arrow1'}
                            color={highlightColor}
                        />
                    )}
                    <Xarrow
                        divContainerStyle={{ transform: `scale(${scale})` }}
                        start={aid}
                        end={bid} //
                        strokeWidth={strokeWidth}
                        dashness={!!Number(strokeDash)}
                        showHead={!!strokeStartMarker}
                        showTail={!!strokeEndMarker}
                        headSize={NumCast(strokeMarkerScale, 3)}
                        tailSize={NumCast(strokeMarkerScale, 3)}
                        tailShape={strokeEndMarker === 'dot' ? 'circle' : 'arrow1'}
                        headShape={strokeStartMarker === 'dot' ? 'circle' : 'arrow1'}
                        color={color}
                        labels={
                            <div
                                id={this.DocumentView?.().DocUniqueId}
                                className="linkBox-label"
                                tabIndex={-1}
                                ref={this.setRef}
                                onPointerDown={e => e.stopPropagation()}
                                onFocus={() => {
                                    RichTextMenu.Instance?.updateMenu(undefined, undefined, undefined, this.dataDoc);
                                    this._divRef?.removeEventListener('focusout', this.keepFocus);
                                    this._divRef?.addEventListener('focusout', this.keepFocus);
                                }}
                                onBlur={() => {
                                    if (document.activeElement !== this._divRef && document.activeElement?.parentElement !== this._divRef) {
                                        this._divRef?.removeEventListener('focusout', this.keepFocus);
                                        RichTextMenu.Instance?.updateMenu(undefined, undefined, undefined, undefined);
                                    }
                                }}
                                style={{
                                    borderRadius: '8px',
                                    transform: `scale(${1 / scale})`,
                                    pointerEvents: this._props.isDocumentActive?.() ? 'all' : undefined,
                                    fontSize,
                                    fontFamily /* , fontStyle: 'italic' */,
                                    color: fontColor || lightOrDark(DashColor(color).fade(0.5).toString()),
                                    paddingLeft: 4,
                                    paddingRight: 4,
                                    paddingTop: 3,
                                    paddingBottom: 3,
                                    background: DashColor((!docView?.isSelected() && highlightColor) || color)
                                        .fade(0.5)
                                        .toString(),
                                }}>
                                <EditableView
                                    key="editableView"
                                    oneLine
                                    contents={labelText}
                                    height={fontSize + 4}
                                    fontSize={fontSize}
                                    GetValue={() => linkDesc}
                                    SetValue={action(val => {
                                        this.Document.$link_description = val;
                                        return true;
                                    })}
                                />

                                {/* <EditableText
                                    placeholder={labelText}
                                    background={color}
                                    color={fontColor || lightOrDark(DashColor(color).fade(0.5).toString())}
                                    type={Type.PRIM}
                                    val={StrCast(this.Document.$link_description)}
                                    setVal={action(val => (this.Document.$link_description = val))}
                                    fillWidth
                                /> */}
                            </div>
                        }
                        passProps={{}}
                    />
                </>
            );
        }

        setTimeout(
            action(() => {
                this._forceAnimate += 1;
            }),
            2
        );
        return (
            <div className={`linkBox-container${this._props.isContentActive() ? '-interactive' : ''}`} style={{ background: this._props.styleProvider?.(this.layoutDoc, this._props, StyleProp.BackgroundColor) as string }}>
                <ComparisonBox
                    {...this.props} //
                    fieldKey="link_anchor"
                    setHeight={emptyFunction}
                    dontRegisterView
                    renderDepth={this._props.renderDepth + 1}
                    addDocument={returnFalse}
                    removeDocument={returnFalse}
                    moveDocument={returnFalse}
                />
            </div>
        );
    }
}

Docs.Prototypes.TemplateMap.set(DocumentType.LINK, {
    layout: { view: LinkBox, dataField: 'link' },
    options: {
        acl: '',
        childDontRegisterViews: true,
        layout_hideLinkAnchors: true,
        _height: 1,
        _width: 1,
        link: '',
        link_description: '',
        color: 'lightBlue', // lightblue is default color for linking dot and link documents text comment area
        _dropPropertiesToRemove: new List(['onClick']),
    },
});