aboutsummaryrefslogtreecommitdiff
path: root/src/client/views/pdf/AnchorMenu.tsx
blob: 1a1120b6cda80e90bcd8cd3c054fed0831c1206f (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
import React = require('react');
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
import { Tooltip } from '@material-ui/core';
import { action, computed, IReactionDisposer, observable, ObservableMap, reaction } from 'mobx';
import { observer } from 'mobx-react';
import { ColorState } from 'react-color';
import { Doc, Opt } from '../../../fields/Doc';
import { returnFalse, setupMoveUpEvents, unimplementedFunction, Utils } from '../../../Utils';
import { SelectionManager } from '../../util/SelectionManager';
import { AntimodeMenu, AntimodeMenuProps } from '../AntimodeMenu';
import { LinkPopup } from '../linking/LinkPopup';
import { ButtonDropdown } from '../nodes/formattedText/RichTextMenu';
import './AnchorMenu.scss';

@observer
export class AnchorMenu extends AntimodeMenu<AntimodeMenuProps> {
    static Instance: AnchorMenu;

    private _disposer: IReactionDisposer | undefined;
    private _commentCont = React.createRef<HTMLButtonElement>();
    private _palette = [
        'rgba(208, 2, 27, 0.8)',
        'rgba(238, 0, 0, 0.8)',
        'rgba(245, 166, 35, 0.8)',
        'rgba(248, 231, 28, 0.8)',
        'rgba(245, 230, 95, 0.616)',
        'rgba(139, 87, 42, 0.8)',
        'rgba(126, 211, 33, 0.8)',
        'rgba(65, 117, 5, 0.8)',
        'rgba(144, 19, 254, 0.8)',
        'rgba(238, 169, 184, 0.8)',
        'rgba(224, 187, 228, 0.8)',
        'rgba(225, 223, 211, 0.8)',
        'rgba(255, 255, 255, 0.8)',
        'rgba(155, 155, 155, 0.8)',
        'rgba(0, 0, 0, 0.8)',
    ];

    @observable private _keyValue: string = '';
    @observable private _valueValue: string = '';
    @observable private _added: boolean = false;
    @observable private highlightColor: string = 'rgba(245, 230, 95, 0.616)';
    @observable private _showLinkPopup: boolean = false;

    @observable public Highlighting: boolean = false;
    @observable public Status: 'marquee' | 'annotation' | '' = '';

    public onMakeAnchor: () => Opt<Doc> = () => undefined; // Method to get anchor from text search

    public OnCrop: (e: PointerEvent) => void = unimplementedFunction;
    public OnClick: (e: PointerEvent) => void = unimplementedFunction;
    public StartDrag: (e: PointerEvent, ele: HTMLElement) => void = unimplementedFunction;
    public StartCropDrag: (e: PointerEvent, ele: HTMLElement) => void = unimplementedFunction;
    public Highlight: (color: string, isPushpin: boolean) => Opt<Doc> = (color: string, isPushpin: boolean) => undefined;
    public GetAnchor: (savedAnnotations?: ObservableMap<number, HTMLDivElement[]>) => Opt<Doc> = () => undefined;
    public Delete: () => void = unimplementedFunction;
    public AddTag: (key: string, value: string) => boolean = returnFalse;
    public PinToPres: () => void = unimplementedFunction;
    public MakePushpin: () => void = unimplementedFunction;
    public IsPushpin: () => boolean = returnFalse;
    public get Active() {
        return this._left > 0;
    }

    constructor(props: Readonly<{}>) {
        super(props);

        AnchorMenu.Instance = this;
        AnchorMenu.Instance._canFade = false;
    }

    componentDidMount() {
        this._disposer = reaction(
            () => SelectionManager.Views(),
            selected => {
                this._showLinkPopup = false;
                AnchorMenu.Instance.fadeOut(true);
            }
        );
    }

    pointerDown = (e: React.PointerEvent) => {
        setupMoveUpEvents(
            this,
            e,
            (e: PointerEvent) => {
                this.StartDrag(e, this._commentCont.current!);
                return true;
            },
            returnFalse,
            e => this.OnClick?.(e)
        );
    };

    cropDown = (e: React.PointerEvent) => {
        setupMoveUpEvents(
            this,
            e,
            (e: PointerEvent) => {
                this.StartCropDrag(e, this._commentCont.current!);
                return true;
            },
            returnFalse,
            e => this.OnCrop?.(e)
        );
    };

    @action
    highlightClicked = (e: React.MouseEvent) => {
        if (!this.Highlight(this.highlightColor, false) && this.Pinned) {
            this.Highlighting = !this.Highlighting;
        }
        AnchorMenu.Instance.fadeOut(true);
    };

    @action
    toggleLinkPopup = (e: React.MouseEvent) => {
        //ignore the potential null type error because this method cannot be called unless the user selects text and clicks the link button
        //change popup visibility field to visible
        this._showLinkPopup = !this._showLinkPopup;
    };

    @computed get highlighter() {
        const button = (
            <button className="antimodeMenu-button anchor-color-preview-button" title="" key="highlighter-button" onClick={this.highlightClicked}>
                <div className="anchor-color-preview">
                    <FontAwesomeIcon icon="highlighter" size="lg" style={{ transition: 'transform 0.1s', transform: this.Highlighting ? '' : 'rotate(-45deg)' }} />
                    <div className="color-preview" style={{ backgroundColor: this.highlightColor }}></div>
                </div>
            </button>
        );

        const dropdownContent = (
            <div className="dropdown">
                <p>Change highlighter color:</p>
                <div className="color-wrapper">
                    {this._palette.map(color => {
                        if (color) {
                            return this.highlightColor === color ? (
                                <button className="color-button active" key={`active ${color}`} style={{ backgroundColor: color }} onPointerDown={e => this.changeHighlightColor(color, e)}></button>
                            ) : (
                                <button className="color-button" key={`inactive ${color}`} style={{ backgroundColor: color }} onPointerDown={e => this.changeHighlightColor(color, e)}></button>
                            );
                        }
                    })}
                </div>
            </div>
        );
        return (
            <Tooltip key="highlighter" title={<div className="dash-tooltip">{'Click to Highlight'}</div>}>
                <div className="anchorMenu-highlighter">
                    <ButtonDropdown key={'highlighter'} button={button} dropdownContent={dropdownContent} pdf={true} />
                </div>
            </Tooltip>
        );
    }

    @action changeHighlightColor = (color: string, e: React.PointerEvent) => {
        const col: ColorState = {
            hex: color,
            hsl: { a: 0, h: 0, s: 0, l: 0, source: '' },
            hsv: { a: 0, h: 0, s: 0, v: 0, source: '' },
            rgb: { a: 0, r: 0, b: 0, g: 0, source: '' },
            oldHue: 0,
            source: '',
        };
        e.preventDefault();
        e.stopPropagation();
        this.highlightColor = Utils.colorString(col);
    };

    @action keyChanged = (e: React.ChangeEvent<HTMLInputElement>) => {
        this._keyValue = e.currentTarget.value;
    };
    @action valueChanged = (e: React.ChangeEvent<HTMLInputElement>) => {
        this._valueValue = e.currentTarget.value;
    };
    @action addTag = (e: React.PointerEvent) => {
        if (this._keyValue.length > 0 && this._valueValue.length > 0) {
            this._added = this.AddTag(this._keyValue, this._valueValue);
            setTimeout(
                action(() => (this._added = false)),
                1000
            );
        }
    };

    render() {
        const buttons =
            this.Status === 'marquee'
                ? [
                      this.highlighter,

                      <Tooltip key="annotate" title={<div className="dash-tooltip">{'Drag to Place Annotation'}</div>}>
                          <button className="antimodeMenu-button annotate" ref={this._commentCont} onPointerDown={this.pointerDown} style={{ cursor: 'grab' }}>
                              <FontAwesomeIcon icon="comment-alt" size="lg" />
                          </button>
                      </Tooltip>,
                      //NOTE: link popup is currently in progress
                      <Tooltip key="link" title={<div className="dash-tooltip">{'Find document to link to selected text'}</div>}>
                          <button className="antimodeMenu-button link" onPointerDown={this.toggleLinkPopup} style={{}}>
                              <FontAwesomeIcon style={{ position: 'absolute', transform: 'scale(1.5)' }} icon={'search'} size="lg" />
                              <FontAwesomeIcon style={{ position: 'absolute', transform: 'scale(0.5)', transformOrigin: 'top left', top: 12, left: 12 }} icon={'link'} size="lg" />
                          </button>
                      </Tooltip>,
                      <LinkPopup key="popup" showPopup={this._showLinkPopup} linkFrom={this.onMakeAnchor} />,
                      AnchorMenu.Instance.StartCropDrag === unimplementedFunction ? (
                          <></>
                      ) : (
                          <Tooltip key="crop" title={<div className="dash-tooltip">{'Click/Drag to create cropped image'}</div>}>
                              <button className="antimodeMenu-button annotate" onPointerDown={this.cropDown} style={{ cursor: 'grab' }}>
                                  <FontAwesomeIcon icon="image" size="lg" />
                              </button>
                          </Tooltip>
                      ),
                  ]
                : [
                      <Tooltip key="trash" title={<div className="dash-tooltip">{'Remove Link Anchor'}</div>}>
                          <button className="antimodeMenu-button" onPointerDown={this.Delete}>
                              <FontAwesomeIcon icon="trash-alt" size="lg" />
                          </button>
                      </Tooltip>,
                      <Tooltip key="Pin" title={<div className="dash-tooltip">{'Pin to Presentation'}</div>}>
                          <button className="antimodeMenu-button" onPointerDown={this.PinToPres}>
                              <FontAwesomeIcon icon="map-pin" size="lg" />
                          </button>
                      </Tooltip>,
                      <Tooltip key="pushpin" title={<div className="dash-tooltip">{'toggle pushpin behavior'}</div>}>
                          <button className="antimodeMenu-button" style={{ color: this.IsPushpin() ? 'black' : 'white', backgroundColor: this.IsPushpin() ? 'white' : 'black' }} onPointerDown={this.MakePushpin}>
                              <FontAwesomeIcon icon="thumbtack" size="lg" />
                          </button>
                      </Tooltip>,
                      // <div key="7" className="anchorMenu-addTag" >
                      //     <input onChange={this.keyChanged} placeholder="Key" style={{ gridColumn: 1 }} />
                      //     <input onChange={this.valueChanged} placeholder="Value" style={{ gridColumn: 3 }} />
                      // </div>,
                      // <button key="8" className="antimodeMenu-button" title={`Add tag: ${this._keyValue} with value: ${this._valueValue}`} onPointerDown={this.addTag}>
                      //     <FontAwesomeIcon style={{ transition: "all .2s" }} color={this._added ? "#42f560" : "white"} icon="check" size="lg" /></button>,
                  ];

        return this.getElement(buttons);
    }
}