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