PageRenderTime 39ms CodeModel.GetById 0ms RepoModel.GetById 1ms app.codeStats 0ms

/BlogEngine/BlogEngine.NET/editors/tiny_mce_3_4_3_1/plugins/autolink/editor_plugin_src.js

#
JavaScript | 172 lines | 108 code | 28 blank | 36 comment | 45 complexity | 02670af2c9ce3b9179331e1ffb5c80b8 MD5 | raw file
Possible License(s): LGPL-2.1, Apache-2.0, BSD-3-Clause
  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. // Internet Explorer has built-in automatic linking
  23. if (tinyMCE.isIE)
  24. return;
  25. // Add a key down handler
  26. ed.onKeyDown.add(function(ed, e) {
  27. if (e.keyCode == 13)
  28. return t.handleEnter(ed);
  29. });
  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().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. r.setStart(endContainer, 0);
  77. r.setEnd(endContainer, endContainer.nodeValue.length);
  78. }
  79. if (r.endOffset == 1)
  80. end = 2;
  81. else
  82. end = r.endOffset - 1 - end_offset;
  83. }
  84. start = end;
  85. do
  86. {
  87. // Move the selection one character backwards.
  88. r.setStart(endContainer, end - 2);
  89. r.setEnd(endContainer, end - 1);
  90. end -= 1;
  91. // Loop until one of the following is found: a blank space, &nbsp;, delimeter, (end-2) >= 0
  92. } while (r.toString() != ' ' && r.toString() != '' && r.toString().charCodeAt(0) != 160 && (end -2) >= 0 && r.toString() != delimiter);
  93. if (r.toString() == delimiter || r.toString().charCodeAt(0) == 160) {
  94. r.setStart(endContainer, end);
  95. r.setEnd(endContainer, start);
  96. end += 1;
  97. } else if (r.startOffset == 0) {
  98. r.setStart(endContainer, 0);
  99. r.setEnd(endContainer, start);
  100. }
  101. else {
  102. r.setStart(endContainer, end);
  103. r.setEnd(endContainer, start);
  104. }
  105. text = r.toString();
  106. matches = text.match(/^(https?:\/\/|ssh:\/\/|ftp:\/\/|file:\/|www\.)(.+)$/i);
  107. if (matches) {
  108. if (matches[1] == 'www.') {
  109. matches[1] = 'http://www.';
  110. }
  111. bookmark = ed.selection.getBookmark();
  112. ed.selection.setRng(r);
  113. tinyMCE.execCommand('mceInsertLink',false, matches[1] + matches[2]);
  114. ed.selection.moveToBookmark(bookmark);
  115. // TODO: Determine if this is still needed.
  116. if (tinyMCE.isWebKit) {
  117. // move the caret to its original position
  118. ed.selection.collapse(false);
  119. var max = Math.min(endContainer.length, start + 1);
  120. r.setStart(endContainer, max);
  121. r.setEnd(endContainer, max);
  122. ed.selection.setRng(r);
  123. }
  124. }
  125. },
  126. /**
  127. * Returns information about the plugin as a name/value array.
  128. * The current keys are longname, author, authorurl, infourl and version.
  129. *
  130. * @return {Object} Name/value array containing information about the plugin.
  131. */
  132. getInfo : function() {
  133. return {
  134. longname : 'Autolink',
  135. author : 'Moxiecode Systems AB',
  136. authorurl : 'http://tinymce.moxiecode.com',
  137. infourl : 'http://wiki.moxiecode.com/index.php/TinyMCE:Plugins/autolink',
  138. version : tinymce.majorVersion + "." + tinymce.minorVersion
  139. };
  140. }
  141. });
  142. // Register plugin
  143. tinymce.PluginManager.add('autolink', tinymce.plugins.AutolinkPlugin);
  144. })();