shortcut.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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, _options) {
  41. let pos = getCaretPos();
  42. // strPart = '';
  43. if(pos) {
  44. index = -1;
  45. options = _options;
  46. if(strPart.length > 1) {
  47. options = _options.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. if(_initial) {
  61. menu
  62. .css({
  63. left: pos[0] + 'px',
  64. top: (pos[1] + $(window).scrollTop()) + 'px',
  65. })
  66. .show();
  67. document.execCommand("insertText", true, '@');
  68. document.execCommand("delete", true, null);
  69. }
  70. }
  71. }
  72. function discard() {
  73. if($('.stag-shortcuts:visible').length) {
  74. $('.stag-shortcuts').hide();
  75. return false;
  76. }
  77. }
  78. function highlightOption() {
  79. menu.find('.sc').removeClass('active');
  80. if(options && options.length && index >= 0 && index < options.length) {
  81. menu.find('.sc:eq(' + index + ')').addClass('active');
  82. }
  83. }
  84. function apply() {
  85. if(input && options && options.length && index >= 0 && index < options.length) {
  86. // $(input).focus();
  87. if(backtrackAfterApplying) {
  88. for (let i = 0; i < strPart.length; i++) {
  89. document.execCommand("delete", true, null);
  90. }
  91. }
  92. document.execCommand("insertText", true, options[index].value);
  93. discard();
  94. }
  95. }
  96. function isVisible() {
  97. return !!$('.stag-shortcuts:visible').length;
  98. }
  99. function init() {
  100. var selectedText = '';
  101. $('.stag-shortcuts').remove();
  102. options = [];
  103. menu = $('<div/>')
  104. .addClass('stag-shortcuts')
  105. .appendTo('body');
  106. let refreshOptions = false;
  107. $(document)
  108. .off('mousedown.outside-shortcuts')
  109. .on('mousedown.outside-shortcuts', function(_e) {
  110. if($(_e.target).closest('.stag-shortcuts').length) return;
  111. return discard();
  112. });
  113. $(document)
  114. .off('keypress.shortcuts', '[with-shortcuts]')
  115. .on('keypress.shortcuts', '[with-shortcuts]', function(_e) {
  116. input = this;
  117. switch(_e.which) {
  118. case 64:
  119. backtrackAfterApplying = true;
  120. strPart = '@';
  121. let options = window.userShortcuts, noteRTE = $(input).closest('[note-rte]');
  122. if(noteRTE.length && noteRTE.is('[use-shortcuts]') && noteRTE.attr('use-shortcuts') && noteRTE.attr('use-shortcuts') !== 'user') {
  123. if(window[noteRTE.attr('use-shortcuts') + 'Shortcuts']) {
  124. options = window[noteRTE.attr('use-shortcuts') + 'Shortcuts'];
  125. }
  126. }
  127. show(true, options);
  128. break;
  129. default:
  130. if(!isVisible()) return;
  131. let char = String.fromCharCode(_e.which);
  132. if(_e.ctrlKey || _e.metaKey || !char.match(/[a-z0-9 ]/i)) {
  133. return false;
  134. }
  135. break;
  136. }
  137. })
  138. .off('keydown.shortcuts', '[with-shortcuts]')
  139. .on('keydown.shortcuts', '[with-shortcuts]', function(_e) {
  140. input = this;
  141. let consumed = false;
  142. refreshOptions = false;
  143. switch(_e.which) {
  144. /*
  145. case 32:
  146. if(_e.ctrlKey && !isVisible()) {
  147. backtrackAfterApplying = false;
  148. show(this);
  149. return false;
  150. }
  151. else {
  152. if(!isVisible()) return;
  153. _e.preventDefault();
  154. apply();
  155. consumed = true;
  156. }
  157. break;
  158. */
  159. case 27:
  160. if(!isVisible()) return;
  161. consumed = !discard();
  162. if(consumed) markEventAsConsumed(e);
  163. break;
  164. case 38:
  165. if(!isVisible()) return;
  166. if(index > 0) index--;
  167. highlightOption();
  168. consumed = true;
  169. break;
  170. case 40:
  171. if(!isVisible()) return;
  172. if(index < options.length - 1) index++;
  173. highlightOption();
  174. consumed = true;
  175. break;
  176. case 13:
  177. if(!isVisible()) return;
  178. apply();
  179. consumed = true;
  180. break;
  181. case 8:
  182. if(!isVisible()) break;
  183. if(strPart.length === 1) {
  184. strPart = '';
  185. discard();
  186. }
  187. else {
  188. strPart = strPart.substr(0, strPart.length - 1);
  189. refreshOptions = true;
  190. }
  191. consumed = false;
  192. break;
  193. default:
  194. if(!isVisible()) break;
  195. let char = String.fromCharCode(_e.which);
  196. if(!_e.ctrlKey && !_e.metaKey && char.match(/[a-z0-9 ]/i)) {
  197. strPart += char;
  198. refreshOptions = true;
  199. consumed = false;
  200. }
  201. else {
  202. consumed = true;
  203. }
  204. break;
  205. }
  206. if(consumed) {
  207. _e.stopImmediatePropagation();
  208. _e.preventDefault();
  209. return false;
  210. }
  211. })
  212. .off('keyup.shortcuts', '[with-shortcuts]')
  213. .on('keyup.shortcuts', '[with-shortcuts]', function(_e) {
  214. let input = this;
  215. let options = window.userShortcuts, noteRTE = $(input).closest('[note-rte]');
  216. if(noteRTE.length && noteRTE.is('[use-shortcuts]') && noteRTE.attr('use-shortcuts') && noteRTE.attr('use-shortcuts') !== 'user') {
  217. if(window[noteRTE.attr('use-shortcuts') + 'Shortcuts']) {
  218. options = window[noteRTE.attr('use-shortcuts') + 'Shortcuts'];
  219. }
  220. }
  221. if(isVisible() && refreshOptions) {
  222. show(false, options);
  223. }
  224. })
  225. .off('paste.shortcuts', '[with-shortcuts]')
  226. .on('paste.shortcuts', '[with-shortcuts]', function(_e) {
  227. if(isVisible()) return false;
  228. });
  229. $(document)
  230. .off('mousedown.apply-shortcuts', '.stag-shortcuts>.sc')
  231. .on('mousedown.apply-shortcuts', '.stag-shortcuts>.sc', function(_e) {
  232. index = $(this).index();
  233. apply();
  234. return false;
  235. });
  236. $(document)
  237. .off('mousedown.add-shortcuts', '.add-shortcut')
  238. .on('mousedown.add-shortcuts', '.add-shortcut', function(_e) {
  239. let hasFocus = $(document.activeElement).closest('.note-content, .rte-holder').length;
  240. if(hasFocus) {
  241. selectedText = window.getSelection().toString();
  242. if(selectedText !== '') return;
  243. }
  244. return false;
  245. })
  246. .off('click.add-shortcuts', '.add-shortcut')
  247. .on('click.add-shortcuts', '.add-shortcut', function(_e) {
  248. // if(selectedText === '') return;
  249. $('#selected-sc-text').val(selectedText);
  250. $('#create-shortcut-form')
  251. .attr('data-editor-id', $(this).attr('data-editor-id'))
  252. .css({
  253. left: $(this).offset().left + 'px',
  254. top: ($(this).offset().top + $(this).outerHeight()) + 'px'
  255. })
  256. .show();
  257. showMoeFormMask();
  258. return false;
  259. })
  260. .off('submit.add-shortcut', '#create-shortcut-form')
  261. .on('submit.add-shortcut', '#create-shortcut-form', function(_e) {
  262. if(!input) input = $('.note-content [contenteditable][data-editor-id="' + $(this).attr('data-editor-id') + '"]').first();
  263. if(!input) return false;
  264. var label = $(this).find('[name="shortcut"]').val(),
  265. content = $(this).find('[name="text"]').val();
  266. if(!this.checkValidity()) return false;
  267. $.post('/api/proTextShortcut/create', $(this).serialize(), function(_data) {
  268. if(_data && _data.success && input) {
  269. window.userShortcuts.push({
  270. name: label,
  271. value: content
  272. });
  273. toastr.success('Shortcut saved');
  274. hideMoeFormMask();
  275. $('#create-shortcut-form').hide();
  276. $(input).focus();
  277. }
  278. }, 'json');
  279. return false;
  280. })
  281. .off('reset.add-shortcut', '#create-shortcut-form')
  282. .on('reset.add-shortcut', '#create-shortcut-form', function(_e) {
  283. if(!input) input = $('.note-content [contenteditable][data-editor-id="' + $(this).attr('data-editor-id') + '"]').first();
  284. if(!input) return false;
  285. hideMoeFormMask();
  286. $('#create-shortcut-form').hide();
  287. $(input).focus();
  288. });
  289. }
  290. addMCInitializer('shortcut-suggest', init);
  291. })();