Shortcuts.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /**
  2. * Shortcuts.js
  3. *
  4. * Copyright, Moxiecode Systems AB
  5. * Released under LGPL License.
  6. *
  7. * License: http://www.tinymce.com/license
  8. * Contributing: http://www.tinymce.com/contributing
  9. */
  10. /**
  11. * Contains all logic for handling of keyboard shortcuts.
  12. */
  13. define("tinymce/Shortcuts", [
  14. "tinymce/util/Tools",
  15. "tinymce/Env"
  16. ], function(Tools, Env) {
  17. var each = Tools.each, explode = Tools.explode;
  18. var keyCodeLookup = {
  19. "f9": 120,
  20. "f10": 121,
  21. "f11": 122
  22. };
  23. return function(editor) {
  24. var self = this, shortcuts = {};
  25. editor.on('keyup keypress keydown', function(e) {
  26. if (e.altKey || e.ctrlKey || e.metaKey) {
  27. each(shortcuts, function(shortcut) {
  28. var ctrlKey = Env.mac ? e.metaKey : e.ctrlKey;
  29. if (shortcut.ctrl != ctrlKey || shortcut.alt != e.altKey || shortcut.shift != e.shiftKey) {
  30. return;
  31. }
  32. if (e.keyCode == shortcut.keyCode || (e.charCode && e.charCode == shortcut.charCode)) {
  33. e.preventDefault();
  34. if (e.type == "keydown") {
  35. shortcut.func.call(shortcut.scope);
  36. }
  37. return true;
  38. }
  39. });
  40. }
  41. });
  42. /**
  43. * Adds a keyboard shortcut for some command or function.
  44. *
  45. * @method addShortcut
  46. * @param {String} pattern Shortcut pattern. Like for example: ctrl+alt+o.
  47. * @param {String} desc Text description for the command.
  48. * @param {String/Function} cmdFunc Command name string or function to execute when the key is pressed.
  49. * @param {Object} sc Optional scope to execute the function in.
  50. * @return {Boolean} true/false state if the shortcut was added or not.
  51. */
  52. self.add = function(pattern, desc, cmdFunc, scope) {
  53. var cmd;
  54. cmd = cmdFunc;
  55. if (typeof(cmdFunc) === 'string') {
  56. cmdFunc = function() {
  57. editor.execCommand(cmd, false, null);
  58. };
  59. } else if (Tools.isArray(cmd)) {
  60. cmdFunc = function() {
  61. editor.execCommand(cmd[0], cmd[1], cmd[2]);
  62. };
  63. }
  64. each(explode(pattern.toLowerCase()), function(pattern) {
  65. var shortcut = {
  66. func: cmdFunc,
  67. scope: scope || editor,
  68. desc: editor.translate(desc),
  69. alt: false,
  70. ctrl: false,
  71. shift: false
  72. };
  73. each(explode(pattern, '+'), function(value) {
  74. switch (value) {
  75. case 'alt':
  76. case 'ctrl':
  77. case 'shift':
  78. shortcut[value] = true;
  79. break;
  80. default:
  81. shortcut.charCode = value.charCodeAt(0);
  82. shortcut.keyCode = keyCodeLookup[value] || value.toUpperCase().charCodeAt(0);
  83. }
  84. });
  85. shortcuts[
  86. (shortcut.ctrl ? 'ctrl' : '') + ',' +
  87. (shortcut.alt ? 'alt' : '') + ',' +
  88. (shortcut.shift ? 'shift' : '') + ',' +
  89. shortcut.keyCode
  90. ] = shortcut;
  91. });
  92. return true;
  93. };
  94. };
  95. });