/* eslint-disable react/no-unused-prop-types */ import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'; import { action, computed, observable } from 'mobx'; import { observer } from 'mobx-react'; import * as React from 'react'; import { returnEmptyDoclist, returnEmptyFilter, returnFalse, returnZero, setupMoveUpEvents } from '../../../../ClientUtils'; import { emptyFunction } from '../../../../Utils'; import { Colors } from '../../global/globalEnums'; import './CollectionSchemaView.scss'; import { EditableView } from '../../EditableView'; import { ObservableReactComponent } from '../../ObservableReactComponent'; import { DefaultStyleProvider, returnEmptyDocViewList } from '../../StyleProvider'; import { FieldViewProps } from '../../nodes/FieldView'; import { Doc, Field } from '../../../../fields/Doc'; import { dropActionType } from '../../../util/DropActionTypes'; import { Transform } from '../../../util/Transform'; import { SchemaTableCell } from './SchemaTableCell'; import { DocCast } from '../../../../fields/Types'; import { computedFn } from 'mobx-utils'; import { CollectionSchemaView } from './CollectionSchemaView'; import { SnappingManager } from '../../../util/SnappingManager'; import { undoable } from '../../../util/UndoManager'; import { FInfo } from '../../../documents/Documents'; import { ColumnType } from '../../../../fields/SchemaHeaderField'; export interface SchemaColumnHeaderProps { Document: Doc; autoFocus?: boolean; columnKeys: string[]; columnWidths: number[]; columnIndex: number; sortField: string; sortDesc: boolean; schemaView: CollectionSchemaView; //cleanupField: (s: string) => string; isContentActive: (outsideReaction?: boolean | undefined) => boolean | undefined; setSort: (field: string | undefined, desc?: boolean) => void; removeColumn: (index: number) => void; rowHeight: () => number; resizeColumn: (e: any, index: number) => void; dragColumn: (e: any, index: number) => boolean; openContextMenu: (x: number, y: number, index: number, fieldType: ColumnType) => void; setColRef: (index: number, ref: HTMLDivElement) => void; rootSelected?: () => boolean; columnWidth: () => number; finishEdit?: () => void; // notify container that edit is over (eg. to hide view in DashFieldView) //transform: () => Transform; } @observer export class SchemaColumnHeader extends ObservableReactComponent { @observable _editing: boolean | undefined = false; @observable _fieldType: ColumnType = ColumnType.String; @computed get fieldKey() { return this._props.columnKeys[this._props.columnIndex]; } getFinfo = computedFn((fieldKey: string) => this._props.schemaView?.fieldInfos.get(fieldKey)); setColumnValues = (field: string, defaultValue: string) => {this._props.schemaView?.setKey(field, defaultValue, this._props.columnIndex);} @action sortClicked = (e: React.PointerEvent) => { e.stopPropagation(); e.preventDefault(); if (this._props.sortField === this.fieldKey && this._props.sortDesc) { this._props.setSort(undefined); } else if (this._props.sortField === this.fieldKey) { this._props.setSort(this.fieldKey, true); } else { this._props.setSort(this.fieldKey, false); } }; @action setupDrag = (e: React.PointerEvent) => { this._props.isContentActive(true) && setupMoveUpEvents(this, e, moveEv => this._props.dragColumn(moveEv, this._props.columnIndex), emptyFunction, emptyFunction); }; renderProps = (props: SchemaColumnHeaderProps) => { const { columnKeys, columnWidth, Document } = props; const fieldKey = columnKeys[props.columnIndex]; const color = 'black'; // color of text in cells const fieldProps: FieldViewProps = { childFilters: returnEmptyFilter, childFiltersByRanges: returnEmptyFilter, docViewPath: returnEmptyDocViewList, searchFilterDocs: returnEmptyDoclist, styleProvider: DefaultStyleProvider, isSelected: returnFalse, setHeight: returnFalse, select: emptyFunction, dragAction: dropActionType.move, renderDepth: 1, noSidebar: true, isContentActive: returnFalse, whenChildContentsActiveChanged: emptyFunction, ScreenToLocalTransform: Transform.Identity, focus: emptyFunction, addDocTab: SchemaTableCell.addFieldDoc, pinToPres: returnZero, Document: DocCast(Document.rootDocument, Document), fieldKey: fieldKey, PanelWidth: columnWidth, PanelHeight: props.rowHeight, rootSelected: props.rootSelected, }; const readOnly = this.getFinfo(fieldKey)?.readOnly ?? false; const cursor = !readOnly ? 'text' : 'default'; const pointerEvents: 'all' | 'none' = 'all'; return { color, fieldProps, cursor, pointerEvents }; } @computed get editableView() { const { color, fieldProps, pointerEvents } = this.renderProps(this._props); return
this._props.autoFocus && r?.setIsFocused(true)} oneLine={true} allowCRs={false} contents={undefined} fieldContents={fieldProps} editing={undefined} showKeyNotVal={true} // tells the EditableView to display the fieldKey itself, and not its value GetValue={() => this.fieldKey} SetValue={undoable((value: string, shiftKey?: boolean, enterKey?: boolean) => { if (shiftKey && enterKey) { // if shift & enter, set value of each cell in column this.setColumnValues(value, value); this._props.finishEdit?.(); return true; } this._props.finishEdit?.(); // else save new value to header field return true; }, 'edit column header')} />
} // staticView = () => { // return
{this._editing = true; console.log(this._editing)}}>{this.fieldKey}
// } render() { return (
{ // console.log(true); // this._editing = true}} // onPointerLeave={() => { // console.log(false); // this._editing = false}} ref={col => { if (col) { this._props.setColRef(this._props.columnIndex, col); } }}>
this._props.resizeColumn(e, this._props.columnIndex)} />
{this.editableView}
this._props.openContextMenu(e.clientX, e.clientY, this._props.columnIndex, this._fieldType)}>
); } }