shortcut.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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(_clearReturn = false) {
  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. if(_clearReturn) document.execCommand("delete", true, null);
  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. let options = window.userShortcuts, noteRTE = $(input).closest('[note-rte]');
  123. if(noteRTE.length && noteRTE.is('[use-shortcuts]') && noteRTE.attr('use-shortcuts') && noteRTE.attr('use-shortcuts') !== 'user') {
  124. if(window[noteRTE.attr('use-shortcuts') + 'Shortcuts']) {
  125. options = window[noteRTE.attr('use-shortcuts') + 'Shortcuts'];
  126. }
  127. }
  128. show(true, options);
  129. break;
  130. default:
  131. if(!isVisible()) return;
  132. let char = String.fromCharCode(_e.which);
  133. if(_e.ctrlKey || _e.metaKey || !char.match(/[a-z0-9 ]/i)) {
  134. return false;
  135. }
  136. break;
  137. }
  138. })
  139. .off('keydown.shortcuts', '[with-shortcuts]')
  140. .on('keydown.shortcuts', '[with-shortcuts]', function(_e) {
  141. input = this;
  142. let consumed = false;
  143. refreshOptions = false;
  144. switch(_e.which) {
  145. /*
  146. case 32:
  147. if(_e.ctrlKey && !isVisible()) {
  148. backtrackAfterApplying = false;
  149. show(this);
  150. return false;
  151. }
  152. else {
  153. if(!isVisible()) return;
  154. _e.preventDefault();
  155. apply();
  156. consumed = true;
  157. }
  158. break;
  159. */
  160. case 27:
  161. if(!isVisible()) return;
  162. consumed = !discard();
  163. if(consumed) markEventAsConsumed(_e);
  164. break;
  165. case 38:
  166. if(!isVisible()) return;
  167. if(index > 0) index--;
  168. highlightOption();
  169. consumed = true;
  170. break;
  171. case 40:
  172. if(!isVisible()) return;
  173. if(index < options.length - 1) index++;
  174. highlightOption();
  175. consumed = true;
  176. break;
  177. case 13:
  178. if(!isVisible()) return;
  179. apply(true);
  180. consumed = true;
  181. break;
  182. case 8:
  183. if(!isVisible()) break;
  184. if(strPart.length === 1) {
  185. strPart = '';
  186. discard();
  187. }
  188. else {
  189. strPart = strPart.substr(0, strPart.length - 1);
  190. refreshOptions = true;
  191. }
  192. consumed = false;
  193. break;
  194. default:
  195. if(!isVisible()) break;
  196. let char = String.fromCharCode(_e.which);
  197. if(!_e.ctrlKey && !_e.metaKey && char.match(/[a-z0-9 ]/i)) {
  198. strPart += char;
  199. refreshOptions = true;
  200. consumed = false;
  201. }
  202. else {
  203. consumed = true;
  204. }
  205. break;
  206. }
  207. if(consumed) {
  208. _e.stopImmediatePropagation();
  209. _e.preventDefault();
  210. return false;
  211. }
  212. })
  213. .off('keyup.shortcuts', '[with-shortcuts]')
  214. .on('keyup.shortcuts', '[with-shortcuts]', function(_e) {
  215. let input = this;
  216. let options = window.userShortcuts, noteRTE = $(input).closest('[note-rte]');
  217. if(noteRTE.length && noteRTE.is('[use-shortcuts]') && noteRTE.attr('use-shortcuts') && noteRTE.attr('use-shortcuts') !== 'user') {
  218. if(window[noteRTE.attr('use-shortcuts') + 'Shortcuts']) {
  219. options = window[noteRTE.attr('use-shortcuts') + 'Shortcuts'];
  220. }
  221. }
  222. if(isVisible() && refreshOptions) {
  223. show(false, options);
  224. }
  225. })
  226. .off('paste.shortcuts', '[with-shortcuts]')
  227. .on('paste.shortcuts', '[with-shortcuts]', function(_e) {
  228. if(isVisible()) return false;
  229. });
  230. $(document)
  231. .off('mousedown.apply-shortcuts', '.stag-shortcuts>.sc')
  232. .on('mousedown.apply-shortcuts', '.stag-shortcuts>.sc', function(_e) {
  233. index = $(this).index();
  234. apply();
  235. return false;
  236. });
  237. $(document)
  238. .off('mousedown.add-shortcuts', '.add-shortcut')
  239. .on('mousedown.add-shortcuts', '.add-shortcut', function(_e) {
  240. let hasFocus = $(document.activeElement).closest('.note-content, .rte-holder').length;
  241. if(hasFocus) {
  242. selectedText = window.getSelection().toString();
  243. if(selectedText !== '') return;
  244. }
  245. return false;
  246. })
  247. .off('click.add-shortcuts', '.add-shortcut')
  248. .on('click.add-shortcuts', '.add-shortcut', function(_e) {
  249. // if(selectedText === '') return;
  250. $('#selected-sc-text').val(selectedText);
  251. $('#create-shortcut-form')
  252. .attr('data-editor-id', $(this).attr('data-editor-id'))
  253. .css({
  254. left: $(this).offset().left + 'px',
  255. top: ($(this).offset().top + $(this).outerHeight()) + 'px'
  256. })
  257. .show();
  258. showMoeFormMask();
  259. return false;
  260. })
  261. .off('submit.add-shortcut', '#create-shortcut-form')
  262. .on('submit.add-shortcut', '#create-shortcut-form', function(_e) {
  263. if(!input) input = $('.note-content [contenteditable][data-editor-id="' + $(this).attr('data-editor-id') + '"]').first();
  264. if(!input) return false;
  265. var label = $(this).find('[name="shortcut"]').val(),
  266. content = $(this).find('[name="text"]').val();
  267. if(!this.checkValidity()) return false;
  268. $.post('/api/proTextShortcut/create', $(this).serialize(), function(_data) {
  269. if(_data && _data.success && input) {
  270. window.userShortcuts.push({
  271. name: label,
  272. value: content
  273. });
  274. toastr.success('Shortcut saved');
  275. hideMoeFormMask();
  276. $('#create-shortcut-form').hide();
  277. $(input).focus();
  278. }
  279. }, 'json');
  280. return false;
  281. })
  282. .off('reset.add-shortcut', '#create-shortcut-form')
  283. .on('reset.add-shortcut', '#create-shortcut-form', function(_e) {
  284. if(!input) input = $('.note-content [contenteditable][data-editor-id="' + $(this).attr('data-editor-id') + '"]').first();
  285. if(!input) return false;
  286. hideMoeFormMask();
  287. $('#create-shortcut-form').hide();
  288. $(input).focus();
  289. });
  290. }
  291. addMCInitializer('shortcut-suggest', init);
  292. })();