1 ( function( tinymce ) {
2 tinymce.ui.WPLinkPreview = tinymce.ui.Control.extend( {
4 renderHtml: function() {
6 '<div id="' + this._id + '" class="wp-link-preview">' +
7 '<a href="' + this.url + '" target="_blank" tabindex="-1">' + this.url + '</a>' +
11 setURL: function( url ) {
14 if ( this.url !== url ) {
17 url = window.decodeURIComponent( url );
19 url = url.replace( /^(?:https?:)?\/\/(?:www\.)?/, '' );
21 if ( ( index = url.indexOf( '?' ) ) !== -1 ) {
22 url = url.slice( 0, index );
25 if ( ( index = url.indexOf( '#' ) ) !== -1 ) {
26 url = url.slice( 0, index );
29 url = url.replace( /(?:index)?\.html$/, '' );
31 if ( url.charAt( url.length - 1 ) === '/' ) {
32 url = url.slice( 0, -1 );
35 // If the URL is longer that 40 chars, concatenate the beginning (after the domain) and ending with ...
36 if ( url.length > 40 && ( index = url.indexOf( '/' ) ) !== -1 && ( lastIndex = url.lastIndexOf( '/' ) ) !== -1 && lastIndex !== index ) {
37 // If the beginning + ending are shorter that 40 chars, show more of the ending
38 if ( index + url.length - lastIndex < 40 ) {
39 lastIndex = -( 40 - ( index + 1 ) );
42 url = url.slice( 0, index + 1 ) + '\u2026' + url.slice( lastIndex );
45 tinymce.$( this.getEl().firstChild ).attr( 'href', this.url ).text( url );
50 tinymce.PluginManager.add( 'wplink', function( editor ) {
53 editor.addCommand( 'WP_Link', function() {
54 window.wpLink && window.wpLink.open( editor.id );
57 // WP default shortcut
58 editor.addShortcut( 'access+a', '', 'WP_Link' );
59 // The "de-facto standard" shortcut, see #27305
60 editor.addShortcut( 'meta+k', '', 'WP_Link' );
62 editor.addButton( 'link', {
64 tooltip: 'Insert/edit link',
66 stateSelector: 'a[href]'
69 editor.addButton( 'unlink', {
71 tooltip: 'Remove link',
75 editor.addMenuItem( 'link', {
77 text: 'Insert/edit link',
79 stateSelector: 'a[href]',
81 prependToContext: true
84 editor.on( 'pastepreprocess', function( event ) {
85 var pastedStr = event.content,
86 regExp = /^(?:https?:)?\/\/\S+$/i;
88 if ( ! editor.selection.isCollapsed() && ! regExp.test( editor.selection.getContent() ) ) {
89 pastedStr = pastedStr.replace( /<[^>]+>/g, '' );
90 pastedStr = tinymce.trim( pastedStr );
92 if ( regExp.test( pastedStr ) ) {
93 editor.execCommand( 'mceInsertLink', false, {
94 href: editor.dom.decode( pastedStr )
97 event.preventDefault();
102 editor.addButton( 'wp_link_preview', {
103 type: 'WPLinkPreview',
104 onPostRender: function() {
107 editor.on( 'wptoolbar', function( event ) {
108 var anchor = editor.dom.getParent( event.element, 'a' ),
113 $anchor = editor.$( anchor );
114 href = $anchor.attr( 'href' );
116 if ( href && ! $anchor.find( 'img' ).length ) {
118 event.element = anchor;
119 event.toolbar = toolbar;
126 editor.addButton( 'wp_link_edit', {
127 tooltip: 'Edit ', // trailing space is needed, used for context
128 icon: 'dashicon dashicons-edit',
132 editor.addButton( 'wp_link_remove', {
134 icon: 'dashicon dashicons-no',
138 editor.on( 'preinit', function() {
139 if ( editor.wp && editor.wp._createToolbar ) {
140 toolbar = editor.wp._createToolbar( [
148 } )( window.tinymce );