shortcut.js 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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. let rawOptions = $(_input).closest('[data-shortcuts]').attr('data-shortcuts');
  44. options = [];
  45. rawOptions = rawOptions.split('^^^');
  46. for (let i = 0; i < rawOptions.length; i++) {
  47. let parts = rawOptions[i].split('|||');
  48. options.push({
  49. name: parts[0],
  50. value: parts[1]
  51. });
  52. }
  53. menu.empty();
  54. for(let i = 0; i < options.length; i++) {
  55. menu.append(
  56. $('<div/>')
  57. .addClass('sc')
  58. .text(options[i].name)
  59. .attr('title', options[i].value)
  60. );
  61. }
  62. menu
  63. .css({
  64. left: pos[0] + 'px',
  65. top: (pos[1] + $(window).scrollTop()) + 'px',
  66. })
  67. .show();
  68. }
  69. }
  70. function discard() {
  71. if($('.stag-shortcuts:visible').length) {
  72. $('.stag-shortcuts').hide();
  73. return false;
  74. }
  75. }
  76. function highlightOption() {
  77. menu.find('.sc').removeClass('active');
  78. if(options && options.length && index >= 0 && index < options.length) {
  79. menu.find('.sc:eq(' + index + ')').addClass('active');
  80. }
  81. }
  82. function apply() {
  83. if(input && options && options.length && index >= 0 && index < options.length) {
  84. // $(input).focus();
  85. if(backtrackAfterApplying) {
  86. document.execCommand("delete", true, null);
  87. }
  88. document.execCommand("insertText", true, options[index].value);
  89. discard();
  90. }
  91. }
  92. function isVisible() {
  93. return !!$('.stag-shortcuts:visible').length;
  94. }
  95. function init() {
  96. var selectedText = '';
  97. $('.stag-shortcuts').remove();
  98. options = [];
  99. menu = $('<div/>')
  100. .addClass('stag-shortcuts')
  101. .appendTo('body');
  102. $(document)
  103. .off('mousedown.outside-shortcuts')
  104. .on('mousedown.outside-shortcuts', function(_e) {
  105. if($(_e.target).closest('.stag-shortcuts').length) return;
  106. return discard();
  107. });
  108. $(document)
  109. .off('keypress.shortcuts', '[with-shortcuts]')
  110. .on('keypress.shortcuts', '[with-shortcuts]', function(_e) {
  111. // console.log('KP: ', _e.which);
  112. input = this;
  113. switch(_e.which) {
  114. case 64:
  115. backtrackAfterApplying = true;
  116. show(this);
  117. break;
  118. default:
  119. if(isVisible()) return false;
  120. break;
  121. }
  122. })
  123. .off('keydown.shortcuts', '[with-shortcuts]')
  124. .on('keydown.shortcuts', '[with-shortcuts]', function(_e) {
  125. // console.log('KD: ', _e.which);
  126. input = this;
  127. let consumed = false;
  128. switch(_e.which) {
  129. case 32:
  130. if(_e.ctrlKey && !isVisible()) {
  131. backtrackAfterApplying = false;
  132. show(this);
  133. return false;
  134. }
  135. else {
  136. if(!isVisible()) return;
  137. _e.preventDefault();
  138. apply();
  139. consumed = true;
  140. }
  141. break;
  142. case 27:
  143. if(!isVisible()) return;
  144. consumed = !discard();
  145. break;
  146. case 38:
  147. if(!isVisible()) return;
  148. if(index > 0) index--;
  149. highlightOption();
  150. consumed = true;
  151. break;
  152. case 40:
  153. if(!isVisible()) return;
  154. if(index < options.length - 1) index++;
  155. highlightOption();
  156. consumed = true;
  157. break;
  158. case 13:
  159. if(!isVisible()) return;
  160. apply();
  161. consumed = true;
  162. break;
  163. default:
  164. consumed = isVisible();
  165. break;
  166. }
  167. if(consumed) return false;
  168. });
  169. $(document)
  170. .off('mousedown.apply-shortcuts', '.stag-shortcuts>.sc')
  171. .on('mousedown.apply-shortcuts', '.stag-shortcuts>.sc', function(_e) {
  172. index = $(this).index();
  173. apply();
  174. return false;
  175. });
  176. $(document)
  177. .off('mousedown.add-shortcuts', '.add-shortcut')
  178. .on('mousedown.add-shortcuts', '.add-shortcut', function(_e) {
  179. let hasFocus = $(document.activeElement).closest('.note-content, .rte-holder').length;
  180. if(hasFocus) {
  181. selectedText = window.getSelection().toString();
  182. if(selectedText !== '') return;
  183. }
  184. return false;
  185. })
  186. .off('click.add-shortcuts', '.add-shortcut')
  187. .on('click.add-shortcuts', '.add-shortcut', function(_e) {
  188. // if(selectedText === '') return;
  189. $('#selected-sc-text').val(selectedText);
  190. $('#create-shortcut-form')
  191. .attr('data-editor-id', $(this).attr('data-editor-id'))
  192. .css({
  193. left: $(this).offset().left + 'px',
  194. top: ($(this).offset().top + $(this).outerHeight()) + 'px'
  195. })
  196. .show();
  197. showMoeFormMask();
  198. return false;
  199. })
  200. .off('submit.add-shortcut', '#create-shortcut-form')
  201. .on('submit.add-shortcut', '#create-shortcut-form', function(_e) {
  202. if(!input) input = $('.note-content [contenteditable][data-editor-id="' + $(this).attr('data-editor-id') + '"]').first();
  203. if(!input) return false;
  204. var label = $(this).find('[name="shortcut"]').val(),
  205. content = $(this).find('[name="text"]').val();
  206. if(!this.checkValidity()) return false;
  207. $.post('/api/proTextShortcut/create', $(this).serialize(), function(_data) {
  208. if(_data && _data.success && input) {
  209. var options = [$(input).closest('[data-shortcuts]').attr('data-shortcuts')]
  210. options.push(label + '|||' + content);
  211. options = options.join('^^^');
  212. $(input).closest('[data-shortcuts]').attr('data-shortcuts', options);
  213. toastr.success('Shortcut saved');
  214. hideMoeFormMask();
  215. $('#create-shortcut-form').hide();
  216. $(input).focus();
  217. }
  218. }, 'json');
  219. return false;
  220. })
  221. .off('reset.add-shortcut', '#create-shortcut-form')
  222. .on('reset.add-shortcut', '#create-shortcut-form', function(_e) {
  223. if(!input) input = $('.note-content [contenteditable][data-editor-id="' + $(this).attr('data-editor-id') + '"]').first();
  224. if(!input) return false;
  225. hideMoeFormMask();
  226. $('#create-shortcut-form').hide();
  227. $(input).focus();
  228. });
  229. }
  230. addMCInitializer('shortcut-suggest', init);
  231. })();