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
|
import { Colors, IconButton } from 'browndash-components';
import { action, computed, makeObservable, observable, reaction } from 'mobx';
import { observer } from 'mobx-react';
import React from 'react';
import { Doc } from '../../fields/Doc';
import { DocData } from '../../fields/DocSymbols';
import { List } from '../../fields/List';
import { DragManager, SetupDrag } from '../util/DragManager';
import { SnappingManager } from '../util/SnappingManager';
import { DocumentView } from './nodes/DocumentView';
import { ObservableReactComponent } from './ObservableReactComponent';
interface KeywordItemProps {
doc: Doc;
label: string;
setToEditing: () => void;
isEditing: boolean;
}
@observer
export class KeywordItem extends ObservableReactComponent<KeywordItemProps> {
constructor(props: any) {
super(props);
makeObservable(this);
this.ref = React.createRef();
}
private _dropDisposer?: DragManager.DragDropDisposer;
private ref: React.RefObject<HTMLDivElement>;
protected createDropTarget = (ele: HTMLDivElement) => {
this._dropDisposer?.();
SetupDrag(this.ref, () => undefined);
//ele && (this._dropDisposer = DragManager. (ele, this.onInternalDrop.bind(this), this.layoutDoc));
//ele && (this._dropDisposer = DragManager.MakeDropTarget(ele, this.onInternalDrop.bind(this), this.layoutDoc));
};
@action
removeLabel = () => {
if (this._props.doc[DocData].data_labels) {
this._props.doc[DocData].data_labels = (this._props.doc[DocData].data_labels as List<string>).filter(label => label !== this._props.label) as List<string>;
this._props.doc![DocData][`${this._props.label}`] = false;
}
};
render() {
return (
<div className="keyword" onClick={this._props.setToEditing} onPointerDown={() => {}} ref={this.ref}>
{this._props.label}
{this.props.isEditing && <IconButton tooltip={'Remove label'} onPointerDown={this.removeLabel} icon={'X'} style={{ width: '8px', height: '8px', marginLeft: '10px' }} />}
</div>
);
}
}
interface KeywordBoxProps {
doc: Doc;
isEditing: boolean;
}
@observer
export class KeywordBox extends ObservableReactComponent<KeywordBoxProps> {
@observable _currentInput: string = '';
//private disposer: () => void;
constructor(props: any) {
super(props);
makeObservable(this);
}
// componentDidMount(): void {
// reaction(
// () => ({
// isDragging: SnappingManager.IsDragging,
// selectedDoc: DocumentView.SelectedDocs().lastElement(),
// isEditing: this._props.isEditing,
// }),
// ({ isDragging, selectedDoc, isEditing }) => {
// if (isDragging || selectedDoc !== this._props.doc || !isEditing) {
// this.setToView();
// }
// }
// );
// }
// componentWillUnmount() {
// this.disposer();
// }
@action
setToEditing = () => {
this._props.isEditing = true;
};
@action
setToView = () => {
this._props.isEditing = false;
};
submitLabel = () => {
if (this._currentInput.trim()) {
if (!this._props.doc[DocData].data_labels) {
this._props.doc[DocData].data_labels = new List<string>();
}
(this._props.doc![DocData].data_labels! as List<string>).push(this._currentInput.trim());
this._props.doc![DocData][`${this._currentInput}`] = true;
this._currentInput = ''; // Clear the input box
}
};
@action
onInputChange = (e: React.ChangeEvent<HTMLInputElement>) => {
this._currentInput = e.target.value;
};
render() {
const keywordsList = this._props.doc[DocData].data_labels ? this._props.doc[DocData].data_labels : new List<string>();
const seldoc = DocumentView.SelectedDocs().lastElement();
if (SnappingManager.IsDragging || !(seldoc === this._props.doc) || !this._props.isEditing) {
setTimeout(
action(() => {
if ((keywordsList as List<string>).length === 0) {
this._props.doc[DocData].showLabels = false;
}
this.setToView();
})
);
}
return (
<div className="keywords-container" style={{ backgroundColor: this._props.isEditing ? Colors.LIGHT_GRAY : Colors.TRANSPARENT, borderColor: this._props.isEditing ? Colors.BLACK : Colors.TRANSPARENT }}>
<div className="keywords-list">
{(keywordsList as List<string>).map(label => {
return <KeywordItem doc={this._props.doc} label={label} setToEditing={this.setToEditing} isEditing={this._props.isEditing}></KeywordItem>;
})}
</div>
{this._props.isEditing ? (
<div className="keyword-editing-box">
<div className="keyword-input-box">
<input
value={this._currentInput}
autoComplete="off"
onChange={this.onInputChange}
onKeyDown={e => {
e.key === 'Enter' ? this.submitLabel() : null;
e.stopPropagation();
}}
type="text"
placeholder="Input keywords for document..."
aria-label="keyword-input"
className="keyword-input"
style={{ width: '100%', borderRadius: '5px' }}
/>
</div>
<div className="keyword-buttons">
<IconButton
tooltip={'Close Menu'}
onPointerDown={() => {
if ((keywordsList as List<string>).length === 0) {
this._props.doc[DocData].showLabels = false;
} else {
this.setToView();
}
}}
icon={'x'}
style={{ width: '4px' }}
/>
</div>
</div>
) : (
<div></div>
)}
</div>
);
}
}
|