|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace luya\texttospeech; |
|
4
|
|
|
|
|
5
|
|
|
use luya\base\Widget; |
|
6
|
|
|
use luya\helpers\Json; |
|
7
|
|
|
use Yii; |
|
8
|
|
|
use yii\base\InvalidConfigException; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Text to Speech. |
|
12
|
|
|
* |
|
13
|
|
|
* Using the browsers text to speech option to return a websites text. Either be providing the text as string or read the text from a given css class selector. |
|
14
|
|
|
* |
|
15
|
|
|
* Example using a Selector: |
|
16
|
|
|
* |
|
17
|
|
|
* ``` |
|
18
|
|
|
* <?php |
|
19
|
|
|
* TextToSpeechWidget::widget(['targetSelector' => '#content']); |
|
20
|
|
|
* ?> |
|
21
|
|
|
* <div id="content"> |
|
22
|
|
|
* Hello World, this is LUYA Text to Speech! |
|
23
|
|
|
* </div> |
|
24
|
|
|
* ``` |
|
25
|
|
|
* |
|
26
|
|
|
* By default the Widget will generate a play, pause and stop icon at the location the widget is integrated. |
|
27
|
|
|
* |
|
28
|
|
|
* @author Martin Petrasch <[email protected]> |
|
29
|
|
|
* @author Basil Suter <[email protected]> |
|
30
|
|
|
* @since 1.1.0 |
|
31
|
|
|
*/ |
|
32
|
|
|
class TextToSpeechWidget extends Widget |
|
33
|
|
|
{ |
|
34
|
|
|
/** |
|
35
|
|
|
* @var string The jQuery selector which should be used to read the content from, examples: |
|
36
|
|
|
* - `.container`: All elements which class `.container` will be spoken. |
|
37
|
|
|
* - `#content`: The element with id `id="content"` only will be spoken. |
|
38
|
|
|
* |
|
39
|
|
|
* The input data will be wrapped with `$()`. |
|
40
|
|
|
*/ |
|
41
|
|
|
public $targetSelector; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @var string If enabled, the {{$targetSelector}} attribute has no effect and only the text from the property will be read - on start. |
|
45
|
|
|
*/ |
|
46
|
|
|
public $text; |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @var string The selector for the play button. |
|
50
|
|
|
*/ |
|
51
|
|
|
public $playButtonSelector = '#play-button'; |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @var string The selector for the pause button. |
|
55
|
|
|
*/ |
|
56
|
|
|
public $pauseButtonSelector = '#pause-button'; |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* @var string The selector for the stop button. |
|
60
|
|
|
*/ |
|
61
|
|
|
public $stopButtonSelector = '#stop-button'; |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @var string The class which will be assigned to the play button when playing sound. |
|
65
|
|
|
*/ |
|
66
|
|
|
public $playButtonActiveClass = 'playing'; |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* @var string The class which will be assigned to the pause button when pause button is clicked. |
|
70
|
|
|
*/ |
|
71
|
|
|
public $pauseButtonActiveClass = 'paused'; |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* @var string The class of the div in which the buttons are located, the button wrapper div class. |
|
75
|
|
|
*/ |
|
76
|
|
|
public $containerClass = 'text-to-speech-container'; |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* @var string The class which each of the text-to-speech buttons recieves. |
|
80
|
|
|
*/ |
|
81
|
|
|
public $buttonClass = 'text-to-speech-button'; |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* @var array|boolean You can either disable the default buttons by setting buttons to false, or you can provide an array with button configurations, each element requires the following keys: |
|
85
|
|
|
* - label: |
|
86
|
|
|
* - id: |
|
87
|
|
|
* - content: |
|
88
|
|
|
*/ |
|
89
|
|
|
public $buttons; |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* @var string The SVG code for the play icon when using default buttons. |
|
93
|
|
|
*/ |
|
94
|
|
|
public $playSVG = '<svg version="1.1" id="Capa_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" viewBox="0 0 60 60" style="enable-background:new 0 0 60 60;" xml:space="preserve"><g><path d="M45.563,29.174l-22-15c-0.307-0.208-0.703-0.231-1.031-0.058C22.205,14.289,22,14.629,22,15v30c0,0.371,0.205,0.711,0.533,0.884C22.679,45.962,22.84,46,23,46c0.197,0,0.394-0.059,0.563-0.174l22-15C45.836,30.64,46,30.331,46,30S45.836,29.36,45.563,29.174z M24,43.107V16.893L43.225,30L24,43.107z"/><path d="M30,0C13.458,0,0,13.458,0,30s13.458,30,30,30s30-13.458,30-30S46.542,0,30,0z M30,58C14.561,58,2,45.439,2,30S14.561,2,30,2s28,12.561,28,28S45.439,58,30,58z"/></g><g></g><g></g><g></g><g></g><g></g><g></g><g></g><g></g><g></g><g></g><g></g><g></g><g></g><g></g><g></g></svg>'; |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* @var string The SVG code for the stop icon when using default buttons. |
|
98
|
|
|
*/ |
|
99
|
|
|
public $stopSVG = '<svg version="1.1" id="Capa_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" viewBox="0 0 60 60" style="enable-background:new 0 0 60 60;" xml:space="preserve"><path d="M30,0C13.458,0,0,13.458,0,30s13.458,30,30,30s30-13.458,30-30S46.542,0,30,0z M44,44H16V16h28V44z"/><g></g><g></g><g></g><g></g><g></g><g></g><g></g><g></g><g></g><g></g><g></g><g></g><g></g><g></g><g></g></svg>'; |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* @var string The SVG code for the pause icon when using default buttons. |
|
103
|
|
|
*/ |
|
104
|
|
|
public $pauseSVG = '<svg version="1.1" id="Capa_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" viewBox="0 0 202.205 202.205" style="enable-background:new 0 0 202.205 202.205;" xml:space="preserve"><g> <g> <path style="fill:#010002;" d="M23.483,202.205H85.83V0H23.483V202.205z M31.417,7.934h46.479v186.336H31.417V7.934z"/> <path style="fill:#010002;" d="M116.372,0v202.205h62.351V0H116.372z M170.788,194.271h-46.486V7.934h46.482v186.336H170.788z"/> </g></g><g></g><g></g><g></g><g></g><g></g><g></g><g></g><g></g><g></g><g></g><g></g><g></g><g></g><g></g><g></g></svg>'; |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* {@inheritDoc} |
|
108
|
|
|
*/ |
|
109
|
|
|
public function init() |
|
110
|
|
|
{ |
|
111
|
|
|
if (!$this->text && !$this->targetSelector) { |
|
112
|
|
|
throw new InvalidConfigException("Either text or targetSelector property must be configured."); |
|
113
|
|
|
} |
|
114
|
|
|
if ($this->buttons === null || $this->buttons === true) { |
|
115
|
|
|
$this->buttons[] = $this->createButton('Play', 'play-button', $this->playSVG); |
|
116
|
|
|
$this->buttons[] = $this->createButton('Pause', 'pause-button', $this->pauseSVG); |
|
117
|
|
|
$this->buttons[] = $this->createButton('Stop', 'stop-button', $this->stopSVG); |
|
118
|
|
|
} |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* {@inheritDoc} |
|
123
|
|
|
*/ |
|
124
|
|
|
public function run() |
|
125
|
|
|
{ |
|
126
|
|
|
TextToSpeechAsset::register($this->view); |
|
127
|
|
|
|
|
128
|
|
|
$config = Json::htmlEncode([ |
|
129
|
|
|
'playButtonSelector' => $this->playButtonSelector, |
|
130
|
|
|
'pauseButtonSelector' => $this->pauseButtonSelector, |
|
131
|
|
|
'stopButtonSelector' => $this->stopButtonSelector, |
|
132
|
|
|
'targetSelector' => $this->targetSelector, |
|
133
|
|
|
'playClass' => $this->playButtonActiveClass, |
|
134
|
|
|
'pauseClass' => $this->pauseButtonActiveClass, |
|
135
|
|
|
'text' => $this->text ? $this->text : '', // must be an empty string as it will checked with .length |
|
136
|
|
|
'language' => Yii::$app->composition->langShortCode, |
|
137
|
|
|
]); |
|
138
|
|
|
|
|
139
|
|
|
$this->view->registerJs("$.textToSpeech({$config});"); |
|
140
|
|
|
|
|
141
|
|
|
return $this->render('texttospeech', [ |
|
142
|
|
|
'buttons' => $this->buttons, |
|
143
|
|
|
'id' => $this->getId(), |
|
144
|
|
|
'containerClass' => $this->containerClass, |
|
145
|
|
|
'buttonClass' => $this->buttonClass, |
|
146
|
|
|
]); |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
/** |
|
150
|
|
|
* Create a default button element. |
|
151
|
|
|
* |
|
152
|
|
|
* @param string $label |
|
153
|
|
|
* @param string $id |
|
154
|
|
|
* @param string $svg |
|
155
|
|
|
* @return array |
|
156
|
|
|
*/ |
|
157
|
|
|
protected function createButton($label, $id, $svg) |
|
158
|
|
|
{ |
|
159
|
|
|
return [ |
|
160
|
|
|
'label' => $label, |
|
161
|
|
|
'id' => $id, |
|
162
|
|
|
'content' => $svg, |
|
163
|
|
|
]; |
|
164
|
|
|
} |
|
165
|
|
|
} |