1 ( function( window, $, undefined ) {
4 var $window = $( window ),
5 $document = $( document ),
6 $adminBar = $( '#wpadminbar' ),
7 $footer = $( '#wpfooter' );
9 /* Autoresize editor. */
11 var $wrap = $( '#postdivrich' ),
12 $contentWrap = $( '#wp-content-wrap' ),
13 $tools = $( '#wp-content-editor-tools' ),
16 $textTop = $( '#ed_toolbar' ),
17 $textEditor = $( '#content' ),
18 textEditor = $textEditor[0],
20 $bottom = $( '#post-status-info' ),
23 $sideSortables = $( '#side-sortables' ),
24 $postboxContainer = $( '#postbox-container-1' ),
25 $postBody = $('#post-body'),
26 fullscreen = window.wp.editor && window.wp.editor.fullscreen,
28 mceBind = function(){},
29 mceUnbind = function(){},
33 fixedSideBottom = false,
35 lastScrollPosition = 0,
36 pageYOffsetAtTop = 130,
39 autoresizeMinHeight = 300,
40 initialMode = $contentWrap.hasClass( 'tmce-active' ) ? 'tinymce' : 'html',
41 advanced = !! parseInt( window.getUserSetting( 'hidetb' ), 10 ),
42 // These are corrected when adjust() runs, except on scrolling if already set.
53 sideSortablesHeight: 0
56 function getHeights() {
57 var windowWidth = $window.width();
60 windowHeight: $window.height(),
61 windowWidth: windowWidth,
62 adminBarHeight: ( windowWidth > 600 ? $adminBar.outerHeight() : 0 ),
63 toolsHeight: $tools.outerHeight() || 0,
64 menuBarHeight: $menuBar.outerHeight() || 0,
65 visualTopHeight: $visualTop.outerHeight() || 0,
66 textTopHeight: $textTop.outerHeight() || 0,
67 bottomHeight: $bottom.outerHeight() || 0,
68 statusBarHeight: $statusBar.outerHeight() || 0,
69 sideSortablesHeight: $sideSortables.height() || 0
73 if ( heights.menuBarHeight < 3 ) {
74 heights.menuBarHeight = 0;
78 function textEditorResize() {
79 if ( mceEditor && ! mceEditor.isHidden() ) {
83 if ( ! mceEditor && initialMode === 'tinymce' ) {
87 var length = textEditor.value.length;
88 var height = parseInt( textEditor.style.height, 10 );
89 var top = window.scrollTop;
91 if ( length < oldTextLength ) {
92 // textEditor.scrollHeight is not adjusted until the next line.
93 textEditor.style.height = 'auto';
95 if ( textEditor.scrollHeight > autoresizeMinHeight ) {
96 textEditor.style.height = textEditor.scrollHeight + 'px';
98 textEditor.style.height = autoresizeMinHeight + 'px';
101 // Prevent scroll-jumping in Firefox and IE.
102 window.scrollTop = top;
104 if ( textEditor.scrollHeight < height ) {
107 } else if ( height < textEditor.scrollHeight ) {
108 textEditor.style.height = textEditor.scrollHeight + 'px';
112 oldTextLength = length;
115 // We need to wait for TinyMCE to initialize.
116 $document.on( 'tinymce-editor-init.editor-expand', function( event, editor ) {
117 var VK = window.tinymce.util.VK,
118 hideFloatPanels = _.debounce( function() {
119 ! $( '.mce-floatpanel:hover' ).length && window.tinymce.ui.FloatPanel.hideAll();
120 $( '.mce-tooltip' ).hide();
123 // Make sure it's the main editor.
124 if ( editor.id !== 'content' ) {
128 // Copy the editor instance.
131 // Set the minimum height to the initial viewport height.
132 editor.settings.autoresize_min_height = autoresizeMinHeight;
134 // Get the necessary UI elements.
135 $visualTop = $contentWrap.find( '.mce-toolbar-grp' );
136 $visualEditor = $contentWrap.find( '.mce-edit-area' );
137 $statusBar = $contentWrap.find( '.mce-statusbar' );
138 $menuBar = $contentWrap.find( '.mce-menubar' );
140 function mceGetCursorOffset() {
141 var node = editor.selection.getNode(),
144 if ( editor.wp && editor.wp.getView && ( view = editor.wp.getView( node ) ) ) {
145 offset = view.getBoundingClientRect();
147 range = editor.selection.getRng();
150 offset = range.getClientRects()[0];
154 offset = node.getBoundingClientRect();
158 return offset.height ? offset : false;
161 // Make sure the cursor is always visible.
162 // This is not only necessary to keep the cursor between the toolbars,
163 // but also to scroll the window when the cursor moves out of the viewport to a wpview.
164 // Setting a buffer > 0 will prevent the browser default.
165 // Some browsers will scroll to the middle,
166 // others to the top/bottom of the *window* when moving the cursor out of the viewport.
167 function mceKeyup( event ) {
168 var key = event.keyCode;
170 // Bail on special keys.
171 if ( key <= 47 && ! ( key === VK.SPACEBAR || key === VK.ENTER || key === VK.DELETE || key === VK.BACKSPACE || key === VK.UP || key === VK.LEFT || key === VK.DOWN || key === VK.UP ) ) {
173 // OS keys, function keys, num lock, scroll lock
174 } else if ( ( key >= 91 && key <= 93 ) || ( key >= 112 && key <= 123 ) || key === 144 || key === 145 ) {
181 function mceScroll( key ) {
182 var offset = mceGetCursorOffset(),
184 cursorTop, cursorBottom, editorTop, editorBottom;
190 cursorTop = offset.top + editor.iframeElement.getBoundingClientRect().top;
191 cursorBottom = cursorTop + offset.height;
192 cursorTop = cursorTop - buffer;
193 cursorBottom = cursorBottom + buffer;
194 editorTop = heights.adminBarHeight + heights.toolsHeight + heights.menuBarHeight + heights.visualTopHeight;
195 editorBottom = heights.windowHeight - ( advanced ? heights.bottomHeight + heights.statusBarHeight : 0 );
197 // Don't scroll if the node is taller than the visible part of the editor
198 if ( editorBottom - editorTop < offset.height ) {
202 if ( cursorTop < editorTop && ( key === VK.UP || key === VK.LEFT || key === VK.BACKSPACE ) ) {
203 window.scrollTo( window.pageXOffset, cursorTop + window.pageYOffset - editorTop );
204 } else if ( cursorBottom > editorBottom ) {
205 window.scrollTo( window.pageXOffset, cursorBottom + window.pageYOffset - editorBottom );
209 function mceFullscreenToggled( event ) {
210 if ( ! event.state ) {
215 // Adjust when switching editor modes.
217 $window.on( 'scroll.mce-float-panels', hideFloatPanels );
219 setTimeout( function() {
220 editor.execCommand( 'wpAutoResize' );
226 $window.off( 'scroll.mce-float-panels' );
228 setTimeout( function() {
229 var top = $contentWrap.offset().top;
231 if ( window.pageYOffset > top ) {
232 window.scrollTo( window.pageXOffset, top - heights.adminBarHeight );
242 function toggleAdvanced() {
243 advanced = ! advanced;
246 mceBind = function() {
247 editor.on( 'keyup', mceKeyup );
248 editor.on( 'show', mceShow );
249 editor.on( 'hide', mceHide );
250 editor.on( 'wp-toolbar-toggle', toggleAdvanced );
251 // Adjust when the editor resizes.
252 editor.on( 'setcontent wp-autoresize wp-toolbar-toggle', adjust );
253 // Don't hide the caret after undo/redo.
254 editor.on( 'undo redo', mceScroll );
255 // Adjust when exiting TinyMCE's fullscreen mode.
256 editor.on( 'FullscreenStateChanged', mceFullscreenToggled );
258 $window.off( 'scroll.mce-float-panels' ).on( 'scroll.mce-float-panels', hideFloatPanels );
261 mceUnbind = function() {
262 editor.off( 'keyup', mceKeyup );
263 editor.off( 'show', mceShow );
264 editor.off( 'hide', mceHide );
265 editor.off( 'wp-toolbar-toggle', toggleAdvanced );
266 editor.off( 'setcontent wp-autoresize wp-toolbar-toggle', adjust );
267 editor.off( 'undo redo', mceScroll );
268 editor.off( 'FullscreenStateChanged', mceFullscreenToggled );
270 $window.off( 'scroll.mce-float-panels' );
273 if ( $wrap.hasClass( 'wp-editor-expand' ) ) {
274 // Adjust "immediately"
276 initialResize( adjust );
280 // Adjust the toolbars based on the active editor mode.
281 function adjust( event ) {
282 // Make sure we're not in fullscreen mode.
283 if ( fullscreen && fullscreen.settings.visible ) {
287 var windowPos = $window.scrollTop(),
288 type = event && event.type,
289 resize = type !== 'scroll',
290 visual = mceEditor && ! mceEditor.isHidden(),
291 buffer = autoresizeMinHeight,
292 postBodyTop = $postBody.offset().top,
294 contentWrapWidth = $contentWrap.width(),
295 $top, $editor, sidebarTop, footerTop, canPin,
296 topPos, topHeight, editorPos, editorHeight;
298 // Refresh the heights
299 if ( resize || ! heights.windowHeight ) {
303 if ( ! visual && type === 'resize' ) {
309 $editor = $visualEditor;
310 topHeight = heights.visualTopHeight;
313 $editor = $textEditor;
314 topHeight = heights.textTopHeight;
317 // TinyMCE still intializing.
318 if ( ! visual && ! $top.length ) {
322 topPos = $top.parent().offset().top;
323 editorPos = $editor.offset().top;
324 editorHeight = $editor.outerHeight();
327 canPin = visual ? autoresizeMinHeight + topHeight : autoresizeMinHeight + 20; // 20px from textarea padding
328 canPin = editorHeight > ( canPin + 5 );
333 position: 'absolute',
335 width: contentWrapWidth
338 if ( visual && $menuBar.length ) {
340 position: 'absolute',
342 width: contentWrapWidth - ( borderWidth * 2 )
347 position: 'absolute',
348 top: heights.menuBarHeight,
349 width: contentWrapWidth - ( borderWidth * 2 ) - ( visual ? 0 : ( $top.outerWidth() - $top.width() ) )
352 $statusBar.attr( 'style', advanced ? '' : 'visibility: hidden;' );
353 $bottom.attr( 'style', '' );
356 // Maybe pin the top.
357 if ( ( ! fixedTop || resize ) &&
358 // Handle scrolling down.
359 ( windowPos >= ( topPos - heights.toolsHeight - heights.adminBarHeight ) &&
360 // Handle scrolling up.
361 windowPos <= ( topPos - heights.toolsHeight - heights.adminBarHeight + editorHeight - buffer ) ) ) {
366 top: heights.adminBarHeight,
367 width: contentWrapWidth
370 if ( visual && $menuBar.length ) {
373 top: heights.adminBarHeight + heights.toolsHeight,
374 width: contentWrapWidth - ( borderWidth * 2 ) - ( visual ? 0 : ( $top.outerWidth() - $top.width() ) )
380 top: heights.adminBarHeight + heights.toolsHeight + heights.menuBarHeight,
381 width: contentWrapWidth - ( borderWidth * 2 ) - ( visual ? 0 : ( $top.outerWidth() - $top.width() ) )
383 // Maybe unpin the top.
384 } else if ( fixedTop || resize ) {
385 // Handle scrolling up.
386 if ( windowPos <= ( topPos - heights.toolsHeight - heights.adminBarHeight ) ) {
390 position: 'absolute',
392 width: contentWrapWidth
395 if ( visual && $menuBar.length ) {
397 position: 'absolute',
399 width: contentWrapWidth - ( borderWidth * 2 )
404 position: 'absolute',
405 top: heights.menuBarHeight,
406 width: contentWrapWidth - ( borderWidth * 2 ) - ( visual ? 0 : ( $top.outerWidth() - $top.width() ) )
408 // Handle scrolling down.
409 } else if ( windowPos >= ( topPos - heights.toolsHeight - heights.adminBarHeight + editorHeight - buffer ) ) {
413 position: 'absolute',
414 top: editorHeight - buffer,
415 width: contentWrapWidth
418 if ( visual && $menuBar.length ) {
420 position: 'absolute',
421 top: editorHeight - buffer,
422 width: contentWrapWidth - ( borderWidth * 2 )
427 position: 'absolute',
428 top: editorHeight - buffer + heights.menuBarHeight,
429 width: contentWrapWidth - ( borderWidth * 2 ) - ( visual ? 0 : ( $top.outerWidth() - $top.width() ) )
434 // Maybe adjust the bottom bar.
435 if ( ( ! fixedBottom || ( resize && advanced ) ) &&
436 // +[n] for the border around the .wp-editor-container.
437 ( windowPos + heights.windowHeight ) <= ( editorPos + editorHeight + heights.bottomHeight + heights.statusBarHeight + borderWidth ) ) {
439 if ( event && event.deltaHeight > 0 && event.deltaHeight < 100 ) {
440 window.scrollBy( 0, event.deltaHeight );
441 } else if ( visual && advanced ) {
446 bottom: heights.bottomHeight,
448 width: contentWrapWidth - ( borderWidth * 2 )
454 width: contentWrapWidth
457 } else if ( ( ! advanced && fixedBottom ) ||
458 ( ( fixedBottom || resize ) &&
459 ( windowPos + heights.windowHeight ) > ( editorPos + editorHeight + heights.bottomHeight + heights.statusBarHeight - borderWidth ) ) ) {
462 $statusBar.attr( 'style', advanced ? '' : 'visibility: hidden;' );
463 $bottom.attr( 'style', '' );
468 if ( $postboxContainer.width() < 300 && heights.windowWidth > 600 && // sidebar position is changed with @media from CSS, make sure it is on the side
469 $document.height() > ( $sideSortables.height() + postBodyTop + 120 ) && // the sidebar is not the tallest element
470 heights.windowHeight < editorHeight ) { // the editor is taller than the viewport
472 if ( ( heights.sideSortablesHeight + pinnedToolsTop + sidebarBottom ) > heights.windowHeight || fixedSideTop || fixedSideBottom ) {
473 // Reset when scrolling to the top
474 if ( windowPos + pinnedToolsTop <= postBodyTop ) {
475 $sideSortables.attr( 'style', '' );
476 fixedSideTop = fixedSideBottom = false;
478 if ( windowPos > lastScrollPosition ) {
480 if ( fixedSideTop ) {
482 fixedSideTop = false;
483 sidebarTop = $sideSortables.offset().top - heights.adminBarHeight;
484 footerTop = $footer.offset().top;
486 // don't get over the footer
487 if ( footerTop < sidebarTop + heights.sideSortablesHeight + sidebarBottom ) {
488 sidebarTop = footerTop - heights.sideSortablesHeight - 12;
492 position: 'absolute',
496 } else if ( ! fixedSideBottom && heights.sideSortablesHeight + $sideSortables.offset().top + sidebarBottom < windowPos + heights.windowHeight ) {
498 fixedSideBottom = true;
503 bottom: sidebarBottom
506 } else if ( windowPos < lastScrollPosition ) {
508 if ( fixedSideBottom ) {
510 fixedSideBottom = false;
511 sidebarTop = $sideSortables.offset().top - sidebarBottom;
512 footerTop = $footer.offset().top;
514 // don't get over the footer
515 if ( footerTop < sidebarTop + heights.sideSortablesHeight + sidebarBottom ) {
516 sidebarTop = footerTop - heights.sideSortablesHeight - 12;
520 position: 'absolute',
524 } else if ( ! fixedSideTop && $sideSortables.offset().top >= windowPos + pinnedToolsTop ) {
537 // if the sidebar container is smaller than the viewport, then pin/unpin the top when scrolling
538 if ( windowPos >= ( postBodyTop - pinnedToolsTop ) ) {
540 $sideSortables.css( {
545 $sideSortables.attr( 'style', '' );
548 fixedSideTop = fixedSideBottom = false;
551 lastScrollPosition = windowPos;
553 $sideSortables.attr( 'style', '' );
554 fixedSideTop = fixedSideBottom = false;
559 paddingTop: heights.toolsHeight
564 paddingTop: heights.visualTopHeight + heights.menuBarHeight
568 marginTop: heights.textTopHeight
574 function fullscreenHide() {
579 function initialResize( callback ) {
580 for ( var i = 1; i < 6; i++ ) {
581 setTimeout( callback, 500 * i );
585 function afterScroll() {
586 clearTimeout( scrollTimer );
587 scrollTimer = setTimeout( adjust, 100 );
591 // Scroll to the top when triggering this from JS.
592 // Ensures toolbars are pinned properly.
593 if ( window.pageYOffset && window.pageYOffset > pageYOffsetAtTop ) {
594 window.scrollTo( window.pageXOffset, 0 );
597 $wrap.addClass( 'wp-editor-expand' );
599 // Adjust when the window is scrolled or resized.
600 $window.on( 'scroll.editor-expand resize.editor-expand', function( event ) {
601 adjust( event.type );
605 // Adjust when collapsing the menu, changing the columns, changing the body class.
606 $document.on( 'wp-collapse-menu.editor-expand postboxes-columnchange.editor-expand editor-classchange.editor-expand', adjust )
607 .on( 'postbox-toggled.editor-expand postbox-moved.editor-expand', function() {
608 if ( ! fixedSideTop && ! fixedSideBottom && window.pageYOffset > pinnedToolsTop ) {
609 fixedSideBottom = true;
610 window.scrollBy( 0, -1 );
612 window.scrollBy( 0, 1 );
616 }).on( 'wp-window-resized.editor-expand', function() {
617 if ( mceEditor && ! mceEditor.isHidden() ) {
618 mceEditor.execCommand( 'wpAutoResize' );
624 $textEditor.on( 'focus.editor-expand input.editor-expand propertychange.editor-expand', textEditorResize );
627 // Adjust when entering/exiting fullscreen mode.
628 fullscreen && fullscreen.pubsub.subscribe( 'hidden', fullscreenHide );
631 mceEditor.settings.wp_autoresize_on = true;
632 mceEditor.execCommand( 'wpAutoResizeOn' );
634 if ( ! mceEditor.isHidden() ) {
635 mceEditor.execCommand( 'wpAutoResize' );
639 if ( ! mceEditor || mceEditor.isHidden() ) {
645 $document.trigger( 'editor-expand-on' );
649 var height = parseInt( window.getUserSetting( 'ed_size', 300 ), 10 );
653 } else if ( height > 5000 ) {
657 // Scroll to the top when triggering this from JS.
658 // Ensures toolbars are reset properly.
659 if ( window.pageYOffset && window.pageYOffset > pageYOffsetAtTop ) {
660 window.scrollTo( window.pageXOffset, 0 );
663 $wrap.removeClass( 'wp-editor-expand' );
665 $window.off( '.editor-expand' );
666 $document.off( '.editor-expand' );
667 $textEditor.off( '.editor-expand' );
670 // Adjust when entering/exiting fullscreen mode.
671 fullscreen && fullscreen.pubsub.unsubscribe( 'hidden', fullscreenHide );
674 $.each( [ $visualTop, $textTop, $tools, $menuBar, $bottom, $statusBar, $contentWrap, $visualEditor, $textEditor, $sideSortables ], function( i, element ) {
675 element && element.attr( 'style', '' );
678 fixedTop = fixedBottom = fixedSideTop = fixedSideBottom = false;
681 mceEditor.settings.wp_autoresize_on = false;
682 mceEditor.execCommand( 'wpAutoResizeOff' );
684 if ( ! mceEditor.isHidden() ) {
688 mceEditor.theme.resizeTo( null, height );
694 $textEditor.height( height );
697 $document.trigger( 'editor-expand-off' );
701 if ( $wrap.hasClass( 'wp-editor-expand' ) ) {
704 // Ideally we need to resize just after CSS has fully loaded and QuickTags is ready.
705 if ( $contentWrap.hasClass( 'html-active' ) ) {
706 initialResize( function() {
713 // Show the on/off checkbox
714 $( '#adv-settings .editor-expand' ).show();
715 $( '#editor-expand-toggle' ).on( 'change.editor-expand', function() {
716 if ( $(this).prop( 'checked' ) ) {
718 window.setUserSetting( 'editor_expand', 'on' );
721 window.setUserSetting( 'editor_expand', 'off' );
725 // Expose on() and off()
726 window.editorExpand = {
734 var $body = $( document.body ),
735 $wrap = $( '#wpcontent' ),
736 $editor = $( '#post-body-content' ),
737 $title = $( '#title' ),
738 $content = $( '#content' ),
739 $overlay = $( document.createElement( 'DIV' ) ),
740 $slug = $( '#edit-slug-box' ),
741 $slugFocusEl = $slug.find( 'a' )
742 .add( $slug.find( 'button' ) )
743 .add( $slug.find( 'input' ) ),
744 $menuWrap = $( '#adminmenuwrap' ),
747 _isActive = window.getUserSetting( 'editor_expand', 'on' ) === 'on',
748 _isOn = _isActive ? window.getUserSetting( 'post_dfw' ) === 'on' : false,
752 faded, fadedAdminBar, fadedSlug,
753 editorRect, x, y, mouseY, scrollY,
754 focusLostTimer, overlayTimer, editorHasFocus;
756 $body.append( $overlay );
761 top: $adminBar.height(),
772 $window.on( 'mousemove.focus', function( event ) {
773 mouseY = event.pageY;
776 function recalcEditorRect() {
777 editorRect = $editor.offset();
778 editorRect.right = editorRect.left + $editor.outerWidth();
779 editorRect.bottom = editorRect.top + $editor.outerHeight();
782 function activate() {
786 $document.trigger( 'dfw-activate' );
787 $content.on( 'keydown.focus-shortcut', toggleViaKeyboard );
791 function deactivate() {
797 $document.trigger( 'dfw-deactivate' );
798 $content.off( 'keydown.focus-shortcut' );
802 function isActive() {
807 if ( ! _isOn && _isActive ) {
810 $content.on( 'keydown.focus', fadeOut );
812 $title.add( $content ).on( 'blur.focus', maybeFadeIn );
816 window.setUserSetting( 'post_dfw', 'on' );
818 $document.trigger( 'dfw-on' );
826 $title.add( $content ).off( '.focus' );
830 $editor.off( '.focus' );
832 window.setUserSetting( 'post_dfw', 'off' );
834 $document.trigger( 'dfw-off' );
850 function fadeOut( event ) {
852 key = event && event.keyCode;
854 if ( window.navigator.platform ) {
855 isMac = ( window.navigator.platform.indexOf( 'Mac' ) > -1 );
858 // fadeIn and return on Escape and keyboard shortcut Alt+Shift+W and Ctrl+Opt+W.
859 if ( key === 27 || ( key === 87 && event.altKey && ( ( ! isMac && event.shiftKey ) || ( isMac && event.ctrlKey ) ) ) ) {
864 if ( event && ( event.metaKey || ( event.ctrlKey && ! event.altKey ) || ( event.altKey && event.shiftKey ) || ( key && (
865 // Special keys ( tab, ctrl, alt, esc, arrow keys... )
866 ( key <= 47 && key !== 8 && key !== 13 && key !== 32 && key !== 46 ) ||
868 ( key >= 91 && key <= 93 ) ||
870 ( key >= 112 && key <= 135 ) ||
871 // Num Lock, Scroll Lock, OEM
872 ( key >= 144 && key <= 150 ) ||
873 // OEM or non-printable
882 clearTimeout( overlayTimer );
884 overlayTimer = setTimeout( function() {
888 $editor.css( 'z-index', 9998 );
891 // Always recalculate the editor area entering the overlay with the mouse.
892 .on( 'mouseenter.focus', function() {
895 $window.on( 'scroll.focus', function() {
896 var nScrollY = window.pageYOffset;
902 mouseY < editorRect.top - buffer ||
903 mouseY > editorRect.bottom + buffer
911 .on( 'mouseleave.focus', function() {
913 traveledX = traveledY = 0;
915 $window.off( 'scroll.focus' );
917 // Fade in when the mouse moves away form the editor area.
918 .on( 'mousemove.focus', function( event ) {
919 var nx = event.clientX,
921 pageYOffset = window.pageYOffset,
922 pageXOffset = window.pageXOffset;
924 if ( x && y && ( nx !== x || ny !== y ) ) {
926 ( ny <= y && ny < editorRect.top - pageYOffset ) ||
927 ( ny >= y && ny > editorRect.bottom - pageYOffset ) ||
928 ( nx <= x && nx < editorRect.left - pageXOffset ) ||
929 ( nx >= x && nx > editorRect.right - pageXOffset )
931 traveledX += Math.abs( x - nx );
932 traveledY += Math.abs( y - ny );
935 ny <= editorRect.top - buffer - pageYOffset ||
936 ny >= editorRect.bottom + buffer - pageYOffset ||
937 nx <= editorRect.left - buffer - pageXOffset ||
938 nx >= editorRect.right + buffer - pageXOffset
946 traveledX = traveledY = 0;
951 traveledX = traveledY = 0;
958 // When the overlay is touched, always fade in and cancel the event.
959 .on( 'touchstart.focus', function( event ) {
960 event.preventDefault();
964 $editor.off( 'mouseenter.focus' );
966 if ( focusLostTimer ) {
967 clearTimeout( focusLostTimer );
968 focusLostTimer = null;
971 $body.addClass( 'focus-on' ).removeClass( 'focus-off' );
978 function fadeIn( event ) {
982 clearTimeout( overlayTimer );
984 overlayTimer = setTimeout( function() {
988 $editor.css( 'z-index', '' );
990 $overlay.off( 'mouseenter.focus mouseleave.focus mousemove.focus touchstart.focus' );
993 * When fading in, temporarily watch for refocus and fade back out - helps
994 * with 'accidental' editor exits with the mouse. When fading in and the event
995 * is a key event (Escape or Alt+Shift+W) don't watch for refocus.
997 if ( 'undefined' === typeof event ) {
998 $editor.on( 'mouseenter.focus', function() {
999 if ( $.contains( $editor.get( 0 ), document.activeElement ) || editorHasFocus ) {
1005 focusLostTimer = setTimeout( function() {
1006 focusLostTimer = null;
1007 $editor.off( 'mouseenter.focus' );
1010 $body.addClass( 'focus-off' ).removeClass( 'focus-on' );
1017 function maybeFadeIn() {
1018 setTimeout( function() {
1019 var position = document.activeElement.compareDocumentPosition( $editor.get( 0 ) );
1021 function hasFocus( $el ) {
1022 return $.contains( $el.get( 0 ), document.activeElement );
1025 // The focused node is before or behind the editor area, and not outside the wrap.
1026 if ( ( position === 2 || position === 4 ) && ( hasFocus( $menuWrap ) || hasFocus( $wrap ) || hasFocus( $footer ) ) ) {
1032 function fadeOutAdminBar() {
1033 if ( ! fadedAdminBar && faded ) {
1034 fadedAdminBar = true;
1037 .on( 'mouseenter.focus', function() {
1038 $adminBar.addClass( 'focus-off' );
1040 .on( 'mouseleave.focus', function() {
1041 $adminBar.removeClass( 'focus-off' );
1046 function fadeInAdminBar() {
1047 if ( fadedAdminBar ) {
1048 fadedAdminBar = false;
1050 $adminBar.off( '.focus' );
1054 function fadeOutSlug() {
1055 if ( ! fadedSlug && faded && ! $slug.find( ':focus').length ) {
1058 $slug.stop().fadeTo( 'fast', 0.3 ).on( 'mouseenter.focus', fadeInSlug ).off( 'mouseleave.focus' );
1060 $slugFocusEl.on( 'focus.focus', fadeInSlug ).off( 'blur.focus' );
1064 function fadeInSlug() {
1068 $slug.stop().fadeTo( 'fast', 1 ).on( 'mouseleave.focus', fadeOutSlug ).off( 'mouseenter.focus' );
1070 $slugFocusEl.on( 'blur.focus', fadeOutSlug ).off( 'focus.focus' );
1074 function toggleViaKeyboard( event ) {
1075 if ( event.altKey && event.shiftKey && 87 === event.keyCode ) {
1080 if ( $( '#postdivrich' ).hasClass( 'wp-editor-expand' ) ) {
1081 $content.on( 'keydown.focus-shortcut', toggleViaKeyboard );
1084 $document.on( 'tinymce-editor-setup.focus', function( event, editor ) {
1085 editor.addButton( 'dfw', {
1087 classes: 'wp-dfw btn widget',
1088 disabled: ! _isActive,
1090 onPostRender: function() {
1094 .on( 'dfw-activate.focus', function() {
1095 button.disabled( false );
1097 .on( 'dfw-deactivate.focus', function() {
1098 button.disabled( true );
1100 .on( 'dfw-on.focus', function() {
1101 button.active( true );
1103 .on( 'dfw-off.focus', function() {
1104 button.active( false );
1107 tooltip: 'Distraction-free writing mode',
1108 shortcut: 'Alt+Shift+W'
1111 editor.addCommand( 'wpToggleDFW', toggle );
1112 editor.addShortcut( 'access+w', '', 'wpToggleDFW' );
1115 $document.on( 'tinymce-editor-init.focus', function( event, editor ) {
1116 var mceBind, mceUnbind;
1119 editorHasFocus = true;
1123 editorHasFocus = false;
1126 if ( editor.id === 'content' ) {
1127 $editorWindow = $( editor.getWin() );
1128 $editorIframe = $( editor.getContentAreaContainer() ).find( 'iframe' );
1130 mceBind = function() {
1131 editor.on( 'keydown', fadeOut );
1132 editor.on( 'blur', maybeFadeIn );
1133 editor.on( 'focus', focus );
1134 editor.on( 'blur', blur );
1135 editor.on( 'wp-autoresize', recalcEditorRect );
1138 mceUnbind = function() {
1139 editor.off( 'keydown', fadeOut );
1140 editor.off( 'blur', maybeFadeIn );
1141 editor.off( 'focus', focus );
1142 editor.off( 'blur', blur );
1143 editor.off( 'wp-autoresize', recalcEditorRect );
1150 $document.on( 'dfw-on.focus', mceBind ).on( 'dfw-off.focus', mceUnbind );
1152 // Make sure the body focuses when clicking outside it.
1153 editor.on( 'click', function( event ) {
1154 if ( event.target === editor.getDoc().documentElement ) {
1161 $document.on( 'quicktags-init', function( event, editor ) {
1164 if ( editor.settings.buttons && ( ',' + editor.settings.buttons + ',' ).indexOf( ',dfw,' ) !== -1 ) {
1165 $button = $( '#' + editor.name + '_dfw' );
1168 .on( 'dfw-activate', function() {
1169 $button.prop( 'disabled', false );
1171 .on( 'dfw-deactivate', function() {
1172 $button.prop( 'disabled', true );
1174 .on( 'dfw-on', function() {
1175 $button.addClass( 'active' );
1177 .on( 'dfw-off', function() {
1178 $button.removeClass( 'active' );
1183 $document.on( 'editor-expand-on.focus', activate ).on( 'editor-expand-off.focus', deactivate );
1186 $content.on( 'keydown.focus', fadeOut );
1188 $title.add( $content ).on( 'blur.focus', maybeFadeIn );
1191 window.wp = window.wp || {};
1192 window.wp.editor = window.wp.editor || {};
1193 window.wp.editor.dfw = {
1195 deactivate: deactivate,
1203 } )( window, window.jQuery );