aboutsummaryrefslogtreecommitdiff
path: root/src/client/views/collections/collectionSchema/SchemaRowBox.tsx
blob: 760089ffb7545c44d7037a00b566f787af4044ec (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
import { IconButton, Size } from 'browndash-components';
import { computed, makeObservable } from 'mobx';
import { observer } from 'mobx-react';
import { computedFn } from 'mobx-utils';
import * as React from 'react';
import { CgClose, CgLock, CgLockUnlock } from 'react-icons/cg';
import { FaExternalLinkAlt } from 'react-icons/fa';
import { returnFalse, setupMoveUpEvents } from '../../../../ClientUtils';
import { emptyFunction } from '../../../../Utils';
import { Doc } from '../../../../fields/Doc';
import { BoolCast } from '../../../../fields/Types';
import { Transform } from '../../../util/Transform';
import { undoable } from '../../../util/UndoManager';
import { ViewBoxBaseComponent } from '../../DocComponent';
import { Colors } from '../../global/globalEnums';
import { FieldView, FieldViewProps } from '../../nodes/FieldView';
import { OpenWhere } from '../../nodes/OpenWhere';
import { CollectionSchemaView } from './CollectionSchemaView';
import './CollectionSchemaView.scss';
import { SchemaTableCell } from './SchemaTableCell';

interface SchemaRowBoxProps extends FieldViewProps {
    rowIndex: number;
}
@observer
export class SchemaRowBox extends ViewBoxBaseComponent<SchemaRowBoxProps>() {
    public static LayoutString(fieldKey: string, rowIndex: number) {
        return FieldView.LayoutString(SchemaRowBox, fieldKey).replace('fieldKey', `rowIndex={${rowIndex}} fieldKey`);
    }
    private _ref: HTMLDivElement | null = null;

    constructor(props: SchemaRowBoxProps) {
        super(props);
        makeObservable(this);
    }

    bounds = () => this._ref?.getBoundingClientRect();

    @computed get schemaView() {
        return this.DocumentView?.().containerViewPath?.().lastElement()?.ComponentView as CollectionSchemaView;
    }

    @computed get schemaDoc() {
        return this.schemaView.Document;
    }

    @computed get rowIndex() {
        return this.schemaView?.rowIndex(this.Document) ?? -1;
    }

    componentDidMount(): void {
        this._props.setContentViewBox?.(this);
    }

    setCursorIndex = (mouseY: number) => this.schemaView?.setRelCursorIndex(mouseY);
    selectedCol = () => this.schemaView._selectedCol;
    getFinfo = computedFn((fieldKey: string) => this.schemaView?.fieldInfos.get(fieldKey));
    selectCell = (doc: Doc, col: number, shift: boolean, ctrl: boolean) => this.schemaView?.selectCell(doc, col, shift, ctrl);
    deselectCell = () => this.schemaView?.deselectAllCells();
    selectedCells = () => this.schemaView?._selectedDocs;
    setColumnValues = (field: any, value: any) => this.schemaView?.setColumnValues(field, value) ?? false;
    setSelectedColumnValues = (field: any, value: any) => this.schemaView?.setSelectedColumnValues(field, value) ?? false;
    columnWidth = computedFn((index: number) => () => this.schemaView?.displayColumnWidths[index] ?? CollectionSchemaView._minColWidth);
    render() {
        return (
            <div
                className="schema-row"
                onPointerDown={e => this.setCursorIndex(e.clientY)}
                style={{ height: this._props.PanelHeight(), backgroundColor: this._props.isSelected() ? Colors.LIGHT_BLUE : undefined }}
                ref={(row: HTMLDivElement | null) => {
                    row && this.schemaView?.addRowRef?.(this.Document, row);
                    this._ref = row;
                }}>
                <div
                    className="row-menu"
                    style={{
                        width: CollectionSchemaView._rowMenuWidth,
                        pointerEvents: !this._props.isContentActive() ? 'none' : undefined,
                    }}>
                    <IconButton
                        tooltip="close"
                        icon={<CgClose size="16px" />}
                        size={Size.XSMALL}
                        onPointerDown={e =>
                            setupMoveUpEvents(
                                this,
                                e,
                                returnFalse,
                                emptyFunction,
                                undoable(clickEv => {
                                    clickEv.stopPropagation();
                                    this._props.removeDocument?.(this.Document);
                                }, 'Delete Row')
                            )
                        }
                    />
                    <IconButton
                        tooltip="whether document interactions are enabled"
                        icon={this.Document._lockedPosition ? <CgLockUnlock size="12px" /> : <CgLock size="12px" />}
                        size={Size.XSMALL}
                        onPointerDown={e =>
                            setupMoveUpEvents(
                                this,
                                e,
                                returnFalse,
                                emptyFunction,
                                undoable(clickEv => {
                                    clickEv.stopPropagation();
                                    Doc.toggleLockedPosition(this.Document);
                                }, 'toggle document lock')
                            )
                        }
                    />
                    <IconButton
                        tooltip="open preview"
                        icon={<FaExternalLinkAlt />}
                        size={Size.XSMALL}
                        onPointerDown={e =>
                            setupMoveUpEvents(
                                this,
                                e,
                                returnFalse,
                                emptyFunction,
                                undoable(clickEv => {
                                    clickEv.stopPropagation();
                                    this._props.addDocTab(this.Document, OpenWhere.addRight);
                                }, 'Open schema Doc preview')
                            )
                        }
                    />
                </div>
                <div className="row-cells">
                    {this.schemaView?.columnKeys?.map((key, index) => (
                        <SchemaTableCell
                            key={key}
                            Document={this.Document}
                            col={index}
                            fieldKey={key}
                            allowCRs={false} // to enter text with new lines, must use \n
                            columnWidth={this.columnWidth(index)}
                            rowHeight={this.schemaView.rowHeightFunc}
                            isRowActive={this._props.isContentActive}
                            getFinfo={this.getFinfo}
                            selectCell={this.selectCell}
                            deselectCell={this.deselectCell}
                            selectedCells={this.selectedCells}
                            selectedCol={this.selectedCol}
                            setColumnValues={this.setColumnValues}
                            setSelectedColumnValues={this.setSelectedColumnValues}
                            oneLine={BoolCast(this.schemaDoc?._singleLine)}
                            menuTarget={this.schemaView.MenuTarget}
                            transform={() => {
                                const ind = index === this.schemaView.columnKeys.length - 1 ? this.schemaView.columnKeys.length - 3 : index;
                                const x = this.schemaView?.displayColumnWidths.reduce((p, c, i) => (i <= ind ? p + c : p), 0);
                                const y = (this._props.rowIndex ?? 0) * this._props.PanelHeight();
                                return new Transform(x + CollectionSchemaView._rowMenuWidth, y, 1);
                            }}
                        />
                    ))}
                </div>
            </div>
        );
    }
}