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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
|
import { Button, Colors, IconButton } from 'browndash-components';
import { action, computed, makeObservable, observable, reaction } from 'mobx';
import { observer } from 'mobx-react';
import React from 'react';
import { returnFalse, setupMoveUpEvents } from '../../ClientUtils';
import { Doc, DocListCast } from '../../fields/Doc';
import { DocData } from '../../fields/DocSymbols';
import { List } from '../../fields/List';
import { NumCast, StrCast } from '../../fields/Types';
import { emptyFunction } from '../../Utils';
import { DocumentType } from '../documents/DocumentTypes';
import { DragManager } from '../util/DragManager';
import { SnappingManager } from '../util/SnappingManager';
import { DocumentView } from './nodes/DocumentView';
import { ObservableReactComponent } from './ObservableReactComponent';
interface KeywordItemProps {
doc: Doc;
keyword: string;
keywordDoc: Doc;
setToEditing: () => void;
isEditing: boolean;
}
/**
* A component that handles individual keywords.
*/
@observer
export class KeywordItem extends ObservableReactComponent<KeywordItemProps> {
constructor(props: any) {
super(props);
makeObservable(this);
this.ref = React.createRef();
}
private ref: React.RefObject<HTMLDivElement>;
/**
* Gets the documents that a keyword is associated with.
* @returns An array of documents that contain the keyword.
*/
getKeywordCollectionDocs = () => {
for (const doc of DocListCast(Doc.ActiveDashboard?.myKeywordCollections)) {
if (doc.title === this._props.keyword) {
return doc[DocData].docs;
}
}
return null;
};
/**
* Creates a smart collection.
* @returns
*/
createCollection = () => {
// Get the documents that contain the keyword.
const selected = DocListCast(this.getKeywordCollectionDocs()!);
const newEmbeddings = selected.map(doc => Doc.MakeEmbedding(doc));
// Create a new collection and set up configurations.
const newCollection = ((doc: Doc) => {
const docData = doc[DocData];
docData.data = new List<Doc>(newEmbeddings);
docData.title = this._props.keyword;
doc._freeform_panX = doc._freeform_panY = 0;
return doc;
})(Doc.MakeCopy(Doc.UserDoc().emptyCollection as Doc, true));
newEmbeddings.forEach(embed => (embed.embedContainer = newCollection));
newCollection._width = 900;
newCollection._height = 900;
newCollection.layout_fitWidth = true;
// Add the collection to the keyword document's list of associated smart collections.
this._props.keywordDoc.collections = new List<Doc>([...DocListCast(this._props.keywordDoc.collections), newCollection]);
newCollection[DocData].data_labels = new List<string>([this._props.keyword]);
newCollection[DocData][`${this._props.keyword}`] = true;
newCollection[DocData].showLabels = true;
return newCollection;
};
@action
handleDragStart = (e: React.PointerEvent) => {
if (this._props.isEditing) {
const clone = this.ref.current?.cloneNode(true) as HTMLElement;
if (!clone) return;
setupMoveUpEvents(
this,
e,
() => {
const dragData = new DragManager.DocumentDragData([this.createCollection()]);
DragManager.StartDocumentDrag([this.ref.current!], dragData, e.clientX, e.clientY, {});
return true;
},
returnFalse,
emptyFunction
);
e.preventDefault();
}
};
@action
removeLabel = () => {
if (this._props.doc[DocData].data_labels) {
if (this._props.doc.type === DocumentType.COL) {
const filtered_collections = new List<Doc>(DocListCast(this._props.keywordDoc.collections).filter(doc => doc !== this._props.doc));
this._props.keywordDoc.collections = filtered_collections;
for (const cur_doc of DocListCast(this.getKeywordCollectionDocs()!, [])) {
this._props.doc[DocData].data = new List<Doc>(DocListCast(this._props.doc[DocData].data).filter(doc => !Doc.AreProtosEqual(cur_doc, doc)));
}
} else {
const filtered_docs = new List<Doc>(DocListCast(this.getKeywordCollectionDocs()!).filter(doc => doc !== this._props.doc));
this._props.keywordDoc[DocData].docs = filtered_docs;
for (const collection of DocListCast(this._props.keywordDoc.collections)) {
collection[DocData].data = new List<Doc>(DocListCast(collection[DocData].data).filter(doc => !Doc.AreProtosEqual(this._props.doc, doc)));
}
}
}
this._props.doc[DocData].data_labels = (this._props.doc[DocData].data_labels as List<string>).filter(label => label !== this._props.keyword) as List<string>;
this._props.doc![DocData][`${this._props.keyword}`] = false;
};
render() {
return (
<div className="keyword" onClick={this._props.setToEditing} onPointerDown={this.handleDragStart} ref={this.ref}>
{this._props.keyword}
{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;
}
/**
* A component that handles the keyword display for documents.
*/
@observer
export class KeywordBox extends ObservableReactComponent<KeywordBoxProps> {
@observable _currentInput: string = '';
private height: number = 0;
private ref: React.RefObject<HTMLDivElement>;
@computed
get currentScale() {
return NumCast((this._props.doc.embedContainer as Doc)?._freeform_scale, 1);
}
@computed
get cur_height() {
return this.ref.current?.offsetHeight ? this.ref.current?.offsetHeight : 0;
}
constructor(props: any) {
super(props);
makeObservable(this);
this.ref = React.createRef();
reaction(
() => this.cur_height,
() => {
this._props.doc[DocData].keywordHeight = this.height;
}
);
}
componentDidMount(): void {
this.height = this.ref.current?.offsetHeight ? this.ref.current?.offsetHeight : 0;
this._props.doc[DocData].keywordHeight = this.height;
}
componentDidUpdate(prevProps: Readonly<KeywordBoxProps>): void {
this.height = this.ref.current?.offsetHeight ? this.ref.current?.offsetHeight : 0;
this._props.doc[DocData].keywordHeight = this.height;
}
@action
setToEditing = () => {
this._props.isEditing = true;
};
@action
setToView = () => {
this._props.isEditing = false;
};
/**
* Gets the document associated with a keyword.
* @param keyword The keyword being searched for
* @returns A Doc containing keyword information
*/
getKeywordCollection = (keyword: string) => {
// Look for the keyword document.
for (const doc of DocListCast(Doc.ActiveDashboard!.myKeywordCollections)) {
if (doc.title === keyword) {
return doc;
}
}
// If not contained, create a new document and add it to the active Dashboard's keyword list.
const keywordCollection = new Doc();
keywordCollection.title = keyword;
keywordCollection[DocData].docs = new List<Doc>();
keywordCollection.collections = new List<Doc>();
if (Doc.ActiveDashboard) {
Doc.ActiveDashboard.myKeywordCollections = new List<Doc>([...DocListCast(Doc.ActiveDashboard.myKeywordCollections), keywordCollection]);
}
return keywordCollection;
};
/**
* Adds the keyword to the document.
* @param keyword
*/
submitLabel = (keyword: string) => {
// If the active Dashboard does not have a keyword collection, create it.
if (Doc.ActiveDashboard && !Doc.ActiveDashboard.myKeywordCollections) {
Doc.ActiveDashboard.myKeywordCollections = new List<Doc>();
}
const submittedLabel = keyword.trim();
if (submittedLabel && !this._props.doc![DocData][`${submittedLabel}`]) {
// If the keyword collection is not in active Dashboard, add it as a new doc, with the keyword as its title.
const keywordCollection = this.getKeywordCollection(submittedLabel);
// If the document has no keywords field, create the field.
if (!this._props.doc[DocData].data_labels) {
this._props.doc[DocData].data_labels = new List<string>();
}
// If the document is of type COLLECTION, make it a smart collection, otherwise, add the keyword to the document.
if (this._props.doc.type === DocumentType.COL) {
keywordCollection.collections = new List<Doc>([...DocListCast(keywordCollection.collections), this._props.doc]);
// Iterate through the keyword document's collections and add a copy of the document to each collection
for (const doc of DocListCast(keywordCollection[DocData].docs)) {
const newEmbedding = Doc.MakeEmbedding(doc);
this._props.doc[DocData].data = new List<Doc>([...DocListCast(this._props.doc[DocData].data), newEmbedding]);
newEmbedding.embedContainer = this._props.doc;
}
} else {
// Add this document to the keyword's collection of associated documents.
keywordCollection[DocData].docs = new List<Doc>([...DocListCast(keywordCollection[DocData].docs), this._props.doc]);
// Iterate through the keyword document's collections and add a copy of the document to each collection
for (const collection of DocListCast(keywordCollection.collections)) {
const newEmbedding = Doc.MakeEmbedding(this._props.doc);
collection[DocData].data = new List<Doc>([...DocListCast(collection.data), newEmbedding]);
newEmbedding.embedContainer = collection;
}
}
// Push the keyword to the document's keyword list field.
(this._props.doc![DocData].data_labels! as List<string>).push(submittedLabel);
this._props.doc![DocData][`${submittedLabel}`] = 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"
ref={this.ref}
style={{
transformOrigin: 'top left',
overflow: 'hidden',
transform: `scale(${1 / this.currentScale})`,
backgroundColor: this._props.isEditing ? Colors.LIGHT_GRAY : Colors.TRANSPARENT,
borderColor: this._props.isEditing ? Colors.BLACK : Colors.TRANSPARENT,
maxWidth: `400px`,
}}>
<div className="keywords-content">
<div className="keywords-list">
{(keywordsList as List<string>).map(keyword => {
return <KeywordItem doc={this._props.doc} keyword={keyword} keywordDoc={this.getKeywordCollection(keyword)} 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(this._currentInput) : null;
e.stopPropagation();
}}
type="text"
placeholder="Input keywords for document..."
aria-label="keyword-input"
className="keyword-input"
style={{ width: '100%', borderRadius: '5px' }}
/>
</div>
{Doc.ActiveDashboard?.myKeywordCollections ? (
<div className="keyword-suggestions-box">
{DocListCast(Doc.ActiveDashboard?.myKeywordCollections).map(doc => {
const keyword = StrCast(doc.title);
return (
<Button
style={{ margin: '2px 2px', border: '1px solid black', backgroundColor: 'lightblue', color: 'black' }}
text={keyword}
color={SnappingManager.userVariantColor}
tooltip={'Add existing keyword'}
onClick={() => {
this.submitLabel(keyword);
}}
/>
);
})}
</div>
) : (
<div></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>
</div>
);
}
}
|