mirror of
https://github.com/piskelapp/piskel.git
synced 2023-08-10 21:12:52 +03:00
feature : add keyboard shortcuts : keycodes
+ moved keycode translation to KeycodeTranslator + made KeycodeTranslator more generic to handle 0-9 and a-z keys + small refactor in KeyboardEventService
This commit is contained in:
parent
5d83a39cf0
commit
25e6470499
@ -1,13 +1,8 @@
|
||||
(function () {
|
||||
var ns = $.namespace("pskl.service");
|
||||
|
||||
ns.KeyboardEventService = function () {};
|
||||
|
||||
/**
|
||||
* @private
|
||||
*/
|
||||
ns.KeyboardEventService.prototype.KeyboardActions_ = {
|
||||
|
||||
ns.KeyboardEventService = function () {
|
||||
this.keyboardActions_ = {
|
||||
"ctrl" : {
|
||||
"z" : Events.UNDO,
|
||||
"y" : Events.REDO,
|
||||
@ -16,43 +11,8 @@
|
||||
"v" : Events.PASTE
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* @private
|
||||
*/
|
||||
ns.KeyboardEventService.prototype.CharCodeToKeyCodeMap_ = {
|
||||
|
||||
90 : "z",
|
||||
89 : "y",
|
||||
88 : "x",
|
||||
67 : "c",
|
||||
86 : "v"
|
||||
};
|
||||
|
||||
/**
|
||||
* @private
|
||||
*/
|
||||
ns.KeyboardEventService.prototype.onKeyUp_ = function(evt) {
|
||||
var isMac = false;
|
||||
if (navigator.appVersion.indexOf("Mac")!=-1) {
|
||||
// Welcome in mac world where vowels are consons and meta used instead of ctrl:
|
||||
isMac = true;
|
||||
}
|
||||
|
||||
if (isMac ? evt.metaKey : evt.ctrlKey) {
|
||||
// Get key pressed:
|
||||
var letter = this.CharCodeToKeyCodeMap_[evt.which];
|
||||
if(letter) {
|
||||
var eventToTrigger = this.KeyboardActions_.ctrl[letter];
|
||||
if(eventToTrigger) {
|
||||
$.publish(eventToTrigger);
|
||||
|
||||
evt.preventDefault();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* @public
|
||||
@ -61,4 +21,33 @@
|
||||
$(document.body).keydown($.proxy(this.onKeyUp_, this));
|
||||
};
|
||||
|
||||
/**
|
||||
* @private
|
||||
*/
|
||||
ns.KeyboardEventService.prototype.onKeyUp_ = function(evt) {
|
||||
var eventToTrigger;
|
||||
|
||||
// jquery names FTW ...
|
||||
var keycode = evt.which;
|
||||
var charkey = pskl.service.keyboard.KeycodeTranslator.toChar(keycode);
|
||||
|
||||
if(charkey) {
|
||||
if (this.isCtrlKeyPressed_(evt)) {
|
||||
eventToTrigger = this.keyboardActions_.ctrl[charkey];
|
||||
}
|
||||
}
|
||||
|
||||
if(eventToTrigger) {
|
||||
$.publish(eventToTrigger);
|
||||
evt.preventDefault();
|
||||
}
|
||||
};
|
||||
|
||||
ns.KeyboardEventService.prototype.isCtrlKeyPressed_ = function (evt) {
|
||||
return this.isMac_() ? evt.metaKey : evt.ctrlKey;
|
||||
};
|
||||
|
||||
ns.KeyboardEventService.prototype.isMac_ = function () {
|
||||
return navigator.appVersion.indexOf("Mac") != -1;
|
||||
};
|
||||
})();
|
17
js/service/keyboard/KeyboardEvent.js
Normal file
17
js/service/keyboard/KeyboardEvent.js
Normal file
@ -0,0 +1,17 @@
|
||||
(function () {
|
||||
var ns = $.namespace('service.keyboard');
|
||||
|
||||
ns.KeyboardEvent = function (eventName, args, description) {
|
||||
this.eventName = eventName;
|
||||
this.args = args;
|
||||
this.description = description;
|
||||
};
|
||||
|
||||
ns.KeyboardEvent.prototype.fire = function () {
|
||||
$.publish(this.eventName, this.args);
|
||||
};
|
||||
|
||||
ns.KeyboardEvent.prototype.getDescription = function () {
|
||||
return this.description;
|
||||
};
|
||||
})();
|
19
js/service/keyboard/KeycodeTranslator.js
Normal file
19
js/service/keyboard/KeycodeTranslator.js
Normal file
@ -0,0 +1,19 @@
|
||||
(function () {
|
||||
var specialKeys = {};
|
||||
|
||||
var ns = $.namespace('service.keyboard');
|
||||
|
||||
ns.KeycodeTranslator= {
|
||||
toChar : function (keycode) {
|
||||
if (keycode >= 48 && keycode <= 57) {
|
||||
// key is 0-9
|
||||
return (keycode - 48) + "";
|
||||
} else if (keycode >= 65 && keycode <= 90) {
|
||||
// key is a-z, we'll use base 36 to get the string representation
|
||||
return (keycode - 65 + 10).toString(36);
|
||||
} else {
|
||||
return specialKeys[keycode];
|
||||
}
|
||||
}
|
||||
};
|
||||
})();
|
@ -62,6 +62,7 @@ exports.scripts = [
|
||||
// Services
|
||||
"js/service/LocalStorageService.js",
|
||||
"js/service/HistoryService.js",
|
||||
"js/service/keyboard/KeycodeTranslator.js",
|
||||
"js/service/KeyboardEventService.js",
|
||||
"js/service/ImageUploadService.js",
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user