aboutsummaryrefslogtreecommitdiff
path: root/src/client/views/pdf/Annotation.tsx
blob: a1f5ce7039e089830ace2ebff68e217f5c03ac85 (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
import { action, computed, makeObservable } from 'mobx';
import { observer } from 'mobx-react';
import * as React from 'react';
import { Doc, DocListCast, Opt } from '../../../fields/Doc';
import { Id } from '../../../fields/FieldSymbols';
import { List } from '../../../fields/List';
import { BoolCast, Cast, DocCast, NumCast, StrCast } from '../../../fields/Types';
import { LinkFollower } from '../../util/LinkFollower';
import { LinkManager } from '../../util/LinkManager';
import { undoBatch } from '../../util/UndoManager';
import { OpenWhere } from '../nodes/DocumentView';
import { FieldViewProps } from '../nodes/FieldView';
import { AnchorMenu } from './AnchorMenu';
import './Annotation.scss';
import { ObservableReactComponent } from '../ObservableReactComponent';

interface IAnnotationProps extends FieldViewProps {
    anno: Doc;
    dataDoc: Doc;
    fieldKey: string;
    showInfo?: (anno: Opt<Doc>) => void;
    pointerEvents?: () => Opt<string>;
}
@observer
export class Annotation extends ObservableReactComponent<IAnnotationProps> {
    constructor(props: any) {
        super(props);
        makeObservable(this);
    }
    render() {
        return (
            <div style={{ display: this._props.anno.textCopied && !Doc.GetBrushHighlightStatus(this._props.anno) ? 'none' : undefined }}>
                {DocListCast(this._props.anno.text_inlineAnnotations).map(a => (
                    <RegionAnnotation pointerEvents={this._props.pointerEvents} {...this._props} document={a} key={a[Id]} />
                ))}
            </div>
        );
    }
}

interface IRegionAnnotationProps extends IAnnotationProps {
    document: Doc;
    pointerEvents?: () => Opt<string>;
}
@observer
class RegionAnnotation extends ObservableReactComponent<IRegionAnnotationProps> {
    private _mainCont: React.RefObject<HTMLDivElement> = React.createRef();

    @computed get annoTextRegion() {
        return Cast(this._props.document.annoTextRegion, Doc, null) || this._props.document;
    }

    @undoBatch
    deleteAnnotation = () => {
        const docAnnotations = DocListCast(this._props.dataDoc[this._props.fieldKey]);
        this._props.dataDoc[this._props.fieldKey] = new List<Doc>(docAnnotations.filter(a => a !== this.annoTextRegion));
        AnchorMenu.Instance.fadeOut(true);
        this._props.select(false);
    };

    @undoBatch
    pinToPres = () => this._props.pinToPres(this.annoTextRegion, {});

    @undoBatch
    makeTargretToggle = () => (this.annoTextRegion.followLinkToggle = !this.annoTextRegion.followLinkToggle);

    isTargetToggler = () => BoolCast(this.annoTextRegion.followLinkToggle);
    @undoBatch
    showTargetTrail = (anchor: Doc) => {
        const trail = DocCast(anchor.presentationTrail);
        if (trail) {
            Doc.ActivePresentation = trail;
            this._props.addDocTab(trail, OpenWhere.replaceRight);
        }
    };

    @action
    onContextMenu = (e: React.MouseEvent) => {
        AnchorMenu.Instance.Status = 'annotation';
        AnchorMenu.Instance.Delete = this.deleteAnnotation.bind(this);
        AnchorMenu.Instance.Pinned = false;
        AnchorMenu.Instance.PinToPres = this.pinToPres;
        AnchorMenu.Instance.MakeTargetToggle = this.makeTargretToggle;
        AnchorMenu.Instance.IsTargetToggler = this.isTargetToggler;
        AnchorMenu.Instance.ShowTargetTrail = () => this.showTargetTrail(this.annoTextRegion);
        AnchorMenu.Instance.jumpTo(e.clientX, e.clientY, true);
        e.stopPropagation();
        e.preventDefault();
    };
    @action
    onPointerDown = (e: React.PointerEvent) => {
        if (e.button === 2 || e.ctrlKey) {
            e.stopPropagation();
            e.preventDefault();
        } else if (e.button === 0) {
            e.stopPropagation();
            LinkFollower.FollowLink(undefined, this.annoTextRegion, false);
        }
    };

    @computed get linkHighlighted() {
        for (const link of LinkManager.Instance.getAllDirectLinks(this._props.document)) {
            const a1 = LinkManager.getOppositeAnchor(link, this._props.document);
            if (a1 && Doc.GetBrushStatus(DocCast(a1.annotationOn, this._props.document))) return true;
        }
    }

    render() {
        const brushed = this.annoTextRegion && Doc.GetBrushHighlightStatus(this.annoTextRegion);
        return (
            <div
                className="htmlAnnotation"
                ref={this._mainCont}
                onPointerEnter={action(() => {
                    Doc.BrushDoc(this._props.anno);
                    this._props.showInfo?.(this._props.anno);
                })}
                onPointerLeave={action(() => {
                    Doc.UnBrushDoc(this._props.anno);
                    this._props.showInfo?.(undefined);
                })}
                onPointerDown={this.onPointerDown}
                onContextMenu={this.onContextMenu}
                style={{
                    left: NumCast(this._props.document.x),
                    top: NumCast(this._props.document.y),
                    width: NumCast(this._props.document._width),
                    height: NumCast(this._props.document._height),
                    opacity: brushed === Doc.DocBrushStatus.highlighted ? 0.5 : undefined,
                    pointerEvents: this._props.pointerEvents?.() as any,
                    outline: brushed === Doc.DocBrushStatus.unbrushed && this.linkHighlighted ? 'solid 1px lightBlue' : undefined,
                    backgroundColor: brushed === Doc.DocBrushStatus.highlighted ? 'orange' : StrCast(this._props.document.backgroundColor),
                }}
            />
        );
    }
}