shortcut.js 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. // shortcut suggest functionality
  2. // auto attaches to all [with-shortcuts] elements
  3. (function() {
  4. let input = null, menu = null, options = [], index = -1;
  5. let backtrackAfterApplying = false;
  6. function getCaretPos(win) {
  7. win = win || window;
  8. let doc = win.document;
  9. let sel, range, rects, rect;
  10. let x = 0, y = 0;
  11. if (win.getSelection) { // won't work if getSelection() isn't available!
  12. sel = win.getSelection();
  13. if(sel.toString() !== '') return null; // won't work if there isa
  14. if (sel.rangeCount) {
  15. range = sel.getRangeAt(0).cloneRange();
  16. if (range.getClientRects) {
  17. range.collapse(true);
  18. let span = doc.createElement("span");
  19. if (span.getClientRects) {
  20. // Ensure span has dimensions and position by
  21. // adding a zero-width space character
  22. span.appendChild( doc.createTextNode("\u200bXX") );
  23. range.insertNode(span);
  24. rect = span.getClientRects()[0];
  25. x = rect.left;
  26. y = rect.top;
  27. let spanParent = span.parentNode;
  28. spanParent.removeChild(span);
  29. // Glue any broken text nodes back together
  30. spanParent.normalize();
  31. return [x, y];
  32. }
  33. return null
  34. }
  35. }
  36. }
  37. return { x: x, y: y };
  38. }
  39. function show(_input) {
  40. let pos = getCaretPos();
  41. if(pos) {
  42. index = -1;
  43. options = window.userShortcuts;
  44. menu.empty();
  45. for(let i = 0; i < options.length; i++) {
  46. menu.append(
  47. $('<div/>')
  48. .addClass('sc')
  49. .text(options[i].name)
  50. .attr('title', options[i].value)
  51. );
  52. }
  53. menu
  54. .css({
  55. left: pos[0] + 'px',
  56. top: (pos[1] + $(window).scrollTop()) + 'px',
  57. })
  58. .show();
  59. }
  60. }
  61. function discard() {
  62. if($('.stag-shortcuts:visible').length) {
  63. $('.stag-shortcuts').hide();
  64. return false;
  65. }
  66. }
  67. function highlightOption() {
  68. menu.find('.sc').removeClass('active');
  69. if(options && options.length && index >= 0 && index < options.length) {
  70. menu.find('.sc:eq(' + index + ')').addClass('active');
  71. }
  72. }
  73. function apply() {
  74. if(input && options && options.length && index >= 0 && index < options.length) {
  75. // $(input).focus();
  76. if(backtrackAfterApplying) {
  77. document.execCommand("delete", true, null);
  78. }
  79. document.execCommand("insertText", true, options[index].value);
  80. discard();
  81. }
  82. }
  83. function isVisible() {
  84. return !!$('.stag-shortcuts:visible').length;
  85. }
  86. function init() {
  87. var selectedText = '';
  88. $('.stag-shortcuts').remove();
  89. options = [];
  90. menu = $('<div/>')
  91. .addClass('stag-shortcuts')
  92. .appendTo('body');
  93. $(document)
  94. .off('mousedown.outside-shortcuts')
  95. .on('mousedown.outside-shortcuts', function(_e) {
  96. if($(_e.target).closest('.stag-shortcuts').length) return;
  97. return discard();
  98. });
  99. $(document)
  100. .off('keypress.shortcuts', '[with-shortcuts]')
  101. .on('keypress.shortcuts', '[with-shortcuts]', function(_e) {
  102. // console.log('KP: ', _e.which);
  103. input = this;
  104. switch(_e.which) {
  105. case 64:
  106. backtrackAfterApplying = true;
  107. show(this);
  108. break;
  109. default:
  110. if(isVisible()) return false;
  111. break;
  112. }
  113. })
  114. .off('keydown.shortcuts', '[with-shortcuts]')
  115. .on('keydown.shortcuts', '[with-shortcuts]', function(_e) {
  116. // console.log('KD: ', _e.which);
  117. input = this;
  118. let consumed = false;
  119. switch(_e.which) {
  120. case 32:
  121. if(_e.ctrlKey && !isVisible()) {
  122. backtrackAfterApplying = false;
  123. show(this);
  124. return false;
  125. }
  126. else {
  127. if(!isVisible()) return;
  128. _e.preventDefault();
  129. apply();
  130. consumed = true;
  131. }
  132. break;
  133. case 27:
  134. if(!isVisible()) return;
  135. consumed = !discard();
  136. break;
  137. case 38:
  138. if(!isVisible()) return;
  139. if(index > 0) index--;
  140. highlightOption();
  141. consumed = true;
  142. break;
  143. case 40:
  144. if(!isVisible()) return;
  145. if(index < options.length - 1) index++;
  146. highlightOption();
  147. consumed = true;
  148. break;
  149. case 13:
  150. if(!isVisible()) return;
  151. apply();
  152. consumed = true;
  153. break;
  154. default:
  155. consumed = isVisible();
  156. break;
  157. }
  158. if(consumed) return false;
  159. });
  160. $(document)
  161. .off('mousedown.apply-shortcuts', '.stag-shortcuts>.sc')
  162. .on('mousedown.apply-shortcuts', '.stag-shortcuts>.sc', function(_e) {
  163. index = $(this).index();
  164. apply();
  165. return false;
  166. });
  167. $(document)
  168. .off('mousedown.add-shortcuts', '.add-shortcut')
  169. .on('mousedown.add-shortcuts', '.add-shortcut', function(_e) {
  170. let hasFocus = $(document.activeElement).closest('.note-content, .rte-holder').length;
  171. if(hasFocus) {
  172. selectedText = window.getSelection().toString();
  173. if(selectedText !== '') return;
  174. }
  175. return false;
  176. })
  177. .off('click.add-shortcuts', '.add-shortcut')
  178. .on('click.add-shortcuts', '.add-shortcut', function(_e) {
  179. // if(selectedText === '') return;
  180. $('#selected-sc-text').val(selectedText);
  181. $('#create-shortcut-form')
  182. .attr('data-editor-id', $(this).attr('data-editor-id'))
  183. .css({
  184. left: $(this).offset().left + 'px',
  185. top: ($(this).offset().top + $(this).outerHeight()) + 'px'
  186. })
  187. .show();
  188. showMoeFormMask();
  189. return false;
  190. })
  191. .off('submit.add-shortcut', '#create-shortcut-form')
  192. .on('submit.add-shortcut', '#create-shortcut-form', function(_e) {
  193. if(!input) input = $('.note-content [contenteditable][data-editor-id="' + $(this).attr('data-editor-id') + '"]').first();
  194. if(!input) return false;
  195. var label = $(this).find('[name="shortcut"]').val(),
  196. content = $(this).find('[name="text"]').val();
  197. if(!this.checkValidity()) return false;
  198. $.post('/api/proTextShortcut/create', $(this).serialize(), function(_data) {
  199. if(_data && _data.success && input) {
  200. window.userShortcuts.push({
  201. name: label,
  202. value: content
  203. });
  204. toastr.success('Shortcut saved');
  205. hideMoeFormMask();
  206. $('#create-shortcut-form').hide();
  207. $(input).focus();
  208. }
  209. }, 'json');
  210. return false;
  211. })
  212. .off('reset.add-shortcut', '#create-shortcut-form')
  213. .on('reset.add-shortcut', '#create-shortcut-form', function(_e) {
  214. if(!input) input = $('.note-content [contenteditable][data-editor-id="' + $(this).attr('data-editor-id') + '"]').first();
  215. if(!input) return false;
  216. hideMoeFormMask();
  217. $('#create-shortcut-form').hide();
  218. $(input).focus();
  219. });
  220. }
  221. addMCInitializer('shortcut-suggest', init);
  222. })();