forked from Kagami/vmsg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
172 lines (166 loc) · 4.56 KB
/
index.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
170
171
172
import React from "react";
import ReactDOM from "react-dom";
import vmsg from "..";
// https://github.com/parcel-bundler/parcel/issues/289
if (module.hot) {
module.hot.dispose(() => {
location.reload();
});
}
class Post extends React.Component {
render() {
return (
<article className="post">
<h3 className="post__header">Example post</h3>
<section className="post__body">
So here is a simple demo of <a href="https://github.com/Kagami/vmsg">vmsg library</a>. Imagine this is a blog post or forum thread. Below you can leave text comments, as usual. But there is one more button: “Record”. If you press it vmsg library will open you a microphone recording form. Resulting record will automatically be encoded to MP3 so file won't weight too much. So you can easily share your voice messages even on mobile network and server needs to neither waste CPU time by encoding to MP3 by itself nor using a lot of disk space to store records.
</section>
<Comments />
</article>
);
}
}
class Comments extends React.Component {
constructor(props) {
super(props);
this.state = {
comments: [],
};
this.idCounter = 0;
}
handleReplySend = (comment) => {
const { comments } = this.state;
comment = { ...comment, id: this.idCounter++ };
this.setState({comments: comments.concat(comment)});
};
render() {
const { comments } = this.state;
return (
<aside className="comments">
{comments.map(props =>
<Comment key={props.id} {...props} />
)}
<Reply onSend={this.handleReplySend} />
</aside>
);
}
}
class Comment extends React.Component {
constructor(props) {
super(props);
if (props.record) {
this.audio = new Audio();
this.audio.src = URL.createObjectURL(props.record);
}
}
handleRecordOver = () => {
this.audio.currentTime = 0;
this.audio.play();
};
handleRecordOut = () => {
this.audio.pause();
};
handleRecordClick = () => {
const a = document.createElement("a");
if (!("download" in a)) {
window.open(this.audio.src);
return;
}
a.href = this.audio.src;
a.download = "record.mp3";
a.style.display = "none";
document.body.appendChild(a);
a.click();
a.remove();
};
render() {
const { id, body } = this.props;
return (
<article className="comment">
<header className="comment__header">
<h5 className="comment__id">
Comment #{id + 1}
</h5>
{this.renderRecord()}
</header>
<blockquote className="comment__body">
{body}
</blockquote>
</article>
);
}
renderRecord() {
const { record } = this.props;
if (!record) return null;
return (
<span
className="comment__record"
title="Hover to play / click to download"
onMouseOver={this.handleRecordOver}
onMouseOut={this.handleRecordOut}
onClick={this.handleRecordClick}
>
♫
</span>
)
}
}
class Reply extends React.Component {
constructor(props) {
super(props);
this.state = {
body: "",
record: null,
};
}
handleBodyChange = (e) => {
this.setState({body: e.target.value});
};
handleRecord = () => {
vmsg.record({
wasmURL: require("../vmsg.wasm"),
shimURL: "https://unpkg.com/[email protected]/wasm-polyfill.js",
}).then(record => {
this.setState({record});
});
};
handleSend = () => {
const { body, record } = this.state;
this.setState({body: "", record: null});
this.props.onSend({body, record});
};
render() {
const { body, record } = this.state;
return (
<article className="reply">
<textarea
className="reply__body"
placeholder="Enter your comment here…"
autoFocus
value={body}
onChange={this.handleBodyChange}
/>
<footer className="reply__controls">
<button className="reply-control" disabled={!body} onClick={this.handleSend}>
Send
</button>
<button className="reply-control" onClick={this.handleRecord}>
Record
</button>
{this.renderRecord()}
</footer>
</article>
);
}
renderRecord() {
const { record } = this.state;
if (!record) return null;
const size = (record.size / 1024).toFixed(1) + "KB";
return (
<span className="reply-record">
record.mp3 ({size})
</span>
);
}
}
ReactDOM.render(<Post/>, document.querySelector(".app"));