2 * editor_plugin_src.js
\r
4 * Copyright 2011, Moxiecode Systems AB
\r
5 * Released under LGPL License.
\r
7 * License: http://tinymce.moxiecode.com/license
\r
8 * Contributing: http://tinymce.moxiecode.com/contributing
\r
12 tinymce.create('tinymce.plugins.AutolinkPlugin', {
\r
14 * Initializes the plugin, this will be executed after the plugin has been created.
\r
15 * This call is done before the editor instance has finished it's initialization so use the onInit event
\r
16 * of the editor instance to intercept that event.
\r
18 * @param {tinymce.Editor} ed Editor instance that the plugin is initialized in.
\r
19 * @param {string} url Absolute URL to where the plugin is located.
\r
22 init : function(ed, url) {
\r
25 // Internet Explorer has built-in automatic linking
\r
29 // Add a key down handler
\r
30 ed.onKeyDown.add(function(ed, e) {
\r
31 if (e.keyCode == 13)
\r
32 return t.handleEnter(ed);
\r
35 ed.onKeyPress.add(function(ed, e) {
\r
37 return t.handleEclipse(ed);
\r
40 // Add a key up handler
\r
41 ed.onKeyUp.add(function(ed, e) {
\r
42 if (e.keyCode == 32)
\r
43 return t.handleSpacebar(ed);
\r
47 handleEclipse : function(ed) {
\r
48 this.parseCurrentLine(ed, -1, '(', true);
\r
51 handleSpacebar : function(ed) {
\r
52 this.parseCurrentLine(ed, 0, '', true);
\r
55 handleEnter : function(ed) {
\r
56 this.parseCurrentLine(ed, -1, '', false);
\r
59 parseCurrentLine : function(ed, end_offset, delimiter, goback) {
\r
60 var r, end, start, endContainer, bookmark, text, matches, prev, len;
\r
62 // We need at least five characters to form a URL,
\r
63 // hence, at minimum, five characters from the beginning of the line.
\r
64 r = ed.selection.getRng().cloneRange();
\r
65 if (r.startOffset < 5) {
\r
66 // During testing, the caret is placed inbetween two text nodes.
\r
67 // The previous text node contains the URL.
\r
68 prev = r.endContainer.previousSibling;
\r
70 if (r.endContainer.firstChild == null || r.endContainer.firstChild.nextSibling == null)
\r
73 prev = r.endContainer.firstChild.nextSibling;
\r
76 r.setStart(prev, len);
\r
77 r.setEnd(prev, len);
\r
79 if (r.endOffset < 5)
\r
83 endContainer = prev;
\r
85 endContainer = r.endContainer;
\r
88 if (endContainer.nodeType != 3 && endContainer.firstChild) {
\r
89 while (endContainer.nodeType != 3 && endContainer.firstChild)
\r
90 endContainer = endContainer.firstChild;
\r
92 r.setStart(endContainer, 0);
\r
93 r.setEnd(endContainer, endContainer.nodeValue.length);
\r
96 if (r.endOffset == 1)
\r
99 end = r.endOffset - 1 - end_offset;
\r
106 // Move the selection one character backwards.
\r
107 r.setStart(endContainer, end - 2);
\r
108 r.setEnd(endContainer, end - 1);
\r
111 // Loop until one of the following is found: a blank space, , delimeter, (end-2) >= 0
\r
112 } while (r.toString() != ' ' && r.toString() != '' && r.toString().charCodeAt(0) != 160 && (end -2) >= 0 && r.toString() != delimiter);
\r
114 if (r.toString() == delimiter || r.toString().charCodeAt(0) == 160) {
\r
115 r.setStart(endContainer, end);
\r
116 r.setEnd(endContainer, start);
\r
118 } else if (r.startOffset == 0) {
\r
119 r.setStart(endContainer, 0);
\r
120 r.setEnd(endContainer, start);
\r
123 r.setStart(endContainer, end);
\r
124 r.setEnd(endContainer, start);
\r
127 text = r.toString();
\r
128 matches = text.match(/^(https?:\/\/|ssh:\/\/|ftp:\/\/|file:\/|www\.|[A-Z0-9._%+-]+@)(.+)$/i);
\r
131 if (matches[1] == 'www.') {
\r
132 matches[1] = 'http://www.';
\r
133 } else if (/@$/.test(matches[1])) {
\r
134 matches[1] = 'mailto:' + matches[1];
\r
137 bookmark = ed.selection.getBookmark();
\r
139 ed.selection.setRng(r);
\r
140 tinyMCE.execCommand('createlink',false, matches[1] + matches[2]);
\r
141 ed.selection.moveToBookmark(bookmark);
\r
143 // TODO: Determine if this is still needed.
\r
144 if (tinyMCE.isWebKit) {
\r
145 // move the caret to its original position
\r
146 ed.selection.collapse(false);
\r
147 var max = Math.min(endContainer.length, start + 1);
\r
148 r.setStart(endContainer, max);
\r
149 r.setEnd(endContainer, max);
\r
150 ed.selection.setRng(r);
\r
156 * Returns information about the plugin as a name/value array.
\r
157 * The current keys are longname, author, authorurl, infourl and version.
\r
159 * @return {Object} Name/value array containing information about the plugin.
\r
161 getInfo : function() {
\r
163 longname : 'Autolink',
\r
164 author : 'Moxiecode Systems AB',
\r
165 authorurl : 'http://tinymce.moxiecode.com',
\r
166 infourl : 'http://wiki.moxiecode.com/index.php/TinyMCE:Plugins/autolink',
\r
167 version : tinymce.majorVersion + "." + tinymce.minorVersion
\r
173 tinymce.PluginManager.add('autolink', tinymce.plugins.AutolinkPlugin);
\r