]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/TinyMCE/js/plugins/linkautodetect/editor_plugin_src.js
TinyMCE: add Shane Tomlinson's linkautodetect plugin so typed URLs get linked for...
[quix0rs-gnu-social.git] / plugins / TinyMCE / js / plugins / linkautodetect / editor_plugin_src.js
1 /**\r
2  * $Id: editor_plugin_src.js 2009-05-10\r
3  *\r
4  * @author Ubernote/Shane Tomlinson\r
5  * http://www.ubernote.com\r
6  * set117@gmail.com\r
7  *\r
8  * Detect emails and urls as they are typed in Mozilla/Safari/Chrome and Opera\r
9  * Borrowed from both Typo3 http://typo3.org/ and Xinha http://xinha.gogo.co.nz/\r
10  * Heavily modified and cleaned up.\r
11  * \r
12  * Original license info from Xinha at the bottom.\r
13  */\r
14 \r
15 (function() {\r
16         tinymce.create('tinymce.plugins.LinkAutoDetect', {\r
17                 init : function(ed, url) {\r
18                         var t = this;\r
19                         t.RE_email = /^[a-z0-9_\-]+(\.[_a-z0-9\-]+)*@([_a-z0-9\-]+\.)+([a-z]{2}|aero|arpa|biz|com|coop|edu|gov|info|int|jobs|mil|museum|name|nato|net|org|pro|travel)$/i;\r
20                         t.RE_url   = /^((https?|ftp|news):\/\/)?([a-z]([a-z0-9\-]*\.)+([a-z]{2}|aero|arpa|biz|com|coop|edu|gov|info|int|jobs|mil|museum|name|nato|net|org|pro|travel)|(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]))(\/[a-z0-9_\-\.~]+)*(\/([a-z0-9_\-\.]*)(\?[a-z0-9+_\-\.%=&]*)?)?(#[a-z][a-z0-9_]*)?$/i;\r
21                         ed.onKeyPress.add( t.onKeyPress, t );\r
22                         ed.onKeyUp.add( t.onKeyUp, t );\r
23                 },\r
24                 \r
25                 getInfo : function() {\r
26                         return {\r
27                                 longname : 'Link Auto Detect',\r
28                                 author : 'Ubernote/Shane Tomlinson',\r
29                                 authorurl : 'http://www.ubernote.com',\r
30                                 infourl : 'http://www.ubernote.com',\r
31                                 version : '0.2'\r
32                         };\r
33                 },\r
34                 \r
35                 onKeyPress : function(ed, ev, o) {\r
36                         if(tinymce.isIE) {   \r
37                                 // IE already does auto-detect, so no worries.\r
38                                 return;\r
39                         } // end if\r
40                         \r
41                         var s = ed.selection.getSel();\r
42                         var textNode = s.anchorNode;\r
43                         \r
44                         var createLink = function (searchfor, hlink, midStart) {\r
45                                 var leftText  = textNode;\r
46                                 var rightText = leftText.splitText(s.anchorOffset);\r
47                                 var midText   = leftText.splitText(midStart);\r
48                                 var midEnd = midText.data.search(searchfor);\r
49                                 if (midEnd != -1) {\r
50                                         rightText = midText.splitText(midEnd);\r
51                                 } // end if\r
52                                 var tag = ed.dom.create('a', { href: hlink }, midText.data);\r
53                                 var a = midText.parentNode.insertBefore(tag, rightText);\r
54                                 \r
55                                 // We are going to put the cursor into the first position after\r
56                                 //  the anchor and let the browser take care of inserting the space/enter.\r
57                                 s.collapse(rightText, 0);\r
58                                 ed.dom.remove(midText);\r
59                         };\r
60                         \r
61                         // Space or Enter, see if the text just typed looks like a URL, or email address and link it accordingly\r
62                         if((ev.which == 13 || ev.which == 32) \r
63                                 && textNode.nodeType == 3 && textNode.data.length > 3 \r
64                                 && textNode.data.indexOf('.') >= 0 && !ed.dom.getParent(textNode, 'a')) {\r
65                                 var midStart = textNode.data.substring(0,s.anchorOffset).search(/\S{4,}$/);\r
66                                 if(midStart != -1) {\r
67                                         var matchData = textNode.data.substring(0,s.anchorOffset).replace(/^.*?(\S*)$/, '$1');\r
68                                         var matchURL = matchData.match(this.RE_url);\r
69                                         var matchEmail = matchData.match(this.RE_email);\r
70                                         if(matchEmail) {\r
71                                                 createLink(/[^a-zA-Z0-9\.@_\-]/, 'mailto:' + matchEmail[0], midStart);\r
72                                         }\r
73                                         else if(matchURL) {\r
74                                                 createLink( /[^a-zA-Z0-9\._\-\/\&\?#=:@]/, (matchURL[1] ? '' : 'http://') + matchURL[0], midStart);\r
75                                         }\r
76                                 } // end if\r
77                         }\r
78                 },\r
79                 \r
80                 onKeyUp : function(ed, ev, o) {\r
81                         if(tinymce.isIE) {   \r
82                                 // IE already does auto-detect, so no worries.\r
83                                 return;\r
84                         } // end if\r
85                         \r
86                         var s = ed.selection.getSel();\r
87                         var textNode = s.anchorNode;\r
88                                 var a = ed.dom.getParent(textNode, 'a');\r
89                                 \r
90                                 if( ! (ev.which && ( ev.which == 13 || ev.which == 32)) \r
91                                         && (ev.which || ev.keyCode == 8 || ev.keyCode == 46)\r
92                                         && (textNode.nodeType == 3) && (a)) {\r
93                                         // See if we might be changing a link\r
94                                         var matchEmail = s.anchorNode.data.match(this.RE_email);\r
95                                         var matchURL = textNode.data.match(this.RE_url);\r
96                                         if(matchEmail) {\r
97                                                 ed.dom.setAttrib(a, 'href', 'mailto:' + textNode.data);\r
98                                         } // end if\r
99                                         if(matchURL) {\r
100                                                 ed.dom.setAttrib(a, 'href', (matchURL[1] ? '' : 'http://') + matchURL[0]);\r
101                                         } // end if\r
102                                 } // end if\r
103                         }\r
104         });\r
105 \r
106         // Register plugin\r
107         tinymce.PluginManager.add('linkautodetect', tinymce.plugins.LinkAutoDetect);\r
108 })();\r
109 \r
110 /*\r
111 htmlArea License (based on BSD license)\r
112 Copyright (c) 2002-2004, interactivetools.com, inc.\r
113 Copyright (c) 2003-2004 dynarch.com\r
114 All rights reserved.\r
115 \r
116 Redistribution and use in source and binary forms, with or without\r
117 modification, are permitted provided that the following conditions are met:\r
118 \r
119 1) Redistributions of source code must retain the above copyright notice,\r
120    this list of conditions and the following disclaimer.\r
121 \r
122 2) Redistributions in binary form must reproduce the above copyright notice,\r
123    this list of conditions and the following disclaimer in the documentation\r
124    and/or other materials provided with the distribution.\r
125 \r
126 3) Neither the name of interactivetools.com, inc. nor the names of its\r
127    contributors may be used to endorse or promote products derived from this\r
128    software without specific prior written permission.\r
129 \r
130 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"\r
131 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\r
132 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\r
133 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE\r
134 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR\r
135 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF\r
136 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS\r
137 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN\r
138 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\r
139 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE\r
140 POSSIBILITY OF SUCH DAMAGE.\r
141 */