annotate static/js/tiny_mce/plugins/autolink/editor_plugin_src.js @ 829:5a00dcf563a8

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