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

interface IRegionAnnotationProps {
    x: number;
    y: number;
    width: number;
    height: number;
    opacity: () => number;
    background: () => string;
    outline: () => string | undefined;
}

const RegionAnnotation = function (props: IRegionAnnotationProps) {
    return (
        <div
            className="htmlAnnotation"
            style={{
                left: NumCast(props.x),
                top: NumCast(props.y),
                width: NumCast(props.width),
                height: NumCast(props.height),
                opacity: props.opacity(),
                outline: props.outline(),
                backgroundColor: props.background(),
            }}
        />
    );
};

interface IAnnotationProps extends FieldViewProps {
    annoDoc: Doc;
    containerDataDoc: Doc;
    fieldKey: string;
    pointerEvents?: () => Opt<Property.PointerEvents>;
}
@observer
export class Annotation extends ObservableReactComponent<IAnnotationProps> {
    constructor(props: IAnnotationProps) {
        super(props);
        makeObservable(this);
    }

    @computed get linkHighlighted() {
        const found = LinkManager.Instance.getAllDirectLinks(this._props.annoDoc).find(link => {
            const a1 = Doc.getOppositeAnchor(link, this._props.annoDoc);
            return a1 && Doc.GetBrushStatus(DocCast(a1.annotationOn, a1)!);
        });
        return found;
    }

    deleteAnnotation = undoable(() => {
        const docAnnotations = DocListCast(this._props.containerDataDoc[this._props.fieldKey]);
        this._props.containerDataDoc[this._props.fieldKey] = new List<Doc>(docAnnotations.filter(a => a !== this._props.annoDoc));
        AnchorMenu.Instance.fadeOut(true);
        this._props.select(false);
    }, 'delete annotation');

    pinToPres = undoable(() => this._props.pinToPres(this._props.annoDoc, {}), 'pin to pres');

    makeTargetToggle = undoable(() => { this._props.annoDoc.followLinkToggle = !this._props.annoDoc.followLinkToggle }, "set link toggle"); // prettier-ignore

    isTargetToggler = () => BoolCast(this._props.annoDoc.followLinkToggle);

    showTargetTrail = undoable((anchor: Doc) => {
        const trail = DocCast(anchor.presentationTrail);
        if (trail) {
            Doc.ActivePresentation = trail;
            this._props.addDocTab(trail, OpenWhere.replaceRight);
        }
    }, 'show target trail');

    @action
    onContextMenu = (e: React.MouseEvent) => {
        AnchorMenu.Instance.Status = 'annotation';
        AnchorMenu.Instance.Delete = this.deleteAnnotation;
        AnchorMenu.Instance.Pinned = false;
        AnchorMenu.Instance.PinToPres = this.pinToPres;
        AnchorMenu.Instance.MakeTargetToggle = this.makeTargetToggle;
        AnchorMenu.Instance.IsTargetToggler = this.isTargetToggler;
        AnchorMenu.Instance.ShowTargetTrail = () => this.showTargetTrail(this._props.annoDoc);
        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();
            DocumentView.FollowLink(undefined, this._props.annoDoc, false);
        }
    };
    brushed = () => this._props.annoDoc && Doc.GetBrushHighlightStatus(this._props.annoDoc);
    opacity = () => (this.brushed() === Doc.DocBrushStatus.highlighted ? 0.5 : 1);
    outline = () => (this.linkHighlighted ? 'solid 1px lightBlue' : undefined);
    background = () => (this._props.annoDoc[Highlight] ? 'orange' : StrCast(this._props.annoDoc.backgroundColor));
    render() {
        // eslint-disable-next-line @typescript-eslint/no-unused-vars
        const forceRenderHack = [this.background(), this.outline(), this.opacity()]; // forces a re-render when these change -- because RegionAnnotation doesn't do this internally..
        return (
            <div style={{ display: this._props.annoDoc.textCopied && !Doc.GetBrushHighlightStatus(this._props.annoDoc) ? 'none' : undefined }}>
                {StrListCast(this._props.annoDoc.text_inlineAnnotations)
                    .map(a => a.split?.(':'))
                    .filter(fields => fields)
                    .map(([x, y, width, height], i) => (
                        <div
                            key={'' + x + y + width + height + i}
                            style={{ pointerEvents: this._props.pointerEvents?.() as Property.PointerEvents }}
                            onPointerDown={this.onPointerDown}
                            onContextMenu={this.onContextMenu}
                            onPointerEnter={() => {
                                Doc.BrushDoc(this._props.annoDoc);
                            }}
                            onPointerLeave={() => {
                                Doc.UnBrushDoc(this._props.annoDoc);
                            }}>
                            <RegionAnnotation //
                                x={Number(x)}
                                y={Number(y)}
                                width={Number(width)}
                                height={Number(height)}
                                outline={this.outline}
                                background={this.background}
                                opacity={this.opacity}
                            />
                        </div>
                    ))}
            </div>
        );
    }
}