WordPress/wp-includes/js/tinymce/plugins/wplink/plugin.min.js
Andrew Ozz 3a2ef071d4 TinyMCE inline link:
- Fix not displaying anything when the URL is only a fragment. Show the whole URL.
- Fix editing a link when it is the very first word in the editor.
- Fix editing a link then some of the surrounding text or space is selected. Change the selection to only the link node.
- Add placeholder when adding new link.

See #33301.
Built from https://develop.svn.wordpress.org/trunk@36483


git-svn-id: http://core.svn.wordpress.org/trunk@36450 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2016-02-06 00:51:27 +00:00

1 line
5.1 KiB
JavaScript

!function(a){a.ui.WPLinkPreview=a.ui.Control.extend({url:"#",renderHtml:function(){return'<div id="'+this._id+'" class="wp-link-preview"><a href="'+this.url+'" target="_blank" tabindex="-1">'+this.url+"</a></div>"},setURL:function(b){var c,d;this.url!==b&&(this.url=b,b=window.decodeURIComponent(b),b=b.replace(/^(?:https?:)?\/\/(?:www\.)?/,""),-1!==(c=b.indexOf("?"))&&(b=b.slice(0,c)),-1!==(c=b.indexOf("#"))&&(b=b.slice(0,c)),b=b.replace(/(?:index)?\.html$/,""),"/"===b.charAt(b.length-1)&&(b=b.slice(0,-1)),""===b&&(b=this.url),b.length>40&&-1!==(c=b.indexOf("/"))&&-1!==(d=b.lastIndexOf("/"))&&d!==c&&(c+b.length-d<40&&(d=-(40-(c+1))),b=b.slice(0,c+1)+"\u2026"+b.slice(d)),a.$(this.getEl().firstChild).attr("href",this.url).text(b))}}),a.ui.WPLinkInput=a.ui.Control.extend({renderHtml:function(){return'<div id="'+this._id+'" class="wp-link-input"><input type="text" value="" tabindex="-1" placeholder="'+a.translate("Paste URL or type to search")+'" /></div>'},setURL:function(a){this.getEl().firstChild.value=a}}),a.PluginManager.add("wplink",function(b){function c(){var a,c=b.selection.getNode(),d=b.selection.getContent(),e=b.dom.getParent(c,"a[href]");return e||-1===d.indexOf("</a>")||(a=d.match(/href="([^">]+)"/),a&&a[1]&&(e=b.$('a[href="'+a[1]+'"]',c)[0]),e&&(b.selection.select(e),b.nodeChanged())),e}var d,e,f,g,h,i=window.jQuery;b.on("preinit",function(){b.wp&&b.wp._createToolbar&&(e=b.wp._createToolbar(["wp_link_preview","wp_link_edit","wp_link_remove"],!0),f=b.wp._createToolbar(["wp_link_input","wp_link_apply","wp_link_advanced"],!0),f.on("show",function(){var a=f.find("toolbar")[0];a&&a.focus(!0),d=c()}),f.on("hide",function(){f.scrolling||b.execCommand("wp_link_cancel")}))}),b.addCommand("WP_Link",function(){var a=c();a?b.dom.setAttribs(a,{"data-wp-edit":!0}):(b.execCommand("mceInsertLink",!1,{href:"_wp_link_placeholder"}),b.nodeChanged())}),b.addCommand("wp_link_apply",function(){if(!f.scrolling){var c=a.trim(h.getEl().firstChild.value);if(c&&!/^(?:[a-z]+:|#|\?|\.|\/)/.test(c)&&(c="http://"+c),!c)return void b.dom.remove(d,!0);d&&b.dom.setAttribs(d,{href:c,"data-wp-edit":null}),d=!1,b.nodeChanged(),b.focus()}}),b.addCommand("wp_link_cancel",function(){d&&("_wp_link_placeholder"===b.$(d).attr("href")?b.dom.remove(d,!0):b.dom.setAttribs(d,{"data-wp-edit":null})),d=!1,b.nodeChanged(),b.focus()}),b.addShortcut("access+a","","WP_Link"),b.addShortcut("meta+k","","WP_Link"),b.addButton("link",{icon:"link",tooltip:"Insert/edit link",cmd:"WP_Link",stateSelector:"a[href]"}),b.addButton("unlink",{icon:"unlink",tooltip:"Remove link",cmd:"unlink"}),b.addMenuItem("link",{icon:"link",text:"Insert/edit link",cmd:"WP_Link",stateSelector:"a[href]",context:"insert",prependToContext:!0}),b.on("pastepreprocess",function(c){var d=c.content,e=/^(?:https?:)?\/\/\S+$/i;b.selection.isCollapsed()||e.test(b.selection.getContent())||(d=d.replace(/<[^>]+>/g,""),d=a.trim(d),e.test(d)&&(b.execCommand("mceInsertLink",!1,{href:b.dom.decode(d)}),c.preventDefault()))}),b.addButton("wp_link_preview",{type:"WPLinkPreview",onPostRender:function(){g=this}}),b.addButton("wp_link_input",{type:"WPLinkInput",onPostRender:function(){var c,d,e=this.getEl().firstChild;h=this,i&&(i(e).on("keydown",function(){i(e).removeAttr("aria-activedescendant")}).autocomplete({source:function(a,b){return d===a.term?void b(c):/^https?:/.test(a.term)||-1!==a.term.indexOf(".")?b():(i.post(window.ajaxurl,{action:"wp-link-ajax",page:1,search:a.term,_ajax_linking_nonce:i("#_ajax_linking_nonce").val()},function(a){c=a,b(a)},"json"),void(d=a.term))},focus:function(a,b){i(e).attr("aria-activedescendant","mce-wp-autocomplete-"+b.item.ID)},select:function(a,b){return i(e).val(b.item.permalink),!1},open:function(){i(e).attr("aria-expanded","true"),f.blockHide=!0},close:function(){i(e).attr("aria-expanded","false"),f.blockHide=!1},minLength:2,position:{my:"left top+5"}}).autocomplete("instance")._renderItem=function(a,b){return i('<li role="option" id="mce-wp-autocomplete-'+b.ID+'">').append("<span>"+b.title+'</span>&nbsp;<span class="alignright">'+b.info+"</span>").appendTo(a)},i(e).attr({role:"combobox","aria-autocomplete":"list","aria-expanded":"false","aria-owns":i(e).autocomplete("widget").attr("id")}).on("focus",function(){i(e).autocomplete("search")}).autocomplete("widget").addClass("mce-wp-autocomplete").attr("role","listbox")),a.$(e).on("keydown",function(a){13===a.keyCode&&b.execCommand("wp_link_apply")})}}),b.on("wptoolbar",function(a){var c,d,i,j=b.dom.getParent(a.element,"a");j&&(c=b.$(j),d=c.attr("href"),i=c.attr("data-wp-edit"),"_wp_link_placeholder"===d||i?(h.setURL(i?d:""),a.element=j,a.toolbar=f):d&&!c.find("img").length&&(g.setURL(d),a.element=j,a.toolbar=e))}),b.addButton("wp_link_edit",{tooltip:"Edit ",icon:"dashicon dashicons-edit",cmd:"WP_Link"}),b.addButton("wp_link_remove",{tooltip:"Remove",icon:"dashicon dashicons-no",cmd:"unlink"}),b.addButton("wp_link_advanced",{tooltip:"Advanced",icon:"dashicon dashicons-admin-generic",onclick:function(){b.execCommand("wp_link_apply"),window.wpLink&&window.wpLink.open(b.id)}}),b.addButton("wp_link_apply",{tooltip:"Apply",icon:"dashicon dashicons-editor-break",cmd:"wp_link_apply",classes:"widget btn primary"})})}(window.tinymce);