From 742cecaff828dfe023ab8f3b72cb68d5991d4e36 Mon Sep 17 00:00:00 2001 From: Kai Hao Date: Mon, 25 Oct 2021 19:08:04 +0800 Subject: [PATCH] Auto resize the height of template part focus mode --- .../src/components/block-editor/index.js | 28 ++++-- .../block-editor/resizable-editor.js | 86 +++++++++---------- .../components/block-editor/resize-handle.js | 6 +- .../src/components/block-editor/style.scss | 10 ++- .../src/components/editor/style.scss | 4 +- .../edit-site/src/components/header/index.js | 12 ++- 6 files changed, 88 insertions(+), 58 deletions(-) diff --git a/packages/edit-site/src/components/block-editor/index.js b/packages/edit-site/src/components/block-editor/index.js index 8429e58419f725..6b3f1b5ce24d2e 100644 --- a/packages/edit-site/src/components/block-editor/index.js +++ b/packages/edit-site/src/components/block-editor/index.js @@ -10,6 +10,7 @@ import { useSelect, useDispatch } from '@wordpress/data'; import { useCallback, useRef } from '@wordpress/element'; import { useEntityBlockEditor } from '@wordpress/core-data'; import { + BlockList, BlockEditorProvider, __experimentalLinkControl, BlockInspector, @@ -32,16 +33,26 @@ import EditTemplatePartMenuButton from '../edit-template-part-menu-button'; import BackButton from './back-button'; import ResizableEditor from './resizable-editor'; +const LAYOUT = { + type: 'default', + // At the root level of the site editor, no alignments should be allowed. + alignments: [], +}; + export default function BlockEditor( { setIsInserterOpen } ) { - const { settings, templateType, page } = useSelect( + const { settings, templateType, templateId, page } = useSelect( ( select ) => { - const { getSettings, getEditedPostType, getPage } = select( - editSiteStore - ); + const { + getSettings, + getEditedPostType, + getEditedPostId, + getPage, + } = select( editSiteStore ); return { settings: getSettings( setIsInserterOpen ), templateType: getEditedPostType(), + templateId: getEditedPostId(), page: getPage(), }; }, @@ -99,6 +110,8 @@ export default function BlockEditor( { setIsInserterOpen } ) { + > + + <__unstableBlockSettingsMenuFirstItem> { ( { onClose } ) => ( diff --git a/packages/edit-site/src/components/block-editor/resizable-editor.js b/packages/edit-site/src/components/block-editor/resizable-editor.js index cfd85615917fcb..7b1e44881e0d18 100644 --- a/packages/edit-site/src/components/block-editor/resizable-editor.js +++ b/packages/edit-site/src/components/block-editor/resizable-editor.js @@ -1,21 +1,15 @@ -/** - * External dependencies - */ -import { omit } from 'lodash'; - /** * WordPress dependencies */ import { useState, useEffect, useRef, useCallback } from '@wordpress/element'; import { ResizableBox } from '@wordpress/components'; import { - BlockList, __experimentalUseResizeCanvas as useResizeCanvas, __unstableEditorStyles as EditorStyles, __unstableIframe as Iframe, __unstableUseMouseMoveTypingReset as useMouseMoveTypingReset, } from '@wordpress/block-editor'; -import { useDispatch, useSelect } from '@wordpress/data'; +import { useSelect } from '@wordpress/data'; import { useMergeRefs } from '@wordpress/compose'; /** @@ -24,14 +18,6 @@ import { useMergeRefs } from '@wordpress/compose'; import { store as editSiteStore } from '../../store'; import ResizeHandle from './resize-handle'; -const LAYOUT = { - type: 'default', - // At the root level of the site editor, no alignments should be allowed. - alignments: [], -}; - -const RESPONSIVE_DEVICE = 'responsive'; - const DEFAULT_STYLES = { width: '100%', height: '100%', @@ -56,34 +42,45 @@ function ResizableEditor( { enableResizing, settings, ...props } ) { select( editSiteStore ).__experimentalGetPreviewDeviceType(), [] ); - const resizedCanvasStyles = useResizeCanvas( deviceType ) ?? DEFAULT_STYLES; - const previousResizedCanvasStylesRef = useRef( resizedCanvasStyles ); - // Keep the height of the canvas when resizing on each device type. - const styles = - deviceType === RESPONSIVE_DEVICE - ? previousResizedCanvasStylesRef.current - : resizedCanvasStyles; - const [ width, setWidth ] = useState( styles.width ); - const { - __experimentalSetPreviewDeviceType: setPreviewDeviceType, - } = useDispatch( editSiteStore ); + const deviceStyles = useResizeCanvas( deviceType ); + const [ width, setWidth ] = useState( DEFAULT_STYLES.width ); + const [ height, setHeight ] = useState( DEFAULT_STYLES.height ); const iframeRef = useRef(); const mouseMoveTypingResetRef = useMouseMoveTypingReset(); const ref = useMergeRefs( [ iframeRef, mouseMoveTypingResetRef ] ); useEffect( - function setWidthWhenDeviceTypeChanged() { - if ( deviceType !== RESPONSIVE_DEVICE ) { - setWidth( resizedCanvasStyles.width ); - previousResizedCanvasStylesRef.current = resizedCanvasStyles; + function autoResizeIframeHeight() { + const iframe = iframeRef.current; + + if ( ! iframe || ! enableResizing ) { + return; } + + const resizeObserver = new iframe.contentWindow.ResizeObserver( + () => { + setHeight( + iframe.contentDocument.querySelector( + `.edit-site-block-editor__block-list` + ).offsetHeight + ); + } + ); + + // Observing the rather than the because the latter + // gets destroyed and remounted after initialization in + /> ); } diff --git a/packages/edit-site/src/components/block-editor/resize-handle.js b/packages/edit-site/src/components/block-editor/resize-handle.js index 12fc79396a2fb2..d49bfdf4447b66 100644 --- a/packages/edit-site/src/components/block-editor/resize-handle.js +++ b/packages/edit-site/src/components/block-editor/resize-handle.js @@ -5,6 +5,8 @@ import { __ } from '@wordpress/i18n'; import { LEFT, RIGHT } from '@wordpress/keycodes'; import { VisuallyHidden } from '@wordpress/components'; +const DELTA_DISTANCE = 20; // The distance to resize per keydown in pixels. + export default function ResizeHandle( { direction, resizeWidthBy } ) { function handleKeyDown( event ) { const { keyCode } = event; @@ -13,12 +15,12 @@ export default function ResizeHandle( { direction, resizeWidthBy } ) { ( direction === 'left' && keyCode === LEFT ) || ( direction === 'right' && keyCode === RIGHT ) ) { - resizeWidthBy( 20 ); + resizeWidthBy( DELTA_DISTANCE ); } else if ( ( direction === 'left' && keyCode === RIGHT ) || ( direction === 'right' && keyCode === LEFT ) ) { - resizeWidthBy( -20 ); + resizeWidthBy( -DELTA_DISTANCE ); } } diff --git a/packages/edit-site/src/components/block-editor/style.scss b/packages/edit-site/src/components/block-editor/style.scss index 3125c0424dfbbc..0cebf83a7a7e69 100644 --- a/packages/edit-site/src/components/block-editor/style.scss +++ b/packages/edit-site/src/components/block-editor/style.scss @@ -19,7 +19,11 @@ align-items: center; &.is-focus-mode { - padding: 48px 48px 0; + padding: 48px; + + .edit-site-visual-editor__editor-canvas { + border-radius: 2px; + } } } @@ -40,6 +44,10 @@ } } +.components-resizable-box__container { + margin: 0 auto; +} + .resizable-editor__drag-handle { $height: 100px; position: absolute; diff --git a/packages/edit-site/src/components/editor/style.scss b/packages/edit-site/src/components/editor/style.scss index 1b8283f3d1a159..711da70daede43 100644 --- a/packages/edit-site/src/components/editor/style.scss +++ b/packages/edit-site/src/components/editor/style.scss @@ -23,8 +23,8 @@ .edit-site-visual-editor { position: relative; height: 100%; - display: flex; - flex-flow: column; + display: block; + overflow: hidden; iframe { display: block; diff --git a/packages/edit-site/src/components/header/index.js b/packages/edit-site/src/components/header/index.js index ba782b1baaae02..5845036f5a443a 100644 --- a/packages/edit-site/src/components/header/index.js +++ b/packages/edit-site/src/components/header/index.js @@ -100,6 +100,8 @@ export default function Header( { [ setIsListViewOpened, isListViewOpen ] ); + const isFocusMode = templateType === 'wp_template_part'; + return (
@@ -157,10 +159,12 @@ export default function Header( {
- + { ! isFocusMode && ( + + ) }