]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-includes/js/tinymce/plugins/wordpress/plugin.js
WordPress 4.7.1-scripts
[autoinstalls/wordpress.git] / wp-includes / js / tinymce / plugins / wordpress / plugin.js
1 /* global getUserSetting, setUserSetting */
2 ( function( tinymce ) {
3 // Set the minimum value for the modals z-index higher than #wpadminbar (100000)
4 tinymce.ui.FloatPanel.zIndex = 100100;
5
6 tinymce.PluginManager.add( 'wordpress', function( editor ) {
7         var wpAdvButton, style,
8                 DOM = tinymce.DOM,
9                 each = tinymce.each,
10                 __ = editor.editorManager.i18n.translate,
11                 $ = window.jQuery,
12                 wp = window.wp,
13                 hasWpautop = ( wp && wp.editor && wp.editor.autop && editor.getParam( 'wpautop', true ) );
14
15         if ( $ ) {
16                 $( document ).triggerHandler( 'tinymce-editor-setup', [ editor ] );
17         }
18
19         function toggleToolbars( state ) {
20                 var iframe, initial, toolbars,
21                         pixels = 0;
22
23                 initial = ( state === 'hide' );
24
25                 if ( editor.theme.panel ) {
26                         toolbars = editor.theme.panel.find('.toolbar:not(.menubar)');
27                 }
28
29                 if ( ! toolbars || toolbars.length < 2 || ( state === 'hide' && ! toolbars[1].visible() ) ) {
30                         return;
31                 }
32
33                 if ( ! state && toolbars[1].visible() ) {
34                         state = 'hide';
35                 }
36
37                 each( toolbars, function( toolbar, i ) {
38                         if ( i > 0 ) {
39                                 if ( state === 'hide' ) {
40                                         toolbar.hide();
41                                         pixels += 30;
42                                 } else {
43                                         toolbar.show();
44                                         pixels -= 30;
45                                 }
46                         }
47                 });
48
49                 if ( pixels && ! initial ) {
50                         // Resize iframe, not needed in iOS
51                         if ( ! tinymce.Env.iOS ) {
52                                 iframe = editor.getContentAreaContainer().firstChild;
53                                 DOM.setStyle( iframe, 'height', iframe.clientHeight + pixels );
54                         }
55
56                         if ( state === 'hide' ) {
57                                 setUserSetting('hidetb', '0');
58                                 wpAdvButton && wpAdvButton.active( false );
59                         } else {
60                                 setUserSetting('hidetb', '1');
61                                 wpAdvButton && wpAdvButton.active( true );
62                         }
63                 }
64
65                 editor.fire( 'wp-toolbar-toggle' );
66         }
67
68         // Add the kitchen sink button :)
69         editor.addButton( 'wp_adv', {
70                 tooltip: 'Toolbar Toggle',
71                 cmd: 'WP_Adv',
72                 onPostRender: function() {
73                         wpAdvButton = this;
74                         wpAdvButton.active( getUserSetting( 'hidetb' ) === '1' ? true : false );
75                 }
76         });
77
78         // Hide the toolbars after loading
79         editor.on( 'PostRender', function() {
80                 if ( editor.getParam( 'wordpress_adv_hidden', true ) && getUserSetting( 'hidetb', '0' ) === '0' ) {
81                         toggleToolbars( 'hide' );
82                 }
83         });
84
85         editor.addCommand( 'WP_Adv', function() {
86                 toggleToolbars();
87         });
88
89         editor.on( 'focus', function() {
90         window.wpActiveEditor = editor.id;
91     });
92
93         editor.on( 'BeforeSetContent', function( event ) {
94                 var title;
95
96                 if ( event.content ) {
97                         if ( event.content.indexOf( '<!--more' ) !== -1 ) {
98                                 title = __( 'Read more...' );
99
100                                 event.content = event.content.replace( /<!--more(.*?)-->/g, function( match, moretext ) {
101                                         return '<img src="' + tinymce.Env.transparentSrc + '" data-wp-more="more" data-wp-more-text="' + moretext + '" ' +
102                                                 'class="wp-more-tag mce-wp-more" alt="" title="' + title + '" data-mce-resize="false" data-mce-placeholder="1" />';
103                                 });
104                         }
105
106                         if ( event.content.indexOf( '<!--nextpage-->' ) !== -1 ) {
107                                 title = __( 'Page break' );
108
109                                 event.content = event.content.replace( /<!--nextpage-->/g,
110                                         '<img src="' + tinymce.Env.transparentSrc + '" data-wp-more="nextpage" class="wp-more-tag mce-wp-nextpage" ' +
111                                                 'alt="" title="' + title + '" data-mce-resize="false" data-mce-placeholder="1" />' );
112                         }
113
114                         if ( event.load && event.format !== 'raw' && hasWpautop ) {
115                                 event.content = wp.editor.autop( event.content );
116                         }
117
118                         if ( event.content.indexOf( '<script' ) !== -1 || event.content.indexOf( '<style' ) !== -1 ) {
119                                 event.content = event.content.replace( /<(script|style)[^>]*>[\s\S]*?<\/\1>/g, function( match, tag ) {
120                                         return '<img ' +
121                                                 'src="' + tinymce.Env.transparentSrc + '" ' +
122                                                 'data-wp-preserve="' + encodeURIComponent( match ) + '" ' +
123                                                 'data-mce-resize="false" ' +
124                                                 'data-mce-placeholder="1" '+
125                                                 'class="mce-object" ' +
126                                                 'width="20" height="20" '+
127                                                 'alt="&lt;' + tag + '&gt;" ' +
128                                                 'title="&lt;' + tag + '&gt;" ' +
129                                         '/>';
130                                 } );
131                         }
132
133                         // Remove spaces from empty paragraphs.
134                         // Try to avoid a lot of backtracking, can freeze the editor. See #35890 and #38294.
135                         event.content = event.content.replace( /<p>([^<>]+)<\/p>/gi, function( tag, text ) {
136                                 if ( text === '&nbsp;' || ! /\S/.test( text ) ) {
137                                         return '<p><br /></p>';
138                                 }
139
140                                 return tag;
141                         });
142                 }
143         });
144
145         editor.on( 'PostProcess', function( event ) {
146                 if ( event.get ) {
147                         event.content = event.content.replace(/<img[^>]+>/g, function( image ) {
148                                 var match,
149                                         string,
150                                         moretext = '';
151
152                                 if ( image.indexOf( 'data-wp-more="more"' ) !== -1 ) {
153                                         if ( match = image.match( /data-wp-more-text="([^"]+)"/ ) ) {
154                                                 moretext = match[1];
155                                         }
156
157                                         string = '<!--more' + moretext + '-->';
158                                 } else if ( image.indexOf( 'data-wp-more="nextpage"' ) !== -1 ) {
159                                         string = '<!--nextpage-->';
160                                 } else if ( image.indexOf( 'data-wp-preserve' ) !== -1 ) {
161                                         if ( match = image.match( / data-wp-preserve="([^"]+)"/ ) ) {
162                                                 string = decodeURIComponent( match[1] );
163                                         }
164                                 }
165
166                                 return string || image;
167                         });
168                 }
169         });
170
171         // Display the tag name instead of img in element path
172         editor.on( 'ResolveName', function( event ) {
173                 var attr;
174
175                 if ( event.target.nodeName === 'IMG' && ( attr = editor.dom.getAttrib( event.target, 'data-wp-more' ) ) ) {
176                         event.name = attr;
177                 }
178         });
179
180         // Register commands
181         editor.addCommand( 'WP_More', function( tag ) {
182                 var parent, html, title,
183                         classname = 'wp-more-tag',
184                         dom = editor.dom,
185                         node = editor.selection.getNode();
186
187                 tag = tag || 'more';
188                 classname += ' mce-wp-' + tag;
189                 title = tag === 'more' ? 'Read more...' : 'Next page';
190                 title = __( title );
191                 html = '<img src="' + tinymce.Env.transparentSrc + '" alt="" title="' + title + '" class="' + classname + '" ' +
192                         'data-wp-more="' + tag + '" data-mce-resize="false" data-mce-placeholder="1" />';
193
194                 // Most common case
195                 if ( node.nodeName === 'BODY' || ( node.nodeName === 'P' && node.parentNode.nodeName === 'BODY' ) ) {
196                         editor.insertContent( html );
197                         return;
198                 }
199
200                 // Get the top level parent node
201                 parent = dom.getParent( node, function( found ) {
202                         if ( found.parentNode && found.parentNode.nodeName === 'BODY' ) {
203                                 return true;
204                         }
205
206                         return false;
207                 }, editor.getBody() );
208
209                 if ( parent ) {
210                         if ( parent.nodeName === 'P' ) {
211                                 parent.appendChild( dom.create( 'p', null, html ).firstChild );
212                         } else {
213                                 dom.insertAfter( dom.create( 'p', null, html ), parent );
214                         }
215
216                         editor.nodeChanged();
217                 }
218         });
219
220         editor.addCommand( 'WP_Code', function() {
221                 editor.formatter.toggle('code');
222         });
223
224         editor.addCommand( 'WP_Page', function() {
225                 editor.execCommand( 'WP_More', 'nextpage' );
226         });
227
228         editor.addCommand( 'WP_Help', function() {
229                 var access = tinymce.Env.mac ? __( 'Ctrl + Alt + letter:' ) : __( 'Shift + Alt + letter:' ),
230                         meta = tinymce.Env.mac ? __( 'Cmd + letter:' ) : __( 'Ctrl + letter:' ),
231                         table1 = [],
232                         table2 = [],
233                         row1 = {},
234                         row2 = {},
235                         i1 = 0,
236                         i2 = 0,
237                         labels = editor.settings.wp_shortcut_labels,
238                         header, html, dialog, $wrap;
239
240                 if ( ! labels ) {
241                         return;
242                 }
243
244                 function tr( row, columns ) {
245                         var out = '<tr>';
246                         var i = 0;
247
248                         columns = columns || 1;
249
250                         each( row, function( text, key ) {
251                                 out += '<td><kbd>' + key + '</kbd></td><td>' + __( text ) + '</td>';
252                                 i++;
253                         });
254
255                         while ( i < columns ) {
256                                 out += '<td></td><td></td>';
257                                 i++;
258                         }
259
260                         return out + '</tr>';
261                 }
262
263                 each ( labels, function( label, name ) {
264                         var letter;
265
266                         if ( label.indexOf( 'meta' ) !== -1 ) {
267                                 i1++;
268                                 letter = label.replace( 'meta', '' ).toLowerCase();
269
270                                 if ( letter ) {
271                                         row1[ letter ] = name;
272
273                                         if ( i1 % 2 === 0 ) {
274                                                 table1.push( tr( row1, 2 ) );
275                                                 row1 = {};
276                                         }
277                                 }
278                         } else if ( label.indexOf( 'access' ) !== -1 ) {
279                                 i2++;
280                                 letter = label.replace( 'access', '' ).toLowerCase();
281
282                                 if ( letter ) {
283                                         row2[ letter ] = name;
284
285                                         if ( i2 % 2 === 0 ) {
286                                                 table2.push( tr( row2, 2 ) );
287                                                 row2 = {};
288                                         }
289                                 }
290                         }
291                 } );
292
293                 // Add remaining single entries.
294                 if ( i1 % 2 > 0 ) {
295                         table1.push( tr( row1, 2 ) );
296                 }
297
298                 if ( i2 % 2 > 0 ) {
299                         table2.push( tr( row2, 2 ) );
300                 }
301
302                 header = [ __( 'Letter' ), __( 'Action' ), __( 'Letter' ), __( 'Action' ) ];
303                 header = '<tr><th>' + header.join( '</th><th>' ) + '</th></tr>';
304
305                 html = '<div class="wp-editor-help">';
306
307                 // Main section, default and additional shortcuts
308                 html = html +
309                         '<h2>' + __( 'Default shortcuts,' ) + ' ' + meta + '</h2>' +
310                         '<table class="wp-help-th-center fixed">' +
311                                 header +
312                                 table1.join('') +
313                         '</table>' +
314                         '<h2>' + __( 'Additional shortcuts,' ) + ' ' + access + '</h2>' +
315                         '<table class="wp-help-th-center fixed">' +
316                                 header +
317                                 table2.join('') +
318                         '</table>';
319
320                 if ( editor.plugins.wptextpattern && ( ! tinymce.Env.ie || tinymce.Env.ie > 8 ) ) {
321                         // Text pattern section
322                         html = html +
323                                 '<h2>' + __( 'When starting a new paragraph with one of these formatting shortcuts followed by a space, the formatting will be applied automatically. Press Backspace or Escape to undo.' ) + '</h2>' +
324                                 '<table class="wp-help-th-center fixed">' +
325                                         tr({ '*':  'Bullet list', '1.':  'Numbered list' }) +
326                                         tr({ '-':  'Bullet list', '1)':  'Numbered list' }) +
327                                 '</table>';
328
329                         html = html +
330                                 '<h2>' + __( 'The following formatting shortcuts are replaced when pressing Enter. Press Escape or the Undo button to undo.' ) + '</h2>' +
331                                 '<table class="wp-help-single">' +
332                                         tr({ '>': 'Blockquote' }) +
333                                         tr({ '##': 'Heading 2' }) +
334                                         tr({ '###': 'Heading 3' }) +
335                                         tr({ '####': 'Heading 4' }) +
336                                         tr({ '#####': 'Heading 5' }) +
337                                         tr({ '######': 'Heading 6' }) +
338                                         tr({ '---': 'Horizontal line' }) +
339                                 '</table>';
340                 }
341
342                 // Focus management section
343                 html = html +
344                         '<h2>' + __( 'Focus shortcuts:' ) + '</h2>' +
345                         '<table class="wp-help-single">' +
346                                 tr({ 'Alt + F8':  'Inline toolbar (when an image, link or preview is selected)' }) +
347                                 tr({ 'Alt + F9':  'Editor menu (when enabled)' }) +
348                                 tr({ 'Alt + F10': 'Editor toolbar' }) +
349                                 tr({ 'Alt + F11': 'Elements path' }) +
350                         '</table>' +
351                         '<p>' + __( 'To move focus to other buttons use Tab or the arrow keys. To return focus to the editor press Escape or use one of the buttons.' ) + '</p>';
352
353                 html += '</div>';
354
355                 dialog = editor.windowManager.open( {
356                         title: 'Keyboard Shortcuts',
357                         items: {
358                                 type: 'container',
359                                 classes: 'wp-help',
360                                 html: html
361                         },
362                         buttons: {
363                                 text: 'Close',
364                                 onclick: 'close'
365                         }
366                 } );
367
368                 if ( dialog.$el ) {
369                         dialog.$el.find( 'div[role="application"]' ).attr( 'role', 'document' );
370                         $wrap = dialog.$el.find( '.mce-wp-help' );
371
372                         if ( $wrap[0] ) {
373                                 $wrap.attr( 'tabindex', '0' );
374                                 $wrap[0].focus();
375                                 $wrap.on( 'keydown', function( event ) {
376                                         // Prevent use of: page up, page down, end, home, left arrow, up arrow, right arrow, down arrow
377                                         // in the dialog keydown handler.
378                                         if ( event.keyCode >= 33 && event.keyCode <= 40 ) {
379                                                 event.stopPropagation();
380                                         }
381                                 });
382                         }
383                 }
384         } );
385
386         editor.addCommand( 'WP_Medialib', function() {
387                 if ( wp && wp.media && wp.media.editor ) {
388                         wp.media.editor.open( editor.id );
389                 }
390         });
391
392         // Register buttons
393         editor.addButton( 'wp_more', {
394                 tooltip: 'Insert Read More tag',
395                 onclick: function() {
396                         editor.execCommand( 'WP_More', 'more' );
397                 }
398         });
399
400         editor.addButton( 'wp_page', {
401                 tooltip: 'Page break',
402                 onclick: function() {
403                         editor.execCommand( 'WP_More', 'nextpage' );
404                 }
405         });
406
407         editor.addButton( 'wp_help', {
408                 tooltip: 'Keyboard Shortcuts',
409                 cmd: 'WP_Help'
410         });
411
412         editor.addButton( 'wp_code', {
413                 tooltip: 'Code',
414                 cmd: 'WP_Code',
415                 stateSelector: 'code'
416         });
417
418         // Menubar
419         // Insert->Add Media
420         if ( wp && wp.media && wp.media.editor ) {
421                 editor.addMenuItem( 'add_media', {
422                         text: 'Add Media',
423                         icon: 'wp-media-library',
424                         context: 'insert',
425                         cmd: 'WP_Medialib'
426                 });
427         }
428
429         // Insert "Read More..."
430         editor.addMenuItem( 'wp_more', {
431                 text: 'Insert Read More tag',
432                 icon: 'wp_more',
433                 context: 'insert',
434                 onclick: function() {
435                         editor.execCommand( 'WP_More', 'more' );
436                 }
437         });
438
439         // Insert "Next Page"
440         editor.addMenuItem( 'wp_page', {
441                 text: 'Page break',
442                 icon: 'wp_page',
443                 context: 'insert',
444                 onclick: function() {
445                         editor.execCommand( 'WP_More', 'nextpage' );
446                 }
447         });
448
449         editor.on( 'BeforeExecCommand', function(e) {
450                 if ( tinymce.Env.webkit && ( e.command === 'InsertUnorderedList' || e.command === 'InsertOrderedList' ) ) {
451                         if ( ! style ) {
452                                 style = editor.dom.create( 'style', {'type': 'text/css'},
453                                         '#tinymce,#tinymce span,#tinymce li,#tinymce li>span,#tinymce p,#tinymce p>span{font:medium sans-serif;color:#000;line-height:normal;}');
454                         }
455
456                         editor.getDoc().head.appendChild( style );
457                 }
458         });
459
460         editor.on( 'ExecCommand', function( e ) {
461                 if ( tinymce.Env.webkit && style &&
462                         ( 'InsertUnorderedList' === e.command || 'InsertOrderedList' === e.command ) ) {
463
464                         editor.dom.remove( style );
465                 }
466         });
467
468         editor.on( 'init', function() {
469                 var env = tinymce.Env,
470                         bodyClass = ['mceContentBody'], // back-compat for themes that use this in editor-style.css...
471                         doc = editor.getDoc(),
472                         dom = editor.dom;
473
474                 if ( env.iOS ) {
475                         dom.addClass( doc.documentElement, 'ios' );
476                 }
477
478                 if ( editor.getParam( 'directionality' ) === 'rtl' ) {
479                         bodyClass.push('rtl');
480                         dom.setAttrib( doc.documentElement, 'dir', 'rtl' );
481                 }
482
483                 dom.setAttrib( doc.documentElement, 'lang', editor.getParam( 'wp_lang_attr' ) );
484
485                 if ( env.ie ) {
486                         if ( parseInt( env.ie, 10 ) === 9 ) {
487                                 bodyClass.push('ie9');
488                         } else if ( parseInt( env.ie, 10 ) === 8 ) {
489                                 bodyClass.push('ie8');
490                         } else if ( env.ie < 8 ) {
491                                 bodyClass.push('ie7');
492                         }
493                 } else if ( env.webkit ) {
494                         bodyClass.push('webkit');
495                 }
496
497                 bodyClass.push('wp-editor');
498
499                 each( bodyClass, function( cls ) {
500                         if ( cls ) {
501                                 dom.addClass( doc.body, cls );
502                         }
503                 });
504
505                 // Remove invalid parent paragraphs when inserting HTML
506                 editor.on( 'BeforeSetContent', function( event ) {
507                         if ( event.content ) {
508                                 event.content = event.content.replace( /<p>\s*<(p|div|ul|ol|dl|table|blockquote|h[1-6]|fieldset|pre)( [^>]*)?>/gi, '<$1$2>' )
509                                         .replace( /<\/(p|div|ul|ol|dl|table|blockquote|h[1-6]|fieldset|pre)>\s*<\/p>/gi, '</$1>' );
510                         }
511                 });
512
513                 if ( $ ) {
514                         $( document ).triggerHandler( 'tinymce-editor-init', [editor] );
515                 }
516
517                 if ( window.tinyMCEPreInit && window.tinyMCEPreInit.dragDropUpload ) {
518                         dom.bind( doc, 'dragstart dragend dragover drop', function( event ) {
519                                 if ( $ ) {
520                                         // Trigger the jQuery handlers.
521                                         $( document ).trigger( new $.Event( event ) );
522                                 }
523                         });
524                 }
525
526                 if ( editor.getParam( 'wp_paste_filters', true ) ) {
527                         editor.on( 'PastePreProcess', function( event ) {
528                                 // Remove trailing <br> added by WebKit browsers to the clipboard
529                                 event.content = event.content.replace( /<br class="?Apple-interchange-newline"?>/gi, '' );
530
531                                 // In WebKit this is handled by removeWebKitStyles()
532                                 if ( ! tinymce.Env.webkit ) {
533                                         // Remove all inline styles
534                                         event.content = event.content.replace( /(<[^>]+) style="[^"]*"([^>]*>)/gi, '$1$2' );
535
536                                         // Put back the internal styles
537                                         event.content = event.content.replace(/(<[^>]+) data-mce-style=([^>]+>)/gi, '$1 style=$2' );
538                                 }
539                         });
540
541                         editor.on( 'PastePostProcess', function( event ) {
542                                 // Remove empty paragraphs
543                                 each( dom.select( 'p', event.node ), function( node ) {
544                                         if ( dom.isEmpty( node ) ) {
545                                                 dom.remove( node );
546                                         }
547                                 });
548                         });
549                 }
550
551                 if ( editor.settings.wp_shortcut_labels && editor.theme.panel ) {
552                         var labels = {};
553                         var access = 'Shift+Alt+';
554                         var meta = 'Ctrl+';
555
556                         // For Mac: ctrl = \u2303, cmd = \u2318, alt = \u2325
557
558                         if ( tinymce.Env.mac ) {
559                                 access = '\u2303\u2325';
560                                 meta = '\u2318';
561                         }
562
563                         each( editor.settings.wp_shortcut_labels, function( value, name ) {
564                                 labels[ name ] = value.replace( 'access', access ).replace( 'meta', meta );
565                         } );
566
567                         each( editor.theme.panel.find('button'), function( button ) {
568                                 if ( button && button.settings.tooltip && labels.hasOwnProperty( button.settings.tooltip ) ) {
569                                         // Need to translate now. We are changing the string so it won't match and cannot be translated later.
570                                         button.settings.tooltip = editor.translate( button.settings.tooltip ) + ' (' + labels[ button.settings.tooltip ] + ')';
571                                 }
572                         } );
573
574                         // listbox for the "blocks" drop-down
575                         each( editor.theme.panel.find('listbox'), function( listbox ) {
576                                 if ( listbox && listbox.settings.text === 'Paragraph' ) {
577                                         each( listbox.settings.values, function( item ) {
578                                                 if ( item.text && labels.hasOwnProperty( item.text ) ) {
579                                                         item.shortcut = '(' + labels[ item.text ] + ')';
580                                                 }
581                                         } );
582                                 }
583                         } );
584                 }
585         });
586
587         editor.on( 'SaveContent', function( event ) {
588                 // If editor is hidden, we just want the textarea's value to be saved
589                 if ( ! editor.inline && editor.isHidden() ) {
590                         event.content = event.element.value;
591                         return;
592                 }
593
594                 // Keep empty paragraphs :(
595                 event.content = event.content.replace( /<p>(?:<br ?\/?>|\u00a0|\uFEFF| )*<\/p>/g, '<p>&nbsp;</p>' );
596
597                 if ( hasWpautop ) {
598                         event.content = wp.editor.removep( event.content );
599                 }
600         });
601
602         editor.on( 'preInit', function() {
603                 var validElementsSetting = '@[id|accesskey|class|dir|lang|style|tabindex|' +
604                         'title|contenteditable|draggable|dropzone|hidden|spellcheck|translate],' + // Global attributes.
605                         'i,' + // Don't replace <i> with <em> and <b> with <strong> and don't remove them when empty.
606                         'b,' +
607                         'script[src|async|defer|type|charset|crossorigin|integrity]'; // Add support for <script>.
608
609                 editor.schema.addValidElements( validElementsSetting );
610
611                 if ( tinymce.Env.iOS ) {
612                         editor.settings.height = 300;
613                 }
614
615                 each( {
616                         c: 'JustifyCenter',
617                         r: 'JustifyRight',
618                         l: 'JustifyLeft',
619                         j: 'JustifyFull',
620                         q: 'mceBlockQuote',
621                         u: 'InsertUnorderedList',
622                         o: 'InsertOrderedList',
623                         m: 'WP_Medialib',
624                         z: 'WP_Adv',
625                         t: 'WP_More',
626                         d: 'Strikethrough',
627                         h: 'WP_Help',
628                         p: 'WP_Page',
629                         x: 'WP_Code'
630                 }, function( command, key ) {
631                         editor.shortcuts.add( 'access+' + key, '', command );
632                 } );
633
634                 editor.addShortcut( 'meta+s', '', function() {
635                         if ( wp && wp.autosave ) {
636                                 wp.autosave.server.triggerSave();
637                         }
638                 } );
639
640                 if ( window.getUserSetting( 'editor_plain_text_paste_warning' ) > 1 ) {
641                         editor.settings.paste_plaintext_inform = false;
642                 }
643
644                 // Change the editor iframe title on MacOS, add the correct help shortcut.
645                 if ( tinymce.Env.mac ) {
646                         tinymce.$( editor.iframeElement ).attr( 'title', __( 'Rich Text Area. Press Control-Option-H for help.' ) );
647                 }
648         } );
649
650         editor.on( 'PastePlainTextToggle', function( event ) {
651                 // Warn twice, then stop.
652                 if ( event.state === true ) {
653                         var times = parseInt( window.getUserSetting( 'editor_plain_text_paste_warning' ), 10 ) || 0;
654
655                         if ( times < 2 ) {
656                                 window.setUserSetting( 'editor_plain_text_paste_warning', ++times );
657                         }
658                 }
659         });
660
661         /**
662          * Experimental: create a floating toolbar.
663          * This functionality will change in the next releases. Not recommended for use by plugins.
664          */
665         editor.on( 'preinit', function() {
666                 var Factory = tinymce.ui.Factory,
667                         settings = editor.settings,
668                         activeToolbar,
669                         currentSelection,
670                         timeout,
671                         container = editor.getContainer(),
672                         wpAdminbar = document.getElementById( 'wpadminbar' ),
673                         mceIframe = document.getElementById( editor.id + '_ifr' ),
674                         mceToolbar,
675                         mceStatusbar,
676                         wpStatusbar;
677
678                         if ( container ) {
679                                 mceToolbar = tinymce.$( '.mce-toolbar-grp', container )[0];
680                                 mceStatusbar = tinymce.$( '.mce-statusbar', container )[0];
681                         }
682
683                         if ( editor.id === 'content' ) {
684                                 wpStatusbar = document.getElementById( 'post-status-info' );
685                         }
686
687                 function create( buttons, bottom ) {
688                         var toolbar,
689                                 toolbarItems = [],
690                                 buttonGroup;
691
692                         each( buttons, function( item ) {
693                                 var itemName;
694
695                                 function bindSelectorChanged() {
696                                         var selection = editor.selection;
697
698                                         if ( itemName === 'bullist' ) {
699                                                 selection.selectorChanged( 'ul > li', function( state, args ) {
700                                                         var i = args.parents.length,
701                                                                 nodeName;
702
703                                                         while ( i-- ) {
704                                                                 nodeName = args.parents[ i ].nodeName;
705
706                                                                 if ( nodeName === 'OL' || nodeName == 'UL' ) {
707                                                                         break;
708                                                                 }
709                                                         }
710
711                                                         item.active( state && nodeName === 'UL' );
712                                                 } );
713                                         }
714
715                                         if ( itemName === 'numlist' ) {
716                                                 selection.selectorChanged( 'ol > li', function( state, args ) {
717                                                         var i = args.parents.length,
718                                                                 nodeName;
719
720                                                         while ( i-- ) {
721                                                                 nodeName = args.parents[ i ].nodeName;
722
723                                                                 if ( nodeName === 'OL' || nodeName === 'UL' ) {
724                                                                         break;
725                                                                 }
726                                                         }
727
728                                                         item.active( state && nodeName === 'OL' );
729                                                 } );
730                                         }
731
732                                         if ( item.settings.stateSelector ) {
733                                                 selection.selectorChanged( item.settings.stateSelector, function( state ) {
734                                                         item.active( state );
735                                                 }, true );
736                                         }
737
738                                         if ( item.settings.disabledStateSelector ) {
739                                                 selection.selectorChanged( item.settings.disabledStateSelector, function( state ) {
740                                                         item.disabled( state );
741                                                 } );
742                                         }
743                                 }
744
745                                 if ( item === '|' ) {
746                                         buttonGroup = null;
747                                 } else {
748                                         if ( Factory.has( item ) ) {
749                                                 item = {
750                                                         type: item
751                                                 };
752
753                                                 if ( settings.toolbar_items_size ) {
754                                                         item.size = settings.toolbar_items_size;
755                                                 }
756
757                                                 toolbarItems.push( item );
758
759                                                 buttonGroup = null;
760                                         } else {
761                                                 if ( ! buttonGroup ) {
762                                                         buttonGroup = {
763                                                                 type: 'buttongroup',
764                                                                 items: []
765                                                         };
766
767                                                         toolbarItems.push( buttonGroup );
768                                                 }
769
770                                                 if ( editor.buttons[ item ] ) {
771                                                         itemName = item;
772                                                         item = editor.buttons[ itemName ];
773
774                                                         if ( typeof item === 'function' ) {
775                                                                 item = item();
776                                                         }
777
778                                                         item.type = item.type || 'button';
779
780                                                         if ( settings.toolbar_items_size ) {
781                                                                 item.size = settings.toolbar_items_size;
782                                                         }
783
784                                                         item = Factory.create( item );
785
786                                                         buttonGroup.items.push( item );
787
788                                                         if ( editor.initialized ) {
789                                                                 bindSelectorChanged();
790                                                         } else {
791                                                                 editor.on( 'init', bindSelectorChanged );
792                                                         }
793                                                 }
794                                         }
795                                 }
796                         } );
797
798                         toolbar = Factory.create( {
799                                 type: 'panel',
800                                 layout: 'stack',
801                                 classes: 'toolbar-grp inline-toolbar-grp',
802                                 ariaRoot: true,
803                                 ariaRemember: true,
804                                 items: [ {
805                                         type: 'toolbar',
806                                         layout: 'flow',
807                                         items: toolbarItems
808                                 } ]
809                         } );
810
811                         toolbar.bottom = bottom;
812
813                         function reposition() {
814                                 if ( ! currentSelection ) {
815                                         return this;
816                                 }
817
818                                 var scrollX = window.pageXOffset || document.documentElement.scrollLeft,
819                                         scrollY = window.pageYOffset || document.documentElement.scrollTop,
820                                         windowWidth = window.innerWidth,
821                                         windowHeight = window.innerHeight,
822                                         iframeRect = mceIframe ? mceIframe.getBoundingClientRect() : {
823                                                 top: 0,
824                                                 right: windowWidth,
825                                                 bottom: windowHeight,
826                                                 left: 0,
827                                                 width: windowWidth,
828                                                 height: windowHeight
829                                         },
830                                         toolbar = this.getEl(),
831                                         toolbarWidth = toolbar.offsetWidth,
832                                         toolbarHeight = toolbar.clientHeight,
833                                         selection = currentSelection.getBoundingClientRect(),
834                                         selectionMiddle = ( selection.left + selection.right ) / 2,
835                                         buffer = 5,
836                                         spaceNeeded = toolbarHeight + buffer,
837                                         wpAdminbarBottom = wpAdminbar ? wpAdminbar.getBoundingClientRect().bottom : 0,
838                                         mceToolbarBottom = mceToolbar ? mceToolbar.getBoundingClientRect().bottom : 0,
839                                         mceStatusbarTop = mceStatusbar ? windowHeight - mceStatusbar.getBoundingClientRect().top : 0,
840                                         wpStatusbarTop = wpStatusbar ? windowHeight - wpStatusbar.getBoundingClientRect().top : 0,
841                                         blockedTop = Math.max( 0, wpAdminbarBottom, mceToolbarBottom, iframeRect.top ),
842                                         blockedBottom = Math.max( 0, mceStatusbarTop, wpStatusbarTop, windowHeight - iframeRect.bottom ),
843                                         spaceTop = selection.top + iframeRect.top - blockedTop,
844                                         spaceBottom = windowHeight - iframeRect.top - selection.bottom - blockedBottom,
845                                         editorHeight = windowHeight - blockedTop - blockedBottom,
846                                         className = '',
847                                         iosOffsetTop = 0,
848                                         iosOffsetBottom = 0,
849                                         top, left;
850
851                                 if ( spaceTop >= editorHeight || spaceBottom >= editorHeight ) {
852                                         this.scrolling = true;
853                                         this.hide();
854                                         this.scrolling = false;
855                                         return this;
856                                 }
857
858                                 // Add offset in iOS to move the menu over the image, out of the way of the default iOS menu.
859                                 if ( tinymce.Env.iOS && currentSelection.nodeName === 'IMG' ) {
860                                         iosOffsetTop = 54;
861                                         iosOffsetBottom = 46;
862                                 }
863
864                                 if ( this.bottom ) {
865                                         if ( spaceBottom >= spaceNeeded ) {
866                                                 className = ' mce-arrow-up';
867                                                 top = selection.bottom + iframeRect.top + scrollY - iosOffsetBottom;
868                                         } else if ( spaceTop >= spaceNeeded ) {
869                                                 className = ' mce-arrow-down';
870                                                 top = selection.top + iframeRect.top + scrollY - toolbarHeight + iosOffsetTop;
871                                         }
872                                 } else {
873                                         if ( spaceTop >= spaceNeeded ) {
874                                                 className = ' mce-arrow-down';
875                                                 top = selection.top + iframeRect.top + scrollY - toolbarHeight + iosOffsetTop;
876                                         } else if ( spaceBottom >= spaceNeeded && editorHeight / 2 > selection.bottom + iframeRect.top - blockedTop ) {
877                                                 className = ' mce-arrow-up';
878                                                 top = selection.bottom + iframeRect.top + scrollY - iosOffsetBottom;
879                                         }
880                                 }
881
882                                 if ( typeof top === 'undefined' ) {
883                                         top = scrollY + blockedTop + buffer + iosOffsetBottom;
884                                 }
885
886                                 left = selectionMiddle - toolbarWidth / 2 + iframeRect.left + scrollX;
887
888                                 if ( selection.left < 0 || selection.right > iframeRect.width ) {
889                                         left = iframeRect.left + scrollX + ( iframeRect.width - toolbarWidth ) / 2;
890                                 } else if ( toolbarWidth >= windowWidth ) {
891                                         className += ' mce-arrow-full';
892                                         left = 0;
893                                 } else if ( ( left < 0 && selection.left + toolbarWidth > windowWidth ) || ( left + toolbarWidth > windowWidth && selection.right - toolbarWidth < 0 ) ) {
894                                         left = ( windowWidth - toolbarWidth ) / 2;
895                                 } else if ( left < iframeRect.left + scrollX ) {
896                                         className += ' mce-arrow-left';
897                                         left = selection.left + iframeRect.left + scrollX;
898                                 } else if ( left + toolbarWidth > iframeRect.width + iframeRect.left + scrollX ) {
899                                         className += ' mce-arrow-right';
900                                         left = selection.right - toolbarWidth + iframeRect.left + scrollX;
901                                 }
902
903                                 // No up/down arrows on the menu over images in iOS.
904                                 if ( tinymce.Env.iOS && currentSelection.nodeName === 'IMG' ) {
905                                         className = className.replace( / ?mce-arrow-(up|down)/g, '' );
906                                 }
907
908                                 toolbar.className = toolbar.className.replace( / ?mce-arrow-[\w]+/g, '' ) + className;
909
910                                 DOM.setStyles( toolbar, {
911                                         'left': left,
912                                         'top': top
913                                 } );
914
915                                 return this;
916                         }
917
918                         toolbar.on( 'show', function() {
919                                 this.reposition();
920                         } );
921
922                         toolbar.on( 'keydown', function( event ) {
923                                 if ( event.keyCode === 27 ) {
924                                         this.hide();
925                                         editor.focus();
926                                 }
927                         } );
928
929                         editor.on( 'remove', function() {
930                                 toolbar.remove();
931                         } );
932
933                         toolbar.reposition = reposition;
934                         toolbar.hide().renderTo( document.body );
935
936                         return toolbar;
937                 }
938
939                 editor.shortcuts.add( 'alt+119', '', function() {
940                         var node;
941
942                         if ( activeToolbar ) {
943                                 node = activeToolbar.find( 'toolbar' )[0];
944                                 node && node.focus( true );
945                         }
946                 } );
947
948                 editor.on( 'nodechange', function( event ) {
949                         var collapsed = editor.selection.isCollapsed();
950
951                         var args = {
952                                 element: event.element,
953                                 parents: event.parents,
954                                 collapsed: collapsed
955                         };
956
957                         editor.fire( 'wptoolbar', args );
958
959                         currentSelection = args.selection || args.element;
960
961                         if ( activeToolbar && activeToolbar !== args.toolbar ) {
962                                 activeToolbar.hide();
963                         }
964
965                         if ( args.toolbar ) {
966                                 if ( activeToolbar !== args.toolbar ) {
967                                         activeToolbar = args.toolbar;
968                                         activeToolbar.show();
969                                 } else {
970                                         activeToolbar.reposition();
971                                 }
972                         } else {
973                                 activeToolbar = false;
974                         }
975                 } );
976
977                 editor.on( 'focus', function() {
978                         if ( activeToolbar ) {
979                                 activeToolbar.show();
980                         }
981                 } );
982
983                 function hide( event ) {
984                         if ( activeToolbar ) {
985                                 if ( activeToolbar.tempHide || event.type === 'hide' ) {
986                                         activeToolbar.hide();
987                                         activeToolbar = false;
988                                 } else if ( (
989                                         event.type === 'resizewindow' ||
990                                         event.type === 'scrollwindow' ||
991                                         event.type === 'resize' ||
992                                         event.type === 'scroll'
993                                 ) && ! activeToolbar.blockHide ) {
994                                         clearTimeout( timeout );
995
996                                         timeout = setTimeout( function() {
997                                                 if ( activeToolbar && typeof activeToolbar.show === 'function' ) {
998                                                         activeToolbar.scrolling = false;
999                                                         activeToolbar.show();
1000                                                 }
1001                                         }, 250 );
1002
1003                                         activeToolbar.scrolling = true;
1004                                         activeToolbar.hide();
1005                                 }
1006                         }
1007                 }
1008
1009                 // For full height editor.
1010                 editor.on( 'resizewindow scrollwindow', hide );
1011                 // For scrollable editor.
1012                 editor.dom.bind( editor.getWin(), 'resize scroll', hide );
1013
1014                 editor.on( 'remove', function() {
1015                         editor.off( 'resizewindow scrollwindow', hide );
1016                         editor.dom.unbind( editor.getWin(), 'resize scroll', hide );
1017                 } );
1018
1019                 editor.on( 'blur hide', hide );
1020
1021                 editor.wp = editor.wp || {};
1022                 editor.wp._createToolbar = create;
1023         }, true );
1024
1025         function noop() {}
1026
1027         // Expose some functions (back-compat)
1028         return {
1029                 _showButtons: noop,
1030                 _hideButtons: noop,
1031                 _setEmbed: noop,
1032                 _getEmbed: noop
1033         };
1034 });
1035
1036 }( window.tinymce ));