aboutsummaryrefslogtreecommitdiff
path: root/src/client/views/nodes/RecordingBox/RecordingBox.tsx
blob: 25c708da887a8ef7d52c5c77050e3664edcaac84 (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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
import { action, makeObservable, observable } from 'mobx';
import { observer } from 'mobx-react';
import * as React from 'react';
import { DateField } from '../../../../fields/DateField';
import { Doc, DocListCast } from '../../../../fields/Doc';
import { Id } from '../../../../fields/FieldSymbols';
import { List } from '../../../../fields/List';
import { BoolCast, DocCast } from '../../../../fields/Types';
import { VideoField } from '../../../../fields/URLField';
import { Upload } from '../../../../server/SharedMediaTypes';
import { DocumentType } from '../../../documents/DocumentTypes';
import { Docs } from '../../../documents/Documents';
import { DragManager } from '../../../util/DragManager';
import { dropActionType } from '../../../util/DropActionTypes';
import { ScriptingGlobals } from '../../../util/ScriptingGlobals';
import { Presentation } from '../../../util/TrackMovements';
import { undoBatch } from '../../../util/UndoManager';
import { ViewBoxBaseComponent } from '../../DocComponent';
import { CollectionFreeFormView } from '../../collections/collectionFreeForm/CollectionFreeFormView';
import { mediaState } from '../AudioBox';
import { DocumentView } from '../DocumentView';
import { FieldView, FieldViewProps } from '../FieldView';
import { VideoBox } from '../VideoBox';
import { RecordingView } from './RecordingView';

@observer
export class RecordingBox extends ViewBoxBaseComponent<FieldViewProps>() {
    public static LayoutString(fieldKey: string) {
        return FieldView.LayoutString(RecordingBox, fieldKey);
    }

    private _ref: React.RefObject<HTMLDivElement> = React.createRef();

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

    componentDidMount() {
        this._props.setContentViewBox?.(this);
        Doc.SetNativeWidth(this.dataDoc, 1280);
        Doc.SetNativeHeight(this.dataDoc, 720);
    }

    @observable result: Upload.AccessPathInfo | undefined = undefined;

    @action
    setResult = (info: Upload.AccessPathInfo, presentation?: Presentation) => {
        this.result = info;
        this.dataDoc.type = DocumentType.VID;

        this.dataDoc[this.fieldKey + '_recorded'] = this.dataDoc.layout; // save the recording layout to allow re-recording later
        this.dataDoc.layout = VideoBox.LayoutString(this.fieldKey); // then convert the recording box to a video
        this.dataDoc[this._props.fieldKey] = new VideoField(this.result.accessPaths.client);
        // stringify the presentation and store it
        if (presentation?.movements) {
            const presCopy = { ...presentation, movements: presentation.movements.map(movement => ({ ...movement, doc: (movement.doc as Doc)[Id] })) };
            this.dataDoc[this.fieldKey + '_presentation'] = JSON.stringify(presCopy);
        }
    };
    @undoBatch
    public static WorkspaceStopRecording() {
        const remDoc = RecordingBox.screengrabber?.Document;
        if (remDoc) {
            // if recordingbox is true; when we press the stop button. changed vals temporarily to see if changes happening
            RecordingBox.screengrabber?.Pause?.();
            setTimeout(() => {
                RecordingBox.screengrabber?.Finish?.();
                remDoc.overlayX = 70; // was 100
                remDoc.overlayY = 590;
                RecordingBox.screengrabber = undefined;
            }, 100);
            // could break if recording takes too long to turn into videobox. If so, either increase time on setTimeout below or find diff place to do this
            setTimeout(() => Doc.RemFromMyOverlay(remDoc), 1000);
            Doc.UserDoc().workspaceRecordingState = mediaState.Paused;
            Doc.AddDocToList(Doc.UserDoc(), 'workspaceRecordings', remDoc);
        }
    }

    /**
     * This method toggles whether or not we are currently using the RecordingBox to record with the topbar button
     * @param _readOnly_
     * @returns
     */
    @undoBatch
    public static WorkspaceStartRecording(value: string) {
        const screengrabber =
            value === 'Record Workspace'
                ? Docs.Create.ScreenshotDocument({
                      title: `${new DateField()}-${Doc.ActiveDashboard?.title ?? ''}`,
                      _width: 205,
                      _height: 115,
                  })
                : Docs.Create.WebCamDocument(`${new DateField()}-${Doc.ActiveDashboard?.title ?? ''}`, {
                      title: `${new DateField()}-${Doc.ActiveDashboard?.title ?? ''}`,
                      _width: 205,
                      _height: 115,
                  });
        screengrabber.overlayX = 70; // was -400
        screengrabber.overlayY = 590; // was 0
        screengrabber['$' + Doc.LayoutDataKey(screengrabber) + '_trackScreen'] = true;
        Doc.AddToMyOverlay(screengrabber); // just adds doc to overlay
        DocumentView.addViewRenderedCb(screengrabber, docView => {
            RecordingBox.screengrabber = docView.ComponentView as RecordingBox;
            RecordingBox.screengrabber.Record?.();
        });
        Doc.UserDoc().workspaceRecordingState = mediaState.Recording;
    }

    /**
     * This method changes the menu depending on whether or not we are in playback mode
     * @param value  RecordingBox rootdoc
     */
    @undoBatch
    public static replayWorkspace(value: Doc) {
        Doc.UserDoc().currentRecording = value;
        value.overlayX = 70;
        value.overlayY = window.innerHeight - 180;
        Doc.AddToMyOverlay(value);
        DocumentView.addViewRenderedCb(value, docView => {
            Doc.UserDoc().currentRecording = docView.Document;
            docView.select(false);
            RecordingBox.resumeWorkspaceReplaying(value);
        });
    }

    /**
     * Adds the recording box to the canvas
     * @param value current recordingbox
     */
    @undoBatch
    public static addRecToWorkspace(value: RecordingBox) {
        const ffView = DocumentView.allViews().find(view => view.ComponentView instanceof CollectionFreeFormView);
        (ffView?.ComponentView as CollectionFreeFormView)._props.addDocument?.(value.Document);
        Doc.RemoveDocFromList(Doc.UserDoc(), 'workspaceRecordings', value.Document);
        Doc.RemFromMyOverlay(value.Document);
        Doc.UserDoc().currentRecording = undefined;
        Doc.UserDoc().workspaceReplayingState = undefined;
        Doc.UserDoc().workspaceRecordingState = undefined;
    }

    public static resumeWorkspaceReplaying(doc: Doc) {
        const docView = DocumentView.getDocumentView(doc);
        docView?.ComponentView?.Play?.();
        Doc.UserDoc().workspaceReplayingState = mediaState.Playing;
    }

    public static pauseWorkspaceReplaying(doc: Doc) {
        const docView = DocumentView.getDocumentView(doc);
        docView?.ComponentView?.Pause?.();
        Doc.UserDoc().workspaceReplayingState = mediaState.Paused;
    }

    public static stopWorkspaceReplaying(value: Doc) {
        Doc.RemFromMyOverlay(value);
        Doc.UserDoc().currentRecording = undefined;
        Doc.UserDoc().workspaceReplayingState = undefined;
        Doc.UserDoc().workspaceRecordingState = undefined;
        Doc.RemFromMyOverlay(value);
    }

    @undoBatch
    public static removeWorkspaceReplaying(value: Doc) {
        Doc.RemoveDocFromList(Doc.UserDoc(), 'workspaceRecordings', value);
        Doc.RemFromMyOverlay(value);
        Doc.UserDoc().currentRecording = undefined;
        Doc.UserDoc().workspaceReplayingState = undefined;
        Doc.UserDoc().workspaceRecordingState = undefined;
    }

    Record: undefined | (() => void);
    Pause: undefined | (() => void);
    Finish: undefined | (() => void);
    getControls = (record: () => void, pause: () => void, finish: () => void) => {
        this.Record = record;
        this.Pause = pause;
        this.Finish = finish;
    };

    render() {
        return (
            <div className="recordingBox" style={{ width: '100%' }} ref={this._ref}>
                {!this.result && <RecordingView forceTrackScreen={BoolCast(this.layoutDoc[this.fieldKey + '_trackScreen'])} getControls={this.getControls} setResult={this.setResult} id={DocCast(this.Document.proto)?.[Id] || ''} />}
            </div>
        );
    }
    // eslint-disable-next-line no-use-before-define
    static screengrabber: RecordingBox | undefined;
}

// eslint-disable-next-line prefer-arrow-callback
ScriptingGlobals.add(function stopWorkspaceRecording() {
    RecordingBox.WorkspaceStopRecording();
});

// eslint-disable-next-line prefer-arrow-callback
ScriptingGlobals.add(function stopWorkspaceReplaying(value: Doc) {
    RecordingBox.stopWorkspaceReplaying(value);
});
// eslint-disable-next-line prefer-arrow-callback
ScriptingGlobals.add(function removeWorkspaceReplaying(value: Doc) {
    RecordingBox.removeWorkspaceReplaying(value);
});

// eslint-disable-next-line prefer-arrow-callback
ScriptingGlobals.add(function getCurrentRecording() {
    return Doc.UserDoc().currentRecording;
});
// eslint-disable-next-line prefer-arrow-callback
ScriptingGlobals.add(function getWorkspaceRecordings() {
    return new List<string | Doc>(['Record Workspace', `Record Webcam`, ...DocListCast(Doc.UserDoc().workspaceRecordings)]);
});
// eslint-disable-next-line prefer-arrow-callback
ScriptingGlobals.add(function isWorkspaceRecording() {
    return Doc.UserDoc().workspaceRecordingState === mediaState.Recording;
});
// eslint-disable-next-line prefer-arrow-callback
ScriptingGlobals.add(function isWorkspaceReplaying() {
    return Doc.UserDoc().workspaceReplayingState;
});
// eslint-disable-next-line prefer-arrow-callback
ScriptingGlobals.add(function replayWorkspace(value: Doc | string, _readOnly_: boolean) {
    if (_readOnly_) return DocCast(Doc.UserDoc().currentRecording) ?? 'Record Workspace';
    if (typeof value === 'string') RecordingBox.WorkspaceStartRecording(value);
    else RecordingBox.replayWorkspace(value);
    return undefined;
});
// eslint-disable-next-line prefer-arrow-callback
ScriptingGlobals.add(function pauseWorkspaceReplaying(value: Doc) {
    RecordingBox.pauseWorkspaceReplaying(value);
});
// eslint-disable-next-line prefer-arrow-callback
ScriptingGlobals.add(function resumeWorkspaceReplaying(value: Doc) {
    RecordingBox.resumeWorkspaceReplaying(value);
});

// eslint-disable-next-line prefer-arrow-callback
ScriptingGlobals.add(function startRecordingDrag(value: { doc: Doc | string; e: React.PointerEvent }) {
    if (DocCast(value.doc)) {
        DragManager.StartDocumentDrag([value.e.target as HTMLElement], new DragManager.DocumentDragData([DocCast(value.doc)], dropActionType.embed), value.e.clientX, value.e.clientY);
        value.e.preventDefault();
        return true;
    }
    return undefined;
});
// eslint-disable-next-line prefer-arrow-callback
ScriptingGlobals.add(function renderDropdown() {
    if (!Doc.UserDoc().workspaceRecordings || DocListCast(Doc.UserDoc().workspaceRecordings).length === 0) {
        return true;
    }
    return false;
});

Docs.Prototypes.TemplateMap.set(DocumentType.WEBCAM, {
    layout: { view: RecordingBox, dataField: 'data' },
    options: { acl: '', systemIcon: 'BsFillCameraVideoFill' },
});