Skip to content

Commit

Permalink
Issue #287 : Move utility functions from Shortcuts.js to ShortcutService
Browse files Browse the repository at this point in the history
  • Loading branch information
juliandescottes committed Nov 12, 2015
1 parent 460688e commit 6d30941
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 70 deletions.
8 changes: 4 additions & 4 deletions src/js/controller/dialogs/CheatsheetController.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@

ns.CheatsheetController.prototype.onRestoreDefaultsClick_ = function () {
if (window.confirm('Replace all custom shortcuts by the default Piskel shortcuts ?')) {
pskl.service.keyboard.Shortcuts.restoreDefaultShortcuts();
pskl.app.shortcutService.restoreDefaultShortcuts();
}
};

Expand All @@ -47,7 +47,7 @@
}

var shortcutId = shortcutEl.dataset.shortcutId;
var shortcut = pskl.service.keyboard.Shortcuts.getShortcutById(shortcutId);
var shortcut = pskl.app.shortcutService.getShortcutById(shortcutId);

if (shortcutEl.classList.contains(SHORTCUT_EDITING_CLASSNAME)) {
shortcutEl.classList.remove(SHORTCUT_EDITING_CLASSNAME);
Expand All @@ -65,11 +65,11 @@
}

var shortcutId = shortcutEl.dataset.shortcutId;
var shortcut = pskl.service.keyboard.Shortcuts.getShortcutById(shortcutId);
var shortcut = pskl.app.shortcutService.getShortcutById(shortcutId);
var shortcutKeyObject = pskl.service.keyboard.KeyUtils.createKeyFromEvent(evt);
var shortcutKeyString = pskl.service.keyboard.KeyUtils.stringify(shortcutKeyObject);

pskl.service.keyboard.Shortcuts.updateShortcut(shortcut, shortcutKeyString);
pskl.app.shortcutService.updateShortcut(shortcut, shortcutKeyString);

shortcutEl.classList.remove(SHORTCUT_EDITING_CLASSNAME);
this.eventTrapInput.blur();
Expand Down
55 changes: 55 additions & 0 deletions src/js/service/keyboard/ShortcutService.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,59 @@
var targetTagName = evt.target.nodeName.toUpperCase();
return targetTagName === 'INPUT' || targetTagName === 'TEXTAREA';
};

ns.ShortcutService.prototype.getShortcutById = function (id) {
return pskl.utils.Array.find(this.getShortcuts(), function (shortcut) {
return shortcut.getId() === id;
});
};

ns.ShortcutService.prototype.getShortcuts = function () {
var shortcuts = [];
ns.Shortcuts.CATEGORIES.forEach(function (category) {
var shortcutMap = ns.Shortcuts[category];
Object.keys(shortcutMap).forEach(function (shortcutKey) {
shortcuts.push(shortcutMap[shortcutKey]);
});
});
return shortcuts;
};

ns.ShortcutService.prototype.updateShortcut = function (shortcut, keyAsString) {
var key = keyAsString.replace(/\s/g, '');

var isForbiddenKey = ns.Shortcuts.FORBIDDEN_KEYS.indexOf(key) != -1;
if (isForbiddenKey) {
$.publish(Events.SHOW_NOTIFICATION, [{
'content': 'Key cannot be remapped (' + keyAsString + ')',
'hideDelay' : 5000
}]);
} else {
this.removeKeyFromAllShortcuts_(key);
shortcut.updateKeys([key]);
$.publish(Events.SHORTCUTS_CHANGED);
}
};

ns.ShortcutService.prototype.removeKeyFromAllShortcuts_ = function (key) {
this.getShortcuts().forEach(function (s) {
if (s.removeKeys([key])) {
$.publish(Events.SHOW_NOTIFICATION, [{
'content': 'Shortcut key removed for ' + s.getId(),
'hideDelay' : 5000
}]);
}
});
};

/**
* Restore the default piskel key for all shortcuts
*/
ns.ShortcutService.prototype.restoreDefaultShortcuts = function () {
this.getShortcuts().forEach(function (shortcut) {
shortcut.restoreDefault();
});
$.publish(Events.SHORTCUTS_CHANGED);
};

})();
74 changes: 8 additions & 66 deletions src/js/service/keyboard/Shortcuts.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@
return new ns.Shortcut(id, description, defaultKey, displayKey);
};

/**
* List of keys that cannot be remapped. Either alternate keys, which are not displayed.
* Or really custom shortcuts such as the 1-9 for color palette shorctus
*/
var FORBIDDEN_KEYS = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '?', 'shift+?',
'del', 'back', 'ctrl+Y', 'ctrl+shift+Z'];

ns.Shortcuts = {
/**
* List of keys that cannot be remapped. Either alternate keys, which are not displayed.
* Or really custom shortcuts such as the 1-9 for color palette shorctus
*/
FORBIDDEN_KEYS : ['1', '2', '3', '4', '5', '6', '7', '8', '9', '?', 'shift+?',
'del', 'back', 'ctrl+Y', 'ctrl+shift+Z'],

/**
* Syntax : createShortcut(id, description, default key(s))
*/
Expand Down Expand Up @@ -74,64 +74,6 @@
'123456789'.split(''), '1 to 9')
},

CATEGORIES : ['TOOL', 'SELECTION', 'MISC', 'STORAGE', 'COLOR'],

getShortcutById : function (id) {
return pskl.utils.Array.find(ns.Shortcuts.getShortcuts(), function (shortcut) {
return shortcut.getId() === id;
});
},

getShortcuts : function () {
var shortcuts = [];
ns.Shortcuts.CATEGORIES.forEach(function (category) {
var shortcutMap = ns.Shortcuts[category];
Object.keys(shortcutMap).forEach(function (shortcutKey) {
shortcuts.push(shortcutMap[shortcutKey]);
});
});
return shortcuts;
},

updateShortcut : function (shortcut, keysString) {
keysString = keysString.replace(/\s/g, '');
var keys = keysString.split(',');

var hasForbiddenKey = FORBIDDEN_KEYS.some(function (forbiddenKey) {
return keys.some(function (key) {
return forbiddenKey == key;
});
});

if (hasForbiddenKey) {
$.publish(Events.SHOW_NOTIFICATION, [{
'content': 'Key cannot be remapped (' + keysString + ')',
'hideDelay' : 5000
}]);
return;
}

ns.Shortcuts.getShortcuts().forEach(function (s) {
if (s === shortcut) {
return;
}

if (s.removeKeys(keys)) {
$.publish(Events.SHOW_NOTIFICATION, [{
'content': 'Shortcut key removed for ' + s.getId(),
'hideDelay' : 5000
}]);
}
});
shortcut.updateKeys(keys);
$.publish(Events.SHORTCUTS_CHANGED);
},

restoreDefaultShortcuts : function () {
ns.Shortcuts.getShortcuts().forEach(function (shortcut) {
shortcut.restoreDefault();
});
$.publish(Events.SHORTCUTS_CHANGED);
}
CATEGORIES : ['TOOL', 'SELECTION', 'MISC', 'STORAGE', 'COLOR']
};
})();

0 comments on commit 6d30941

Please sign in to comment.