editor_plugin_src.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /**
  2. * editor_plugin_src.js
  3. *
  4. * Copyright 2011, Moxiecode Systems AB
  5. * Released under LGPL License.
  6. *
  7. * License: http://tinymce.moxiecode.com/license
  8. * Contributing: http://tinymce.moxiecode.com/contributing
  9. */
  10. (function() {
  11. tinymce.create('tinymce.plugins.AutolinkPlugin', {
  12. /**
  13. * Initializes the plugin, this will be executed after the plugin has been created.
  14. * This call is done before the editor instance has finished it's initialization so use the onInit event
  15. * of the editor instance to intercept that event.
  16. *
  17. * @param {tinymce.Editor} ed Editor instance that the plugin is initialized in.
  18. * @param {string} url Absolute URL to where the plugin is located.
  19. */
  20. init : function(ed, url) {
  21. var t = this;
  22. // Add a key down handler
  23. ed.onKeyDown.addToTop(function(ed, e) {
  24. if (e.keyCode == 13)
  25. return t.handleEnter(ed);
  26. });
  27. // Internet Explorer has built-in automatic linking for most cases
  28. if (tinyMCE.isIE)
  29. return;
  30. ed.onKeyPress.add(function(ed, e) {
  31. if (e.which == 41)
  32. return t.handleEclipse(ed);
  33. });
  34. // Add a key up handler
  35. ed.onKeyUp.add(function(ed, e) {
  36. if (e.keyCode == 32)
  37. return t.handleSpacebar(ed);
  38. });
  39. },
  40. handleEclipse : function(ed) {
  41. this.parseCurrentLine(ed, -1, '(', true);
  42. },
  43. handleSpacebar : function(ed) {
  44. this.parseCurrentLine(ed, 0, '', true);
  45. },
  46. handleEnter : function(ed) {
  47. this.parseCurrentLine(ed, -1, '', false);
  48. },
  49. parseCurrentLine : function(ed, end_offset, delimiter, goback) {
  50. var r, end, start, endContainer, bookmark, text, matches, prev, len;
  51. // We need at least five characters to form a URL,
  52. // hence, at minimum, five characters from the beginning of the line.
  53. r = ed.selection.getRng(true).cloneRange();
  54. if (r.startOffset < 5) {
  55. // During testing, the caret is placed inbetween two text nodes.
  56. // The previous text node contains the URL.
  57. prev = r.endContainer.previousSibling;
  58. if (prev == null) {
  59. if (r.endContainer.firstChild == null || r.endContainer.firstChild.nextSibling == null)
  60. return;
  61. prev = r.endContainer.firstChild.nextSibling;
  62. }
  63. len = prev.length;
  64. r.setStart(prev, len);
  65. r.setEnd(prev, len);
  66. if (r.endOffset < 5)
  67. return;
  68. end = r.endOffset;
  69. endContainer = prev;
  70. } else {
  71. endContainer = r.endContainer;
  72. // Get a text node
  73. if (endContainer.nodeType != 3 && endContainer.firstChild) {
  74. while (endContainer.nodeType != 3 && endContainer.firstChild)
  75. endContainer = endContainer.firstChild;
  76. // Move range to text node
  77. if (endContainer.nodeType == 3) {
  78. r.setStart(endContainer, 0);
  79. r.setEnd(endContainer, endContainer.nodeValue.length);
  80. }
  81. }
  82. if (r.endOffset == 1)
  83. end = 2;
  84. else
  85. end = r.endOffset - 1 - end_offset;
  86. }
  87. start = end;
  88. do
  89. {
  90. // Move the selection one character backwards.
  91. r.setStart(endContainer, end - 2);
  92. r.setEnd(endContainer, end - 1);
  93. end -= 1;
  94. // Loop until one of the following is found: a blank space, &nbsp;, delimeter, (end-2) >= 0
  95. } while (r.toString() != ' ' && r.toString() != '' && r.toString().charCodeAt(0) != 160 && (end -2) >= 0 && r.toString() != delimiter);
  96. if (r.toString() == delimiter || r.toString().charCodeAt(0) == 160) {
  97. r.setStart(endContainer, end);
  98. r.setEnd(endContainer, start);
  99. end += 1;
  100. } else if (r.startOffset == 0) {
  101. r.setStart(endContainer, 0);
  102. r.setEnd(endContainer, start);
  103. }
  104. else {
  105. r.setStart(endContainer, end);
  106. r.setEnd(endContainer, start);
  107. }
  108. // Exclude last . from word like "www.site.com."
  109. var text = r.toString();
  110. if (text.charAt(text.length - 1) == '.') {
  111. r.setEnd(endContainer, start - 1);
  112. }
  113. text = r.toString();
  114. matches = text.match(/^(https?:\/\/|ssh:\/\/|ftp:\/\/|file:\/|www\.|(?:mailto:)?[A-Z0-9._%+-]+@)(.+)$/i);
  115. if (matches) {
  116. if (matches[1] == 'www.') {
  117. matches[1] = 'http://www.';
  118. } else if (/@$/.test(matches[1]) && !/^mailto:/.test(matches[1])) {
  119. matches[1] = 'mailto:' + matches[1];
  120. }
  121. bookmark = ed.selection.getBookmark();
  122. ed.selection.setRng(r);
  123. tinyMCE.execCommand('createlink',false, matches[1] + matches[2]);
  124. ed.selection.moveToBookmark(bookmark);
  125. ed.nodeChanged();
  126. // TODO: Determine if this is still needed.
  127. if (tinyMCE.isWebKit) {
  128. // move the caret to its original position
  129. ed.selection.collapse(false);
  130. var max = Math.min(endContainer.length, start + 1);
  131. r.setStart(endContainer, max);
  132. r.setEnd(endContainer, max);
  133. ed.selection.setRng(r);
  134. }
  135. }
  136. },
  137. /**
  138. * Returns information about the plugin as a name/value array.
  139. * The current keys are longname, author, authorurl, infourl and version.
  140. *
  141. * @return {Object} Name/value array containing information about the plugin.
  142. */
  143. getInfo : function() {
  144. return {
  145. longname : 'Autolink',
  146. author : 'Moxiecode Systems AB',
  147. authorurl : 'http://tinymce.moxiecode.com',
  148. infourl : 'http://wiki.moxiecode.com/index.php/TinyMCE:Plugins/autolink',
  149. version : tinymce.majorVersion + "." + tinymce.minorVersion
  150. };
  151. }
  152. });
  153. // Register plugin
  154. tinymce.PluginManager.add('autolink', tinymce.plugins.AutolinkPlugin);
  155. })();