plugin.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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.2.0 (2020-02-13)
  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 getDefaultLinkProtocol = function (editor) {
  20. return editor.getParam('link_default_protocol', 'http', 'string');
  21. };
  22. var Settings = {
  23. getAutoLinkPattern: getAutoLinkPattern,
  24. getDefaultLinkTarget: getDefaultLinkTarget,
  25. getDefaultLinkProtocol: getDefaultLinkProtocol
  26. };
  27. var rangeEqualsDelimiterOrSpace = function (rangeString, delimiter) {
  28. return rangeString === delimiter || rangeString === ' ' || rangeString.charCodeAt(0) === 160;
  29. };
  30. var handleEclipse = function (editor) {
  31. parseCurrentLine(editor, -1, '(');
  32. };
  33. var handleSpacebar = function (editor) {
  34. parseCurrentLine(editor, 0, '');
  35. };
  36. var handleEnter = function (editor) {
  37. parseCurrentLine(editor, -1, '');
  38. };
  39. var scopeIndex = function (container, index) {
  40. if (index < 0) {
  41. index = 0;
  42. }
  43. if (container.nodeType === 3) {
  44. var len = container.data.length;
  45. if (index > len) {
  46. index = len;
  47. }
  48. }
  49. return index;
  50. };
  51. var setStart = function (rng, container, offset) {
  52. if (container.nodeType !== 1 || container.hasChildNodes()) {
  53. rng.setStart(container, scopeIndex(container, offset));
  54. } else {
  55. rng.setStartBefore(container);
  56. }
  57. };
  58. var setEnd = function (rng, container, offset) {
  59. if (container.nodeType !== 1 || container.hasChildNodes()) {
  60. rng.setEnd(container, scopeIndex(container, offset));
  61. } else {
  62. rng.setEndAfter(container);
  63. }
  64. };
  65. var parseCurrentLine = function (editor, endOffset, delimiter) {
  66. var rng, end, start, endContainer, bookmark, text, matches, prev, len, rngText;
  67. var autoLinkPattern = Settings.getAutoLinkPattern(editor);
  68. var defaultLinkTarget = Settings.getDefaultLinkTarget(editor);
  69. if (editor.selection.getNode().tagName === 'A') {
  70. return;
  71. }
  72. rng = editor.selection.getRng(true).cloneRange();
  73. if (rng.startOffset < 5) {
  74. prev = rng.endContainer.previousSibling;
  75. if (!prev) {
  76. if (!rng.endContainer.firstChild || !rng.endContainer.firstChild.nextSibling) {
  77. return;
  78. }
  79. prev = rng.endContainer.firstChild.nextSibling;
  80. }
  81. len = prev.length;
  82. setStart(rng, prev, len);
  83. setEnd(rng, prev, len);
  84. if (rng.endOffset < 5) {
  85. return;
  86. }
  87. end = rng.endOffset;
  88. endContainer = prev;
  89. } else {
  90. endContainer = rng.endContainer;
  91. if (endContainer.nodeType !== 3 && endContainer.firstChild) {
  92. while (endContainer.nodeType !== 3 && endContainer.firstChild) {
  93. endContainer = endContainer.firstChild;
  94. }
  95. if (endContainer.nodeType === 3) {
  96. setStart(rng, endContainer, 0);
  97. setEnd(rng, endContainer, endContainer.nodeValue.length);
  98. }
  99. }
  100. if (rng.endOffset === 1) {
  101. end = 2;
  102. } else {
  103. end = rng.endOffset - 1 - endOffset;
  104. }
  105. }
  106. start = end;
  107. do {
  108. setStart(rng, endContainer, end >= 2 ? end - 2 : 0);
  109. setEnd(rng, endContainer, end >= 1 ? end - 1 : 0);
  110. end -= 1;
  111. rngText = rng.toString();
  112. } while (rngText !== ' ' && rngText !== '' && rngText.charCodeAt(0) !== 160 && end - 2 >= 0 && rngText !== delimiter);
  113. if (rangeEqualsDelimiterOrSpace(rng.toString(), delimiter)) {
  114. setStart(rng, endContainer, end);
  115. setEnd(rng, endContainer, start);
  116. end += 1;
  117. } else if (rng.startOffset === 0) {
  118. setStart(rng, endContainer, 0);
  119. setEnd(rng, endContainer, start);
  120. } else {
  121. setStart(rng, endContainer, end);
  122. setEnd(rng, endContainer, start);
  123. }
  124. text = rng.toString();
  125. if (text.charAt(text.length - 1) === '.') {
  126. setEnd(rng, endContainer, start - 1);
  127. }
  128. text = rng.toString().trim();
  129. matches = text.match(autoLinkPattern);
  130. var protocol = Settings.getDefaultLinkProtocol(editor);
  131. if (matches) {
  132. if (matches[1] === 'www.') {
  133. matches[1] = protocol + '://www.';
  134. } else if (/@$/.test(matches[1]) && !/^mailto:/.test(matches[1])) {
  135. matches[1] = 'mailto:' + matches[1];
  136. }
  137. bookmark = editor.selection.getBookmark();
  138. editor.selection.setRng(rng);
  139. editor.execCommand('createlink', false, matches[1] + matches[2]);
  140. if (defaultLinkTarget !== false) {
  141. editor.dom.setAttrib(editor.selection.getNode(), 'target', defaultLinkTarget);
  142. }
  143. editor.selection.moveToBookmark(bookmark);
  144. editor.nodeChanged();
  145. }
  146. };
  147. var setup = function (editor) {
  148. var autoUrlDetectState;
  149. editor.on('keydown', function (e) {
  150. if (e.keyCode === 13) {
  151. return handleEnter(editor);
  152. }
  153. });
  154. if (global$1.browser.isIE()) {
  155. editor.on('focus', function () {
  156. if (!autoUrlDetectState) {
  157. autoUrlDetectState = true;
  158. try {
  159. editor.execCommand('AutoUrlDetect', false, true);
  160. } catch (ex) {
  161. }
  162. }
  163. });
  164. return;
  165. }
  166. editor.on('keypress', function (e) {
  167. if (e.keyCode === 41) {
  168. return handleEclipse(editor);
  169. }
  170. });
  171. editor.on('keyup', function (e) {
  172. if (e.keyCode === 32) {
  173. return handleSpacebar(editor);
  174. }
  175. });
  176. };
  177. var Keys = { setup: setup };
  178. function Plugin () {
  179. global.add('autolink', function (editor) {
  180. Keys.setup(editor);
  181. });
  182. }
  183. Plugin();
  184. }());