This commit is contained in:
life
2014-05-07 13:06:24 +08:00
parent fac05a7b6c
commit 476ade10e7
1085 changed files with 259628 additions and 0 deletions

View File

@ -0,0 +1,160 @@
/**
* plugin.js
*
* Copyright 2011, Moxiecode Systems AB
* Released under LGPL License.
*
* License: http://www.tinymce.com/license
* Contributing: http://www.tinymce.com/contributing
*/
/*global tinymce:true */
tinymce.PluginManager.add('autolink', function(editor) {
editor.on("keydown", function(e) {
if (e.keyCode == 13) {
return handleEnter(editor);
}
});
// Internet Explorer has built-in automatic linking for most cases
if (tinymce.Env.ie) {
return;
}
editor.on("keypress", function(e) {
if (e.which == 41) {
return handleEclipse(editor);
}
});
editor.on("keyup", function(e) {
if (e.keyCode == 32) {
return handleSpacebar(editor);
}
});
function handleEclipse(editor) {
parseCurrentLine(editor, -1, '(', true);
}
function handleSpacebar(editor) {
parseCurrentLine(editor, 0, '', true);
}
function handleEnter(editor) {
parseCurrentLine(editor, -1, '', false);
}
function parseCurrentLine(editor, end_offset, delimiter) {
var rng, end, start, endContainer, bookmark, text, matches, prev, len;
// We need at least five characters to form a URL,
// hence, at minimum, five characters from the beginning of the line.
rng = editor.selection.getRng(true).cloneRange();
if (rng.startOffset < 5) {
// During testing, the caret is placed inbetween two text nodes.
// The previous text node contains the URL.
prev = rng.endContainer.previousSibling;
if (!prev) {
if (!rng.endContainer.firstChild || !rng.endContainer.firstChild.nextSibling) {
return;
}
prev = rng.endContainer.firstChild.nextSibling;
}
len = prev.length;
rng.setStart(prev, len);
rng.setEnd(prev, len);
if (rng.endOffset < 5) {
return;
}
end = rng.endOffset;
endContainer = prev;
} else {
endContainer = rng.endContainer;
// Get a text node
if (endContainer.nodeType != 3 && endContainer.firstChild) {
while (endContainer.nodeType != 3 && endContainer.firstChild) {
endContainer = endContainer.firstChild;
}
// Move range to text node
if (endContainer.nodeType == 3) {
rng.setStart(endContainer, 0);
rng.setEnd(endContainer, endContainer.nodeValue.length);
}
}
if (rng.endOffset == 1) {
end = 2;
} else {
end = rng.endOffset - 1 - end_offset;
}
}
start = end;
do {
// Move the selection one character backwards.
rng.setStart(endContainer, end >= 2 ? end - 2 : 0);
rng.setEnd(endContainer, end >= 1 ? end - 1 : 0);
end -= 1;
// Loop until one of the following is found: a blank space, &nbsp;, delimiter, (end-2) >= 0
} while (rng.toString() != ' ' && rng.toString() !== '' &&
rng.toString().charCodeAt(0) != 160 && (end -2) >= 0 && rng.toString() != delimiter);
if (rng.toString() == delimiter || rng.toString().charCodeAt(0) == 160) {
rng.setStart(endContainer, end);
rng.setEnd(endContainer, start);
end += 1;
} else if (rng.startOffset === 0) {
rng.setStart(endContainer, 0);
rng.setEnd(endContainer, start);
}
else {
rng.setStart(endContainer, end);
rng.setEnd(endContainer, start);
}
// Exclude last . from word like "www.site.com."
text = rng.toString();
if (text.charAt(text.length - 1) == '.') {
rng.setEnd(endContainer, start - 1);
}
text = rng.toString();
matches = text.match(/^(https?:\/\/|ssh:\/\/|ftp:\/\/|file:\/|www\.|(?:mailto:)?[A-Z0-9._%+\-]+@)(.+)$/i);
if (matches) {
if (matches[1] == 'www.') {
matches[1] = 'http://www.';
} else if (/@$/.test(matches[1]) && !/^mailto:/.test(matches[1])) {
matches[1] = 'mailto:' + matches[1];
}
bookmark = editor.selection.getBookmark();
editor.selection.setRng(rng);
editor.execCommand('createlink', false, matches[1] + matches[2]);
editor.selection.moveToBookmark(bookmark);
editor.nodeChanged();
// TODO: Determine if this is still needed.
if (tinymce.Env.webkit) {
// move the caret to its original position
editor.selection.collapse(false);
var max = Math.min(endContainer.length, start + 1);
rng.setStart(endContainer, max);
rng.setEnd(endContainer, max);
editor.selection.setRng(rng);
}
}
}
});

View File

@ -0,0 +1 @@
tinymce.PluginManager.add("autolink",function(e){function t(e){i(e,-1,"(",!0)}function n(e){i(e,0,"",!0)}function r(e){i(e,-1,"",!1)}function i(e,t,n){var r,i,o,a,s,l,c,d,u;if(r=e.selection.getRng(!0).cloneRange(),r.startOffset<5){if(d=r.endContainer.previousSibling,!d){if(!r.endContainer.firstChild||!r.endContainer.firstChild.nextSibling)return;d=r.endContainer.firstChild.nextSibling}if(u=d.length,r.setStart(d,u),r.setEnd(d,u),r.endOffset<5)return;i=r.endOffset,a=d}else{if(a=r.endContainer,3!=a.nodeType&&a.firstChild){for(;3!=a.nodeType&&a.firstChild;)a=a.firstChild;3==a.nodeType&&(r.setStart(a,0),r.setEnd(a,a.nodeValue.length))}i=1==r.endOffset?2:r.endOffset-1-t}o=i;do r.setStart(a,i>=2?i-2:0),r.setEnd(a,i>=1?i-1:0),i-=1;while(" "!=r.toString()&&""!==r.toString()&&160!=r.toString().charCodeAt(0)&&i-2>=0&&r.toString()!=n);if(r.toString()==n||160==r.toString().charCodeAt(0)?(r.setStart(a,i),r.setEnd(a,o),i+=1):0===r.startOffset?(r.setStart(a,0),r.setEnd(a,o)):(r.setStart(a,i),r.setEnd(a,o)),l=r.toString(),"."==l.charAt(l.length-1)&&r.setEnd(a,o-1),l=r.toString(),c=l.match(/^(https?:\/\/|ssh:\/\/|ftp:\/\/|file:\/|www\.|(?:mailto:)?[A-Z0-9._%+\-]+@)(.+)$/i),c&&("www."==c[1]?c[1]="http://www.":/@$/.test(c[1])&&!/^mailto:/.test(c[1])&&(c[1]="mailto:"+c[1]),s=e.selection.getBookmark(),e.selection.setRng(r),e.execCommand("createlink",!1,c[1]+c[2]),e.selection.moveToBookmark(s),e.nodeChanged(),tinymce.Env.webkit)){e.selection.collapse(!1);var f=Math.min(a.length,o+1);r.setStart(a,f),r.setEnd(a,f),e.selection.setRng(r)}}e.on("keydown",function(t){return 13==t.keyCode?r(e):void 0}),tinymce.Env.ie||(e.on("keypress",function(n){return 41==n.which?t(e):void 0}),e.on("keyup",function(t){return 32==t.keyCode?n(e):void 0}))});