Skip to content

Commit

Permalink
Update how key presses are tracked in the recorder. Fixes #5.
Browse files Browse the repository at this point in the history
  • Loading branch information
jeresig committed Apr 23, 2012
1 parent a2c29be commit 57dcb29
Showing 1 changed file with 49 additions and 14 deletions.
63 changes: 49 additions & 14 deletions js/editor.js
Expand Up @@ -27,15 +27,41 @@ var Editor = function( id ) {


editor.offset = editor.content.offset(); editor.offset = editor.content.offset();


var canon = require("pilot/canon");

if ( window.Record ) { if ( window.Record ) {
editor.textarea.bind( "keydown", function( e ) { var event = require("pilot/event"),
if ( e.keyCode && (e.keyCode < 48 && e.keyCode !== 13 && e.keyCode !== 32 || paste = false;
e.altKey || e.ctrlKey || e.metaKey) ) {

editor.editor.keyBinding.setKeyboardHandler({
Record.log({ key: e.keyCode, altKey: e.altKey, ctrlKey: e.ctrlKey, handleKeyboard: function($data, hashId, keyOrText, keyCode, e) {
metaKey: e.metaKey, shiftKey: e.shiftKey }); var isCommand = canon.findKeyCommand({editor: editor.editor}, "editor", hashId, keyOrText),
isEmpty = jQuery.isEmptyObject( e );

if ( isCommand && !isEmpty ) {
Record.log({ cmd: isCommand.name });

} else if ( !isCommand && isEmpty ) {
if ( !paste ) {
Record.log({ key: keyOrText });
}
paste = false;
}
} }
}); });

editor.editor.addEventListener( "copy", function() {
Record.log({ copy: 1 });
});

editor.editor.addEventListener( "paste", function( text ) {
paste = true;
Record.log({ paste: text });
});

editor.editor.addEventListener( "cut", function() {
Record.log({ cut: 1 });
});
} }


editor.reset(); editor.reset();
Expand All @@ -58,21 +84,30 @@ var Editor = function( id ) {


// Add in record playback handlers. // Add in record playback handlers.
jQuery.extend( Record.handlers, { jQuery.extend( Record.handlers, {
cut: function() {
editor.editor.onCut();
},

copy: function() {
editor.editor.getCopyText();
},

paste: function( e ) {
editor.editor.onTextInput( e.paste, true );
},

cmd: function( e ) {
canon.exec( e.cmd, { editor: editor.editor }, "editor" );
},

key: function( e ) { key: function( e ) {
editor.textarea.simulate( "keydown", { keyCode: e.key, editor.editor.onTextInput( e.key, false );
altKey: e.altKey, ctrlKey: e.ctrlKey, metaKey: e.metaKey, shiftKey: e.shiftKey } );
}, },


focus: function() { focus: function() {
editor.textarea[0].focus(); editor.textarea[0].focus();
}, },


text: function( e ) {
var evt = document.createEvent("TextEvent");
evt.initTextEvent( "textInput", true, true, null, e.text );
Editor.textarea[0].dispatchEvent( evt );
},

x: function( e ) { x: function( e ) {
var evt = { clientX: editor.offset.left + e.x, clientY: editor.offset.top + e.y }; var evt = { clientX: editor.offset.left + e.x, clientY: editor.offset.top + e.y };
editor.content.simulate( "mousedown", evt ); editor.content.simulate( "mousedown", evt );
Expand Down

0 comments on commit 57dcb29

Please sign in to comment.