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