Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Voice Recognition for Editor #1010

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion app/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,10 @@

<link rel="stylesheet" href="styles/core.css">
<link rel="stylesheet" href="styles/theme-default.css">

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.4.0/css/font-awesome.min.css">

<script src="bower_components/modernizr/modernizr.js"></script>

</head>
<body class="-noscroll">

Expand Down
8 changes: 7 additions & 1 deletion app/scripts/apps/notes/form/templates/form.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@
</div>

<ul class="nav navbar-nav navbar-right header--right">
<li>
<button type= "button" title="click to add notes by voice" class="editor--mic btn header--btn ">
<i id = "editor--micIcon" class="fa fa-microphone"></i>
</button>
</li>
<li class="dropdown">
<button type="button" title="Mode" class="btn header--btn btn-default" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">
<i class="icon-fullscreen"></i> <b class="caret hidden-xs"></b>
Expand Down Expand Up @@ -44,7 +49,8 @@
</nav>

<div class="layout--body -scroll -form">
<div class="container-fluid editor--container">
<div class="container-fluid editor--container">

<form class="editor--form form-horizontal col-xs-12" action="#" method="post">
<div class="form-group">
<input id="editor--input--title" class="form-control -borderless" name="title"
Expand Down
94 changes: 87 additions & 7 deletions app/scripts/apps/notes/form/views/formView.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ define([
'behaviors/content',
'apps/notes/form/behaviors/desktop',
'apps/notes/form/behaviors/mobile',
'modules/codemirror/module',
'mousetrap.global'
], function($, _, Marionette, Radio, Mousetrap, Tmpl, Behavior, Desktop, Mobile) {
], function($, _, Marionette, Radio, Mousetrap, Tmpl, Behavior, Desktop, Mobile,codeMirror) {
'use strict';

/**
Expand Down Expand Up @@ -69,7 +70,9 @@ define([
// Form
form : '.editor--form',
saveBtn : '.editor--save',
title : '#editor--input--title'
title : '#editor--input--title',
micBtn : '.editor--mic',
micIcon : '#editor--micIcon'
},

events: {
Expand All @@ -78,7 +81,9 @@ define([
// Handle saving
'submit @ui.form' : 'save',
'click @ui.saveBtn' : 'save',
'click .editor--cancel' : 'cancel'
'click .editor--cancel' : 'cancel',
'click @ui.micBtn' : 'micBtnClick',

},

initialize: function() {
Expand Down Expand Up @@ -156,9 +161,9 @@ define([
return;
}

// Don't save tags when auto save notes
// so that no unfinished tags are saved
this.options.saveTags = false;
// Don't save tags when auto save notes
// so that no unfinished tags are saved
this.options.saveTags = false;

this.options.redirect = false;
console.log('Auto saving the note...');
Expand All @@ -170,14 +175,89 @@ define([
e.preventDefault();
}

this.options.saveTags = true;
this.options.saveTags = true;
this.options.isClosed = true;
this.options.redirect = true;
this.trigger('save');

return false;
},

micBtnClick: function() {

const SpeechRecognition = window.SpeechRecognition || window.webkitSpeechRecognition; // if none exists -> undefined

if(SpeechRecognition) {

const recognition = new SpeechRecognition(); // manages full recognition process
recognition.continuous = true;
SpeechRecognition.interimResults = true; //display interim results


if(this.ui.micIcon[0].classList.contains("fa-microphone")) { // Start Voice Recognition

// toggle micTcon
this.ui.micIcon[0].classList.remove("fa-microphone");
this.ui.micIcon[0].classList.add("fa-microphone-slash");

//start recognition
recognition.start();


}
else { // end voice recognition

//toggle micIcon
this.ui.micIcon[0].classList.remove("fa-microphone-slash");
this.ui.micIcon[0].classList.add("fa-microphone");

//stop recognition
recognition.stop();

}


recognition.onresult = function (event) {

//recognize voice
const current = event.resultIndex;
const transcript = event.results[current][0].transcript;

//update the editor

if (transcript.toLowerCase().trim() === "enter") {
var text = codeMirror.controller.editor.getValue();
codeMirror.controller.editor.setValue(text+"\n");
}
else if (transcript.toLowerCase().trim() === "comma") {
var text = codeMirror.controller.editor.getValue();
codeMirror.controller.editor.setValue(text+",");
}
else if (transcript.toLowerCase().trim() === "period") {
var text = codeMirror.controller.editor.getValue();
codeMirror.controller.editor.setValue(text+".");
}
else if (transcript.toLowerCase().trim() === "exclamation mark") {
var text = codeMirror.controller.editor.getValue();
codeMirror.controller.editor.setValue(text+"!");
}
else {
var text = codeMirror.controller.editor.getValue();
codeMirror.controller.editor.setValue(text+transcript);

}


}

}

else{
alert("Sorry, Your Browser does not support Voice Recognition");
}
return false;
},

switchMode: function(e) {
var mode = (typeof e !== 'string' ? $(e.currentTarget).attr('data-mode') : e);
if (!mode) {
Expand Down