Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

getDefaultKeyBinding.js.flow 3.5 KiB

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