shortcut.js 11 KB

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