aboutsummaryrefslogtreecommitdiff
path: root/src/client/views/linking/LinkEditor.tsx
blob: 219f7d3a21ed4243e934d2e42198d4751ef2a6c5 (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
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
import { Tooltip } from "@material-ui/core";
import { action, computed, observable } from "mobx";
import { observer } from "mobx-react";
import { Doc, StrListCast, Field } from "../../../fields/Doc";
import { DateCast, StrCast, Cast } from "../../../fields/Types";
import { LinkManager } from "../../util/LinkManager";
import { undoBatch } from "../../util/UndoManager";
import './LinkEditor.scss';
import { LinkRelationshipSearch } from "./LinkRelationshipSearch";
import React = require("react");
import { ToString } from "../../../fields/FieldSymbols";


interface LinkEditorProps {
    sourceDoc: Doc;
    linkDoc: Doc;
    showLinks: () => void;
    hideback?: boolean;
}
@observer
export class LinkEditor extends React.Component<LinkEditorProps> {

    @observable description = Field.toString(LinkManager.currentLink?.description as any as Field);
    @observable relationship = StrCast(LinkManager.currentLink?.linkRelationship);
    @observable openDropdown: boolean = false;
    @observable showInfo: boolean = false;
    @computed get infoIcon() { if (this.showInfo) { return "chevron-up"; } return "chevron-down"; }
    @observable private buttonColor: string = "";
    @observable private relationshipButtonColor: string = "";
    @observable private relationshipSearchVisibility: string = "none";
    @observable private searchIsActive: boolean = false;

    //@observable description = this.props.linkDoc.description ? StrCast(this.props.linkDoc.description) : "DESCRIPTION";

    @undoBatch
    deleteLink = (): void => {
        LinkManager.Instance.deleteLink(this.props.linkDoc);
        this.props.showLinks();
    }

    @undoBatch
    setRelationshipValue = action((value: string) => {
        if (LinkManager.currentLink) {
            Doc.GetProto(LinkManager.currentLink).linkRelationship = value;
            const linkRelationshipList = StrListCast(Doc.UserDoc().linkRelationshipList);
            const linkColorList = StrListCast(Doc.UserDoc().linkColorList);
            // if the relationship does not exist in the list, add it and a corresponding unique randomly generated color 
            if (linkRelationshipList && !linkRelationshipList.includes(value)) {
                linkRelationshipList.push(value);
                const randColor = "rgb(" + Math.floor(Math.random() * 255) + "," + Math.floor(Math.random() * 255) + "," + Math.floor(Math.random() * 255) + ")";
                linkColorList.push(randColor);
            }
            this.relationshipButtonColor = "rgb(62, 133, 55)";
            setTimeout(action(() => this.relationshipButtonColor = ""), 750);
            return true;
        }
    });

    /**
     * returns list of strings with possible existing relationships that contain what is currently in the input field
     */
    @action
    getRelationshipResults = () => {
        const query = this.relationship; //current content in input box
        const linkRelationshipList = StrListCast(Doc.UserDoc().linkRelationshipList);
        if (linkRelationshipList) {
            return linkRelationshipList.filter(rel => rel.includes(query));
        }
    }

    /**
     * toggles visibility of the relationship search results when the input field is focused on
     */
    @action
    toggleRelationshipResults = () => {
        this.relationshipSearchVisibility = this.relationshipSearchVisibility === "none" ? "block" : "none";
    }

    @undoBatch
    setDescripValue = action((value: string) => {
        if (LinkManager.currentLink) {
            Doc.GetProto(LinkManager.currentLink).description = value;
            this.buttonColor = "rgb(62, 133, 55)";
            setTimeout(action(() => this.buttonColor = ""), 750);
            return true;
        }
    });

    onDescriptionKey = (e: React.KeyboardEvent<HTMLInputElement>) => {
        if (e.key === "Enter") {
            this.setDescripValue(this.description);
            document.getElementById('input')?.blur();
        }
    }

    onRelationshipKey = (e: React.KeyboardEvent<HTMLInputElement>) => {
        if (e.key === "Enter") {
            this.setRelationshipValue(this.relationship);
            document.getElementById('input')?.blur();
        }
    }

    onDescriptionDown = () => this.setDescripValue(this.description);
    onRelationshipDown = () => this.setRelationshipValue(this.relationship);

    onBlur = () => {
        //only hide the search results if the user clicks out of the input AND not on any of the search results 
        // i.e. if search is not active
        if (!this.searchIsActive) {
            this.toggleRelationshipResults();
        }
    }
    onFocus = () => {
        this.toggleRelationshipResults();
    }
    toggleSearchIsActive = () => {
        this.searchIsActive = !this.searchIsActive;
    }

    @action
    handleDescriptionChange = (e: React.ChangeEvent<HTMLInputElement>) => { this.description = e.target.value; }
    @action
    handleRelationshipChange = (e: React.ChangeEvent<HTMLInputElement>) => {
        this.relationship = e.target.value;
    }
    @action
    handleRelationshipSearchChange = (result: string) => {
        this.setRelationshipValue(result);
        this.toggleRelationshipResults();
        this.relationship = result;
    }
    @computed
    get editRelationship() {
        //NOTE: confusingly, the classnames for the following relationship JSX elements are the same as the for the description elements for shared CSS
        return <div className="linkEditor-description">
            <div className="linkEditor-description-label">Link Relationship:</div>
            <div className="linkEditor-description-input">
                <div className="linkEditor-description-editing">
                    <input
                        style={{ width: "100%" }}
                        id="input"
                        value={this.relationship}
                        placeholder={"Enter link relationship"}
                        onKeyDown={this.onRelationshipKey}
                        onChange={this.handleRelationshipChange}
                        onFocus={this.onFocus}
                        onBlur={this.onBlur}
                    ></input>
                    <LinkRelationshipSearch
                        results={this.getRelationshipResults()}
                        display={this.relationshipSearchVisibility}
                        handleRelationshipSearchChange={this.handleRelationshipSearchChange}
                        toggleSearch={this.toggleSearchIsActive}
                    />
                </div>
                <div className="linkEditor-description-add-button"
                    style={{ background: this.relationshipButtonColor }}
                    onPointerDown={this.onRelationshipDown}>Set</div>
            </div>
        </div>;
    }

    @computed
    get editDescription() {
        return <div className="linkEditor-description">
            <div className="linkEditor-description-label">Link Description:</div>
            <div className="linkEditor-description-input">
                <div className="linkEditor-description-editing">
                    <input
                        style={{ width: "100%" }}
                        id="input"
                        value={this.description}
                        placeholder={"Enter link description"}
                        onKeyDown={this.onDescriptionKey}
                        onChange={this.handleDescriptionChange}
                    ></input>
                </div>
                <div className="linkEditor-description-add-button"
                    style={{ background: this.buttonColor }}
                    onPointerDown={this.onDescriptionDown}>Set</div>
            </div>
        </div>;
    }

    @action
    changeDropdown = () => { this.openDropdown = !this.openDropdown; }

    @undoBatch
    changeFollowBehavior = action((follow: string) => {
        this.openDropdown = false;
        Doc.GetProto(this.props.linkDoc).followLinkLocation = follow;
    });

    @computed
    get followingDropdown() {
        return <div className="linkEditor-followingDropdown">
            <div className="linkEditor-followingDropdown-label">Follow Behavior:</div>
            <div className="linkEditor-followingDropdown-dropdown">
                <div className="linkEditor-followingDropdown-header"
                    onPointerDown={this.changeDropdown}>
                    {StrCast(this.props.linkDoc.followLinkLocation, "default")}
                    <FontAwesomeIcon className="linkEditor-followingDropdown-icon"
                        icon={this.openDropdown ? "chevron-up" : "chevron-down"}
                        size={"lg"} />
                </div>
                <div className="linkEditor-followingDropdown-optionsList"
                    style={{ display: this.openDropdown ? "" : "none" }}>
                    <div className="linkEditor-followingDropdown-option"
                        onPointerDown={() => this.changeFollowBehavior("default")}>
                        Default
                    </div>
                    <div className="linkEditor-followingDropdown-option"
                        onPointerDown={() => this.changeFollowBehavior("add:left")}>
                        Always open in new left pane
                    </div>
                    <div className="linkEditor-followingDropdown-option"
                        onPointerDown={() => this.changeFollowBehavior("add:right")}>
                        Always open in new right pane
                    </div>
                    <div className="linkEditor-followingDropdown-option"
                        onPointerDown={() => this.changeFollowBehavior("replace:right")}>
                        Always replace right tab
                    </div>
                    <div className="linkEditor-followingDropdown-option"
                        onPointerDown={() => this.changeFollowBehavior("replace:left")}>
                        Always replace left tab
                    </div>
                    <div className="linkEditor-followingDropdown-option"
                        onPointerDown={() => this.changeFollowBehavior("fullScreen")}>
                        Always open full screen
                    </div>
                    <div className="linkEditor-followingDropdown-option"
                        onPointerDown={() => this.changeFollowBehavior("add")}>
                        Always open in a new tab
                    </div>
                    <div className="linkEditor-followingDropdown-option"
                        onPointerDown={() => this.changeFollowBehavior("replace")}>
                        Replace Tab
                    </div>
                    {this.props.linkDoc.linksToAnnotation ?
                        <div className="linkEditor-followingDropdown-option"
                            onPointerDown={() => this.changeFollowBehavior("openExternal")}>
                            Always open in external page
                        </div>
                        : null}
                </div>
            </div>
        </div>;
    }

    @action
    changeInfo = () => { this.showInfo = !this.showInfo; }

    render() {
        const destination = LinkManager.getOppositeAnchor(this.props.linkDoc, this.props.sourceDoc);


        return !destination ? (null) : (
            <div className="linkEditor">
                <div className="linkEditor-info">
                    <Tooltip title={<><div className="dash-tooltip">Return to link menu</div></>} placement="top">
                        <button className="linkEditor-button-back"
                            style={{ display: this.props.hideback ? "none" : "" }}
                            onClick={this.props.showLinks}>
                            <FontAwesomeIcon icon="arrow-left" size="sm" /> </button>
                    </Tooltip>
                    <p className="linkEditor-linkedTo">Editing Link to: <b>{
                        destination.proto?.title ?? destination.title ?? "untitled"}</b></p>
                    <Tooltip title={<><div className="dash-tooltip">Show more link information</div></>} placement="top">
                        <div className="linkEditor-downArrow"><FontAwesomeIcon className="button" icon={this.infoIcon} size="lg" onPointerDown={this.changeInfo} /></div>
                    </Tooltip>
                </div>
                {this.showInfo ? <div className="linkEditor-moreInfo">
                    <div>{this.props.linkDoc.author ? <div> <b>Author:</b> {this.props.linkDoc.author}</div> : null}</div>
                    <div>{this.props.linkDoc.creationDate ? <div> <b>Creation Date:</b>
                        {DateCast(this.props.linkDoc.creationDate).toString()}</div> : null}</div>
                </div> : null}

                {this.editDescription}
                {this.editRelationship}
                {this.followingDropdown}
            </div>

        );
    }
}