aboutsummaryrefslogtreecommitdiff
path: root/src/client/views/nodes/CollectionFreeFormDocumentView.tsx
blob: e19e2d5254d72cd01d01417a3f65ba46d552e44b (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
import { action, computed, observable } from 'mobx';
import { observer } from 'mobx-react';
import { Doc, Opt } from '../../../fields/Doc';
import { List } from '../../../fields/List';
import { listSpec } from '../../../fields/Schema';
import { ComputedField } from '../../../fields/ScriptField';
import { Cast, NumCast, StrCast } from '../../../fields/Types';
import { TraceMobx } from '../../../fields/util';
import { numberRange } from '../../../Utils';
import { DocumentManager } from '../../util/DocumentManager';
import { SelectionManager } from '../../util/SelectionManager';
import { Transform } from '../../util/Transform';
import { CollectionFreeFormView } from '../collections/collectionFreeForm/CollectionFreeFormView';
import { DocComponent } from '../DocComponent';
import { StyleProp } from '../StyleProvider';
import './CollectionFreeFormDocumentView.scss';
import { DocumentView, DocumentViewProps } from './DocumentView';
import React = require('react');

export interface CollectionFreeFormDocumentViewProps extends DocumentViewProps {
    dataProvider?: (doc: Doc, replica: string) => { x: number; y: number; zIndex?: number; opacity?: number; highlight?: boolean; z: number; transition?: string } | undefined;
    sizeProvider?: (doc: Doc, replica: string) => { width: number; height: number } | undefined;
    renderCutoffProvider: (doc: Doc) => boolean;
    zIndex?: number;
    highlight?: boolean;
    jitterRotation: number;
    dataTransition?: string;
    replica: string;
    CollectionFreeFormView: CollectionFreeFormView;
}

@observer
export class CollectionFreeFormDocumentView extends DocComponent<CollectionFreeFormDocumentViewProps>() {
    public static animFields = ['_height', '_width', 'x', 'y', '_scrollTop', 'opacity']; // fields that are configured to be animatable using animation frames
    @observable _animPos: number[] | undefined = undefined;
    @observable _contentView: DocumentView | undefined | null;
    get displayName() {
        return 'CollectionFreeFormDocumentView(' + this.rootDoc.title + ')';
    } // this makes mobx trace() statements more descriptive
    get transform() {
        return `translate(${this.X}px, ${this.Y}px) rotate(${NumCast(this.Document.jitterRotation, this.props.jitterRotation)}deg)`;
    }
    get X() {
        return this.dataProvider ? this.dataProvider.x : NumCast(this.Document.x);
    }
    get Y() {
        return this.dataProvider ? this.dataProvider.y : NumCast(this.Document.y);
    }
    get ZInd() {
        return this.dataProvider ? this.dataProvider.zIndex : NumCast(this.Document.zIndex);
    }
    get Opacity() {
        return this.dataProvider ? this.dataProvider.opacity : undefined;
    }
    get Highlight() {
        return this.dataProvider?.highlight;
    }
    @computed get ShowTitle() {
        return this.props.styleProvider?.(this.rootDoc, this.props, StyleProp.ShowTitle) as Opt<string>;
    }
    @computed get dataProvider() {
        return this.props.dataProvider?.(this.props.Document, this.props.replica);
    }
    @computed get sizeProvider() {
        return this.props.sizeProvider?.(this.props.Document, this.props.replica);
    }

    styleProvider = (doc: Doc | undefined, props: Opt<DocumentViewProps>, property: string) => {
        if (property === StyleProp.Opacity && doc === this.layoutDoc) return this.Opacity; // only change the opacity for this specific document, not its children
        return this.props.styleProvider?.(doc, props, property);
    };

    public static getValues(doc: Doc, time: number) {
        return CollectionFreeFormDocumentView.animFields.reduce((p, val) => {
            p[val] = Cast(`${val}-indexed`, listSpec('number'), [NumCast(doc[val])]).reduce((p, v, i) => ((i <= Math.round(time) && v !== undefined) || p === undefined ? v : p), undefined as any as number);
            return p;
        }, {} as { [val: string]: Opt<number> });
    }

    public static setValues(time: number, d: Doc, vals: { [val: string]: Opt<number> }) {
        const timecode = Math.round(time);
        Object.keys(vals).forEach(val => {
            const findexed = Cast(d[`${val}-indexed`], listSpec('number'), []).slice();
            findexed[timecode] = vals[val] as any as number;
            d[`${val}-indexed`] = new List<number>(findexed);
        });
    }

    public static updateKeyframe(docs: Doc[], time: number, targetDoc?: Doc) {
        const timecode = Math.round(time);
        docs.forEach(
            action(doc => {
                doc._viewTransition = doc.dataTransition = 'all 1s';
                CollectionFreeFormDocumentView.animFields.forEach(val => {
                    const findexed = Cast(doc[`${val}-indexed`], listSpec('number'), null);
                    findexed?.length <= timecode + 1 && findexed.push(undefined as any as number);
                });
            })
        );
        setTimeout(
            () =>
                docs.forEach(doc => {
                    doc._viewTransition = undefined;
                    doc.dataTransition = 'inherit';
                }),
            1010
        );
    }

    public static gotoKeyframe(docs: Doc[]) {
        docs.forEach(doc => (doc._viewTransition = doc.dataTransition = 'all 1s'));
        setTimeout(
            () =>
                docs.forEach(doc => {
                    doc._viewTransition = undefined;
                    doc.dataTransition = 'inherit';
                }),
            1010
        );
    }

    public static setupZoom(doc: Doc, targDoc: Doc) {
        const width = new List<number>();
        const height = new List<number>();
        const top = new List<number>();
        const left = new List<number>();
        width.push(NumCast(targDoc._width));
        height.push(NumCast(targDoc._height));
        top.push(NumCast(targDoc._height) / -2);
        left.push(NumCast(targDoc._width) / -2);
        doc['viewfinder-width-indexed'] = width;
        doc['viewfinder-height-indexed'] = height;
        doc['viewfinder-top-indexed'] = top;
        doc['viewfinder-left-indexed'] = left;
    }

    public static setupKeyframes(docs: Doc[], currTimecode: number, makeAppear: boolean = false) {
        docs.forEach(doc => {
            if (doc.appearFrame === undefined) doc.appearFrame = currTimecode;
            if (!doc['opacity-indexed']) {
                // opacity is unlike other fields because it's value should not be undefined before it appears to enable it to fade-in
                doc['opacity-indexed'] = new List<number>(numberRange(currTimecode + 1).map(t => (!doc.z && makeAppear && t < NumCast(doc.appearFrame) ? 0 : 1)));
            }
            CollectionFreeFormDocumentView.animFields.forEach(val => (doc[val] = ComputedField.MakeInterpolated(val, 'activeFrame', doc, currTimecode)));
            doc.activeFrame = ComputedField.MakeFunction('self.context?._currentFrame||0');
            doc.dataTransition = 'inherit';
        });
    }

    @action public float = () => {
        const { Document: topDoc, ContainingCollectionView: container } = this.props;
        const screenXf = container?.screenToLocalTransform();
        if (screenXf) {
            SelectionManager.DeselectAll();
            if (topDoc.z) {
                const spt = screenXf.inverse().transformPoint(NumCast(topDoc.x), NumCast(topDoc.y));
                topDoc.z = 0;
                topDoc.x = spt[0];
                topDoc.y = spt[1];
                this.props.removeDocument?.(topDoc);
                this.props.addDocTab(topDoc, 'inParent');
            } else {
                const spt = this.screenToLocalTransform().inverse().transformPoint(0, 0);
                const fpt = screenXf.transformPoint(spt[0], spt[1]);
                topDoc.z = 1;
                topDoc.x = fpt[0];
                topDoc.y = fpt[1];
            }
            setTimeout(() => SelectionManager.SelectView(DocumentManager.Instance.getDocumentView(topDoc, container)!, false), 0);
        }
    };

    nudge = (x: number, y: number) => {
        this.props.Document.x = NumCast(this.props.Document.x) + x;
        this.props.Document.y = NumCast(this.props.Document.y) + y;
    };
    panelWidth = () => this.sizeProvider?.width || this.props.PanelWidth?.();
    panelHeight = () => this.sizeProvider?.height || this.props.PanelHeight?.();
    screenToLocalTransform = (): Transform => this.props.ScreenToLocalTransform().translate(-this.X, -this.Y);
    focusDoc = (doc: Doc) => this.props.focus(doc);
    returnThis = () => this;
    render() {
        TraceMobx();
        const divProps: DocumentViewProps = {
            ...this.props,
            CollectionFreeFormDocumentView: this.returnThis,
            styleProvider: this.styleProvider,
            ScreenToLocalTransform: this.screenToLocalTransform,
            PanelWidth: this.panelWidth,
            PanelHeight: this.panelHeight,
        };
        const background = this.props.styleProvider?.(this.rootDoc, this.props, StyleProp.BackgroundColor);
        const mixBlendMode = undefined; // (StrCast(this.layoutDoc.mixBlendMode) as any) || (typeof background === 'string' && background && !background.startsWith('linear') && DashColor(background).alpha() !== 1 ? 'multiply' : undefined);
        return (
            <div
                className={'collectionFreeFormDocumentView-container'}
                style={{
                    outline: this.Highlight ? 'orange solid 2px' : '',
                    width: this.panelWidth(),
                    height: this.panelHeight(),
                    transform: this.transform,
                    transformOrigin: '50% 50%',
                    transition: this.dataProvider?.transition ?? (this.props.dataTransition ? this.props.dataTransition : this.dataProvider ? this.dataProvider.transition : StrCast(this.layoutDoc.dataTransition)),
                    zIndex: this.ZInd,
                    mixBlendMode: mixBlendMode,
                    display: this.ZInd === -99 ? 'none' : undefined,
                }}>
                {this.props.renderCutoffProvider(this.props.Document) ? (
                    <div style={{ position: 'absolute', width: this.panelWidth(), height: this.panelHeight(), background: 'lightGreen' }} />
                ) : (
                    <DocumentView {...divProps} ref={action((r: DocumentView | null) => (this._contentView = r))} />
                )}
            </div>
        );
    }
}