| 1 | | /* global tinymce */ |
| 2 | | tinymce.PluginManager.add( 'wplink', function( editor ) { |
| 3 | | editor.addCommand( 'WP_Link', function() { |
| 4 | | window.wpLink && window.wpLink.open( editor.id ); |
| 5 | | }); |
| 6 | | |
| 7 | | // WP default shortcut |
| 8 | | editor.addShortcut( 'Alt+Shift+A', '', 'WP_Link' ); |
| 9 | | // The "de-facto standard" shortcut, see #27305 |
| 10 | | editor.addShortcut( 'Meta+K', '', 'WP_Link' ); |
| 11 | | |
| 12 | | editor.addButton( 'link', { |
| 13 | | icon: 'link', |
| 14 | | tooltip: 'Insert/edit link', |
| 15 | | cmd: 'WP_Link', |
| 16 | | stateSelector: 'a[href]' |
| 17 | | }); |
| 18 | | |
| 19 | | editor.addButton( 'unlink', { |
| 20 | | icon: 'unlink', |
| 21 | | tooltip: 'Remove link', |
| 22 | | cmd: 'unlink' |
| 23 | | }); |
| 24 | | |
| 25 | | editor.addMenuItem( 'link', { |
| 26 | | icon: 'link', |
| 27 | | text: 'Insert/edit link', |
| 28 | | cmd: 'WP_Link', |
| 29 | | stateSelector: 'a[href]', |
| 30 | | context: 'insert', |
| 31 | | prependToContext: true |
| 32 | | }); |
| 33 | | |
| 34 | | editor.on( 'pastepreprocess', function( event ) { |
| 35 | | var pastedStr = event.content, |
| 36 | | regExp = /^(?:https?:)?\/\/\S+$/i; |
| 37 | | |
| 38 | | if ( ! editor.selection.isCollapsed() && ! regExp.test( editor.selection.getContent() ) ) { |
| 39 | | pastedStr = pastedStr.replace( /<[^>]+>/g, '' ); |
| 40 | | pastedStr = tinymce.trim( pastedStr ); |
| 41 | | |
| 42 | | if ( regExp.test( pastedStr ) ) { |
| 43 | | editor.execCommand( 'mceInsertLink', false, { |
| 44 | | href: editor.dom.decode( pastedStr ) |
| | 1 | ( function( tinymce ) { |
| | 2 | tinymce.PluginManager.add( 'wplink', function( editor ) { |
| | 3 | var toolbar; |
| | 4 | |
| | 5 | editor.addCommand( 'WP_Link', function() { |
| | 6 | window.wpLink && window.wpLink.open( editor.id ); |
| | 7 | } ); |
| | 8 | |
| | 9 | // WP default shortcut. |
| | 10 | editor.addShortcut( 'Alt+Shift+A', '', 'WP_Link' ); |
| | 11 | // The "de-facto standard" shortcut, see #27305. |
| | 12 | editor.addShortcut( 'Meta+K', '', 'WP_Link' ); |
| | 13 | |
| | 14 | editor.addButton( 'link', { |
| | 15 | icon: 'link', |
| | 16 | tooltip: 'Insert/edit link', |
| | 17 | cmd: 'WP_Link', |
| | 18 | stateSelector: 'a[href]' |
| | 19 | } ); |
| | 20 | |
| | 21 | editor.addButton( 'unlink', { |
| | 22 | icon: 'unlink', |
| | 23 | tooltip: 'Remove link', |
| | 24 | cmd: 'unlink' |
| | 25 | } ); |
| | 26 | |
| | 27 | editor.addMenuItem( 'link', { |
| | 28 | icon: 'link', |
| | 29 | text: 'Insert/edit link', |
| | 30 | cmd: 'WP_Link', |
| | 31 | stateSelector: 'a[href]', |
| | 32 | context: 'insert', |
| | 33 | prependToContext: true |
| | 34 | } ); |
| | 35 | |
| | 36 | editor.on( 'pastepreprocess', function( event ) { |
| | 37 | var pastedStr = event.content, |
| | 38 | regExp = /^(?:https?:)?\/\/\S+$/i; |
| | 39 | |
| | 40 | if ( ! editor.selection.isCollapsed() && ! regExp.test( editor.selection.getContent() ) ) { |
| | 41 | pastedStr = pastedStr.replace( /<[^>]+>/g, '' ); |
| | 42 | pastedStr = tinymce.trim( pastedStr ); |
| | 43 | |
| | 44 | if ( regExp.test( pastedStr ) ) { |
| | 45 | editor.execCommand( 'mceInsertLink', false, { |
| | 46 | href: editor.dom.decode( pastedStr ) |
| | 47 | } ); |
| | 48 | |
| | 49 | event.preventDefault(); |
| | 50 | } |
| | 51 | } |
| | 52 | } ); |
| | 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">' + 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 = url.replace( /^(?:https?:)?\/\/(?:www\.)?/, '' ); |
| | 69 | |
| | 70 | if ( ( index = url.indexOf( '?' ) ) !== -1 ) { |
| | 71 | url = url.slice( 0, index ); |
| | 72 | } |
| | 73 | |
| | 74 | if ( ( index = url.indexOf( '#' ) ) !== -1 ) { |
| | 75 | url = url.slice( 0, index ); |
| | 76 | } |
| | 77 | |
| | 78 | url = url.replace( /(?:index)?\.html$/, '' ); |
| | 79 | |
| | 80 | if ( ( lastIndex = url.lastIndexOf( '/' ) ) === url.length - 1 ) { |
| | 81 | url = url.slice( 0, lastIndex ); |
| | 82 | } |
| | 83 | |
| | 84 | if ( ( index = url.indexOf( '/' ) ) !== -1 && ( lastIndex = url.lastIndexOf( '/' ) ) !== -1 && lastIndex !== index ) { |
| | 85 | url = url.replace( url.slice( index + 1, lastIndex ), '\u2026' ); |
| | 86 | } |
| | 87 | |
| | 88 | tinymce.$( this.getEl().firstChild ).attr( 'href', this.url ).text( url ); |
| | 89 | } |
| | 90 | }, |
| | 91 | postRender: function() { |
| | 92 | var self = this; |
| | 93 | |
| | 94 | editor.on( 'nodechange', function( event ) { |
| | 95 | if ( event.element.nodeName === 'A' ) { |
| | 96 | self.setURL( editor.$( event.element ).attr( 'href' ) ); |
| | 97 | } |