This repository was archived by the owner on Mar 28, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 164
/
Copy pathpage.js
169 lines (150 loc) · 3.89 KB
/
page.js
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
import PropTypes from 'prop-types';
/* global window, document */
import React from 'react';
import Head from 'next/head';
import debounce from 'lodash.debounce';
import getAlgoName from '../utils/names';
import Menu from './menu';
import Player from './player';
const getWindowSize = () => ({
width: document.body.clientWidth || window.innerWidth, // Fallback for jsdom
height: window.innerHeight,
});
const createLayout = (props, state) => {
const {
algorithm,
computeLayout,
} = props;
const { width, height } = state;
return computeLayout({
width,
height,
code: algorithm.code,
});
};
class Page extends React.Component {
constructor(props) {
super(props);
this.handleResize = debounce(this.handleResize.bind(this), 300);
this.state = {
renderedOnClient: false,
// IPhone 6 portrait resolution
width: 375,
height: 667,
};
this.layout = createLayout(props, this.state);
}
componentDidMount() {
window.addEventListener('resize', this.handleResize);
this.setState({
renderedOnClient: true,
...getWindowSize(),
});
}
componentWillUpdate(nextProps, nextState) {
this.layout = createLayout(nextProps, nextState);
}
componentWillUnmount() {
window.removeEventListener('resize', this.handleResize);
}
handleResize() {
this.setState(getWindowSize());
}
getChildContext() {
return {
layout: this.layout,
};
}
render() {
const {
currentPath,
algorithm,
illustration,
computeFrame,
steps,
actions,
} = this.props;
const {
renderedOnClient,
} = this.state;
const {
color,
} = this.layout;
return (
<div>
<Head>
<title>{`Illustrated ${getAlgoName(currentPath)} algorithm`}</title>
<meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width"/>
<meta name="apple-mobile-web-app-capable" content="yes"/>
<style>{`
body {
margin: 0;
padding: 0;
transition: background 0.5s;
font-family: 'Helvetica Neue', Helvetica, sans-serif;
}
@font-face {
font-family: 'FiraCode-Light';
src: url('/static/FiraCode-Light.woff');
}
pre,
.code {
font-family: 'FiraCode-Light', monospace;
}
`}
</style>
<style>{`
body {
background: ${color};
}
`}
</style>
</Head>
<div className="body" style={{ opacity: renderedOnClient ? 1 : 0 }}>
<div className="header">
<Menu currentPath={currentPath}/>
</div>
<div className="content">
<Player
algorithm={algorithm}
illustration={illustration}
computeFrame={computeFrame}
steps={steps}
actions={actions}
/>
</div>
<style jsx>{`
.body {
transition: opacity 0.5s;
}
.header {
position: absolute;
z-index: 2;
width: 100%;
}
.content {
position: absolute;
z-index: 1;
}
`}
</style>
</div>
</div>
);
}
}
Page.propTypes = {
currentPath: PropTypes.string.isRequired,
algorithm: PropTypes.func.isRequired,
illustration: PropTypes.func.isRequired,
// ESLint plugin bug
// eslint-disable-next-line react/no-unused-prop-types
computeLayout: PropTypes.func.isRequired,
computeFrame: PropTypes.func.isRequired,
steps: PropTypes.array.isRequired,
actions: PropTypes.object.isRequired,
};
Page.childContextTypes = {
layout: PropTypes.object,
};
export default Page;