mirror of
https://github.com/piskelapp/piskel.git
synced 2023-08-10 21:12:52 +03:00
Issue #287 : Move utility functions from Shortcuts.js to ShortcutService
This commit is contained in:
@ -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();
|
||||
}
|
||||
};
|
||||
|
||||
@ -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);
|
||||
@ -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();
|
||||
|
@ -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);
|
||||
};
|
||||
|
||||
})();
|
||||
|
@ -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))
|
||||
*/
|
||||
@ -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']
|
||||
};
|
||||
})();
|
||||
|
Reference in New Issue
Block a user