aboutsummaryrefslogtreecommitdiff
path: root/src/client/views/nodes/DocHolderBox.tsx
blob: 0c5239d660729a8d8c576f103efaa363df7a752e (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
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
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
import { action, IReactionDisposer, reaction } from "mobx";
import { observer } from "mobx-react";
import { Doc, Field } from "../../../fields/Doc";
import { collectionSchema, documentSchema } from "../../../fields/documentSchemas";
import { makeInterface, listSpec } from "../../../fields/Schema";
import { ComputedField } from "../../../fields/ScriptField";
import { Cast, NumCast, StrCast } from "../../../fields/Types";
import { TraceMobx } from "../../../fields/util";
import { emptyPath, returnFalse, returnOne, returnZero } from "../../../Utils";
import { DocumentType } from "../../documents/DocumentTypes";
import { DragManager } from "../../util/DragManager";
import { undoBatch } from "../../util/UndoManager";
import { ContextMenu } from "../ContextMenu";
import { ContextMenuProps } from "../ContextMenuItem";
import { ViewBoxAnnotatableComponent } from "../DocComponent";
import { ContentFittingDocumentView } from "./ContentFittingDocumentView";
import "./DocHolderBox.scss";
import { DocumentView } from "./DocumentView";
import { FieldView, FieldViewProps } from "./FieldView";
import React = require("react");

type DocHolderBoxSchema = makeInterface<[typeof documentSchema, typeof collectionSchema]>;
const DocHolderBoxDocument = makeInterface(documentSchema, collectionSchema);

@observer
export class DocHolderBox extends ViewBoxAnnotatableComponent<FieldViewProps, DocHolderBoxSchema>(DocHolderBoxDocument) {
    public static LayoutString(fieldKey: string) { return FieldView.LayoutString(DocHolderBox, fieldKey); }
    _prevSelectionDisposer: IReactionDisposer | undefined;
    _dropDisposer?: DragManager.DragDropDisposer;
    _selections: Doc[] = [];
    _contRef = React.createRef<HTMLDivElement>();
    _curSelection = -1;
    componentDidMount() {
        this._prevSelectionDisposer = reaction(() => this.dataDoc[this.fieldKey], (data) => {
            if (data instanceof Doc && !this.isSelectionLocked()) {
                this._selections.indexOf(data) !== -1 && this._selections.splice(this._selections.indexOf(data), 1);
                this._selections.push(data);
                this._curSelection = this._selections.length - 1;
            }
        });
    }
    componentWillUnmount() {
        this._prevSelectionDisposer?.();
    }
    specificContextMenu = (e: React.MouseEvent): void => {
        const funcs: ContextMenuProps[] = [];
        funcs.push({ description: (this.isSelectionLocked() ? "Show" : "Lock") + " Selection", event: () => this.toggleLockSelection, icon: "expand-arrows-alt" });
        funcs.push({ description: (this.layoutDoc.excludeCollections ? "Include" : "Exclude") + " Collections", event: () => this.layoutDoc.excludeCollections = !this.layoutDoc.excludeCollections, icon: "expand-arrows-alt" });
        funcs.push({ description: `${this.layoutDoc.forceActive ? "Select" : "Force"} Contents Active`, event: () => this.layoutDoc.forceActive = !this.layoutDoc.forceActive, icon: "project-diagram" });
        funcs.push({ description: `Show ${this.layoutDoc.childLayoutTemplateName !== "keyValue" ? "key values" : "contents"}`, event: () => this.layoutDoc.childLayoutString = this.layoutDoc.childLayoutString ? undefined : "<KeyValueBox {...props} />", icon: "project-diagram" });

        ContextMenu.Instance.addItem({ description: "Options...", subitems: funcs, icon: "asterisk" });
    }
    lockSelection = () => {
        this.dataDoc[this.fieldKey] = this.dataDoc[this.fieldKey];
    }
    showSelection = () => {
        this.dataDoc[this.fieldKey] = ComputedField.MakeFunction(`selectedDocs(self,this.excludeCollections,[_last_])?.[0]`);
    }
    isSelectionLocked = () => {
        const kvpstring = Field.toKeyValueString(this.dataDoc, this.fieldKey);
        return !kvpstring || kvpstring.includes("DOC");
    }
    toggleLockSelection = () => {
        !this.isSelectionLocked() ? this.lockSelection() : this.showSelection();
        return true;
    }
    prevSelection = () => {
        this.lockSelection();
        if (this._curSelection > 0) {
            this.dataDoc[this.fieldKey] = this._selections[--this._curSelection];
            return true;
        }
    }
    nextSelection = () => {
        if (this._curSelection < this._selections.length - 1 && this._selections.length) {
            this.dataDoc[this.fieldKey] = this._selections[++this._curSelection];
            return true;
        }
    }
    onPointerDown = (e: React.PointerEvent) => {
        if (this.active() && e.button === 0 && !e.ctrlKey) {
            e.stopPropagation();
        }
    }
    onLockClick = (e: React.MouseEvent) => {
        this.toggleLockSelection();
        (e.nativeEvent as any).formattedHandled = true;
        e.stopPropagation();
    }
    get xPad() { return NumCast(this.rootDoc._xPadding); }
    get yPad() { return NumCast(this.rootDoc._yPadding); }
    onClick = (e: React.MouseEvent) => {
        let hitWidget: boolean | undefined = false;
        if (this._contRef.current!.getBoundingClientRect().top + this.yPad > e.clientY) hitWidget = (() => { this.props.select(false); return true; })();
        else if (this._contRef.current!.getBoundingClientRect().bottom - this.yPad < e.clientY) hitWidget = (() => { this.props.select(false); return true; })();
        else {
            if (this._contRef.current!.getBoundingClientRect().left + this.xPad > e.clientX) hitWidget = this.prevSelection();
            if (this._contRef.current!.getBoundingClientRect().right - this.xPad < e.clientX) hitWidget = this.nextSelection();
        }
        if (hitWidget) {
            (e.nativeEvent as any).formattedHandled = true;
            e.stopPropagation();
        }
    }
    pwidth = () => this.props.PanelWidth() - 2 * this.xPad;
    pheight = () => this.props.PanelHeight() - 2 * this.yPad;
    getTransform = () => this.props.ScreenToLocalTransform().translate(-this.xPad, -this.yPad);
    isActive = (outsideReaction: boolean) => this.active(outsideReaction) || this.props.renderDepth <= 1;
    layoutTemplateDoc = () => Cast(this.layoutDoc.childLayoutTemplate, Doc, null);
    get renderContents() {
        const containedDoc = Cast(this.dataDoc[this.fieldKey], Doc, null);
        const layoutTemplate = StrCast(this.layoutDoc.childLayoutString);
        const contents = !(containedDoc instanceof Doc) ||
            Cast(containedDoc[Doc.LayoutFieldKey(containedDoc)], listSpec(Doc), null)?.includes(this.rootDoc)
            ? (null) : this.layoutDoc.childLayoutString || this.layoutTemplateDoc() ?
                <DocumentView
                    Document={containedDoc}
                    DataDoc={undefined}
                    LibraryPath={emptyPath}
                    ContainingCollectionView={this as any} // bcz: hack!  need to pass a prop that can be used to select the container (ie, 'this') when the up selector in document decorations is clicked.  currently, the up selector allows only a containing collection to be selected
                    ContainingCollectionDoc={undefined}
                    fitToBox={true}
                    backgroundColor={this.props.backgroundColor}
                    LayoutTemplateString={layoutTemplate}
                    LayoutTemplate={this.layoutTemplateDoc}
                    rootSelected={this.props.isSelected}
                    addDocument={this.props.addDocument}
                    moveDocument={this.props.moveDocument}
                    removeDocument={this.props.removeDocument}
                    addDocTab={this.props.addDocTab}
                    pinToPres={this.props.pinToPres}
                    ScreenToLocalTransform={this.getTransform}
                    renderDepth={containedDoc.type !== DocumentType.DOCHOLDER && !this.props.renderDepth ? 0 : this.props.renderDepth + 1}
                    NativeHeight={returnZero}
                    NativeWidth={returnZero}
                    PanelWidth={this.pwidth}
                    PanelHeight={this.pheight}
                    focus={this.props.focus}
                    parentActive={this.isActive}
                    dontRegisterView={true}
                    whenActiveChanged={this.props.whenActiveChanged}
                    bringToFront={returnFalse}
                    ContentScaling={returnOne} /> :
                <ContentFittingDocumentView
                    Document={containedDoc}
                    DataDoc={undefined}
                    LibraryPath={emptyPath}
                    ContainingCollectionView={this as any} // bcz: hack!  need to pass a prop that can be used to select the container (ie, 'this') when the up selector in document decorations is clicked.  currently, the up selector allows only a containing collection to be selected
                    ContainingCollectionDoc={undefined}
                    fitToBox={true}
                    backgroundColor={this.props.backgroundColor}
                    ignoreAutoHeight={true}
                    LayoutTemplateString={layoutTemplate}
                    LayoutTemplate={this.layoutTemplateDoc}
                    rootSelected={this.props.isSelected}
                    addDocument={this.props.addDocument}
                    moveDocument={this.props.moveDocument}
                    removeDocument={this.props.removeDocument}
                    addDocTab={this.props.addDocTab}
                    pinToPres={this.props.pinToPres}
                    ScreenToLocalTransform={this.getTransform}
                    renderDepth={containedDoc.type !== DocumentType.DOCHOLDER && !this.props.renderDepth ? 0 : this.props.renderDepth + 1}
                    NativeHeight={returnZero}
                    NativeWidth={returnZero}
                    PanelWidth={this.pwidth}
                    PanelHeight={this.pheight}
                    focus={this.props.focus}
                    parentActive={this.isActive}
                    dontRegisterView={true}
                    whenActiveChanged={this.props.whenActiveChanged}
                    bringToFront={returnFalse}
                    ContentScaling={returnOne}
                />;
        return contents;
    }
    render() {
        const containedDoc = Cast(this.dataDoc[this.fieldKey], Doc, null);
        TraceMobx();
        return <div className="documentBox-container" ref={this._contRef}
            onContextMenu={this.specificContextMenu}
            onPointerDown={this.onPointerDown} onClick={this.onClick}
            style={{
                background: this.props.backgroundColor?.(containedDoc),
                border: `#00000021 solid ${this.xPad}px`,
                borderTop: `#0000005e solid ${this.yPad}px`,
                borderBottom: `#0000005e solid ${this.yPad}px`,
            }}>
            {this.renderContents}
            <div className="documentBox-lock" onClick={this.onLockClick} ref={this.createDropTarget}
                style={{ marginTop: - this.yPad, background: "black" }}>
                <FontAwesomeIcon icon={this.isSelectionLocked() ? "lock" : "unlock"} size="sm" />
            </div>
        </div >;
    }

    @undoBatch
    @action
    drop = (e: Event, de: DragManager.DropEvent) => {
        if (de.complete.docDragData) {
            if (de.complete.docDragData.draggedDocuments[0].type === DocumentType.FONTICON) {
                const doc = Cast(de.complete.docDragData.draggedDocuments[0].dragFactory, Doc, null);
                this.layoutDoc.childLayoutTemplate = doc;
            }
        }
    }
    protected createDropTarget = (ele: HTMLDivElement) => {
        this._dropDisposer?.();
        ele && (this._dropDisposer = DragManager.MakeDropTarget(ele, this.drop.bind(this), this.rootDoc));
    }

}