forked from manki11/ExerciserReact
-
Notifications
You must be signed in to change notification settings - Fork 49
/
WithMultimedia.js
170 lines (156 loc) · 6.23 KB
/
WithMultimedia.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
import React from 'react';
import datastore from 'lib/sugar-web/datastore';
import chooser from 'lib/sugar-web/graphics/journalchooser';
import env from 'lib/sugar-web/env';
import picoModal from 'picomodal';
import meSpeak from 'mespeak';
const withMultimedia = (defaultThumbnail) => (Component) => {
class MultimediaHoc extends React.Component {
constructor(props) {
super(props);
this.state = {
thumbnail: '',
userLanguage: ''
}
}
componentDidMount() {
if (this.props.location.state) {
const { thumbnail, userLanguage } = this.props.location.state.exercise;
let newThumbnail = thumbnail;
// For default exercises
if(thumbnail && !thumbnail.startsWith('data:image'))
newThumbnail = require(`../images/defaultExerciseThumbnail/${thumbnail}`);
this.setState({
...this.state,
thumbnail: newThumbnail,
userLanguage: userLanguage
});
}
this.textToSpeechSetup();
}
textToSpeechSetup = () => {
env.getEnvironment((err, environment) => {
var defaultLanguage = (typeof window.chrome !== 'undefined' && window.chrome.app && window.chrome.app.runtime) ? window.chrome.i18n.getUILanguage() : navigator.language;
if (!environment.user) environment.user = { language: defaultLanguage };
let userLanguage = environment.user.language;
try{
if(userLanguage.startsWith('en'))
require(`mespeak/voices/en/${userLanguage}.json`);
else
require(`mespeak/voices/${userLanguage}.json`);
} catch(error) {
userLanguage = 'en';
}
this.setState({
...this.state,
userLanguage: userLanguage
}, () => {
if(userLanguage.startsWith('en'))
meSpeak.loadVoice(require(`mespeak/voices/en/${this.state.userLanguage}.json`));
else
meSpeak.loadVoice(require(`mespeak/voices/${this.state.userLanguage}.json`));
})
});
}
insertThumbnail = () => {
env.getEnvironment( (err, environment) => {
if(environment.user) {
// Display journal dialog popup
chooser.show((entry) => {
if (!entry) {
return;
}
var dataentry = new datastore.DatastoreObject(entry.objectId);
dataentry.loadAsText((err, metadata, text) => {
this.setState({
...this.state,
thumbnail: text
});
});
}, {mimetype: 'image/png'}, {mimetype: 'image/jpeg'});
}
});
};
showMedia = (imageSource) => {
picoModal({
content: (
`<img src = ${imageSource} \
style='max-height: 100%;\
max-width: 100%;\
margin: auto;\
left: 0;\
right: 0;\
top: 0;\
bottom: 0;\
position: absolute;'>\
</img>\
<button id='close-button' style='background-image: url(${require('../icons/exercise/delete.svg')});\
position: absolute; right: 0px; width: 50px; height: 50px; margin-top: 5px;\
border-radius: 25px; background-position: center; background-size: contain; \
background-repeat: no-repeat'>\
</button>`),
closeButton: false,
modalStyles: {
backgroundColor: "#e5e5e5",
height: "400px",
width: "600px",
maxWidth: "90%"
}
})
.afterShow(function(modal) {
let closeButton = document.getElementById('close-button');
closeButton.addEventListener('click', function() {
modal.close();
});
})
.afterClose((modal) => {
modal.destroy();
})
.show();
};
deleteThumbnail = () => {
this.setState({
...this.state,
thumbnail:''
});
}
render() {
// Thumbnail
let thumbnail;
if(this.state.thumbnail === '' && defaultThumbnail) {
thumbnail = (
<div className = "media-background">
<img src = {defaultThumbnail}
style = {{height: '200px'}}
onClick = {() => {this.showMedia(defaultThumbnail)}}
alt="Thumbnail"/>
</div>
);
} else {
thumbnail = (
<div className = "media-background">
<img src = {this.state.thumbnail}
style = {{height: '200px'}}
onClick = {() => {this.showMedia(this.state.thumbnail)}}
alt="Thumbnail"/>
<button className="btn button-cancel"
onClick={this.deleteThumbnail}>
</button>
</div>
);
}
return (
<Component
{...this.props}
srcThumbnail={this.state.thumbnail}
userLanguage={this.state.userLanguage}
thumbnail={thumbnail}
insertThumbnail={this.insertThumbnail}
showMedia={this.showMedia}
/>
);
}
}
return MultimediaHoc;
}
export default withMultimedia