keybinding.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /* ***** BEGIN LICENSE BLOCK *****
  2. * Distributed under the BSD license:
  3. *
  4. * Copyright (c) 2010, Ajax.org B.V.
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are met:
  9. * * Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * * Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * * Neither the name of Ajax.org B.V. nor the
  15. * names of its contributors may be used to endorse or promote products
  16. * derived from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  19. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. * DISCLAIMED. IN NO EVENT SHALL AJAX.ORG B.V. BE LIABLE FOR ANY
  22. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  25. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  27. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. *
  29. * ***** END LICENSE BLOCK ***** */
  30. define(function(require, exports, module) {
  31. "use strict";
  32. var keyUtil = require("../lib/keys");
  33. var event = require("../lib/event");
  34. var KeyBinding = function(editor) {
  35. this.$editor = editor;
  36. this.$data = {editor: editor};
  37. this.$handlers = [];
  38. this.setDefaultHandler(editor.commands);
  39. };
  40. (function() {
  41. this.setDefaultHandler = function(kb) {
  42. this.removeKeyboardHandler(this.$defaultHandler);
  43. this.$defaultHandler = kb;
  44. this.addKeyboardHandler(kb, 0);
  45. };
  46. this.setKeyboardHandler = function(kb) {
  47. var h = this.$handlers;
  48. if (h[h.length - 1] == kb)
  49. return;
  50. while (h[h.length - 1] && h[h.length - 1] != this.$defaultHandler)
  51. this.removeKeyboardHandler(h[h.length - 1]);
  52. this.addKeyboardHandler(kb, 1);
  53. };
  54. this.addKeyboardHandler = function(kb, pos) {
  55. if (!kb)
  56. return;
  57. if (typeof kb == "function" && !kb.handleKeyboard)
  58. kb.handleKeyboard = kb;
  59. var i = this.$handlers.indexOf(kb);
  60. if (i != -1)
  61. this.$handlers.splice(i, 1);
  62. if (pos == undefined)
  63. this.$handlers.push(kb);
  64. else
  65. this.$handlers.splice(pos, 0, kb);
  66. if (i == -1 && kb.attach)
  67. kb.attach(this.$editor);
  68. };
  69. this.removeKeyboardHandler = function(kb) {
  70. var i = this.$handlers.indexOf(kb);
  71. if (i == -1)
  72. return false;
  73. this.$handlers.splice(i, 1);
  74. kb.detach && kb.detach(this.$editor);
  75. return true;
  76. };
  77. this.getKeyboardHandler = function() {
  78. return this.$handlers[this.$handlers.length - 1];
  79. };
  80. this.getStatusText = function() {
  81. var data = this.$data;
  82. var editor = data.editor;
  83. return this.$handlers.map(function(h) {
  84. return h.getStatusText && h.getStatusText(editor, data) || "";
  85. }).filter(Boolean).join(" ");
  86. };
  87. this.$callKeyboardHandlers = function(hashId, keyString, keyCode, e) {
  88. var toExecute;
  89. var success = false;
  90. var commands = this.$editor.commands;
  91. for (var i = this.$handlers.length; i--;) {
  92. toExecute = this.$handlers[i].handleKeyboard(
  93. this.$data, hashId, keyString, keyCode, e
  94. );
  95. if (!toExecute || !toExecute.command)
  96. continue;
  97. // allow keyboardHandler to consume keys
  98. if (toExecute.command == "null") {
  99. success = true;
  100. } else {
  101. success = commands.exec(toExecute.command, this.$editor, toExecute.args, e);
  102. }
  103. // do not stop input events to not break repeating
  104. if (success && e && hashId != -1 &&
  105. toExecute.passEvent != true && toExecute.command.passEvent != true
  106. ) {
  107. event.stopEvent(e);
  108. }
  109. if (success)
  110. break;
  111. }
  112. if (!success && hashId == -1) {
  113. toExecute = {command: "insertstring"};
  114. success = commands.exec("insertstring", this.$editor, keyString);
  115. }
  116. if (success)
  117. this.$editor._signal("keyboardActivity", toExecute);
  118. return success;
  119. };
  120. this.onCommandKey = function(e, hashId, keyCode) {
  121. var keyString = keyUtil.keyCodeToString(keyCode);
  122. this.$callKeyboardHandlers(hashId, keyString, keyCode, e);
  123. };
  124. this.onTextInput = function(text) {
  125. this.$callKeyboardHandlers(-1, text);
  126. };
  127. }).call(KeyBinding.prototype);
  128. exports.KeyBinding = KeyBinding;
  129. });