aboutsummaryrefslogtreecommitdiff
path: root/src/client/views/nodes/formattedText/DashDocView.tsx
blob: 93371685dd3afdc5de5df8d590cda1611f856d4f (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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
/* eslint-disable jsx-a11y/no-static-element-interactions */
import { action, computed, IReactionDisposer, makeObservable, observable, reaction } from 'mobx';
import { observer } from 'mobx-react';
import { NodeSelection } from 'prosemirror-state';
import * as React from 'react';
import * as ReactDOM from 'react-dom/client';
import { ClientUtils, returnFalse } from '../../../../ClientUtils';
import { Doc } from '../../../../fields/Doc';
import { Height, Width } from '../../../../fields/DocSymbols';
import { NumCast } from '../../../../fields/Types';
import { DocServer } from '../../../DocServer';
import { Docs } from '../../../documents/Documents';
import { DocUtils } from '../../../documents/DocUtils';
import { Transform } from '../../../util/Transform';
import { ObservableReactComponent } from '../../ObservableReactComponent';
import { DocumentView } from '../DocumentView';
import { FocusViewOptions } from '../FocusViewOptions';
import { FormattedTextBox } from './FormattedTextBox';

const horizPadding = 3; // horizontal padding to container to allow cursor to show up on either side.
interface IDashDocViewInternal {
    docId: string;
    embedding: string;
    tbox: FormattedTextBox;
    width: string;
    height: string;
    hidden: boolean;
    fieldKey: string;
    view: any;
    node: any;
    getPos: any;
}

@observer
export class DashDocViewInternal extends ObservableReactComponent<IDashDocViewInternal> {
    _spanRef = React.createRef<HTMLDivElement>();
    _disposers: { [name: string]: IReactionDisposer } = {};
    _textBox: FormattedTextBox;
    @observable _dashDoc: Doc | undefined = undefined;
    @computed get _width() {
        return NumCast(this._dashDoc?._width);
    }
    @computed get _height() {
        return NumCast(this._dashDoc?._height);
    }

    updateDoc = action((dashDoc: Doc) => {
        this._dashDoc = dashDoc;

        if (this._props.width !== (this._dashDoc?._width ?? '') + 'px' || this._props.height !== (this._dashDoc?._height ?? '') + 'px') {
            try {
                // bcz: an exception will be thrown if two embeddings are open at the same time when a doc view comment is made
                this._props.view.dispatch(
                    this._props.view.state.tr.setNodeMarkup(this._props.getPos(), null, {
                        ...this._props.node.attrs,
                        width: this._width + 'px',
                        height: this._height + 'px',
                    })
                );
            } catch (e) {
                console.log('DashDocView:' + e);
            }
        }
    });

    constructor(props: IDashDocViewInternal) {
        super(props);
        makeObservable(this);
        this._textBox = this._props.tbox;

        DocServer.GetRefField(this._props.docId + this._props.embedding).then(async dashDoc => {
            if (!(dashDoc instanceof Doc)) {
                this._props.embedding &&
                    DocServer.GetRefField(this._props.docId).then(async dashDocBase => {
                        if (dashDocBase instanceof Doc) {
                            const embedding = Doc.MakeEmbedding(dashDocBase, this._props.docId + this._props.embedding);
                            embedding.layout_fieldKey = 'layout';
                            this._props.fieldKey && DocUtils.makeCustomViewClicked(embedding, Docs.Create.StackingDocument, this._props.fieldKey, undefined);
                            this.updateDoc(embedding);
                        }
                    });
            } else {
                this.updateDoc(dashDoc);
            }
        });
    }

    componentDidMount() {
        this._disposers.upater = reaction(
            () => ({ width: this._width, height: this._height, parent: this._spanRef.current?.parentNode as HTMLElement }),
            action(({ width, height, parent }) => {
                if (parent) {
                    parent.style.width = width + 'px';
                    parent.style.height = height + 'px';
                }
            })
        );
    }

    removeDoc = () => {
        this._props.view.dispatch(this._props.view.state.tr.setSelection(new NodeSelection(this._props.view.state.doc.resolve(this._props.getPos()))).deleteSelection());
        return true;
    };

    getDocTransform = () => {
        if (!this._spanRef.current) return Transform.Identity();
        const { scale, translateX, translateY } = ClientUtils.GetScreenTransform(this._spanRef.current);
        return new Transform(-translateX, -translateY, 1).scale(1 / scale);
    };
    outerFocus = (target: Doc, options: FocusViewOptions) => this._textBox.focus(target, options); // ideally, this would scroll to show the focus target

    onKeyDown = (e: any) => {
        e.stopPropagation();
        if (e.key === 'Tab' || e.key === 'Enter') {
            e.preventDefault();
        }
    };

    onPointerLeave = () => {
        const ele = document.getElementById('DashDocCommentView-' + this._props.docId) as HTMLDivElement;
        ele && (ele.style.backgroundColor = '');
    };

    onPointerEnter = () => {
        const ele = document.getElementById('DashDocCommentView-' + this._props.docId) as HTMLDivElement;
        ele && (ele.style.backgroundColor = 'orange');
    };

    componentWillUnmount = () => Object.values(this._disposers).forEach(disposer => disposer?.());
    isContentActive = () => this._props.tbox._props.isContentActive() || this._props.tbox.isAnyChildContentActive?.();

    render() {
        return !this._dashDoc || this._props.hidden ? null : (
            <div
                ref={this._spanRef}
                className="dash-span"
                style={{
                    width: `calc(100% - ${horizPadding}px)`,
                    height: this._height,
                    position: 'relative',
                    display: 'flex',
                    margin: 'auto',
                    pointerEvents: this.isContentActive() ? undefined : 'none',
                }}
                onPointerLeave={this.onPointerLeave}
                onPointerEnter={this.onPointerEnter}
                onKeyDown={this.onKeyDown}
                onKeyPress={e => e.stopPropagation()}
                onKeyUp={e => e.stopPropagation()}
                onWheel={e => e.preventDefault()}>
                <DocumentView
                    Document={this._dashDoc}
                    addDocument={returnFalse}
                    removeDocument={this.removeDoc}
                    isDocumentActive={returnFalse}
                    isContentActive={this.isContentActive}
                    styleProvider={this._textBox._props.styleProvider}
                    containerViewPath={this._textBox.DocumentView?.().docViewPath}
                    ScreenToLocalTransform={this.getDocTransform}
                    addDocTab={this._textBox._props.addDocTab}
                    pinToPres={returnFalse}
                    renderDepth={this._textBox._props.renderDepth + 1}
                    PanelWidth={this._dashDoc[Width]}
                    PanelHeight={this._dashDoc[Height]}
                    focus={this.outerFocus}
                    whenChildContentsActiveChanged={this._props.tbox.whenChildContentsActiveChanged}
                    dontRegisterView={false}
                    childFilters={this._props.tbox?._props.childFilters}
                    childFiltersByRanges={this._props.tbox?._props.childFiltersByRanges}
                    searchFilterDocs={this._props.tbox?._props.searchFilterDocs}
                />
            </div>
        );
    }
}

export class DashDocView {
    dom: HTMLSpanElement; // container for label and value
    root: any;

    constructor(node: any, view: any, getPos: any, tbox: FormattedTextBox) {
        this.dom = document.createElement('span');
        this.dom.style.position = 'relative';
        this.dom.style.textIndent = '0';
        this.dom.style.width = (+node.attrs.width.toString().replace('px', '') + horizPadding).toString();
        this.dom.style.height = node.attrs.height;
        this.dom.style.display = node.attrs.hidden ? 'none' : 'inline-block';
        (this.dom.style as any).float = node.attrs.float;
        this.dom.onkeypress = function (e: any) {
            e.stopPropagation();
        };
        this.dom.onkeydown = function (e: any) {
            e.stopPropagation();
        };
        this.dom.onkeyup = function (e: any) {
            e.stopPropagation();
        };
        this.dom.onmousedown = function (e: any) {
            e.stopPropagation();
        };

        this.root = ReactDOM.createRoot(this.dom);
        this.root.render(
            <DashDocViewInternal
                docId={node.attrs.docId}
                embedding={node.attrs.embedding}
                width={node.attrs.width}
                height={node.attrs.height}
                hidden={node.attrs.hidden}
                fieldKey={node.attrs.fieldKey}
                tbox={tbox}
                view={view}
                node={node}
                getPos={getPos}
            />
        );
    }
    destroy() {
        setTimeout(() => {
            try {
                this.root.unmount();
            } catch {
                /* empty */
            }
        });
    }
    deselectNode() {
        this.dom.style.backgroundColor = '';
    }
    selectNode() {
        this.dom.style.backgroundColor = 'rgb(141, 182, 247)';
    }
}