plugin.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /**
  2. * Copyright (c) Tiny Technologies, Inc. All rights reserved.
  3. * Licensed under the LGPL or a commercial license.
  4. * For LGPL see License.txt in the project root for license information.
  5. * For commercial licenses see https://www.tiny.cloud/
  6. *
  7. * Version: 5.1.0 (2019-10-17)
  8. */
  9. (function () {
  10. 'use strict';
  11. var global = tinymce.util.Tools.resolve('tinymce.PluginManager');
  12. var global$1 = tinymce.util.Tools.resolve('tinymce.Env');
  13. var getAutoLinkPattern = function (editor) {
  14. return editor.getParam('autolink_pattern', /^(https?:\/\/|ssh:\/\/|ftp:\/\/|file:\/|www\.|(?:mailto:)?[A-Z0-9._%+\-]+@)(.+)$/i);
  15. };
  16. var getDefaultLinkTarget = function (editor) {
  17. return editor.getParam('default_link_target', false);
  18. };
  19. var Settings = {
  20. getAutoLinkPattern: getAutoLinkPattern,
  21. getDefaultLinkTarget: getDefaultLinkTarget
  22. };
  23. var rangeEqualsDelimiterOrSpace = function (rangeString, delimiter) {
  24. return rangeString === delimiter || rangeString === ' ' || rangeString.charCodeAt(0) === 160;
  25. };
  26. var handleEclipse = function (editor) {
  27. parseCurrentLine(editor, -1, '(');
  28. };
  29. var handleSpacebar = function (editor) {
  30. parseCurrentLine(editor, 0, '');
  31. };
  32. var handleEnter = function (editor) {
  33. parseCurrentLine(editor, -1, '');
  34. };
  35. var scopeIndex = function (container, index) {
  36. if (index < 0) {
  37. index = 0;
  38. }
  39. if (container.nodeType === 3) {
  40. var len = container.data.length;
  41. if (index > len) {
  42. index = len;
  43. }
  44. }
  45. return index;
  46. };
  47. var setStart = function (rng, container, offset) {
  48. if (container.nodeType !== 1 || container.hasChildNodes()) {
  49. rng.setStart(container, scopeIndex(container, offset));
  50. } else {
  51. rng.setStartBefore(container);
  52. }
  53. };
  54. var setEnd = function (rng, container, offset) {
  55. if (container.nodeType !== 1 || container.hasChildNodes()) {
  56. rng.setEnd(container, scopeIndex(container, offset));
  57. } else {
  58. rng.setEndAfter(container);
  59. }
  60. };
  61. var parseCurrentLine = function (editor, endOffset, delimiter) {
  62. var rng, end, start, endContainer, bookmark, text, matches, prev, len, rngText;
  63. var autoLinkPattern = Settings.getAutoLinkPattern(editor);
  64. var defaultLinkTarget = Settings.getDefaultLinkTarget(editor);
  65. if (editor.selection.getNode().tagName === 'A') {
  66. return;
  67. }
  68. rng = editor.selection.getRng(true).cloneRange();
  69. if (rng.startOffset < 5) {
  70. prev = rng.endContainer.previousSibling;
  71. if (!prev) {
  72. if (!rng.endContainer.firstChild || !rng.endContainer.firstChild.nextSibling) {
  73. return;
  74. }
  75. prev = rng.endContainer.firstChild.nextSibling;
  76. }
  77. len = prev.length;
  78. setStart(rng, prev, len);
  79. setEnd(rng, prev, len);
  80. if (rng.endOffset < 5) {
  81. return;
  82. }
  83. end = rng.endOffset;
  84. endContainer = prev;
  85. } else {
  86. endContainer = rng.endContainer;
  87. if (endContainer.nodeType !== 3 && endContainer.firstChild) {
  88. while (endContainer.nodeType !== 3 && endContainer.firstChild) {
  89. endContainer = endContainer.firstChild;
  90. }
  91. if (endContainer.nodeType === 3) {
  92. setStart(rng, endContainer, 0);
  93. setEnd(rng, endContainer, endContainer.nodeValue.length);
  94. }
  95. }
  96. if (rng.endOffset === 1) {
  97. end = 2;
  98. } else {
  99. end = rng.endOffset - 1 - endOffset;
  100. }
  101. }
  102. start = end;
  103. do {
  104. setStart(rng, endContainer, end >= 2 ? end - 2 : 0);
  105. setEnd(rng, endContainer, end >= 1 ? end - 1 : 0);
  106. end -= 1;
  107. rngText = rng.toString();
  108. } while (rngText !== ' ' && rngText !== '' && rngText.charCodeAt(0) !== 160 && end - 2 >= 0 && rngText !== delimiter);
  109. if (rangeEqualsDelimiterOrSpace(rng.toString(), delimiter)) {
  110. setStart(rng, endContainer, end);
  111. setEnd(rng, endContainer, start);
  112. end += 1;
  113. } else if (rng.startOffset === 0) {
  114. setStart(rng, endContainer, 0);
  115. setEnd(rng, endContainer, start);
  116. } else {
  117. setStart(rng, endContainer, end);
  118. setEnd(rng, endContainer, start);
  119. }
  120. text = rng.toString();
  121. if (text.charAt(text.length - 1) === '.') {
  122. setEnd(rng, endContainer, start - 1);
  123. }
  124. text = rng.toString().trim();
  125. matches = text.match(autoLinkPattern);
  126. if (matches) {
  127. if (matches[1] === 'www.') {
  128. matches[1] = 'http://www.';
  129. } else if (/@$/.test(matches[1]) && !/^mailto:/.test(matches[1])) {
  130. matches[1] = 'mailto:' + matches[1];
  131. }
  132. bookmark = editor.selection.getBookmark();
  133. editor.selection.setRng(rng);
  134. editor.execCommand('createlink', false, matches[1] + matches[2]);
  135. if (defaultLinkTarget !== false) {
  136. editor.dom.setAttrib(editor.selection.getNode(), 'target', defaultLinkTarget);
  137. }
  138. editor.selection.moveToBookmark(bookmark);
  139. editor.nodeChanged();
  140. }
  141. };
  142. var setup = function (editor) {
  143. var autoUrlDetectState;
  144. editor.on('keydown', function (e) {
  145. if (e.keyCode === 13) {
  146. return handleEnter(editor);
  147. }
  148. });
  149. if (global$1.browser.isIE()) {
  150. editor.on('focus', function () {
  151. if (!autoUrlDetectState) {
  152. autoUrlDetectState = true;
  153. try {
  154. editor.execCommand('AutoUrlDetect', false, true);
  155. } catch (ex) {
  156. }
  157. }
  158. });
  159. return;
  160. }
  161. editor.on('keypress', function (e) {
  162. if (e.keyCode === 41) {
  163. return handleEclipse(editor);
  164. }
  165. });
  166. editor.on('keyup', function (e) {
  167. if (e.keyCode === 32) {
  168. return handleSpacebar(editor);
  169. }
  170. });
  171. };
  172. var Keys = { setup: setup };
  173. function Plugin () {
  174. global.add('autolink', function (editor) {
  175. Keys.setup(editor);
  176. });
  177. }
  178. Plugin();
  179. }());