]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-includes/js/tinymce/plugins/wplink/plugin.js
WordPress 4.3.1
[autoinstalls/wordpress.git] / wp-includes / js / tinymce / plugins / wplink / plugin.js
1 ( function( tinymce ) {
2         tinymce.ui.WPLinkPreview = tinymce.ui.Control.extend( {
3                 url: '#',
4                 renderHtml: function() {
5                         return (
6                                 '<div id="' + this._id + '" class="wp-link-preview">' +
7                                         '<a href="' + this.url + '" target="_blank" tabindex="-1">' + this.url + '</a>' +
8                                 '</div>'
9                         );
10                 },
11                 setURL: function( url ) {
12                         var index, lastIndex;
13
14                         if ( this.url !== url ) {
15                                 this.url = url;
16
17                                 url = window.decodeURIComponent( url );
18
19                                 url = url.replace( /^(?:https?:)?\/\/(?:www\.)?/, '' );
20
21                                 if ( ( index = url.indexOf( '?' ) ) !== -1 ) {
22                                         url = url.slice( 0, index );
23                                 }
24
25                                 if ( ( index = url.indexOf( '#' ) ) !== -1 ) {
26                                         url = url.slice( 0, index );
27                                 }
28
29                                 url = url.replace( /(?:index)?\.html$/, '' );
30
31                                 if ( url.charAt( url.length - 1 ) === '/' ) {
32                                         url = url.slice( 0, -1 );
33                                 }
34
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 ) );
40                                         }
41
42                                         url = url.slice( 0, index + 1 ) + '\u2026' + url.slice( lastIndex );
43                                 }
44
45                                 tinymce.$( this.getEl().firstChild ).attr( 'href', this.url ).text( url );
46                         }
47                 }
48         } );
49
50         tinymce.PluginManager.add( 'wplink', function( editor ) {
51                 var toolbar;
52
53                 editor.addCommand( 'WP_Link', function() {
54                         window.wpLink && window.wpLink.open( editor.id );
55                 });
56
57                 // WP default shortcut
58                 editor.addShortcut( 'Alt+Shift+A', '', 'WP_Link' );
59                 // The "de-facto standard" shortcut, see #27305
60                 editor.addShortcut( 'Meta+K', '', 'WP_Link' );
61
62                 editor.addButton( 'link', {
63                         icon: 'link',
64                         tooltip: 'Insert/edit link',
65                         cmd: 'WP_Link',
66                         stateSelector: 'a[href]'
67                 });
68
69                 editor.addButton( 'unlink', {
70                         icon: 'unlink',
71                         tooltip: 'Remove link',
72                         cmd: 'unlink'
73                 });
74
75                 editor.addMenuItem( 'link', {
76                         icon: 'link',
77                         text: 'Insert/edit link',
78                         cmd: 'WP_Link',
79                         stateSelector: 'a[href]',
80                         context: 'insert',
81                         prependToContext: true
82                 });
83
84                 editor.on( 'pastepreprocess', function( event ) {
85                         var pastedStr = event.content,
86                                 regExp = /^(?:https?:)?\/\/\S+$/i;
87
88                         if ( ! editor.selection.isCollapsed() && ! regExp.test( editor.selection.getContent() ) ) {
89                                 pastedStr = pastedStr.replace( /<[^>]+>/g, '' );
90                                 pastedStr = tinymce.trim( pastedStr );
91
92                                 if ( regExp.test( pastedStr ) ) {
93                                         editor.execCommand( 'mceInsertLink', false, {
94                                                 href: editor.dom.decode( pastedStr )
95                                         } );
96
97                                         event.preventDefault();
98                                 }
99                         }
100                 } );
101
102                 editor.addButton( 'wp_link_preview', {
103                         type: 'WPLinkPreview',
104                         onPostRender: function() {
105                                 var self = this;
106
107                                 editor.on( 'wptoolbar', function( event ) {
108                                         var anchor = editor.dom.getParent( event.element, 'a' ),
109                                                 $anchor,
110                                                 href;
111
112                                         if ( anchor ) {
113                                                 $anchor = editor.$( anchor );
114                                                 href = $anchor.attr( 'href' );
115
116                                                 if ( href && ! $anchor.find( 'img' ).length ) {
117                                                         self.setURL( href );
118                                                         event.element = anchor;
119                                                         event.toolbar = toolbar;
120                                                 }
121                                         }
122                                 } );
123                         }
124                 } );
125
126                 editor.addButton( 'wp_link_edit', {
127                         tooltip: 'Edit ', // trailing space is needed, used for context
128                         icon: 'dashicon dashicons-edit',
129                         cmd: 'WP_Link'
130                 } );
131
132                 editor.addButton( 'wp_link_remove', {
133                         tooltip: 'Remove',
134                         icon: 'dashicon dashicons-no',
135                         cmd: 'unlink'
136                 } );
137
138                 editor.on( 'preinit', function() {
139                         if ( editor.wp && editor.wp._createToolbar ) {
140                                 toolbar = editor.wp._createToolbar( [
141                                         'wp_link_preview',
142                                         'wp_link_edit',
143                                         'wp_link_remove'
144                                 ], true );
145                         }
146                 } );
147         } );
148 } )( window.tinymce );