ext-keybinding_menu.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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, callback) {
  66. var closer = document.createElement('div');
  67. function documentEscListener(e) {
  68. if (e.keyCode === 27) {
  69. close();
  70. }
  71. }
  72. function close() {
  73. if (!closer) return;
  74. document.removeEventListener('keydown', documentEscListener);
  75. closer.parentNode.removeChild(closer);
  76. editor.focus();
  77. closer = null;
  78. callback && callback();
  79. }
  80. closer.style.cssText = 'margin: 0; padding: 0; ' +
  81. 'position: fixed; top:0; bottom:0; left:0; right:0;' +
  82. 'z-index: 9990; ' +
  83. 'background-color: rgba(0, 0, 0, 0.3);';
  84. closer.addEventListener('click', function() {
  85. close();
  86. });
  87. document.addEventListener('keydown', documentEscListener);
  88. contentElement.addEventListener('click', function (e) {
  89. e.stopPropagation();
  90. });
  91. closer.appendChild(contentElement);
  92. document.body.appendChild(closer);
  93. editor.blur();
  94. return {
  95. close: close
  96. };
  97. };
  98. });
  99. ace.define("ace/ext/menu_tools/get_editor_keyboard_shortcuts",[], function(require, exports, module) {
  100. "use strict";
  101. var keys = require("../../lib/keys");
  102. module.exports.getEditorKeybordShortcuts = function(editor) {
  103. var KEY_MODS = keys.KEY_MODS;
  104. var keybindings = [];
  105. var commandMap = {};
  106. editor.keyBinding.$handlers.forEach(function(handler) {
  107. var ckb = handler.commandKeyBinding;
  108. for (var i in ckb) {
  109. var key = i.replace(/(^|-)\w/g, function(x) { return x.toUpperCase(); });
  110. var commands = ckb[i];
  111. if (!Array.isArray(commands))
  112. commands = [commands];
  113. commands.forEach(function(command) {
  114. if (typeof command != "string")
  115. command = command.name;
  116. if (commandMap[command]) {
  117. commandMap[command].key += "|" + key;
  118. } else {
  119. commandMap[command] = {key: key, command: command};
  120. keybindings.push(commandMap[command]);
  121. }
  122. });
  123. }
  124. });
  125. return keybindings;
  126. };
  127. });
  128. ace.define("ace/ext/keybinding_menu",[], function(require, exports, module) {
  129. "use strict";
  130. var Editor = require("../editor").Editor;
  131. function showKeyboardShortcuts (editor) {
  132. if(!document.getElementById('kbshortcutmenu')) {
  133. var overlayPage = require('./menu_tools/overlay_page').overlayPage;
  134. var getEditorKeybordShortcuts = require('./menu_tools/get_editor_keyboard_shortcuts').getEditorKeybordShortcuts;
  135. var kb = getEditorKeybordShortcuts(editor);
  136. var el = document.createElement('div');
  137. var commands = kb.reduce(function(previous, current) {
  138. return previous + '<div class="ace_optionsMenuEntry"><span class="ace_optionsMenuCommand">'
  139. + current.command + '</span> : '
  140. + '<span class="ace_optionsMenuKey">' + current.key + '</span></div>';
  141. }, '');
  142. el.id = 'kbshortcutmenu';
  143. el.innerHTML = '<h1>Keyboard Shortcuts</h1>' + commands + '</div>';
  144. overlayPage(editor, el);
  145. }
  146. }
  147. module.exports.init = function(editor) {
  148. Editor.prototype.showKeyboardShortcuts = function() {
  149. showKeyboardShortcuts(this);
  150. };
  151. editor.commands.addCommands([{
  152. name: "showKeyboardShortcuts",
  153. bindKey: {win: "Ctrl-Alt-h", mac: "Command-Alt-h"},
  154. exec: function(editor, line) {
  155. editor.showKeyboardShortcuts();
  156. }
  157. }]);
  158. };
  159. });
  160. (function() {
  161. ace.require(["ace/ext/keybinding_menu"], function(m) {
  162. if (typeof module == "object" && typeof exports == "object" && module) {
  163. module.exports = m;
  164. }
  165. });
  166. })();