-/* global tinymce */
-tinymce.PluginManager.add( 'wplink', function( editor ) {
- var linkButton;
-
- // Register a command so that it can be invoked by using tinyMCE.activeEditor.execCommand( 'WP_Link' );
- editor.addCommand( 'WP_Link', function() {
- if ( ( ! linkButton || ! linkButton.disabled() ) && typeof window.wpLink !== 'undefined' ) {
- window.wpLink.open( editor.id );
- }
- });
+( function( tinymce ) {
+ tinymce.ui.WPLinkPreview = tinymce.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( url ) {
+ var index, lastIndex;
- // WP default shortcut
- editor.addShortcut( 'alt+shift+a', '', 'WP_Link' );
- // The "de-facto standard" shortcut, see #27305
- editor.addShortcut( 'ctrl+k', '', 'WP_Link' );
+ if ( this.url !== url ) {
+ this.url = url;
- function setState( button, node ) {
- var parent = editor.dom.getParent( node, 'a' ),
- getView = editor.plugins.wpview ? editor.plugins.wpview.getView : function() { return false; };
+ url = window.decodeURIComponent( url );
- button.disabled( ( editor.selection.isCollapsed() && ! parent ) || ( parent && ! parent.href ) || getView( node ) );
- button.active( parent && parent.href );
- }
+ url = url.replace( /^(?:https?:)?\/\/(?:www\.)?/, '' );
- editor.addButton( 'link', {
- icon: 'link',
- tooltip: 'Insert/edit link',
- shortcut: 'Alt+Shift+A',
- cmd: 'WP_Link',
+ if ( ( index = url.indexOf( '?' ) ) !== -1 ) {
+ url = url.slice( 0, index );
+ }
- onPostRender: function() {
- linkButton = this;
+ if ( ( index = url.indexOf( '#' ) ) !== -1 ) {
+ url = url.slice( 0, index );
+ }
- editor.on( 'nodechange', function( event ) {
- setState( linkButton, event.element );
- });
- }
- });
+ url = url.replace( /(?:index)?\.html$/, '' );
+
+ if ( url.charAt( url.length - 1 ) === '/' ) {
+ url = url.slice( 0, -1 );
+ }
- editor.addButton( 'unlink', {
- icon: 'unlink',
- tooltip: 'Remove link',
- cmd: 'unlink',
+ // If the URL is longer that 40 chars, concatenate the beginning (after the domain) and ending with ...
+ if ( url.length > 40 && ( index = url.indexOf( '/' ) ) !== -1 && ( lastIndex = url.lastIndexOf( '/' ) ) !== -1 && lastIndex !== index ) {
+ // If the beginning + ending are shorter that 40 chars, show more of the ending
+ if ( index + url.length - lastIndex < 40 ) {
+ lastIndex = -( 40 - ( index + 1 ) );
+ }
- onPostRender: function() {
- var unlinkButton = this;
+ url = url.slice( 0, index + 1 ) + '\u2026' + url.slice( lastIndex );
+ }
- editor.on( 'nodechange', function( event ) {
- setState( unlinkButton, event.element );
- });
+ tinymce.$( this.getEl().firstChild ).attr( 'href', this.url ).text( url );
+ }
}
- });
-
- editor.addMenuItem( 'link', {
- icon: 'link',
- text: 'Insert link',
- shortcut: 'Alt+Shift+A',
- cmd: 'WP_Link',
- stateSelector: 'a[href]',
- context: 'insert',
- prependToContext: true
- });
-});
+ } );
+
+ tinymce.PluginManager.add( 'wplink', function( editor ) {
+ var toolbar;
+
+ editor.addCommand( 'WP_Link', function() {
+ window.wpLink && window.wpLink.open( editor.id );
+ });
+
+ // WP default shortcut
+ editor.addShortcut( 'access+a', '', 'WP_Link' );
+ // The "de-facto standard" shortcut, see #27305
+ editor.addShortcut( 'meta+k', '', 'WP_Link' );
+
+ editor.addButton( 'link', {
+ icon: 'link',
+ tooltip: 'Insert/edit link',
+ cmd: 'WP_Link',
+ stateSelector: 'a[href]'
+ });
+
+ editor.addButton( 'unlink', {
+ icon: 'unlink',
+ tooltip: 'Remove link',
+ cmd: 'unlink'
+ });
+
+ editor.addMenuItem( 'link', {
+ icon: 'link',
+ text: 'Insert/edit link',
+ cmd: 'WP_Link',
+ stateSelector: 'a[href]',
+ context: 'insert',
+ prependToContext: true
+ });
+
+ editor.on( 'pastepreprocess', function( event ) {
+ var pastedStr = event.content,
+ regExp = /^(?:https?:)?\/\/\S+$/i;
+
+ if ( ! editor.selection.isCollapsed() && ! regExp.test( editor.selection.getContent() ) ) {
+ pastedStr = pastedStr.replace( /<[^>]+>/g, '' );
+ pastedStr = tinymce.trim( pastedStr );
+
+ if ( regExp.test( pastedStr ) ) {
+ editor.execCommand( 'mceInsertLink', false, {
+ href: editor.dom.decode( pastedStr )
+ } );
+
+ event.preventDefault();
+ }
+ }
+ } );
+
+ editor.addButton( 'wp_link_preview', {
+ type: 'WPLinkPreview',
+ onPostRender: function() {
+ var self = this;
+
+ editor.on( 'wptoolbar', function( event ) {
+ var anchor = editor.dom.getParent( event.element, 'a' ),
+ $anchor,
+ href;
+
+ if ( anchor ) {
+ $anchor = editor.$( anchor );
+ href = $anchor.attr( 'href' );
+
+ if ( href && ! $anchor.find( 'img' ).length ) {
+ self.setURL( href );
+ event.element = anchor;
+ event.toolbar = toolbar;
+ }
+ }
+ } );
+ }
+ } );
+
+ editor.addButton( 'wp_link_edit', {
+ tooltip: 'Edit ', // trailing space is needed, used for context
+ icon: 'dashicon dashicons-edit',
+ cmd: 'WP_Link'
+ } );
+
+ editor.addButton( 'wp_link_remove', {
+ tooltip: 'Remove',
+ icon: 'dashicon dashicons-no',
+ cmd: 'unlink'
+ } );
+
+ editor.on( 'preinit', function() {
+ if ( editor.wp && editor.wp._createToolbar ) {
+ toolbar = editor.wp._createToolbar( [
+ 'wp_link_preview',
+ 'wp_link_edit',
+ 'wp_link_remove'
+ ], true );
+ }
+ } );
+ } );
+} )( window.tinymce );