ext-keybinding_menu.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. ace.define("ace/ext/menu_tools/overlay_page",[], function(require, exports, module) {
  2. 'use strict';
  3. var dom = require("../../lib/dom");
  4. var cssText = "#ace_settingsmenu, #kbshortcutmenu {\
  5. background-color: #F7F7F7;\
  6. color: black;\
  7. box-shadow: -5px 4px 5px rgba(126, 126, 126, 0.55);\
  8. padding: 1em 0.5em 2em 1em;\
  9. overflow: auto;\
  10. position: absolute;\
  11. margin: 0;\
  12. bottom: 0;\
  13. right: 0;\
  14. top: 0;\
  15. z-index: 9991;\
  16. cursor: default;\
  17. }\
  18. .ace_dark #ace_settingsmenu, .ace_dark #kbshortcutmenu {\
  19. box-shadow: -20px 10px 25px rgba(126, 126, 126, 0.25);\
  20. background-color: rgba(255, 255, 255, 0.6);\
  21. color: black;\
  22. }\
  23. .ace_optionsMenuEntry:hover {\
  24. background-color: rgba(100, 100, 100, 0.1);\
  25. transition: all 0.3s\
  26. }\
  27. .ace_closeButton {\
  28. background: rgba(245, 146, 146, 0.5);\
  29. border: 1px solid #F48A8A;\
  30. border-radius: 50%;\
  31. padding: 7px;\
  32. position: absolute;\
  33. right: -8px;\
  34. top: -8px;\
  35. z-index: 100000;\
  36. }\
  37. .ace_closeButton{\
  38. background: rgba(245, 146, 146, 0.9);\
  39. }\
  40. .ace_optionsMenuKey {\
  41. color: darkslateblue;\
  42. font-weight: bold;\
  43. }\
  44. .ace_optionsMenuCommand {\
  45. color: darkcyan;\
  46. font-weight: normal;\
  47. }\
  48. .ace_optionsMenuEntry input, .ace_optionsMenuEntry button {\
  49. vertical-align: middle;\
  50. }\
  51. .ace_optionsMenuEntry button[ace_selected_button=true] {\
  52. background: #e7e7e7;\
  53. box-shadow: 1px 0px 2px 0px #adadad inset;\
  54. border-color: #adadad;\
  55. }\
  56. .ace_optionsMenuEntry button {\
  57. background: white;\
  58. border: 1px solid lightgray;\
  59. margin: 0px;\
  60. }\
  61. .ace_optionsMenuEntry button:hover{\
  62. background: #f0f0f0;\
  63. }";
  64. dom.importCssString(cssText);
  65. module.exports.overlayPage = function overlayPage(editor, contentElement, top, right, bottom, left) {
  66. top = top ? 'top: ' + top + ';' : '';
  67. bottom = bottom ? 'bottom: ' + bottom + ';' : '';
  68. right = right ? 'right: ' + right + ';' : '';
  69. left = left ? 'left: ' + left + ';' : '';
  70. var closer = document.createElement('div');
  71. var contentContainer = document.createElement('div');
  72. function documentEscListener(e) {
  73. if (e.keyCode === 27) {
  74. closer.click();
  75. }
  76. }
  77. closer.style.cssText = 'margin: 0; padding: 0; ' +
  78. 'position: fixed; top:0; bottom:0; left:0; right:0;' +
  79. 'z-index: 9990; ' +
  80. 'background-color: rgba(0, 0, 0, 0.3);';
  81. closer.addEventListener('click', function() {
  82. document.removeEventListener('keydown', documentEscListener);
  83. closer.parentNode.removeChild(closer);
  84. editor.focus();
  85. closer = null;
  86. });
  87. document.addEventListener('keydown', documentEscListener);
  88. contentContainer.style.cssText = top + right + bottom + left;
  89. contentContainer.addEventListener('click', function(e) {
  90. e.stopPropagation();
  91. });
  92. var wrapper = dom.createElement("div");
  93. wrapper.style.position = "relative";
  94. var closeButton = dom.createElement("div");
  95. closeButton.className = "ace_closeButton";
  96. closeButton.addEventListener('click', function() {
  97. closer.click();
  98. });
  99. wrapper.appendChild(closeButton);
  100. contentContainer.appendChild(wrapper);
  101. contentContainer.appendChild(contentElement);
  102. closer.appendChild(contentContainer);
  103. document.body.appendChild(closer);
  104. editor.blur();
  105. };
  106. });
  107. ace.define("ace/ext/menu_tools/get_editor_keyboard_shortcuts",[], function(require, exports, module) {
  108. "use strict";
  109. var keys = require("../../lib/keys");
  110. module.exports.getEditorKeybordShortcuts = function(editor) {
  111. var KEY_MODS = keys.KEY_MODS;
  112. var keybindings = [];
  113. var commandMap = {};
  114. editor.keyBinding.$handlers.forEach(function(handler) {
  115. var ckb = handler.commandKeyBinding;
  116. for (var i in ckb) {
  117. var key = i.replace(/(^|-)\w/g, function(x) { return x.toUpperCase(); });
  118. var commands = ckb[i];
  119. if (!Array.isArray(commands))
  120. commands = [commands];
  121. commands.forEach(function(command) {
  122. if (typeof command != "string")
  123. command = command.name;
  124. if (commandMap[command]) {
  125. commandMap[command].key += "|" + key;
  126. } else {
  127. commandMap[command] = {key: key, command: command};
  128. keybindings.push(commandMap[command]);
  129. }
  130. });
  131. }
  132. });
  133. return keybindings;
  134. };
  135. });
  136. ace.define("ace/ext/keybinding_menu",[], function(require, exports, module) {
  137. "use strict";
  138. var Editor = require("ace/editor").Editor;
  139. function showKeyboardShortcuts (editor) {
  140. if(!document.getElementById('kbshortcutmenu')) {
  141. var overlayPage = require('./menu_tools/overlay_page').overlayPage;
  142. var getEditorKeybordShortcuts = require('./menu_tools/get_editor_keyboard_shortcuts').getEditorKeybordShortcuts;
  143. var kb = getEditorKeybordShortcuts(editor);
  144. var el = document.createElement('div');
  145. var commands = kb.reduce(function(previous, current) {
  146. return previous + '<div class="ace_optionsMenuEntry"><span class="ace_optionsMenuCommand">'
  147. + current.command + '</span> : '
  148. + '<span class="ace_optionsMenuKey">' + current.key + '</span></div>';
  149. }, '');
  150. el.id = 'kbshortcutmenu';
  151. el.innerHTML = '<h1>Keyboard Shortcuts</h1>' + commands + '</div>';
  152. overlayPage(editor, el, '0', '0', '0', null);
  153. }
  154. }
  155. module.exports.init = function(editor) {
  156. Editor.prototype.showKeyboardShortcuts = function() {
  157. showKeyboardShortcuts(this);
  158. };
  159. editor.commands.addCommands([{
  160. name: "showKeyboardShortcuts",
  161. bindKey: {win: "Ctrl-Alt-h", mac: "Command-Alt-h"},
  162. exec: function(editor, line) {
  163. editor.showKeyboardShortcuts();
  164. }
  165. }]);
  166. };
  167. });
  168. (function() {
  169. ace.require(["ace/ext/keybinding_menu"], function(m) {
  170. if (typeof module == "object" && typeof exports == "object" && module) {
  171. module.exports = m;
  172. }
  173. });
  174. })();