shortcut.js 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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] + '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. return discard();
  106. });
  107. $(document)
  108. .off('keypress.shortcuts', '[with-shortcuts]')
  109. .on('keypress.shortcuts', '[with-shortcuts]', function(_e) {
  110. // console.log('KP: ', _e.which);
  111. input = this;
  112. switch(_e.which) {
  113. case 64:
  114. backtrackAfterApplying = true;
  115. show(this);
  116. break;
  117. default:
  118. if(isVisible()) return false;
  119. break;
  120. }
  121. })
  122. .off('keydown.shortcuts', '[with-shortcuts]')
  123. .on('keydown.shortcuts', '[with-shortcuts]', function(_e) {
  124. // console.log('KD: ', _e.which);
  125. input = this;
  126. let consumed = false;
  127. switch(_e.which) {
  128. case 32:
  129. if(_e.ctrlKey) {
  130. backtrackAfterApplying = false;
  131. show(this);
  132. return false;
  133. }
  134. else {
  135. if(!isVisible()) return;
  136. _e.preventDefault();
  137. apply();
  138. consumed = true;
  139. }
  140. break;
  141. case 27:
  142. if(!isVisible()) return;
  143. consumed = !discard();
  144. break;
  145. case 38:
  146. if(!isVisible()) return;
  147. if(index > 0) index--;
  148. highlightOption();
  149. consumed = true;
  150. break;
  151. case 40:
  152. if(!isVisible()) return;
  153. if(index < options.length - 1) index++;
  154. highlightOption();
  155. consumed = true;
  156. break;
  157. case 13:
  158. if(!isVisible()) return;
  159. apply();
  160. consumed = true;
  161. break;
  162. default:
  163. consumed = isVisible();
  164. break;
  165. }
  166. if(consumed) return false;
  167. })
  168. .off('selectionchange.shortcuts')
  169. .on('selectionchange.shortcuts', function(_e) {
  170. console.log(_e);
  171. });
  172. $(document)
  173. .off('click.apply-shortcuts', '.stag-shortcuts>.sc')
  174. .on('click.apply-shortcuts', '.stag-shortcuts>.sc', function(_e) {
  175. apply();
  176. return false;
  177. });
  178. menu.off('mousedown.inside-shortcuts')
  179. .on('mousedown.inside-shortcuts', function(_e) {
  180. return false;
  181. });
  182. $(document)
  183. .off('mousedown.add-shortcuts', '.add-shortcut')
  184. .on('mousedown.add-shortcuts', '.add-shortcut', function(_e) {
  185. let hasFocus = $(document.activeElement).closest('.note-content').length;
  186. if(hasFocus) {
  187. selectedText = window.getSelection().toString();
  188. if(selectedText !== '') return;
  189. }
  190. return false;
  191. })
  192. .off('click.add-shortcuts', '.add-shortcut')
  193. .on('click.add-shortcuts', '.add-shortcut', function(_e) {
  194. if(selectedText === '') return;
  195. $('#selected-sc-text').val(selectedText);
  196. $('#create-shortcut-form')
  197. .css({
  198. left: $(this).offset().left + 'px',
  199. top: ($(this).offset().top + $(this).outerHeight()) + 'px'
  200. })
  201. .show();
  202. showMoeFormMask();
  203. return false;
  204. })
  205. .off('mousedown.inside-form', '#create-shortcut-form *')
  206. .on('mousedown.inside-form', '#create-shortcut-form *', function(_e) {
  207. // return false;
  208. })
  209. .off('submit.add-shortcut', '#create-shortcut-form')
  210. .on('submit.add-shortcut', '#create-shortcut-form', function(_e) {
  211. var label = $(this).find('[name="shortcut"]').val(),
  212. content = $(this).find('[name="text"]').val();
  213. $.post('/api/proTextShortcut/create', $(this).serialize(), function(_data) {
  214. if(_data && _data.success && input) {
  215. var options = [$(input).closest('[data-shortcuts]').attr('data-shortcuts')]
  216. options.push(label + '|||' + content);
  217. options = options.join('^^^');
  218. $(input).closest('[data-shortcuts]').attr('data-shortcuts', options);
  219. toastr.success('Shortcut saved');
  220. hideMoeFormMask();
  221. $('#create-shortcut-form').hide();
  222. $(input).focus();
  223. }
  224. }, 'json');
  225. return false;
  226. });
  227. }
  228. addMCInitializer('shortcut-suggest', init);
  229. })();