| | 53 | |
| | 54 | tinymce.ui.WPLinkPreview = tinymce.ui.Control.extend( { |
| | 55 | renderHtml: function() { |
| | 56 | return ( |
| | 57 | '<div id="' + this._id + '" class="wp-link-preview">' + |
| | 58 | '<a href="' + this.url + '" target="_blank" tabindex="-1">' + this.url + '</a>' + |
| | 59 | '</div>' |
| | 60 | ); |
| | 61 | }, |
| | 62 | setURL: function( url ) { |
| | 63 | var index, lastIndex; |
| | 64 | |
| | 65 | if ( this.url !== url && this._rendered ) { |
| | 66 | this.url = url; |
| | 67 | |
| | 68 | url = window.decodeURIComponent( url ); |
| | 69 | |
| | 70 | url = url.replace( /^(?:https?:)?\/\/(?:www\.)?/, '' ); |
| | 71 | |
| | 72 | if ( ( index = url.indexOf( '?' ) ) !== -1 ) { |
| | 73 | url = url.slice( 0, index ); |
| | 74 | } |
| | 75 | |
| | 76 | if ( ( index = url.indexOf( '#' ) ) !== -1 ) { |
| | 77 | url = url.slice( 0, index ); |
| | 78 | } |
| | 79 | |
| | 80 | url = url.replace( /(?:index)?\.html$/, '' ); |
| | 81 | |
| | 82 | if ( ( lastIndex = url.lastIndexOf( '/' ) ) === url.length - 1 ) { |
| | 83 | url = url.slice( 0, lastIndex ); |
| | 84 | } |
| | 85 | |
| | 86 | if ( ( index = url.indexOf( '/' ) ) !== -1 && ( lastIndex = url.lastIndexOf( '/' ) ) !== -1 && lastIndex !== index ) { |
| | 87 | url = url.slice( 0, index + 1 ) + '\u2026' + url.slice( lastIndex, url.length ); |
| | 88 | } |
| | 89 | |
| | 90 | tinymce.$( this.getEl().firstChild ).attr( 'href', this.url ).text( url ); |
| | 91 | } |
| | 92 | }, |
| | 93 | postRender: function() { |
| | 94 | var self = this; |
| | 95 | |
| | 96 | editor.on( 'nodechange', function( event ) { |
| | 97 | if ( event.element.nodeName === 'A' ) { |
| | 98 | self.setURL( editor.$( event.element ).attr( 'href' ) ); |
| | 99 | } |
| | 100 | } ); |
| | 101 | |
| | 102 | self._rendered = true; |
| | 103 | |
| | 104 | self._super(); |
| | 105 | } |
| | 106 | } ); |
| | 107 | |
| | 108 | editor.addButton( 'wp_link_edit', { |
| | 109 | tooltip: 'Edit ', // trailing space is needed, used for context |
| | 110 | icon: 'dashicon dashicons-edit', |
| | 111 | cmd: 'WP_Link' |
| | 112 | } ); |
| | 113 | |
| | 114 | editor.addButton( 'wp_link_remove', { |
| | 115 | tooltip: 'Remove', |
| | 116 | icon: 'dashicon dashicons-no', |
| | 117 | cmd: 'unlink' |
| | 118 | } ); |
| | 119 | |
| | 120 | editor.on( 'preinit', function() { |
| | 121 | toolbar = editor.wp._createToolbar( [ |
| | 122 | 'WPLinkPreview', |
| | 123 | 'wp_link_edit', |
| | 124 | 'wp_link_remove' |
| | 125 | ], true ); |
| | 126 | } ); |
| | 127 | |
| | 128 | editor.on( 'wptoolbar', function( event ) { |
| | 129 | if ( event.element.nodeName === 'A' ) { |
| | 130 | event.toolbar = toolbar; |
| | 131 | } |
| | 132 | } ); |