statusbar.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports["default"] = void 0;
  6. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  7. function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
  8. function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
  9. var VimStatusBar =
  10. /*#__PURE__*/
  11. function () {
  12. function VimStatusBar(node, editor) {
  13. var _this = this;
  14. var sanitizer = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : null;
  15. _classCallCheck(this, VimStatusBar);
  16. this.closeInput = function () {
  17. _this.removeInputListeners();
  18. _this.input = null;
  19. _this.setSec('');
  20. if (_this.editor) {
  21. _this.editor.focus();
  22. }
  23. };
  24. this.clear = function () {
  25. _this.setInnerHtml_(_this.node, '');
  26. };
  27. this.inputKeyUp = function (e) {
  28. var options = _this.input.options;
  29. if (options && options.onKeyUp) {
  30. options.onKeyUp(e, e.target.value, _this.closeInput);
  31. }
  32. };
  33. this.inputBlur = function () {
  34. var options = _this.input.options;
  35. if (options.closeOnBlur) {
  36. _this.closeInput();
  37. }
  38. };
  39. this.inputKeyDown = function (e) {
  40. var _this$input = _this.input,
  41. options = _this$input.options,
  42. callback = _this$input.callback;
  43. if (options && options.onKeyDown && options.onKeyDown(e, e.target.value, _this.closeInput)) {
  44. return;
  45. }
  46. if (e.keyCode === 27 || options && options.closeOnEnter !== false && e.keyCode == 13) {
  47. _this.input.node.blur();
  48. e.stopPropagation();
  49. _this.closeInput();
  50. }
  51. if (e.keyCode === 13 && callback) {
  52. e.stopPropagation();
  53. e.preventDefault();
  54. callback(e.target.value);
  55. }
  56. };
  57. this.node = node;
  58. this.modeInfoNode = document.createElement('span');
  59. this.secInfoNode = document.createElement('span');
  60. this.notifNode = document.createElement('span');
  61. this.notifNode.className = 'vim-notification';
  62. this.keyInfoNode = document.createElement('span');
  63. this.keyInfoNode.setAttribute('style', 'float: right');
  64. this.node.appendChild(this.modeInfoNode);
  65. this.node.appendChild(this.secInfoNode);
  66. this.node.appendChild(this.notifNode);
  67. this.node.appendChild(this.keyInfoNode);
  68. this.toggleVisibility(false);
  69. this.editor = editor;
  70. this.sanitizer = sanitizer;
  71. }
  72. _createClass(VimStatusBar, [{
  73. key: "setMode",
  74. value: function setMode(ev) {
  75. if (ev.mode === 'visual' && ev.subMode === 'linewise') {
  76. this.setText('--VISUAL LINE--');
  77. return;
  78. }
  79. this.setText("--".concat(ev.mode.toUpperCase(), "--"));
  80. }
  81. }, {
  82. key: "setKeyBuffer",
  83. value: function setKeyBuffer(key) {
  84. this.keyInfoNode.textContent = key;
  85. }
  86. }, {
  87. key: "setSec",
  88. value: function setSec(text, callback, options) {
  89. this.notifNode.textContent = '';
  90. if (text === undefined) {
  91. return;
  92. }
  93. this.setInnerHtml_(this.secInfoNode, text);
  94. var input = this.secInfoNode.querySelector('input');
  95. if (input) {
  96. input.focus();
  97. this.input = {
  98. callback: callback,
  99. options: options,
  100. node: input
  101. };
  102. if (options) {
  103. if (options.selectValueOnOpen) {
  104. input.select();
  105. }
  106. if (options.value) {
  107. input.value = options.value;
  108. }
  109. }
  110. this.addInputListeners();
  111. }
  112. return this.closeInput;
  113. }
  114. }, {
  115. key: "setText",
  116. value: function setText(text) {
  117. this.modeInfoNode.textContent = text;
  118. }
  119. }, {
  120. key: "toggleVisibility",
  121. value: function toggleVisibility(toggle) {
  122. if (toggle) {
  123. this.node.style.display = 'block';
  124. } else {
  125. this.node.style.display = 'none';
  126. }
  127. if (this.input) {
  128. this.removeInputListeners();
  129. }
  130. clearInterval(this.notifTimeout);
  131. }
  132. }, {
  133. key: "addInputListeners",
  134. value: function addInputListeners() {
  135. var node = this.input.node;
  136. node.addEventListener('keyup', this.inputKeyUp);
  137. node.addEventListener('keydown', this.inputKeyDown);
  138. node.addEventListener('input', this.inputKeyInput);
  139. node.addEventListener('blur', this.inputBlur);
  140. }
  141. }, {
  142. key: "removeInputListeners",
  143. value: function removeInputListeners() {
  144. if (!this.input || !this.input.node) {
  145. return;
  146. }
  147. var node = this.input.node;
  148. node.removeEventListener('keyup', this.inputKeyUp);
  149. node.removeEventListener('keydown', this.inputKeyDown);
  150. node.removeEventListener('input', this.inputKeyInput);
  151. node.removeEventListener('blur', this.inputBlur);
  152. }
  153. }, {
  154. key: "showNotification",
  155. value: function showNotification(text) {
  156. var _this2 = this;
  157. var sp = document.createElement('span');
  158. this.setInnerHtml_(sp, text);
  159. this.notifNode.textContent = sp.textContent;
  160. this.notifTimeout = setTimeout(function () {
  161. _this2.notifNode.textContent = '';
  162. }, 5000);
  163. }
  164. }, {
  165. key: "setInnerHtml_",
  166. value: function setInnerHtml_(element, htmlContents) {
  167. if (this.sanitizer) {
  168. // Clear out previous contents first.
  169. while (element.children.length) {
  170. element.removeChild(element.children[0]);
  171. }
  172. element.appendChild(this.sanitizer(htmlContents));
  173. } else {
  174. element.innerHTML = htmlContents;
  175. }
  176. }
  177. }]);
  178. return VimStatusBar;
  179. }();
  180. exports["default"] = VimStatusBar;