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 $textEditorClone = $( '<div id="content-textarea-clone"></div>' ),
19 $bottom = $( '#post-status-info' ),
22 $sideSortables = $( '#side-sortables' ),
23 $postboxContainer = $( '#postbox-container-1' ),
24 $postBody = $('#post-body'),
25 fullscreen = window.wp.editor && window.wp.editor.fullscreen,
27 mceBind = function(){},
28 mceUnbind = function(){},
32 fixedSideBottom = false,
34 lastScrollPosition = 0,
35 pageYOffsetAtTop = 130,
38 autoresizeMinHeight = 300,
39 initialMode = $contentWrap.hasClass( 'tmce-active' ) ? 'tinymce' : 'html',
40 advanced = !! parseInt( window.getUserSetting( 'hidetb' ), 10 ),
41 // These are corrected when adjust() runs, except on scrolling if already set.
52 sideSortablesHeight: 0
55 $textEditorClone.insertAfter( $textEditor );
57 $textEditorClone.css( {
58 'font-family': $textEditor.css( 'font-family' ),
59 'font-size': $textEditor.css( 'font-size' ),
60 'line-height': $textEditor.css( 'line-height' ),
61 'white-space': 'pre-wrap',
62 'word-wrap': 'break-word'
65 function getHeights() {
66 var windowWidth = $window.width();
69 windowHeight: $window.height(),
70 windowWidth: windowWidth,
71 adminBarHeight: ( windowWidth > 600 ? $adminBar.outerHeight() : 0 ),
72 toolsHeight: $tools.outerHeight() || 0,
73 menuBarHeight: $menuBar.outerHeight() || 0,
74 visualTopHeight: $visualTop.outerHeight() || 0,
75 textTopHeight: $textTop.outerHeight() || 0,
76 bottomHeight: $bottom.outerHeight() || 0,
77 statusBarHeight: $statusBar.outerHeight() || 0,
78 sideSortablesHeight: $sideSortables.height() || 0
82 if ( heights.menuBarHeight < 3 ) {
83 heights.menuBarHeight = 0;
87 function textEditorKeyup( event ) {
88 var VK = jQuery.ui.keyCode,
90 range = document.createRange(),
91 selStart = $textEditor[0].selectionStart,
92 selEnd = $textEditor[0].selectionEnd,
93 textNode = $textEditorClone[0].firstChild,
95 offset, cursorTop, cursorBottom, editorTop, editorBottom;
97 if ( selStart && selEnd && selStart !== selEnd ) {
101 // These are not TinyMCE ranges.
103 range.setStart( textNode, selStart );
104 range.setEnd( textNode, selEnd + 1 );
107 offset = range.getBoundingClientRect();
109 if ( ! offset.height ) {
113 cursorTop = offset.top - buffer;
114 cursorBottom = cursorTop + offset.height + buffer;
115 editorTop = heights.adminBarHeight + heights.toolsHeight + heights.textTopHeight;
116 editorBottom = heights.windowHeight - heights.bottomHeight;
118 if ( cursorTop < editorTop && ( key === VK.UP || key === VK.LEFT || key === VK.BACKSPACE ) ) {
119 window.scrollTo( window.pageXOffset, cursorTop + window.pageYOffset - editorTop );
120 } else if ( cursorBottom > editorBottom ) {
121 window.scrollTo( window.pageXOffset, cursorBottom + window.pageYOffset - editorBottom );
125 function textEditorResize() {
126 if ( ( mceEditor && ! mceEditor.isHidden() ) || ( ! mceEditor && initialMode === 'tinymce' ) ) {
130 var textEditorHeight = $textEditor.height(),
133 $textEditorClone.width( $textEditor.width() - 22 );
134 $textEditorClone.text( $textEditor.val() + ' ' );
136 hiddenHeight = $textEditorClone.height();
138 if ( hiddenHeight < autoresizeMinHeight ) {
139 hiddenHeight = autoresizeMinHeight;
142 if ( hiddenHeight === textEditorHeight ) {
146 $textEditor.height( hiddenHeight );
151 // We need to wait for TinyMCE to initialize.
152 $document.on( 'tinymce-editor-init.editor-expand', function( event, editor ) {
153 var VK = window.tinymce.util.VK,
154 hideFloatPanels = _.debounce( function() {
155 ! $( '.mce-floatpanel:hover' ).length && window.tinymce.ui.FloatPanel.hideAll();
156 $( '.mce-tooltip' ).hide();
159 // Make sure it's the main editor.
160 if ( editor.id !== 'content' ) {
164 // Copy the editor instance.
167 // Set the minimum height to the initial viewport height.
168 editor.settings.autoresize_min_height = autoresizeMinHeight;
170 // Get the necessary UI elements.
171 $visualTop = $contentWrap.find( '.mce-toolbar-grp' );
172 $visualEditor = $contentWrap.find( '.mce-edit-area' );
173 $statusBar = $contentWrap.find( '.mce-statusbar' );
174 $menuBar = $contentWrap.find( '.mce-menubar' );
176 function mceGetCursorOffset() {
177 var node = editor.selection.getNode(),
180 if ( editor.plugins.wpview && ( view = editor.plugins.wpview.getView( node ) ) ) {
181 offset = view.getBoundingClientRect();
183 range = editor.selection.getRng();
186 offset = range.getClientRects()[0];
190 offset = node.getBoundingClientRect();
194 return offset.height ? offset : false;
197 // Make sure the cursor is always visible.
198 // This is not only necessary to keep the cursor between the toolbars,
199 // but also to scroll the window when the cursor moves out of the viewport to a wpview.
200 // Setting a buffer > 0 will prevent the browser default.
201 // Some browsers will scroll to the middle,
202 // others to the top/bottom of the *window* when moving the cursor out of the viewport.
203 function mceKeyup( event ) {
204 var key = event.keyCode;
206 // Bail on special keys.
207 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 ) ) {
209 // OS keys, function keys, num lock, scroll lock
210 } else if ( ( key >= 91 && key <= 93 ) || ( key >= 112 && key <= 123 ) || key === 144 || key === 145 ) {
217 function mceScroll( key ) {
218 var offset = mceGetCursorOffset(),
220 cursorTop, cursorBottom, editorTop, editorBottom;
226 cursorTop = offset.top + editor.iframeElement.getBoundingClientRect().top;
227 cursorBottom = cursorTop + offset.height;
228 cursorTop = cursorTop - buffer;
229 cursorBottom = cursorBottom + buffer;
230 editorTop = heights.adminBarHeight + heights.toolsHeight + heights.menuBarHeight + heights.visualTopHeight;
231 editorBottom = heights.windowHeight - ( advanced ? heights.bottomHeight + heights.statusBarHeight : 0 );
233 // Don't scroll if the node is taller than the visible part of the editor
234 if ( editorBottom - editorTop < offset.height ) {
238 if ( cursorTop < editorTop && ( key === VK.UP || key === VK.LEFT || key === VK.BACKSPACE ) ) {
239 window.scrollTo( window.pageXOffset, cursorTop + window.pageYOffset - editorTop );
240 } else if ( cursorBottom > editorBottom ) {
241 window.scrollTo( window.pageXOffset, cursorBottom + window.pageYOffset - editorBottom );
245 function mceFullscreenToggled( event ) {
246 if ( ! event.state ) {
251 // Adjust when switching editor modes.
253 $window.on( 'scroll.mce-float-panels', hideFloatPanels );
255 setTimeout( function() {
256 editor.execCommand( 'wpAutoResize' );
262 $window.off( 'scroll.mce-float-panels' );
264 setTimeout( function() {
265 var top = $contentWrap.offset().top;
267 if ( window.pageYOffset > top ) {
268 window.scrollTo( window.pageXOffset, top - heights.adminBarHeight );
278 function toggleAdvanced() {
279 advanced = ! advanced;
282 mceBind = function() {
283 editor.on( 'keyup', mceKeyup );
284 editor.on( 'show', mceShow );
285 editor.on( 'hide', mceHide );
286 editor.on( 'wp-toolbar-toggle', toggleAdvanced );
287 // Adjust when the editor resizes.
288 editor.on( 'setcontent wp-autoresize wp-toolbar-toggle', adjust );
289 // Don't hide the caret after undo/redo.
290 editor.on( 'undo redo', mceScroll );
291 // Adjust when exiting TinyMCE's fullscreen mode.
292 editor.on( 'FullscreenStateChanged', mceFullscreenToggled );
294 $window.off( 'scroll.mce-float-panels' ).on( 'scroll.mce-float-panels', hideFloatPanels );
297 mceUnbind = function() {
298 editor.off( 'keyup', mceKeyup );
299 editor.off( 'show', mceShow );
300 editor.off( 'hide', mceHide );
301 editor.off( 'wp-toolbar-toggle', toggleAdvanced );
302 editor.off( 'setcontent wp-autoresize wp-toolbar-toggle', adjust );
303 editor.off( 'undo redo', mceScroll );
304 editor.off( 'FullscreenStateChanged', mceFullscreenToggled );
306 $window.off( 'scroll.mce-float-panels' );
309 if ( $wrap.hasClass( 'wp-editor-expand' ) ) {
310 // Adjust "immediately"
312 initialResize( adjust );
316 // Adjust the toolbars based on the active editor mode.
317 function adjust( event ) {
318 // Make sure we're not in fullscreen mode.
319 if ( fullscreen && fullscreen.settings.visible ) {
323 var windowPos = $window.scrollTop(),
324 type = event && event.type,
325 resize = type !== 'scroll',
326 visual = ( mceEditor && ! mceEditor.isHidden() ),
327 buffer = autoresizeMinHeight,
328 postBodyTop = $postBody.offset().top,
330 contentWrapWidth = $contentWrap.width(),
331 $top, $editor, sidebarTop, footerTop, canPin,
332 topPos, topHeight, editorPos, editorHeight;
334 // Refresh the heights
335 if ( resize || ! heights.windowHeight ) {
339 if ( ! visual && type === 'resize' ) {
345 $editor = $visualEditor;
346 topHeight = heights.visualTopHeight;
349 $editor = $textEditor;
350 topHeight = heights.textTopHeight;
353 topPos = $top.parent().offset().top;
354 editorPos = $editor.offset().top;
355 editorHeight = $editor.outerHeight();
358 canPin = visual ? autoresizeMinHeight + topHeight : autoresizeMinHeight + 20; // 20px from textarea padding
359 canPin = editorHeight > ( canPin + 5 );
364 position: 'absolute',
366 width: contentWrapWidth
369 if ( visual && $menuBar.length ) {
371 position: 'absolute',
373 width: contentWrapWidth - ( borderWidth * 2 )
378 position: 'absolute',
379 top: heights.menuBarHeight,
380 width: contentWrapWidth - ( borderWidth * 2 ) - ( visual ? 0 : ( $top.outerWidth() - $top.width() ) )
383 $statusBar.add( $bottom ).attr( 'style', '' );
386 // Maybe pin the top.
387 if ( ( ! fixedTop || resize ) &&
388 // Handle scrolling down.
389 ( windowPos >= ( topPos - heights.toolsHeight - heights.adminBarHeight ) &&
390 // Handle scrolling up.
391 windowPos <= ( topPos - heights.toolsHeight - heights.adminBarHeight + editorHeight - buffer ) ) ) {
396 top: heights.adminBarHeight,
397 width: contentWrapWidth
400 if ( visual && $menuBar.length ) {
403 top: heights.adminBarHeight + heights.toolsHeight,
404 width: contentWrapWidth - ( borderWidth * 2 ) - ( visual ? 0 : ( $top.outerWidth() - $top.width() ) )
410 top: heights.adminBarHeight + heights.toolsHeight + heights.menuBarHeight,
411 width: contentWrapWidth - ( borderWidth * 2 ) - ( visual ? 0 : ( $top.outerWidth() - $top.width() ) )
413 // Maybe unpin the top.
414 } else if ( fixedTop || resize ) {
415 // Handle scrolling up.
416 if ( windowPos <= ( topPos - heights.toolsHeight - heights.adminBarHeight ) ) {
420 position: 'absolute',
422 width: contentWrapWidth
425 if ( visual && $menuBar.length ) {
427 position: 'absolute',
429 width: contentWrapWidth - ( borderWidth * 2 )
434 position: 'absolute',
435 top: heights.menuBarHeight,
436 width: contentWrapWidth - ( borderWidth * 2 ) - ( visual ? 0 : ( $top.outerWidth() - $top.width() ) )
438 // Handle scrolling down.
439 } else if ( windowPos >= ( topPos - heights.toolsHeight - heights.adminBarHeight + editorHeight - buffer ) ) {
443 position: 'absolute',
444 top: editorHeight - buffer,
445 width: contentWrapWidth
448 if ( visual && $menuBar.length ) {
450 position: 'absolute',
451 top: editorHeight - buffer,
452 width: contentWrapWidth - ( borderWidth * 2 )
457 position: 'absolute',
458 top: editorHeight - buffer + heights.menuBarHeight,
459 width: contentWrapWidth - ( borderWidth * 2 ) - ( visual ? 0 : ( $top.outerWidth() - $top.width() ) )
464 // Maybe adjust the bottom bar.
465 if ( ( ! fixedBottom || ( resize && advanced ) ) &&
466 // +[n] for the border around the .wp-editor-container.
467 ( windowPos + heights.windowHeight ) <= ( editorPos + editorHeight + heights.bottomHeight + heights.statusBarHeight + borderWidth ) ) {
469 if ( event && event.deltaHeight > 0 && event.deltaHeight < 100 ) {
470 window.scrollBy( 0, event.deltaHeight );
471 } else if ( advanced ) {
476 bottom: heights.bottomHeight,
478 width: contentWrapWidth - ( borderWidth * 2 )
484 width: contentWrapWidth
487 } else if ( ( ! advanced && fixedBottom ) ||
488 ( ( fixedBottom || resize ) &&
489 ( windowPos + heights.windowHeight ) > ( editorPos + editorHeight + heights.bottomHeight + heights.statusBarHeight - borderWidth ) ) ) {
492 $statusBar.add( $bottom ).attr( 'style', '' );
495 $statusBar.css( 'visibility', 'hidden' );
501 if ( $postboxContainer.width() < 300 && heights.windowWidth > 600 && // sidebar position is changed with @media from CSS, make sure it is on the side
502 $document.height() > ( $sideSortables.height() + postBodyTop + 120 ) && // the sidebar is not the tallest element
503 heights.windowHeight < editorHeight ) { // the editor is taller than the viewport
505 if ( ( heights.sideSortablesHeight + pinnedToolsTop + sidebarBottom ) > heights.windowHeight || fixedSideTop || fixedSideBottom ) {
506 // Reset when scrolling to the top
507 if ( windowPos + pinnedToolsTop <= postBodyTop ) {
508 $sideSortables.attr( 'style', '' );
509 fixedSideTop = fixedSideBottom = false;
511 if ( windowPos > lastScrollPosition ) {
513 if ( fixedSideTop ) {
515 fixedSideTop = false;
516 sidebarTop = $sideSortables.offset().top - heights.adminBarHeight;
517 footerTop = $footer.offset().top;
519 // don't get over the footer
520 if ( footerTop < sidebarTop + heights.sideSortablesHeight + sidebarBottom ) {
521 sidebarTop = footerTop - heights.sideSortablesHeight - 12;
525 position: 'absolute',
529 } else if ( ! fixedSideBottom && heights.sideSortablesHeight + $sideSortables.offset().top + sidebarBottom < windowPos + heights.windowHeight ) {
531 fixedSideBottom = true;
536 bottom: sidebarBottom
539 } else if ( windowPos < lastScrollPosition ) {
541 if ( fixedSideBottom ) {
543 fixedSideBottom = false;
544 sidebarTop = $sideSortables.offset().top - sidebarBottom;
545 footerTop = $footer.offset().top;
547 // don't get over the footer
548 if ( footerTop < sidebarTop + heights.sideSortablesHeight + sidebarBottom ) {
549 sidebarTop = footerTop - heights.sideSortablesHeight - 12;
553 position: 'absolute',
557 } else if ( ! fixedSideTop && $sideSortables.offset().top >= windowPos + pinnedToolsTop ) {
570 // if the sidebar container is smaller than the viewport, then pin/unpin the top when scrolling
571 if ( windowPos >= ( postBodyTop - pinnedToolsTop ) ) {
573 $sideSortables.css( {
578 $sideSortables.attr( 'style', '' );
581 fixedSideTop = fixedSideBottom = false;
584 lastScrollPosition = windowPos;
586 $sideSortables.attr( 'style', '' );
587 fixedSideTop = fixedSideBottom = false;
592 paddingTop: heights.toolsHeight
597 paddingTop: heights.visualTopHeight + heights.menuBarHeight
601 marginTop: heights.textTopHeight
604 $textEditorClone.width( contentWrapWidth - 20 - ( borderWidth * 2 ) );
609 function fullscreenHide() {
614 function initialResize( callback ) {
615 for ( var i = 1; i < 6; i++ ) {
616 setTimeout( callback, 500 * i );
620 function afterScroll() {
621 clearTimeout( scrollTimer );
622 scrollTimer = setTimeout( adjust, 100 );
626 // Scroll to the top when triggering this from JS.
627 // Ensures toolbars are pinned properly.
628 if ( window.pageYOffset && window.pageYOffset > pageYOffsetAtTop ) {
629 window.scrollTo( window.pageXOffset, 0 );
632 $wrap.addClass( 'wp-editor-expand' );
634 // Adjust when the window is scrolled or resized.
635 $window.on( 'scroll.editor-expand resize.editor-expand', function( event ) {
636 adjust( event.type );
640 // Adjust when collapsing the menu, changing the columns, changing the body class.
641 $document.on( 'wp-collapse-menu.editor-expand postboxes-columnchange.editor-expand editor-classchange.editor-expand', adjust )
642 .on( 'postbox-toggled.editor-expand', function() {
643 if ( ! fixedSideTop && ! fixedSideBottom && window.pageYOffset > pinnedToolsTop ) {
644 fixedSideBottom = true;
645 window.scrollBy( 0, -1 );
647 window.scrollBy( 0, 1 );
651 }).on( 'wp-window-resized.editor-expand', function() {
652 if ( mceEditor && ! mceEditor.isHidden() ) {
653 mceEditor.execCommand( 'wpAutoResize' );
659 $textEditor.on( 'focus.editor-expand input.editor-expand propertychange.editor-expand', textEditorResize );
660 $textEditor.on( 'keyup.editor-expand', textEditorKeyup );
663 // Adjust when entering/exiting fullscreen mode.
664 fullscreen && fullscreen.pubsub.subscribe( 'hidden', fullscreenHide );
667 mceEditor.settings.wp_autoresize_on = true;
668 mceEditor.execCommand( 'wpAutoResizeOn' );
670 if ( ! mceEditor.isHidden() ) {
671 mceEditor.execCommand( 'wpAutoResize' );
675 if ( ! mceEditor || mceEditor.isHidden() ) {
681 $document.trigger( 'editor-expand-on' );
685 var height = window.getUserSetting('ed_size');
687 // Scroll to the top when triggering this from JS.
688 // Ensures toolbars are reset properly.
689 if ( window.pageYOffset && window.pageYOffset > pageYOffsetAtTop ) {
690 window.scrollTo( window.pageXOffset, 0 );
693 $wrap.removeClass( 'wp-editor-expand' );
695 $window.off( '.editor-expand' );
696 $document.off( '.editor-expand' );
697 $textEditor.off( '.editor-expand' );
700 // Adjust when entering/exiting fullscreen mode.
701 fullscreen && fullscreen.pubsub.unsubscribe( 'hidden', fullscreenHide );
704 $.each( [ $visualTop, $textTop, $tools, $menuBar, $bottom, $statusBar, $contentWrap, $visualEditor, $textEditor, $sideSortables ], function( i, element ) {
705 element && element.attr( 'style', '' );
708 fixedTop = fixedBottom = fixedSideTop = fixedSideBottom = false;
711 mceEditor.settings.wp_autoresize_on = false;
712 mceEditor.execCommand( 'wpAutoResizeOff' );
714 if ( ! mceEditor.isHidden() ) {
718 mceEditor.theme.resizeTo( null, height );
724 $textEditor.height( height );
727 $document.trigger( 'editor-expand-off' );
731 if ( $wrap.hasClass( 'wp-editor-expand' ) ) {
734 // Ideally we need to resize just after CSS has fully loaded and QuickTags is ready.
735 if ( $contentWrap.hasClass( 'html-active' ) ) {
736 initialResize( function() {
743 // Show the on/off checkbox
744 $( '#adv-settings .editor-expand' ).show();
745 $( '#editor-expand-toggle' ).on( 'change.editor-expand', function() {
746 if ( $(this).prop( 'checked' ) ) {
748 window.setUserSetting( 'editor_expand', 'on' );
751 window.setUserSetting( 'editor_expand', 'off' );
755 // Expose on() and off()
756 window.editorExpand = {
764 var $body = $( document.body ),
765 $wrap = $( '#wpcontent' ),
766 $editor = $( '#post-body-content' ),
767 $title = $( '#title' ),
768 $content = $( '#content' ),
769 $overlay = $( document.createElement( 'DIV' ) ),
770 $slug = $( '#edit-slug-box' ),
771 $slugFocusEl = $slug.find( 'a' )
772 .add( $slug.find( 'button' ) )
773 .add( $slug.find( 'input' ) ),
774 $menuWrap = $( '#adminmenuwrap' ),
777 _isActive = window.getUserSetting( 'editor_expand', 'on' ) === 'on',
778 _isOn = _isActive ? window.getUserSetting( 'post_dfw' ) === 'on' : false,
782 faded, fadedAdminBar, fadedSlug,
783 editorRect, x, y, mouseY, scrollY,
784 focusLostTimer, overlayTimer, editorHasFocus;
786 $body.append( $overlay );
791 top: $adminBar.height(),
802 $window.on( 'mousemove.focus', function( event ) {
803 mouseY = event.pageY;
806 function recalcEditorRect() {
807 editorRect = $editor.offset();
808 editorRect.right = editorRect.left + $editor.outerWidth();
809 editorRect.bottom = editorRect.top + $editor.outerHeight();
812 function activate() {
816 $document.trigger( 'dfw-activate' );
817 $content.on( 'keydown.focus-shortcut', toggleViaKeyboard );
821 function deactivate() {
827 $document.trigger( 'dfw-deactivate' );
828 $content.off( 'keydown.focus-shortcut' );
832 function isActive() {
837 if ( ! _isOn && _isActive ) {
840 $content.on( 'keydown.focus', fadeOut );
842 $title.add( $content ).on( 'blur.focus', maybeFadeIn );
846 window.setUserSetting( 'post_dfw', 'on' );
848 $document.trigger( 'dfw-on' );
856 $title.add( $content ).off( '.focus' );
860 $editor.off( '.focus' );
862 window.setUserSetting( 'post_dfw', 'off' );
864 $document.trigger( 'dfw-off' );
880 function fadeOut( event ) {
881 var key = event && event.keyCode;
883 // fadeIn and return on Escape and keyboard shortcut Alt+Shift+W.
884 if ( key === 27 || ( key === 87 && event.altKey && event.shiftKey ) ) {
889 if ( event && ( event.metaKey || ( event.ctrlKey && ! event.altKey ) || ( event.altKey && event.shiftKey ) || ( key && (
890 // Special keys ( tab, ctrl, alt, esc, arrow keys... )
891 ( key <= 47 && key !== 8 && key !== 13 && key !== 32 && key !== 46 ) ||
893 ( key >= 91 && key <= 93 ) ||
895 ( key >= 112 && key <= 135 ) ||
896 // Num Lock, Scroll Lock, OEM
897 ( key >= 144 && key <= 150 ) ||
898 // OEM or non-printable
907 clearTimeout( overlayTimer );
909 overlayTimer = setTimeout( function() {
913 $editor.css( 'z-index', 9998 );
916 // Always recalculate the editor area entering the overlay with the mouse.
917 .on( 'mouseenter.focus', function() {
920 $window.on( 'scroll.focus', function() {
921 var nScrollY = window.pageYOffset;
927 mouseY < editorRect.top - buffer ||
928 mouseY > editorRect.bottom + buffer
936 .on( 'mouseleave.focus', function() {
938 traveledX = traveledY = 0;
940 $window.off( 'scroll.focus' );
942 // Fade in when the mouse moves away form the editor area.
943 .on( 'mousemove.focus', function( event ) {
944 var nx = event.clientX,
946 pageYOffset = window.pageYOffset,
947 pageXOffset = window.pageXOffset;
949 if ( x && y && ( nx !== x || ny !== y ) ) {
951 ( ny <= y && ny < editorRect.top - pageYOffset ) ||
952 ( ny >= y && ny > editorRect.bottom - pageYOffset ) ||
953 ( nx <= x && nx < editorRect.left - pageXOffset ) ||
954 ( nx >= x && nx > editorRect.right - pageXOffset )
956 traveledX += Math.abs( x - nx );
957 traveledY += Math.abs( y - ny );
960 ny <= editorRect.top - buffer - pageYOffset ||
961 ny >= editorRect.bottom + buffer - pageYOffset ||
962 nx <= editorRect.left - buffer - pageXOffset ||
963 nx >= editorRect.right + buffer - pageXOffset
971 traveledX = traveledY = 0;
976 traveledX = traveledY = 0;
983 // When the overlay is touched, always fade in and cancel the event.
984 .on( 'touchstart.focus', function( event ) {
985 event.preventDefault();
989 $editor.off( 'mouseenter.focus' );
991 if ( focusLostTimer ) {
992 clearTimeout( focusLostTimer );
993 focusLostTimer = null;
996 $body.addClass( 'focus-on' ).removeClass( 'focus-off' );
1003 function fadeIn( event ) {
1007 clearTimeout( overlayTimer );
1009 overlayTimer = setTimeout( function() {
1013 $editor.css( 'z-index', '' );
1015 $overlay.off( 'mouseenter.focus mouseleave.focus mousemove.focus touchstart.focus' );
1018 * When fading in, temporarily watch for refocus and fade back out - helps
1019 * with 'accidental' editor exits with the mouse. When fading in and the event
1020 * is a key event (Escape or Alt+Shift+W) don't watch for refocus.
1022 if ( 'undefined' === typeof event ) {
1023 $editor.on( 'mouseenter.focus', function() {
1024 if ( $.contains( $editor.get( 0 ), document.activeElement ) || editorHasFocus ) {
1030 focusLostTimer = setTimeout( function() {
1031 focusLostTimer = null;
1032 $editor.off( 'mouseenter.focus' );
1035 $body.addClass( 'focus-off' ).removeClass( 'focus-on' );
1042 function maybeFadeIn() {
1043 setTimeout( function() {
1044 var position = document.activeElement.compareDocumentPosition( $editor.get( 0 ) );
1046 function hasFocus( $el ) {
1047 return $.contains( $el.get( 0 ), document.activeElement );
1050 // The focused node is before or behind the editor area, and not outside the wrap.
1051 if ( ( position === 2 || position === 4 ) && ( hasFocus( $menuWrap ) || hasFocus( $wrap ) || hasFocus( $footer ) ) ) {
1057 function fadeOutAdminBar() {
1058 if ( ! fadedAdminBar && faded ) {
1059 fadedAdminBar = true;
1062 .on( 'mouseenter.focus', function() {
1063 $adminBar.addClass( 'focus-off' );
1065 .on( 'mouseleave.focus', function() {
1066 $adminBar.removeClass( 'focus-off' );
1071 function fadeInAdminBar() {
1072 if ( fadedAdminBar ) {
1073 fadedAdminBar = false;
1075 $adminBar.off( '.focus' );
1079 function fadeOutSlug() {
1080 if ( ! fadedSlug && faded && ! $slug.find( ':focus').length ) {
1083 $slug.stop().fadeTo( 'fast', 0.3 ).on( 'mouseenter.focus', fadeInSlug ).off( 'mouseleave.focus' );
1085 $slugFocusEl.on( 'focus.focus', fadeInSlug ).off( 'blur.focus' );
1089 function fadeInSlug() {
1093 $slug.stop().fadeTo( 'fast', 1 ).on( 'mouseleave.focus', fadeOutSlug ).off( 'mouseenter.focus' );
1095 $slugFocusEl.on( 'blur.focus', fadeOutSlug ).off( 'focus.focus' );
1099 function toggleViaKeyboard( event ) {
1100 if ( event.altKey && event.shiftKey && 87 === event.keyCode ) {
1105 if ( $( '#postdivrich' ).hasClass( 'wp-editor-expand' ) ) {
1106 $content.on( 'keydown.focus-shortcut', toggleViaKeyboard );
1109 $document.on( 'tinymce-editor-setup.focus', function( event, editor ) {
1110 editor.addButton( 'dfw', {
1112 classes: 'wp-dfw btn widget',
1113 disabled: ! _isActive,
1115 onPostRender: function() {
1119 .on( 'dfw-activate.focus', function() {
1120 button.disabled( false );
1122 .on( 'dfw-deactivate.focus', function() {
1123 button.disabled( true );
1125 .on( 'dfw-on.focus', function() {
1126 button.active( true );
1128 .on( 'dfw-off.focus', function() {
1129 button.active( false );
1132 tooltip: 'Distraction-free writing mode',
1133 shortcut: 'Alt+Shift+W'
1136 editor.addCommand( 'wpToggleDFW', toggle );
1137 editor.addShortcut( 'alt+shift+w', '', 'wpToggleDFW' );
1140 $document.on( 'tinymce-editor-init.focus', function( event, editor ) {
1141 var mceBind, mceUnbind;
1144 editorHasFocus = true;
1148 editorHasFocus = false;
1151 if ( editor.id === 'content' ) {
1152 $editorWindow = $( editor.getWin() );
1153 $editorIframe = $( editor.getContentAreaContainer() ).find( 'iframe' );
1155 mceBind = function() {
1156 editor.on( 'keydown', fadeOut );
1157 editor.on( 'blur', maybeFadeIn );
1158 editor.on( 'focus', focus );
1159 editor.on( 'blur', blur );
1160 editor.on( 'wp-autoresize', recalcEditorRect );
1163 mceUnbind = function() {
1164 editor.off( 'keydown', fadeOut );
1165 editor.off( 'blur', maybeFadeIn );
1166 editor.off( 'focus', focus );
1167 editor.off( 'blur', blur );
1168 editor.off( 'wp-autoresize', recalcEditorRect );
1175 $document.on( 'dfw-on.focus', mceBind ).on( 'dfw-off.focus', mceUnbind );
1177 // Make sure the body focuses when clicking outside it.
1178 editor.on( 'click', function( event ) {
1179 if ( event.target === editor.getDoc().documentElement ) {
1186 $document.on( 'quicktags-init', function( event, editor ) {
1189 if ( editor.settings.buttons && ( ',' + editor.settings.buttons + ',' ).indexOf( ',dfw,' ) !== -1 ) {
1190 $button = $( '#' + editor.name + '_dfw' );
1193 .on( 'dfw-activate', function() {
1194 $button.prop( 'disabled', false );
1196 .on( 'dfw-deactivate', function() {
1197 $button.prop( 'disabled', true );
1199 .on( 'dfw-on', function() {
1200 $button.addClass( 'active' );
1202 .on( 'dfw-off', function() {
1203 $button.removeClass( 'active' );
1208 $document.on( 'editor-expand-on.focus', activate ).on( 'editor-expand-off.focus', deactivate );
1211 $content.on( 'keydown.focus', fadeOut );
1213 $title.add( $content ).on( 'blur.focus', maybeFadeIn );
1216 window.wp = window.wp || {};
1217 window.wp.editor = window.wp.editor || {};
1218 window.wp.editor.dfw = {
1220 deactivate: deactivate,
1228 } )( window, window.jQuery );