diff --git a/lib/atom-dictation-view.coffee b/lib/atom-dictation-view.coffee index 5d1be11..a614c23 100644 --- a/lib/atom-dictation-view.coffee +++ b/lib/atom-dictation-view.coffee @@ -1,9 +1,9 @@ -recognition = {} -speaking = false -listening = false - module.exports = class AtomDictationView + recognition = {} + speaking = false + listening = false + constructor: (serializeState) -> # Create root element @element = document.createElement('div') @@ -39,7 +39,7 @@ class AtomDictationView destroy: -> @element.remove() - listen: -> + toggleListening: -> if not listening # Clear out the UI stuff @interimTranscriptElement.textContent = "" @@ -86,22 +86,3 @@ class AtomDictationView @finalTranscriptElement.textContent += finalTranscript @interimTranscriptElement.textContent = interimTranscript - -# speak: -> -# console.log 'AtomDictationView is speaking!' -# -# if not speaking -# console.log "Speaking to you!" -# else -# console.log "Done speaking to you!" -# -# # Create message element -# message = document.createElement('div') -# message.textContent = "The AtomDictation package about to speak to you!" -# message.classList.add('message') -# @element.appendChild(message) -# -# # if @element.parentElement? -# # @element.remove() -# # else -# # atom.workspaceView.append(@element) diff --git a/lib/atom-dictation.coffee b/lib/atom-dictation.coffee index f756193..3ef737b 100644 --- a/lib/atom-dictation.coffee +++ b/lib/atom-dictation.coffee @@ -1,11 +1,14 @@ AtomDictationView = require './atom-dictation-view' +Speaker = require './speaker' {CompositeDisposable} = require 'atom' module.exports = atomDictationView: null + speaker: null activate: (state) -> @atomDictationView = new AtomDictationView(state.atomDictationViewState) + @speaker = new Speaker() # Events subscribed to in atom's system can be easily cleaned up with a CompositeDisposable @subscriptions = new CompositeDisposable @@ -14,7 +17,7 @@ module.exports = @subscriptions.add atom.commands.add 'atom-workspace', 'atom-dictation:listen': => @toggleListening() # Register command that reads starting from the cursor - # @subscriptions.add atom.commands.add 'atom-workspace', 'atom-dictation:speak': => @toggleSpeaking() + @subscriptions.add atom.commands.add 'atom-workspace', 'atom-dictation:speak': => @toggleSpeaking() deactivate: -> @atomDictationView.destroy() @@ -23,4 +26,8 @@ module.exports = atomDictationViewState: @atomDictationView.serialize() toggleListening: -> + @atomDictationView.toggleListening() @atomDictationView.listen() + + toggleSpeaking: -> + @speaker.speak() diff --git a/lib/speaker.coffee b/lib/speaker.coffee new file mode 100644 index 0000000..779a579 --- /dev/null +++ b/lib/speaker.coffee @@ -0,0 +1,29 @@ +{CompositeDisposable} = require 'atom' + +module.exports = +class Speaker + speaking = false + currentUtterance = null + + constructor: (serializeState) -> + + speak: -> + if not speaking + if editor = atom.workspace.getActiveTextEditor() + text = editor.getText() + currentUtterance = new SpeechSynthesisUtterance(text) + currentUtterance.on('') + speechSynthesis.speak(currentUtterance); + + console.log "Speaking to you!" + + else + if message + message.cancel() + console.log "Done speaking to you!" + + # Toggle the state + speaking = !speaking + + speechFinished: -> + speaking = false