-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplayer.tsx
More file actions
204 lines (172 loc) · 5.74 KB
/
player.tsx
File metadata and controls
204 lines (172 loc) · 5.74 KB
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
import * as React from 'react'
import { useContext, useReducer, useEffect, useState, useMemo, useRef } from 'react'
import { Presentation, Beat } from './scenes'
import { Context as Lifeform, Eval } from './loop'
import { Framebuffer } from './framebuffer'
import DrawTexture from './draw-texture'
import Layers from './layers'
import Inspector from './inspector'
import * as Luma from 'luma.gl'
import { Clock, Presentation as PresentationContext } from './contexts';
import { state as synced, State as StateNode, Loading } from 'parcel-plugin-writable/src/node'
import { BuildIn } from './anim'
import { isTablet } from './view-mode'
import dedent from 'dedent'
import ReactMarkdown from 'react-markdown'
const ReactCSSTransitionGroup = require('react-addons-css-transition-group')
export interface Props {
play: Presentation
initialBeat?: string
}
type State = {
ts: DOMHighResTimeStamp
current: Beat
prev: Beat
}
type Next = { type: 'next' }
type Prev = { type: 'prev' }
type Seek = { type: 'seek', beat: Beat }
type Action = Next | Prev | Seek
export default function Player({ play }: Props) {
const grow = useContext(Lifeform)
const playerState = useMemo(() => synced<string>('player.state'), [])
const [state, go] = useReducer(
(state: State, action: Action) => {
const {current: beat} = state
const target =
action.type === 'next'
? beat.next ? beat.next : beat
:
action.type === 'prev'
? beat.prev ? beat.prev : beat
:
action.beat
if (beat === target) return state
return {
ts: grow(Clock).value || 0,
current: action.type === 'next'
? beat.next ? beat.next : beat
:
action.type === 'prev'
? beat.prev ? beat.prev : beat
:
action.beat,
prev: beat,
}
}, {
ts: grow(Clock).value || 0,
prev: null,
current: playerState.value !== Loading
? play.beats[playerState.value as string] || play.first
: play.first,//initialBeat ? play.beats[initialBeat] : play.first,
})
const [showInspector, toggleInspector] = useReducer(
(isVisible: boolean) => !isVisible, false
)
window['toggleInspector'] = toggleInspector
const [notesExpanded, toggleNotes] = useReducer(
(isExpanded: boolean) => !isExpanded, false
)
const didGesture = useRef<boolean>()
useEffect(
() => grow(PresentationContext.data).write(play), []
)
useEffect(() => {
console.log('State is now:', state.current && state.current.id)
grow(PresentationContext.playState).write(state)
if (didGesture.current) {
playerState.set(state.current && state.current.id)
didGesture.current = false
}
const noteEl = state.current['.noteElement']
noteEl && noteEl.scrollIntoView({ behavior: 'smooth', block: 'start' })
}, [state])
const next = useMemo(() => () => {
didGesture.current = true
return go({type: 'next'})
}, [go])
const prev = useMemo(() => () => {
didGesture.current = true
return go({type: 'prev'})
}, [go])
const seek = useMemo(() => (beat: Beat) => {
didGesture.current = true
return go({type: 'seek', beat})
}, [go])
Object.assign(window, {next, prev, seek})
useEffect(() => {
addEventListener('keydown', onKeyDown)
return () => removeEventListener('keydown', onKeyDown)
function onKeyDown(e: KeyboardEvent) {
switch (e.key) {
case 'ArrowRight':
case 'ArrowDown':
case 'PageDown':
return next()
case 'ArrowLeft':
case 'ArrowUp':
case 'PageUp':
return prev()
case '`':
return toggleInspector('toggle')
}
}
}, [])
useEffect(() => {
if (playerState.value !== Loading)
handleStateChange(playerState.value)
return playerState(handleStateChange)
function handleStateChange(id) {
id && go({ type: 'seek', beat: play.beats[id] || play.first })
}
}, [go])
return <>
<Eval>{
(_, cell) => {
const state = cell.read<State>(PresentationContext.playState)
if (!state) return
const { current, prev } = state
const opacity = cell.read(BuildIn({ beat: current.id, ms: current.fadeMs || 300 }))
if (typeof opacity !== 'number') return
const currentFb = cell.read(DrawTexture({ draw: current && current.draw }))
const prevFb = opacity < 1.0 && cell.read(DrawTexture({ draw: prev && prev.draw }))
cell.read(Layers([
{ output: currentFb, opacity: 1.0 },
opacity < 1.0 && { output: prevFb, opacity: 1.0 - opacity },
]))
}
}</Eval>
<ReactCSSTransitionGroup
transitionName='beat'
transitionEnterTimeout={600}
transitionLeaveTimeout={600}
>{
state.current.overlay ? state.current.overlay : null
}</ReactCSSTransitionGroup>
{ showInspector ? <Inspector /> : null }
{
isTablet ?
<>
<div className='fixed-bar-1'>
<button className='player-prev' onClick={prev}>Prev</button>
<button className='player-next' onClick={next}>Next</button>
</div>
<div
className={`notes ${notesExpanded ? 'expanded' : ''}`}
onClick={toggleNotes}
>{
Object.values(play.beats)
.map(beat =>
<div key={beat.id}
ref={e => beat['.noteElement'] = e}
className={`note ${state.current === beat ? 'current' : ''}`}>
<h1 onClick={e => { e.stopPropagation(); seek(beat) }}>{beat.id}</h1>
<ReactMarkdown source={dedent(beat.note || '')} />
</div>
)
}</div>
</>
: null
}
</>
}