aboutsummaryrefslogtreecommitdiff
path: root/src/client/views/nodes/Keyframe.tsx
blob: c6177e5b3c5da39b66c30e2cf4bc00fb12b2d1cd (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
import * as React from "react";
import * as ReactDOM from "react-dom";
import "./Keyframe.scss";
import "./../globalCssVariables.scss"; 
import { observer } from "mobx-react";
import { observable, reaction, action, IReactionDisposer, observe, IObservableArray, computed, toJS } from "mobx";
import { Doc, DocListCast } from "../../../new_fields/Doc";
import { auto } from "async";
import { Cast, FieldValue, StrCast, NumCast } from "../../../new_fields/Types";
import { StandardLonghandProperties } from "csstype";
import { runInThisContext } from "vm";
import { DateField } from "../../../new_fields/DateField";
import { DocumentManager } from "../../util/DocumentManager";
import { DocumentView } from "./DocumentView";
import { anchorPoints, Flyout } from "../TemplateMenu";
import { LinkMenu } from "./LinkMenu";
import { faCircle } from "@fortawesome/free-solid-svg-icons";
import { node, number } from "prop-types";
import { List } from "../../../new_fields/List";
import { createSchema, defaultSpec, makeInterface, listSpec } from "../../../new_fields/Schema";
import { CollectionBaseView } from "../collections/CollectionBaseView";
import { notDeepEqual } from "assert";
import { DocUtils, Docs } from "../../documents/Documents";
import { DragLinksAsDocuments } from "../../util/DragManager";




interface IProp {
    node: Doc;
    keyframedata: Doc;
}

const KeyDataSchema = createSchema({});  


const TimeAndKeyDataSchema = createSchema({
    time: defaultSpec("number", 0),
    key: Doc
});

type KeyData = makeInterface<[typeof KeyDataSchema]>;
type TimeAndKey = makeInterface<[typeof TimeAndKeyDataSchema]>;
const KeyData = makeInterface(KeyDataSchema);
const TimeAndKey = makeInterface(TimeAndKeyDataSchema);


const KeyframeDataSchema = createSchema({
    position: defaultSpec("number", 0),
    duration: defaultSpec("number", 0),
    kfs: listSpec(Doc)
}); 

type KeyframeData = makeInterface<[typeof KeyframeDataSchema]>;
export const KeyframeData = makeInterface(KeyframeDataSchema); 


@observer
export class Keyframe extends React.Component<IProp> {

    @observable private _display:string = "none"; 
    @observable private _duration:number = 200; 
    @observable private _bar = React.createRef<HTMLDivElement>(); 
    @observable private _keyframes:number[] = []; 


    private _reactionDisposers: IReactionDisposer[] = [];
    private _selectionManagerChanged?: IReactionDisposer;



    @action
    componentDidMount() {
        
        // need edge case here when keyframe data already exists when loading.....................;
        // let keyframes = Cast(this.props.node.keyframes, listSpec(Doc)) as List<Doc>;
        // if keyframes.indexOf() 

    }

    componentWillUnmount() {
        
    }
    
    // @action
    // onPointerEnter = (e: React.PointerEvent) => {
    //     e.preventDefault();
    //     e.stopPropagation(); 
    //     //this._display = "block"; 
    // }

    // @action 
    // onPointerOut = (e: React.PointerEvent) => {
    //     e.preventDefault();
    //     e.stopPropagation();
    //     //this._display = "none"; 
    // }

    @computed
    private get keyframes() {
        return Cast(this.props.node.keyframes, listSpec(Doc)) as List<Doc>;
    }
    @action 
    makeKeyData = (kfpos: number) => { //Kfpos is mouse offsetX, representing time 
        let hasData = false; 
        this.kfd.kfs!.forEach(TK => { //TK is TimeAndKey
            TK = TK as Doc; 
            if (TK.time === kfpos){
                hasData = true; 
            }
        }); 
        if (!hasData){
            let TK:Doc = new Doc(); 
            TK.time = kfpos; 
            TK.key = Doc.MakeCopy(this.props.node); 
            this.kfd.kfs!.push(TK);
        } 
        
    }


    @computed
    private get kfd(){
        let index = this.keyframes.indexOf(this.props.keyframedata); 
        return KeyframeData(this.keyframes[index] as Doc); 
    }

    @action 
    onBarPointerDown = (e: React.PointerEvent) => {
        e.preventDefault(); 
        e.stopPropagation(); 
        document.addEventListener("pointermove", this.onBarPointerMove); 
        document.addEventListener("pointerup", (e:PointerEvent) => {
            document.removeEventListener("pointermove", this.onBarPointerMove); 
        }); 
    }

    @action 
    onBarPointerMove = (e:PointerEvent) => {
        e.preventDefault(); 
        e.stopPropagation(); 
        if (this.kfd.position >= 0){
            let futureX = this.kfd.position + e.movementX; 
            if (futureX <= 0){
                this.kfd.position = 0; 
            } else{
                this.kfd.position += e.movementX; 
            }
        }
    }

    @action 
    onResizeLeft = (e:React.PointerEvent)=>{
        e.preventDefault(); 
        e.stopPropagation(); 
        document.addEventListener("pointermove", this.onDragResizeLeft); 
        document.addEventListener("pointerup", ()=>{
            document.removeEventListener("pointermove", this.onDragResizeLeft);
        }); 
    }

    @action 
    onResizeRight = (e:React.PointerEvent)=> {
        e.preventDefault(); 
        e.stopPropagation(); 
        document.addEventListener("pointermove", this.onDragResizeRight); 
        document.addEventListener("pointerup", ()=>{
            document.removeEventListener("pointermove", this.onDragResizeRight);
        }); 
    }

    @action 
    onDragResizeLeft = (e:PointerEvent)=>{
        e.preventDefault(); 
        e.stopPropagation();     
        let bar = this._bar.current!;  
        let barX = bar.getBoundingClientRect().left; 
        let offset = e.clientX - barX; 
        this._duration -= offset; 
        this.kfd.position += offset; 
        this.kfd.kfs!.forEach(kf => {
            kf = kf as Doc; 
            kf.time = NumCast(kf.time) + offset; 
        }); 
        this._keyframes.forEach(num => {
            num += offset; 
        }); 
    }
   
    
    @action 
    onDragResizeRight = (e:PointerEvent) => {
        e.preventDefault();  
        e.stopPropagation(); 
        let bar = this._bar.current!;  
        let barX = bar.getBoundingClientRect().right; 
        let offset = e.clientX - barX; 
        console.log(offset); 
        this._duration += offset; 
        this.kfd.duration = this._duration; 
    }

    createDivider = (type?: string):JSX.Element => {
        if (type === "left"){
            return <div className="divider" style={{right:"0px"}}></div>; 
        } else if (type === "right"){
            return <div className="divider" style={{left:"0px"}}> </div>; 
        }
        return <div className="divider"></div>; 
    }

    @action
    createKeyframe = (e: React.MouseEvent) => {
        e.preventDefault(); 
        e.stopPropagation(); 
        let mouse = e.nativeEvent;
        let position = NumCast(this.props.keyframedata.position);  
        this._keyframes.push(mouse.offsetX); 
        this.makeKeyData(position + mouse.offsetX); 

    }

    render() {
        return (
            <div>
                <div className="bar" ref={this._bar} style={{ transform: `translate(${this.props.keyframedata.position}px)`, width:`${this._duration}px`}} onPointerDown={this.onBarPointerDown} onDoubleClick={this.createKeyframe}>
                    <div className="leftResize" onPointerDown={this.onResizeLeft} ></div>
                    <div className="rightResize" onPointerDown={this.onResizeRight}></div>
                    {/* <div className="menubox" style={{display: this._display}}></div> */}
                    <div className="fadeLeft" style={{width:`${20}px`}}>{this.createDivider("left")}</div>
                    <div className="fadeRight" style={{width:`${20}px`}}>{this.createDivider("right")}</div>    
                    {this._keyframes.map(kf => {return <div className="keyframe" style={{left: `${kf}px`}}>
                        {this.createDivider()}
                        <div className="keyframeCircle"></div>
                    </div>;})}
                    {this.createDivider("left")}
                    {this.createDivider("right")}
                </div>
            </div>
        );
    }
}