- Restore the "link" button state to disabled by default and enabled when text or image is selected.
- Remove the (recently added) default `link` plugin, not needed for `wpLink`.
See #27309
Built from https://develop.svn.wordpress.org/trunk@27447


git-svn-id: http://core.svn.wordpress.org/trunk@27293 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Andrew Ozz 2014-03-07 02:02:13 +00:00
parent 4bb7e03548
commit 6f1668c43d
6 changed files with 19 additions and 356 deletions

View File

@ -211,7 +211,7 @@ final class _WP_Editors {
$ext_plugins = '';
if ( $set['teeny'] ) {
self::$plugins = $plugins = apply_filters( 'teeny_mce_plugins', array( 'fullscreen', 'link', 'image', 'wordpress', 'wpeditimage', 'wplink' ), $editor_id );
self::$plugins = $plugins = apply_filters( 'teeny_mce_plugins', array( 'fullscreen', 'image', 'wordpress', 'wpeditimage', 'wplink' ), $editor_id );
} else {
/**
* TinyMCE external plugins filter
@ -232,7 +232,6 @@ final class _WP_Editors {
$plugins = array_unique( apply_filters( 'tiny_mce_plugins', array(
'charmap',
'hr',
'link',
'media',
'paste',
'tabfocus',

View File

@ -1,352 +0,0 @@
/**
* plugin.js
*
* Copyright, 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('link', function(editor) {
function createLinkList(callback) {
return function() {
var linkList = editor.settings.link_list;
if (typeof(linkList) == "string") {
tinymce.util.XHR.send({
url: linkList,
success: function(text) {
callback(tinymce.util.JSON.parse(text));
}
});
} else {
callback(linkList);
}
};
}
function showDialog(linkList) {
var data = {}, selection = editor.selection, dom = editor.dom, selectedElm, anchorElm, initialText;
var win, onlyText, textListCtrl, linkListCtrl, relListCtrl, targetListCtrl, classListCtrl;
function linkListChangeHandler(e) {
var textCtrl = win.find('#text');
if (!textCtrl.value() || (e.lastControl && textCtrl.value() == e.lastControl.text())) {
textCtrl.value(e.control.text());
}
win.find('#href').value(e.control.value());
}
function buildLinkList() {
var linkListItems = [{text: 'None', value: ''}];
tinymce.each(linkList, function(link) {
linkListItems.push({
text: link.text || link.title,
value: editor.convertURL(link.value || link.url, 'href'),
menu: link.menu
});
});
return linkListItems;
}
function buildValues(listSettingName, dataItemName, defaultItems) {
var selectedItem, items = [];
tinymce.each(editor.settings[listSettingName] || defaultItems, function(target) {
var item = {
text: target.text || target.title,
value: target.value
};
items.push(item);
if (data[dataItemName] === target.value || (!selectedItem && target.selected)) {
selectedItem = item;
}
});
if (selectedItem && !data[dataItemName]) {
data[dataItemName] = selectedItem.value;
selectedItem.selected = true;
}
return items;
}
function buildAnchorListControl(url) {
var anchorList = [];
tinymce.each(editor.dom.select('a:not([href])'), function(anchor) {
var id = anchor.name || anchor.id;
if (id) {
anchorList.push({
text: id,
value: '#' + id,
selected: url.indexOf('#' + id) != -1
});
}
});
if (anchorList.length) {
anchorList.unshift({text: 'None', value: ''});
return {
name: 'anchor',
type: 'listbox',
label: 'Anchors',
values: anchorList,
onselect: linkListChangeHandler
};
}
}
function urlChange() {
if (linkListCtrl) {
linkListCtrl.value(editor.convertURL(this.value(), 'href'));
}
if (!initialText && data.text.length === 0 && onlyText) {
this.parent().parent().find('#text')[0].value(this.value());
}
}
function isOnlyTextSelected(anchorElm) {
var html = selection.getContent();
// Partial html and not a fully selected anchor element
if (/</.test(html) && (!/^<a [^>]+>[^<]+<\/a>$/.test(html) || html.indexOf('href=') == -1)) {
return false;
}
if (anchorElm) {
var nodes = anchorElm.childNodes, i;
if (nodes.length === 0) {
return false;
}
for (i = nodes.length - 1; i >= 0; i--) {
if (nodes[i].nodeType != 3) {
return false;
}
}
}
return true;
}
selectedElm = selection.getNode();
anchorElm = dom.getParent(selectedElm, 'a[href]');
onlyText = isOnlyTextSelected();
data.text = initialText = anchorElm ? (anchorElm.innerText || anchorElm.textContent) : selection.getContent({format: 'text'});
data.href = anchorElm ? dom.getAttrib(anchorElm, 'href') : '';
data.target = anchorElm ? dom.getAttrib(anchorElm, 'target') : (editor.settings.default_link_target || null);
data.rel = anchorElm ? dom.getAttrib(anchorElm, 'rel') : null;
data['class'] = anchorElm ? dom.getAttrib(anchorElm, 'class') : null;
if (onlyText) {
textListCtrl = {
name: 'text',
type: 'textbox',
size: 40,
label: 'Text to display',
onchange: function() {
data.text = this.value();
}
};
}
if (linkList) {
linkListCtrl = {
type: 'listbox',
label: 'Link list',
values: buildLinkList(),
onselect: linkListChangeHandler,
value: editor.convertURL(data.href, 'href'),
onPostRender: function() {
linkListCtrl = this;
}
};
}
if (editor.settings.target_list !== false) {
targetListCtrl = {
name: 'target',
type: 'listbox',
label: 'Target',
values: buildValues('target_list', 'target', [{text: 'None', value: ''}, {text: 'New window', value: '_blank'}])
};
}
if (editor.settings.rel_list) {
relListCtrl = {
name: 'rel',
type: 'listbox',
label: 'Rel',
values: buildValues('rel_list', 'rel', [{text: 'None', value: ''}])
};
}
if (editor.settings.link_class_list) {
classListCtrl = {
name: 'class',
type: 'listbox',
label: 'Class',
values: buildValues('link_class_list', 'class')
};
}
win = editor.windowManager.open({
title: 'Insert link',
data: data,
body: [
{
name: 'href',
type: 'filepicker',
filetype: 'file',
size: 40,
autofocus: true,
label: 'Url',
onchange: urlChange,
onkeyup: urlChange
},
textListCtrl,
buildAnchorListControl(data.href),
linkListCtrl,
relListCtrl,
targetListCtrl,
classListCtrl
],
onSubmit: function(e) {
var href;
data = tinymce.extend(data, e.data);
href = data.href;
// Delay confirm since onSubmit will move focus
function delayedConfirm(message, callback) {
var rng = editor.selection.getRng();
window.setTimeout(function() {
editor.windowManager.confirm(message, function(state) {
editor.selection.setRng(rng);
callback(state);
});
}, 0);
}
function insertLink() {
if (anchorElm) {
editor.focus();
if (onlyText && data.text != initialText) {
anchorElm.innerText = data.text;
}
dom.setAttribs(anchorElm, {
href: href,
target: data.target ? data.target : null,
rel: data.rel ? data.rel : null,
"class": data["class"] ? data["class"] : null
});
selection.select(anchorElm);
editor.undoManager.add();
} else {
if (onlyText) {
editor.insertContent(dom.createHTML('a', {
href: href,
target: data.target ? data.target : null,
rel: data.rel ? data.rel : null,
"class": data["class"] ? data["class"] : null
}, dom.encode(data.text)));
} else {
editor.execCommand('mceInsertLink', false, {
href: href,
target: data.target,
rel: data.rel ? data.rel : null
});
}
}
}
if (!href) {
editor.execCommand('unlink');
return;
}
// Is email and not //user@domain.com
if (href.indexOf('@') > 0 && href.indexOf('//') == -1 && href.indexOf('mailto:') == -1) {
delayedConfirm(
'The URL you entered seems to be an email address. Do you want to add the required mailto: prefix?',
function(state) {
if (state) {
href = 'mailto:' + href;
}
insertLink();
}
);
return;
}
// Is www. prefixed
if (/^\s*www\./i.test(href)) {
delayedConfirm(
'The URL you entered seems to be an external link. Do you want to add the required http:// prefix?',
function(state) {
if (state) {
href = 'http://' + href;
}
insertLink();
}
);
return;
}
insertLink();
}
});
}
editor.addButton('link', {
icon: 'link',
tooltip: 'Insert/edit link',
shortcut: 'Ctrl+K',
onclick: createLinkList(showDialog),
stateSelector: 'a[href]'
});
editor.addButton('unlink', {
icon: 'unlink',
tooltip: 'Remove link',
cmd: 'unlink',
stateSelector: 'a[href]'
});
editor.addShortcut('Ctrl+K', '', createLinkList(showDialog));
this.showDialog = showDialog;
editor.addMenuItem('link', {
icon: 'link',
text: 'Insert link',
shortcut: 'Ctrl+K',
onclick: createLinkList(showDialog),
stateSelector: 'a[href]',
context: 'insert',
prependToContext: true
});
});

View File

@ -1 +0,0 @@
tinymce.PluginManager.add("link",function(e){function t(t){return function(){var n=e.settings.link_list;"string"==typeof n?tinymce.util.XHR.send({url:n,success:function(e){t(tinymce.util.JSON.parse(e))}}):t(n)}}function n(t){function n(e){var t=f.find("#text");(!t.value()||e.lastControl&&t.value()==e.lastControl.text())&&t.value(e.control.text()),f.find("#href").value(e.control.value())}function l(){var n=[{text:"None",value:""}];return tinymce.each(t,function(t){n.push({text:t.text||t.title,value:e.convertURL(t.value||t.url,"href"),menu:t.menu})}),n}function i(t,n,l){var i,a=[];return tinymce.each(e.settings[t]||l,function(e){var t={text:e.text||e.title,value:e.value};a.push(t),(p[n]===e.value||!i&&e.selected)&&(i=t)}),i&&!p[n]&&(p[n]=i.value,i.selected=!0),a}function a(t){var l=[];return tinymce.each(e.dom.select("a:not([href])"),function(e){var n=e.name||e.id;n&&l.push({text:n,value:"#"+n,selected:-1!=t.indexOf("#"+n)})}),l.length?(l.unshift({text:"None",value:""}),{name:"anchor",type:"listbox",label:"Anchors",values:l,onselect:n}):void 0}function r(){v&&v.value(e.convertURL(this.value(),"href")),!c&&0===p.text.length&&d&&this.parent().parent().find("#text")[0].value(this.value())}function o(e){var t=k.getContent();if(/</.test(t)&&(!/^<a [^>]+>[^<]+<\/a>$/.test(t)||-1==t.indexOf("href=")))return!1;if(e){var n,l=e.childNodes;if(0===l.length)return!1;for(n=l.length-1;n>=0;n--)if(3!=l[n].nodeType)return!1}return!0}var s,u,c,f,d,h,v,x,g,m,p={},k=e.selection,y=e.dom;s=k.getNode(),u=y.getParent(s,"a[href]"),d=o(),p.text=c=u?u.innerText||u.textContent:k.getContent({format:"text"}),p.href=u?y.getAttrib(u,"href"):"",p.target=u?y.getAttrib(u,"target"):e.settings.default_link_target||null,p.rel=u?y.getAttrib(u,"rel"):null,p["class"]=u?y.getAttrib(u,"class"):null,d&&(h={name:"text",type:"textbox",size:40,label:"Text to display",onchange:function(){p.text=this.value()}}),t&&(v={type:"listbox",label:"Link list",values:l(),onselect:n,value:e.convertURL(p.href,"href"),onPostRender:function(){v=this}}),e.settings.target_list!==!1&&(g={name:"target",type:"listbox",label:"Target",values:i("target_list","target",[{text:"None",value:""},{text:"New window",value:"_blank"}])}),e.settings.rel_list&&(x={name:"rel",type:"listbox",label:"Rel",values:i("rel_list","rel",[{text:"None",value:""}])}),e.settings.link_class_list&&(m={name:"class",type:"listbox",label:"Class",values:i("link_class_list","class")}),f=e.windowManager.open({title:"Insert link",data:p,body:[{name:"href",type:"filepicker",filetype:"file",size:40,autofocus:!0,label:"Url",onchange:r,onkeyup:r},h,a(p.href),v,x,g,m],onSubmit:function(t){function n(t,n){var l=e.selection.getRng();window.setTimeout(function(){e.windowManager.confirm(t,function(t){e.selection.setRng(l),n(t)})},0)}function l(){u?(e.focus(),d&&p.text!=c&&(u.innerText=p.text),y.setAttribs(u,{href:i,target:p.target?p.target:null,rel:p.rel?p.rel:null,"class":p["class"]?p["class"]:null}),k.select(u),e.undoManager.add()):d?e.insertContent(y.createHTML("a",{href:i,target:p.target?p.target:null,rel:p.rel?p.rel:null,"class":p["class"]?p["class"]:null},y.encode(p.text))):e.execCommand("mceInsertLink",!1,{href:i,target:p.target,rel:p.rel?p.rel:null})}var i;return p=tinymce.extend(p,t.data),(i=p.href)?i.indexOf("@")>0&&-1==i.indexOf("//")&&-1==i.indexOf("mailto:")?void n("The URL you entered seems to be an email address. Do you want to add the required mailto: prefix?",function(e){e&&(i="mailto:"+i),l()}):/^\s*www\./i.test(i)?void n("The URL you entered seems to be an external link. Do you want to add the required http:// prefix?",function(e){e&&(i="http://"+i),l()}):void l():void e.execCommand("unlink")}})}e.addButton("link",{icon:"link",tooltip:"Insert/edit link",shortcut:"Ctrl+K",onclick:t(n),stateSelector:"a[href]"}),e.addButton("unlink",{icon:"unlink",tooltip:"Remove link",cmd:"unlink",stateSelector:"a[href]"}),e.addShortcut("Ctrl+K","",t(n)),this.showDialog=n,e.addMenuItem("link",{icon:"link",text:"Insert link",shortcut:"Ctrl+K",onclick:t(n),stateSelector:"a[href]",context:"insert",prependToContext:!0})});

View File

@ -12,6 +12,23 @@ tinymce.PluginManager.add( 'wplink', function( editor ) {
tooltip: 'Insert/edit link',
shortcut: 'Alt+Shift+A',
cmd: 'WP_Link',
onPostRender: function() {
var ctrl = this;
editor.on( 'nodechange', function( event ) {
var node = event.element;
ctrl.disabled( editor.selection.isCollapsed() && node.nodeName !== 'A' );
ctrl.active( node.nodeName === 'A' && ! node.name );
});
}
});
editor.addButton( 'unlink', {
icon: 'unlink',
tooltip: 'Remove link',
cmd: 'unlink',
stateSelector: 'a[href]'
});

View File

@ -1 +1 @@
tinymce.PluginManager.add("wplink",function(a){a.addCommand("WP_Link",function(){"undefined"!=typeof window.wpLink&&window.wpLink.open(a.id)}),a.addButton("link",{icon:"link",tooltip:"Insert/edit link",shortcut:"Alt+Shift+A",cmd:"WP_Link",stateSelector:"a[href]"}),a.addMenuItem("link",{icon:"link",text:"Insert link",shortcut:"Alt+Shift+A",cmd:"WP_Link",stateSelector:"a[href]",context:"insert",prependToContext:!0})});
tinymce.PluginManager.add("wplink",function(a){a.addCommand("WP_Link",function(){"undefined"!=typeof window.wpLink&&window.wpLink.open(a.id)}),a.addButton("link",{icon:"link",tooltip:"Insert/edit link",shortcut:"Alt+Shift+A",cmd:"WP_Link",onPostRender:function(){var b=this;a.on("nodechange",function(c){var d=c.element;b.disabled(a.selection.isCollapsed()&&"A"!==d.nodeName),b.active("A"===d.nodeName&&!d.name)})}}),a.addButton("unlink",{icon:"unlink",tooltip:"Remove link",cmd:"unlink",stateSelector:"a[href]"}),a.addMenuItem("link",{icon:"link",text:"Insert link",shortcut:"Alt+Shift+A",cmd:"WP_Link",stateSelector:"a[href]",context:"insert",prependToContext:!0})});