選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

getDefaultKeyBinding.js 3.2 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /**
  2. * Copyright (c) Facebook, Inc. and its affiliates.
  3. *
  4. * This source code is licensed under the MIT license found in the
  5. * LICENSE file in the root directory of this source tree.
  6. *
  7. * @format
  8. *
  9. * @emails oncall+draft_js
  10. */
  11. 'use strict';
  12. var KeyBindingUtil = require("./KeyBindingUtil");
  13. var Keys = require("fbjs/lib/Keys");
  14. var UserAgent = require("fbjs/lib/UserAgent");
  15. var isOSX = UserAgent.isPlatform('Mac OS X'); // Firefox on OSX had a bug resulting in navigation instead of cursor movement.
  16. // This bug was fixed in Firefox 29. Feature detection is virtually impossible
  17. // so we just check the version number. See #342765.
  18. var shouldFixFirefoxMovement = isOSX && UserAgent.isBrowser('Firefox < 29');
  19. var hasCommandModifier = KeyBindingUtil.hasCommandModifier,
  20. isCtrlKeyCommand = KeyBindingUtil.isCtrlKeyCommand;
  21. function shouldRemoveWord(e) {
  22. return isOSX && e.altKey || isCtrlKeyCommand(e);
  23. }
  24. /**
  25. * Get the appropriate undo/redo command for a Z key command.
  26. */
  27. function getZCommand(e) {
  28. if (!hasCommandModifier(e)) {
  29. return null;
  30. }
  31. return e.shiftKey ? 'redo' : 'undo';
  32. }
  33. function getDeleteCommand(e) {
  34. // Allow default "cut" behavior for PCs on Shift + Delete.
  35. if (!isOSX && e.shiftKey) {
  36. return null;
  37. }
  38. return shouldRemoveWord(e) ? 'delete-word' : 'delete';
  39. }
  40. function getBackspaceCommand(e) {
  41. if (hasCommandModifier(e) && isOSX) {
  42. return 'backspace-to-start-of-line';
  43. }
  44. return shouldRemoveWord(e) ? 'backspace-word' : 'backspace';
  45. }
  46. /**
  47. * Retrieve a bound key command for the given event.
  48. */
  49. function getDefaultKeyBinding(e) {
  50. switch (e.keyCode) {
  51. case 66:
  52. // B
  53. return hasCommandModifier(e) ? 'bold' : null;
  54. case 68:
  55. // D
  56. return isCtrlKeyCommand(e) ? 'delete' : null;
  57. case 72:
  58. // H
  59. return isCtrlKeyCommand(e) ? 'backspace' : null;
  60. case 73:
  61. // I
  62. return hasCommandModifier(e) ? 'italic' : null;
  63. case 74:
  64. // J
  65. return hasCommandModifier(e) ? 'code' : null;
  66. case 75:
  67. // K
  68. return isOSX && isCtrlKeyCommand(e) ? 'secondary-cut' : null;
  69. case 77:
  70. // M
  71. return isCtrlKeyCommand(e) ? 'split-block' : null;
  72. case 79:
  73. // O
  74. return isCtrlKeyCommand(e) ? 'split-block' : null;
  75. case 84:
  76. // T
  77. return isOSX && isCtrlKeyCommand(e) ? 'transpose-characters' : null;
  78. case 85:
  79. // U
  80. return hasCommandModifier(e) ? 'underline' : null;
  81. case 87:
  82. // W
  83. return isOSX && isCtrlKeyCommand(e) ? 'backspace-word' : null;
  84. case 89:
  85. // Y
  86. if (isCtrlKeyCommand(e)) {
  87. return isOSX ? 'secondary-paste' : 'redo';
  88. }
  89. return null;
  90. case 90:
  91. // Z
  92. return getZCommand(e) || null;
  93. case Keys.RETURN:
  94. return 'split-block';
  95. case Keys.DELETE:
  96. return getDeleteCommand(e);
  97. case Keys.BACKSPACE:
  98. return getBackspaceCommand(e);
  99. // LEFT/RIGHT handlers serve as a workaround for a Firefox bug.
  100. case Keys.LEFT:
  101. return shouldFixFirefoxMovement && hasCommandModifier(e) ? 'move-selection-to-start-of-block' : null;
  102. case Keys.RIGHT:
  103. return shouldFixFirefoxMovement && hasCommandModifier(e) ? 'move-selection-to-end-of-block' : null;
  104. default:
  105. return null;
  106. }
  107. }
  108. module.exports = getDefaultKeyBinding;