import { 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 { DocCast, NumCast } from '../../fields/Types'; import { emptyFunction } from '../../Utils'; import { Docs } from '../documents/Documents'; import { DocUtils } from '../documents/DocUtils'; import { DragManager, SetupDrag } from '../util/DragManager'; import { SnappingManager } from '../util/SnappingManager'; import { CollectionFreeFormView } from './collections/collectionFreeForm'; import { MainView } from './MainView'; import { DocumentView } from './nodes/DocumentView'; import { ObservableReactComponent } from './ObservableReactComponent'; interface KeywordItemProps { doc: Doc; keyword: string; keywordDoc: Doc; keywordCollection: Doc[]; setToEditing: () => void; isEditing: boolean; } @observer export class KeywordItem extends ObservableReactComponent { constructor(props: any) { super(props); makeObservable(this); this.ref = React.createRef(); } private ref: React.RefObject; getKeywordCollectionDocs = () => { for (const doc of DocListCast(Doc.UserDoc().myKeywordCollections)) { if (doc.title === this._props.keyword) { return doc[DocData].docs; } } return null; }; createCollection = () => { const selected = DocListCast(this.getKeywordCollectionDocs()!); const newEmbeddings = selected.map(doc => Doc.MakeEmbedding(doc)); const newCollection = ((doc: Doc) => { const docData = doc[DocData]; docData.data = new List(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; //newCollection[DocData].smartCollection = this._props.keywordDoc; this._props.keywordDoc.collections = new List([...DocListCast(this._props.keywordDoc.collections), newCollection]); 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) { const filtered_docs = new List(DocListCast(this.getKeywordCollectionDocs()!).filter(doc => doc !== this._props.doc)); this._props.keywordDoc[DocData].docs = filtered_docs; this._props.doc[DocData].data_labels = (this._props.doc[DocData].data_labels as List).filter(label => label !== this._props.keyword) as List; this._props.doc![DocData][`${this._props.keyword}`] = false; for (const collection of DocListCast(this._props.keywordDoc.collections)) { collection[DocData].data = new List(DocListCast(collection[DocData].data).filter(doc => !Doc.AreProtosEqual(this._props.doc, doc))); } } }; render() { return (
{this._props.keyword} {this.props.isEditing && }
); } } interface KeywordBoxProps { doc: Doc; isEditing: boolean; } @observer export class KeywordBox extends ObservableReactComponent { @observable _currentInput: string = ''; private height: number = 0; private ref: React.RefObject; @computed get currentScale() { return NumCast((this._props.doc.embedContainer as Doc)?._freeform_scale, 1); } constructor(props: any) { super(props); makeObservable(this); this.ref = React.createRef(); } componentDidMount(): void { this.height = this.ref.current?.getBoundingClientRect().height ? this.ref.current?.getBoundingClientRect().height : 0; this._props.doc._keywordHeight = this.height; reaction( () => this.currentScale, () => { if (this.currentScale < 1) { this.height = this.ref.current?.getBoundingClientRect().height ? this.ref.current?.getBoundingClientRect().height : 0; this._props.doc._keywordHeight = this.height; } } ); } componentDidUpdate(prevProps: Readonly): void { this.height = this.ref.current?.getBoundingClientRect().height ? this.ref.current?.getBoundingClientRect().height : 0; this._props.doc._keywordHeight = this.height; } @action setToEditing = () => { this._props.isEditing = true; }; @action setToView = () => { this._props.isEditing = false; }; getKeywordCollection = (keyword: string) => { for (const doc of DocListCast(Doc.UserDoc().myKeywordCollections)) { if (doc.title === keyword) { return doc; } } const keywordCollection = new Doc(); keywordCollection.title = keyword; keywordCollection[DocData].docs = new List(); keywordCollection.collections = new List(); Doc.UserDoc().myKeywordCollections = new List([...DocListCast(Doc.UserDoc().myKeywordCollections), keywordCollection]); return keywordCollection; }; submitLabel = () => { if (!Doc.UserDoc().myKeywordCollections) { Doc.UserDoc().myKeywordCollections = new List(); } const submittedLabel = this._currentInput.trim(); if (submittedLabel) { // If the keyword collection is not in the user doc, 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(); } // Add this document to the keyword's collection of associated documents. keywordCollection[DocData].docs = new List([...DocListCast(keywordCollection[DocData].docs), this._props.doc]); // Push the keyword to the document's keyword list field. (this._props.doc![DocData].data_labels! as List).push(submittedLabel); this._props.doc![DocData][`${this._currentInput}`] = true; // 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([...DocListCast(collection.data), newEmbedding]); newEmbedding.embedContainer = collection; } this._currentInput = ''; // Clear the input box } }; @action onInputChange = (e: React.ChangeEvent) => { this._currentInput = e.target.value; }; render() { const keywordsList = this._props.doc[DocData].data_labels ? this._props.doc[DocData].data_labels : new List(); const seldoc = DocumentView.SelectedDocs().lastElement(); if (SnappingManager.IsDragging || !(seldoc === this._props.doc) || !this._props.isEditing) { setTimeout( action(() => { // if ((keywordsList as List).length === 0) { // this._props.doc[DocData].showLabels = false; // } this.setToView(); }) ); } return (
{(keywordsList as List).map(keyword => { return ( ); })}
{this._props.isEditing ? (
{ 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' }} />
{ if ((keywordsList as List).length === 0) { this._props.doc[DocData].showLabels = false; } else { this.setToView(); } }} icon={'x'} style={{ width: '4px' }} />
) : (
)}
); } }