From 5ca136fca2b7abc20fca124696ae8269824a8119 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ella=20van=C2=A0Durpe?= Date: Mon, 7 Dec 2020 13:25:28 +0200 Subject: [PATCH 01/17] FSE: load content in iframe --- .github/workflows/performance.yml | 7 + lib/client-assets.php | 37 +++ .../components/block-list/block-popover.js | 4 +- .../block-list/block-selection-button.js | 6 + .../src/components/iframe/index.js | 226 ++++++++++++++++++ packages/block-editor/src/components/index.js | 1 + .../src/components/use-resize-canvas/index.js | 17 +- packages/block-editor/src/style.scss | 2 + packages/components/src/popover/index.js | 107 +++++++-- packages/e2e-test-utils/README.md | 4 + packages/e2e-test-utils/src/canvas.js | 6 + packages/e2e-test-utils/src/index.js | 1 + packages/e2e-test-utils/src/inserter.js | 12 +- .../e2e-test-utils/src/show-block-toolbar.js | 6 +- packages/e2e-tests/jest.performance.config.js | 1 + .../experiments/multi-entity-editing.test.js | 17 +- .../experiments/multi-entity-saving.test.js | 3 +- .../specs/experiments/template-part.test.js | 30 +-- .../specs/performance/site-editor.test.js | 7 +- .../src/components/block-editor/index.js | 53 ++-- .../edit-site/src/components/editor/index.js | 24 +- .../src/components/editor/style.scss | 5 +- 22 files changed, 478 insertions(+), 98 deletions(-) create mode 100644 packages/block-editor/src/components/iframe/index.js create mode 100644 packages/e2e-test-utils/src/canvas.js diff --git a/.github/workflows/performance.yml b/.github/workflows/performance.yml index 1c6be548c7a519..748ad31e2038e0 100644 --- a/.github/workflows/performance.yml +++ b/.github/workflows/performance.yml @@ -38,3 +38,10 @@ jobs: - name: Run the performance tests run: ./bin/plugin/cli.js perf --ci $GITHUB_SHA master --tests-branch $GITHUB_SHA + + - name: Archive debug artifacts (screenshots, HTML snapshots) + uses: actions/upload-artifact@v2 + if: always() + with: + name: failures-artifacts + path: artifacts diff --git a/lib/client-assets.php b/lib/client-assets.php index 47d178667b2ba6..e8aa88229aaedc 100644 --- a/lib/client-assets.php +++ b/lib/client-assets.php @@ -663,3 +663,40 @@ function gutenberg_extend_block_editor_settings_with_fse_theme_flag( $settings ) return $settings; } add_filter( 'block_editor_settings', 'gutenberg_extend_block_editor_settings_with_fse_theme_flag' ); + +/** + * Sets the editor styles to be consumed by JS. + */ +function gutenberg_extend_block_editor_styles_html() { + $handles = array( + 'wp-block-editor', + 'wp-block-library', + 'wp-edit-blocks', + ); + + $block_registry = WP_Block_Type_Registry::get_instance(); + + foreach ( $block_registry->get_all_registered() as $block_name => $block_type ) { + if ( ! empty( $block_type->style ) ) { + $handles[] = $block_type->style; + } + + if ( ! empty( $block_type->editor_style ) ) { + $handles[] = $block_type->editor_style; + } + } + + $handles = array_unique( $handles ); + $done = wp_styles()->done; + + ob_start(); + + wp_styles()->done = array(); + wp_styles()->do_items( $handles ); + wp_styles()->done = $done; + + $editor_styles = wp_json_encode( array( 'html' => ob_get_clean() ) ); + + echo ""; +} +add_action( 'admin_footer-toplevel_page_gutenberg-edit-site', 'gutenberg_extend_block_editor_styles_html' ); diff --git a/packages/block-editor/src/components/block-list/block-popover.js b/packages/block-editor/src/components/block-list/block-popover.js index 5bf1e5a12164df..97367b3735f958 100644 --- a/packages/block-editor/src/components/block-list/block-popover.js +++ b/packages/block-editor/src/components/block-list/block-popover.js @@ -167,7 +167,9 @@ function BlockPopover( { : 'top right left'; const stickyBoundaryElement = showEmptyBlockSideInserter ? undefined - : getScrollContainer( node ) || ownerDocument.body; + : getScrollContainer( ownerDocument.defaultView.frameElement ) || + getScrollContainer( node ) || + ownerDocument.body; return ( { + // Search the document for stylesheets targetting the editor canvas. + Array.from( document.styleSheets ).forEach( ( styleSheet ) => { + try { + // May fail for external styles. + // eslint-disable-next-line no-unused-expressions + styleSheet.cssRules; + } catch ( e ) { + return; + } + + const { ownerNode, cssRules } = styleSheet; + + if ( ! cssRules ) { + return; + } + + const isMatch = Array.from( cssRules ).find( + ( { selectorText } ) => + selectorText && + ( selectorText.includes( `.${ BODY_CLASS_NAME }` ) || + selectorText.includes( `.${ BLOCK_PREFIX }` ) ) + ); + + if ( isMatch && ! doc.getElementById( ownerNode.id ) ) { + doc.head.appendChild( ownerNode.cloneNode( true ) ); + } + } ); + }, [] ); +} + +/** + * Bubbles some event types (keydown, keypress, and dragover) to parent document + * document to ensure that the keyboard shortcuts and drag and drop work. + * + * Ideally, we should remove event bubbling in the future. Keyboard shortcuts + * should be context dependent, e.g. actions on blocks like Cmd+A should not + * work globally outside the block editor. + * + * @param {Document} doc Document to attach listeners to. + */ +function useBubbleEvents( doc ) { + useEffect( () => { + const { defaultView } = doc; + const { frameElement } = defaultView; + + function bubbleEvent( event ) { + const prototype = Object.getPrototypeOf( event ); + const constructorName = prototype.constructor.name; + const Constructor = window[ constructorName ]; + + const init = {}; + + for ( const key in event ) { + init[ key ] = event[ key ]; + } + + if ( event instanceof defaultView.MouseEvent ) { + const rect = frameElement.getBoundingClientRect(); + init.clientX += rect.left; + init.clientY += rect.top; + } + + const newEvent = new Constructor( event.type, init ); + const cancelled = ! frameElement.dispatchEvent( newEvent ); + + if ( cancelled ) { + event.preventDefault(); + } + } + + const eventTypes = [ 'keydown', 'keypress', 'dragover' ]; + + for ( const name of eventTypes ) { + doc.addEventListener( name, bubbleEvent ); + } + + return () => { + for ( const name of eventTypes ) { + doc.removeEventListener( name, bubbleEvent ); + } + }; + }, [] ); +} + +/** + * Sets the document direction. + * + * Sets the `editor-styles-wrapper` class name on the body. + * + * Copies the `admin-color-*` class name to the body so that the admin color + * scheme applies to components in the iframe. + * + * @param {Document} doc Document to add class name to. + */ +function useBodyClassName( doc ) { + useEffect( () => { + doc.dir = document.dir; + doc.body.className = BODY_CLASS_NAME; + + for ( const name of document.body.classList ) { + if ( name.startsWith( 'admin-color-' ) ) { + doc.body.classList.add( name ); + } + } + }, [] ); +} + +/** + * Positions the body element so that the resize listener works correctly. We're + * using an absolute position here because the resize listener doesn't seem to + * report shrinking when the position is relative, causing the iframe not to + * shrink when content is removed. + * + * @see https://github.com/FezVrasta/react-resize-aware#usage + * + * @param {Document} doc Document to set styles to. + */ +function useResizeListenerCompat( doc ) { + useEffect( () => { + // Necessary for the resize listener to work correctly. + doc.body.style.position = 'absolute'; + doc.body.style.right = '0'; + doc.body.style.left = '0'; + }, [] ); +} + +/** + * Sets the document head and default styles. + * + * @param {Document} doc Document to set the head for. + * @param {string} head HTML to set as the head. + */ +function useHead( doc, head ) { + useEffect( () => { + doc.head.innerHTML = + // Body margin must be overridable by themes. + '' + + '' + + head; + }, [] ); +} + +function IframeContent( { doc, head, children } ) { + useHead( doc, head ); + useStyleSheetsCompat( doc ); + useBubbleEvents( doc ); + useBodyClassName( doc ); + useResizeListenerCompat( doc ); + return createPortal( children, doc.body ); +} + +export default function Iframe( { children, head, style = {}, ...props } ) { + const [ resizeListener, sizes ] = useResizeObserver(); + const [ contentDocument, setContentDocument ] = useState(); + const ref = useRef(); + + function setDocumentIfReady( doc ) { + const { readyState } = doc; + + if ( readyState === 'interactive' || readyState === 'complete' ) { + setContentDocument( doc ); + } + } + + useEffect( () => { + setDocumentIfReady( ref.current.contentDocument ); + }, [] ); + + function setRef( newRef ) { + ref.current = newRef; + + if ( newRef ) { + setDocumentIfReady( newRef.contentDocument ); + } + } + + return ( + + ); +} diff --git a/packages/block-editor/src/components/index.js b/packages/block-editor/src/components/index.js index 3e845204bed823..b9378e2c76f320 100644 --- a/packages/block-editor/src/components/index.js +++ b/packages/block-editor/src/components/index.js @@ -118,6 +118,7 @@ export { export { default as Warning } from './warning'; export { default as WritingFlow } from './writing-flow'; export { useCanvasClickRedirect as __unstableUseCanvasClickRedirect } from './use-canvas-click-redirect'; +export { default as __unstableIframe } from './iframe'; /* * State Related Components diff --git a/packages/block-editor/src/components/use-resize-canvas/index.js b/packages/block-editor/src/components/use-resize-canvas/index.js index d5134d634aabd2..d1ac582f900966 100644 --- a/packages/block-editor/src/components/use-resize-canvas/index.js +++ b/packages/block-editor/src/components/use-resize-canvas/index.js @@ -11,11 +11,15 @@ import { default as useSimulatedMediaQuery } from '../../components/use-simulate /** * Function to resize the editor window. * - * @param {string} deviceType Used for determining the size of the container (e.g. Desktop, Tablet, Mobile) + * @param {string} deviceType Used for determining the size of the container (e.g. Desktop, Tablet, Mobile) + * @param {boolean} __unstableDisableSimulate * * @return {Object} Inline styles to be added to resizable container. */ -export default function useResizeCanvas( deviceType ) { +export default function useResizeCanvas( + deviceType, + __unstableDisableSimulate +) { const [ actualWidth, updateActualWidth ] = useState( window.innerWidth ); useEffect( () => { @@ -69,10 +73,11 @@ export default function useResizeCanvas( deviceType ) { } }; - useSimulatedMediaQuery( - 'resizable-editor-section', - getCanvasWidth( deviceType ) - ); + const width = __unstableDisableSimulate + ? null + : getCanvasWidth( deviceType ); + + useSimulatedMediaQuery( 'resizable-editor-section', width ); return contentInlineStyles( deviceType ); } diff --git a/packages/block-editor/src/style.scss b/packages/block-editor/src/style.scss index 1ea21ed4574523..e4201596a11f10 100644 --- a/packages/block-editor/src/style.scss +++ b/packages/block-editor/src/style.scss @@ -64,3 +64,5 @@ @import "./components/block-toolbar/style.scss"; @import "./components/inserter/style.scss"; @import "./components/preview-options/style.scss"; + +@include wordpress-admin-schemes(); diff --git a/packages/components/src/popover/index.js b/packages/components/src/popover/index.js index 40b080f320d610..783a9266f0aa03 100644 --- a/packages/components/src/popover/index.js +++ b/packages/components/src/popover/index.js @@ -42,6 +42,24 @@ const FocusManaged = withConstrainedTabbing( */ const SLOT_NAME = 'Popover'; +function offsetIframe( rect, ownerDocument ) { + const { defaultView } = ownerDocument; + const { frameElement } = defaultView; + + if ( ! frameElement ) { + return rect; + } + + const iframeRect = frameElement.getBoundingClientRect(); + + return new defaultView.DOMRect( + rect.left + iframeRect.left, + rect.top + iframeRect.top, + rect.width, + rect.height + ); +} + function computeAnchorRect( anchorRefFallback, anchorRect, @@ -75,14 +93,20 @@ function computeAnchorRect( // `anchorRef instanceof window.Range` checks will break across document boundaries // such as in an iframe if ( typeof anchorRef?.cloneRange === 'function' ) { - return getRectangleFromRange( anchorRef ); + return offsetIframe( + getRectangleFromRange( anchorRef ), + anchorRef.endContainer.ownerDocument + ); } // Duck-type to check if `anchorRef` is an instance of Element // `anchorRef instanceof window.Element` checks will break across document boundaries // such as in an iframe if ( typeof anchorRef?.getBoundingClientRect === 'function' ) { - const rect = anchorRef.getBoundingClientRect(); + const rect = offsetIframe( + anchorRef.getBoundingClientRect(), + anchorRef.ownerDocument + ); if ( shouldAnchorIncludePadding ) { return rect; @@ -94,11 +118,14 @@ function computeAnchorRect( const { top, bottom } = anchorRef; const topRect = top.getBoundingClientRect(); const bottomRect = bottom.getBoundingClientRect(); - const rect = new window.DOMRect( - topRect.left, - topRect.top, - topRect.width, - bottomRect.bottom - topRect.top + const rect = offsetIframe( + new window.DOMRect( + topRect.left, + topRect.top, + topRect.width, + bottomRect.bottom - topRect.top + ), + top.ownerDocument ); if ( shouldAnchorIncludePadding ) { @@ -199,6 +226,22 @@ function setClass( element, name, toggle ) { } } +function getAnchorDocument( anchor ) { + if ( ! anchor ) { + return; + } + + if ( anchor.endContainer ) { + return anchor.endContainer.ownerDocument; + } + + if ( anchor.top ) { + return anchor.top.ownerDocument; + } + + return anchor.ownerDocument; +} + const Popover = ( { headerTitle, onClose, @@ -363,6 +406,9 @@ const Popover = ( { refresh(); + const { ownerDocument } = containerRef.current; + const { defaultView } = ownerDocument; + /* * There are sometimes we need to reposition or resize the popover that * are not handled by the resize/scroll window events (i.e. CSS changes @@ -370,35 +416,58 @@ const Popover = ( { * * For these situations, we refresh the popover every 0.5s */ - const intervalHandle = window.setInterval( refresh, 500 ); + const intervalHandle = defaultView.setInterval( refresh, 500 ); let rafId; const refreshOnAnimationFrame = () => { - window.cancelAnimationFrame( rafId ); - rafId = window.requestAnimationFrame( refresh ); + defaultView.cancelAnimationFrame( rafId ); + rafId = defaultView.requestAnimationFrame( refresh ); }; // Sometimes a click trigger a layout change that affects the popover // position. This is an opportunity to immediately refresh rather than // at the interval. - window.addEventListener( 'click', refreshOnAnimationFrame ); - window.addEventListener( 'resize', refresh ); - window.addEventListener( 'scroll', refresh, true ); + defaultView.addEventListener( 'click', refreshOnAnimationFrame ); + defaultView.addEventListener( 'resize', refresh ); + defaultView.addEventListener( 'scroll', refresh, true ); + + const anchorDocument = getAnchorDocument( anchorRef ); + + if ( anchorDocument && anchorDocument !== ownerDocument ) { + anchorDocument.defaultView.addEventListener( 'resize', refresh ); + anchorDocument.defaultView.addEventListener( + 'scroll', + refresh, + true + ); + } let observer; if ( __unstableObserveElement ) { - observer = new window.MutationObserver( refresh ); + observer = new defaultView.MutationObserver( refresh ); observer.observe( __unstableObserveElement, { attributes: true } ); } return () => { - window.clearInterval( intervalHandle ); - window.removeEventListener( 'resize', refresh ); - window.removeEventListener( 'scroll', refresh, true ); - window.removeEventListener( 'click', refreshOnAnimationFrame ); - window.cancelAnimationFrame( rafId ); + defaultView.clearInterval( intervalHandle ); + defaultView.removeEventListener( 'resize', refresh ); + defaultView.removeEventListener( 'scroll', refresh, true ); + defaultView.removeEventListener( 'click', refreshOnAnimationFrame ); + defaultView.cancelAnimationFrame( rafId ); + + if ( anchorDocument && anchorDocument !== ownerDocument ) { + anchorDocument.defaultView.removeEventListener( + 'resize', + refresh + ); + anchorDocument.defaultView.removeEventListener( + 'scroll', + refresh, + true + ); + } if ( observer ) { observer.disconnect(); diff --git a/packages/e2e-test-utils/README.md b/packages/e2e-test-utils/README.md index 29e20742248576..6b909bd03b38ec 100644 --- a/packages/e2e-test-utils/README.md +++ b/packages/e2e-test-utils/README.md @@ -40,6 +40,10 @@ _Returns_ - `Promise`: Boolean which represents the state of prepublish checks. +# **canvas** + +Gets the editor canvas frame. + # **changeSiteTimezone** Visits general settings page and changes the timezone to the given value. diff --git a/packages/e2e-test-utils/src/canvas.js b/packages/e2e-test-utils/src/canvas.js new file mode 100644 index 00000000000000..965007d432d0e9 --- /dev/null +++ b/packages/e2e-test-utils/src/canvas.js @@ -0,0 +1,6 @@ +/** + * Gets the editor canvas frame. + */ +export function canvas() { + return page.frames().find( ( f ) => f.name() === 'editor-canvas' ) || page; +} diff --git a/packages/e2e-test-utils/src/index.js b/packages/e2e-test-utils/src/index.js index c70f7fd15f69c7..06899b154c9833 100644 --- a/packages/e2e-test-utils/src/index.js +++ b/packages/e2e-test-utils/src/index.js @@ -2,6 +2,7 @@ export { activatePlugin } from './activate-plugin'; export { activateTheme } from './activate-theme'; export { arePrePublishChecksEnabled } from './are-pre-publish-checks-enabled'; export { changeSiteTimezone } from './change-site-timezone'; +export { canvas } from './canvas'; export { clearLocalStorage } from './clear-local-storage'; export { clickBlockAppender } from './click-block-appender'; export { clickBlockToolbarButton } from './click-block-toolbar-button'; diff --git a/packages/e2e-test-utils/src/inserter.js b/packages/e2e-test-utils/src/inserter.js index c3d31299f3e42b..bb4c0b8cce7e2f 100644 --- a/packages/e2e-test-utils/src/inserter.js +++ b/packages/e2e-test-utils/src/inserter.js @@ -2,6 +2,7 @@ * Internal dependencies */ import { pressKeyWithModifier } from './press-key-with-modifier'; +import { canvas } from './canvas'; // This selector is written to support the current and old inserter markup // because the performance tests need to be able to run across versions. @@ -57,12 +58,11 @@ export async function toggleGlobalBlockInserter() { * Retrieves the document container by css class and checks to make sure the document's active element is within it */ async function waitForInserterCloseAndContentFocus() { - await page.waitForFunction( () => - document.body - .querySelector( - '.interface-interface-skeleton__content .block-editor-block-list__layout' - ) - .contains( document.activeElement ) + await canvas().waitForFunction( + () => + document.activeElement.closest( + '.block-editor-block-list__layout' + ) !== null ); } diff --git a/packages/e2e-test-utils/src/show-block-toolbar.js b/packages/e2e-test-utils/src/show-block-toolbar.js index f1ac720e028d4f..cda28f80d3c66d 100644 --- a/packages/e2e-test-utils/src/show-block-toolbar.js +++ b/packages/e2e-test-utils/src/show-block-toolbar.js @@ -3,7 +3,9 @@ * Call this function to reveal it. */ export async function showBlockToolbar() { + const content = await page.$( '.interface-interface-skeleton__content' ); + const { x, y } = await content.boundingBox(); // Move the mouse to disable the isTyping mode - await page.mouse.move( 50, 50 ); - await page.mouse.move( 100, 100 ); + await page.mouse.move( x + 50, y + 50 ); + await page.mouse.move( x + 100, y + 100 ); } diff --git a/packages/e2e-tests/jest.performance.config.js b/packages/e2e-tests/jest.performance.config.js index a13d06b943c95f..780aab42d51fb0 100644 --- a/packages/e2e-tests/jest.performance.config.js +++ b/packages/e2e-tests/jest.performance.config.js @@ -3,6 +3,7 @@ module.exports = { testMatch: [ '**/performance/*.test.js' ], setupFiles: [ '/config/gutenberg-phase.js' ], setupFilesAfterEnv: [ + '/config/setup-debug-artifacts.js', '/config/setup-performance-test.js', '@wordpress/jest-console', '@wordpress/jest-puppeteer-axe', diff --git a/packages/e2e-tests/specs/experiments/multi-entity-editing.test.js b/packages/e2e-tests/specs/experiments/multi-entity-editing.test.js index 3a8e75b31158c6..914602847f027e 100644 --- a/packages/e2e-tests/specs/experiments/multi-entity-editing.test.js +++ b/packages/e2e-tests/specs/experiments/multi-entity-editing.test.js @@ -8,6 +8,7 @@ import { publishPost, trashAllPosts, activateTheme, + canvas, } from '@wordpress/e2e-test-utils'; import { addQueryArgs } from '@wordpress/url'; @@ -21,7 +22,7 @@ const visitSiteEditor = async () => { page: 'gutenberg-edit-site', } ).slice( 1 ); await visitAdminPage( 'admin.php', query ); - await page.waitForSelector( '.edit-site-visual-editor' ); + await page.waitForSelector( '.edit-site-visual-editor iframe' ); }; const clickTemplateItem = async ( menus, itemName ) => { @@ -50,8 +51,8 @@ const createTemplatePart = async ( await page.keyboard.type( templatePartName ); }; -const editTemplatePart = async ( textToAdd, isNested = false ) => { - await page.click( +const editTemplatePart = async ( textToAdd, isNested = false, p = page ) => { + await p.click( `${ isNested ? '.wp-block-template-part .wp-block-template-part' @@ -146,13 +147,13 @@ describe( 'Multi-entity editor states', () => { await clickTemplateItem( 'Template Parts', 'header' ); // Wait for blocks to load. - await page.waitForSelector( '.wp-block' ); + await canvas().waitForSelector( '.wp-block' ); expect( await isEntityDirty( 'header' ) ).toBe( false ); expect( await isEntityDirty( 'front-page' ) ).toBe( false ); // Switch back and make sure it is still clean. await clickTemplateItem( 'Templates', 'Front Page' ); - await page.waitForSelector( '.wp-block' ); + await canvas().waitForSelector( '.wp-block' ); expect( await isEntityDirty( 'header' ) ).toBe( false ); expect( await isEntityDirty( 'front-page' ) ).toBe( false ); @@ -186,7 +187,7 @@ describe( 'Multi-entity editor states', () => { await visitSiteEditor(); // Wait for site editor to load. - await page.waitForSelector( + await canvas().waitForSelector( '.wp-block-template-part .block-editor-block-list__layout' ); @@ -217,7 +218,7 @@ describe( 'Multi-entity editor states', () => { } ); it( 'should only dirty the child when editing the child', async () => { - await page.click( + await canvas().click( '.wp-block-template-part .wp-block[data-type="core/paragraph"]' ); await page.keyboard.type( 'Some more test words!' ); @@ -228,7 +229,7 @@ describe( 'Multi-entity editor states', () => { } ); it( 'should only dirty the nested entity when editing the nested entity', async () => { - await page.click( + await canvas().click( '.wp-block-template-part .wp-block-template-part .wp-block[data-type="core/paragraph"]' ); await page.keyboard.type( 'Nested test words!' ); diff --git a/packages/e2e-tests/specs/experiments/multi-entity-saving.test.js b/packages/e2e-tests/specs/experiments/multi-entity-saving.test.js index 1cb3f8aaf773bf..4682ec9e7755ce 100644 --- a/packages/e2e-tests/specs/experiments/multi-entity-saving.test.js +++ b/packages/e2e-tests/specs/experiments/multi-entity-saving.test.js @@ -8,6 +8,7 @@ import { visitAdminPage, trashAllPosts, activateTheme, + canvas, } from '@wordpress/e2e-test-utils'; import { addQueryArgs } from '@wordpress/url'; @@ -200,7 +201,7 @@ describe( 'Multi-entity save flow', () => { await navigationPanel.close(); // Click the first block so that the template part inserts in the right place. - const firstBlock = await page.$( '.wp-block' ); + const firstBlock = await canvas().$( '.wp-block' ); await firstBlock.click(); // Insert something to dirty the editor. diff --git a/packages/e2e-tests/specs/experiments/template-part.test.js b/packages/e2e-tests/specs/experiments/template-part.test.js index e531067f277aaa..7a25548ac18e5c 100644 --- a/packages/e2e-tests/specs/experiments/template-part.test.js +++ b/packages/e2e-tests/specs/experiments/template-part.test.js @@ -11,6 +11,7 @@ import { getAllBlocks, selectBlockByClientId, clickBlockToolbarButton, + canvas, } from '@wordpress/e2e-test-utils'; import { addQueryArgs } from '@wordpress/url'; @@ -39,7 +40,7 @@ describe( 'Template Part', () => { page: 'gutenberg-edit-site', } ).slice( 1 ) ); - await page.waitForSelector( '.edit-site-visual-editor' ); + await page.waitForSelector( '.edit-site-visual-editor iframe' ); } ); async function updateHeader( content ) { @@ -89,7 +90,7 @@ describe( 'Template Part', () => { } async function assertParagraphInTemplatePart( content ) { - const paragraphInTemplatePart = await page.waitForXPath( + const paragraphInTemplatePart = await canvas().waitForXPath( `//*[@data-type="core/template-part"][//p[text()="${ content }"]]` ); expect( paragraphInTemplatePart ).not.toBeNull(); @@ -105,7 +106,7 @@ describe( 'Template Part', () => { it( 'Should detach blocks from template part', async () => { await updateHeader( 'Header Template Part 456' ); - const initialTemplateParts = await page.$$( + const initialTemplateParts = await canvas().$$( '.wp-block-template-part' ); @@ -115,16 +116,17 @@ describe( 'Template Part', () => { ( block ) => block.name === 'core/template-part' ); await selectBlockByClientId( headerBlock.clientId ); + + // Detach blocks from template part using ellipsis menu. + await triggerEllipsisMenuItem( 'Detach blocks from template part' ); + // TODO: Remove when toolbar supports text fields expect( console ).toHaveWarnedWith( 'Using custom components as toolbar controls is deprecated. Please use ToolbarItem or ToolbarButton components instead. See: https://developer.wordpress.org/block-editor/components/toolbar-button/#inside-blockcontrols' ); - // Detach blocks from template part using ellipsis menu. - await triggerEllipsisMenuItem( 'Detach blocks from template part' ); - // Verify there is one less template part on the page. - const finalTemplateParts = await page.$$( + const finalTemplateParts = await canvas().$$( '.wp-block-template-part' ); expect( @@ -132,15 +134,15 @@ describe( 'Template Part', () => { ).toBe( 1 ); // Verify content of the template part is still present. - const [ expectedContent ] = await page.$x( + const [ expectedContent ] = await canvas().$x( '//p[contains(text(), "Header Template Part 456")]' ); expect( expectedContent ).not.toBeUndefined(); } ); it( 'Should convert selected block to template part', async () => { - await page.waitForSelector( '.wp-block-template-part' ); - const initialTemplateParts = await page.$$( + await canvas().waitForSelector( '.wp-block-template-part' ); + const initialTemplateParts = await canvas().$$( '.wp-block-template-part' ); @@ -162,7 +164,7 @@ describe( 'Template Part', () => { ); // Verify there is 1 more template part on the page than previously. - const finalTemplateParts = await page.$$( + const finalTemplateParts = await canvas().$$( '.wp-block-template-part' ); expect( @@ -171,8 +173,8 @@ describe( 'Template Part', () => { } ); it( 'Should convert multiple selected blocks to template part', async () => { - await page.waitForSelector( '.wp-block-template-part' ); - const initialTemplateParts = await page.$$( + await canvas().waitForSelector( '.wp-block-template-part' ); + const initialTemplateParts = await canvas().$$( '.wp-block-template-part' ); @@ -206,7 +208,7 @@ describe( 'Template Part', () => { ); // Verify there is 1 more template part on the page than previously. - const finalTemplateParts = await page.$$( + const finalTemplateParts = await canvas().$$( '.wp-block-template-part' ); expect( diff --git a/packages/e2e-tests/specs/performance/site-editor.test.js b/packages/e2e-tests/specs/performance/site-editor.test.js index c0c076eafe16a2..8bd6c25d700bcc 100644 --- a/packages/e2e-tests/specs/performance/site-editor.test.js +++ b/packages/e2e-tests/specs/performance/site-editor.test.js @@ -11,6 +11,7 @@ import { trashAllPosts, visitAdminPage, activateTheme, + canvas, } from '@wordpress/e2e-test-utils'; import { addQueryArgs } from '@wordpress/url'; @@ -49,7 +50,11 @@ describe( 'Site Editor Performance', () => { while ( i-- ) { const startTime = new Date(); await page.reload(); - await page.waitForSelector( '.wp-block', { timeout: 120000 } ); + await page.waitForSelector( '.edit-site-visual-editor iframe', { + timeout: 120000, + } ); + await canvas().waitForSelector( '.wp-block', { timeout: 120000 } ); + results.load.push( new Date() - startTime ); } diff --git a/packages/edit-site/src/components/block-editor/index.js b/packages/edit-site/src/components/block-editor/index.js index 61313977e33361..45e15e23101f1c 100644 --- a/packages/edit-site/src/components/block-editor/index.js +++ b/packages/edit-site/src/components/block-editor/index.js @@ -2,7 +2,7 @@ * WordPress dependencies */ import { useSelect, useDispatch } from '@wordpress/data'; -import { useCallback, useRef } from '@wordpress/element'; +import { useCallback } from '@wordpress/element'; import { useEntityBlockEditor } from '@wordpress/core-data'; import { BlockEditorProvider, @@ -10,10 +10,14 @@ import { __experimentalLinkControl, BlockInspector, WritingFlow, - ObserveTyping, BlockList, + __experimentalUseResizeCanvas as useResizeCanvas, __unstableUseBlockSelectionClearer as useBlockSelectionClearer, + __unstableUseTypingObserver as useTypingObserver, + __unstableUseEditorStyles as useEditorStyles, + __unstableIframe as Iframe, } from '@wordpress/block-editor'; +import { DropZoneProvider } from '@wordpress/components'; /** * Internal dependencies @@ -22,16 +26,34 @@ import TemplatePartConverter from '../template-part-converter'; import NavigateToLink from '../navigate-to-link'; import { SidebarInspectorFill } from '../sidebar'; +function Canvas( { body, styles } ) { + useBlockSelectionClearer( body ); + useTypingObserver( body ); + useEditorStyles( body, styles ); + + return ( + + + + + + ); +} + export default function BlockEditor( { setIsInserterOpen } ) { - const { settings, templateType, page } = useSelect( + const { settings, templateType, page, deviceType } = useSelect( ( select ) => { - const { getSettings, getTemplateType, getPage } = select( - 'core/edit-site' - ); + const { + getSettings, + getTemplateType, + getPage, + __experimentalGetPreviewDeviceType, + } = select( 'core/edit-site' ); return { settings: getSettings( setIsInserterOpen ), templateType: getTemplateType(), page: getPage(), + deviceType: __experimentalGetPreviewDeviceType(), }; }, [ setIsInserterOpen ] @@ -41,9 +63,8 @@ export default function BlockEditor( { setIsInserterOpen } ) { templateType ); const { setPage } = useDispatch( 'core/edit-site' ); - const ref = useRef(); - useBlockSelectionClearer( ref ); + const resizedCanvasStyles = useResizeCanvas( deviceType, true ); return ( -
- - - - - -
+ { ( body ) => ( + + ) } +
); } diff --git a/packages/edit-site/src/components/editor/index.js b/packages/edit-site/src/components/editor/index.js index 380a1f3d1dae99..0bee13e82359c7 100644 --- a/packages/edit-site/src/components/editor/index.js +++ b/packages/edit-site/src/components/editor/index.js @@ -1,13 +1,7 @@ /** * WordPress dependencies */ -import { - useEffect, - useState, - useMemo, - useCallback, - useRef, -} from '@wordpress/element'; +import { useEffect, useState, useMemo, useCallback } from '@wordpress/element'; import { useSelect, useDispatch } from '@wordpress/data'; import { SlotFillProvider, @@ -19,8 +13,6 @@ import { EntityProvider } from '@wordpress/core-data'; import { BlockContextProvider, BlockBreadcrumb, - __unstableUseEditorStyles as useEditorStyles, - __experimentalUseResizeCanvas as useResizeCanvas, __experimentalLibrary as Library, } from '@wordpress/block-editor'; import { @@ -56,7 +48,6 @@ function Editor() { const { isFullscreenActive, isInserterOpen, - deviceType, sidebarIsOpened, settings, entityId, @@ -68,7 +59,6 @@ function Editor() { const { isFeatureActive, isInserterOpened, - __experimentalGetPreviewDeviceType, getSettings, getTemplateId, getTemplatePartId, @@ -90,7 +80,6 @@ function Editor() { return { isInserterOpen: isInserterOpened(), isFullscreenActive: isFeatureActive( 'fullscreenMode' ), - deviceType: __experimentalGetPreviewDeviceType(), sidebarIsOpened: !! select( 'core/interface' ).getActiveComplementaryArea( 'core/edit-site' ), @@ -121,8 +110,6 @@ function Editor() { updateEditorSettings( { defaultTemplateTypes } ); }, [ defaultTemplateTypes ] ); - const inlineStyles = useResizeCanvas( deviceType ); - const [ isEntitiesSavedStatesOpen, setIsEntitiesSavedStatesOpen, @@ -169,9 +156,6 @@ function Editor() { }, [ isNavigationOpen ] ); const isMobile = useViewportMatch( 'medium', '<' ); - const ref = useRef(); - - useEditorStyles( ref, settings.styles ); return ( <> @@ -202,7 +186,6 @@ function Editor() { } secondarySidebar={ @@ -259,10 +242,7 @@ function Editor() { /> } content={ -
+
{ template && ( diff --git a/packages/edit-site/src/components/editor/style.scss b/packages/edit-site/src/components/editor/style.scss index 52960903d41431..9c3dd1e0a0d5f9 100644 --- a/packages/edit-site/src/components/editor/style.scss +++ b/packages/edit-site/src/components/editor/style.scss @@ -22,7 +22,10 @@ .edit-site-visual-editor { position: relative; - background-color: $white; + + iframe { + background-color: $white; + } } // Ideally we don't need all these nested divs. From f3291f401365dfe345e0c7bdf92161211ff18565 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ella=20van=C2=A0Durpe?= Date: Thu, 10 Dec 2020 13:28:20 +0200 Subject: [PATCH 02/17] Performance fix attempt --- .../specs/performance/site-editor.test.js | 2 +- .../src/components/block-editor/index.js | 21 +++++++++++-------- .../edit-site/src/components/editor/index.js | 5 ++--- 3 files changed, 15 insertions(+), 13 deletions(-) diff --git a/packages/e2e-tests/specs/performance/site-editor.test.js b/packages/e2e-tests/specs/performance/site-editor.test.js index 8bd6c25d700bcc..b9814bc73e1012 100644 --- a/packages/e2e-tests/specs/performance/site-editor.test.js +++ b/packages/e2e-tests/specs/performance/site-editor.test.js @@ -50,7 +50,7 @@ describe( 'Site Editor Performance', () => { while ( i-- ) { const startTime = new Date(); await page.reload(); - await page.waitForSelector( '.edit-site-visual-editor iframe', { + await page.waitForSelector( '.edit-site-visual-editor', { timeout: 120000, } ); await canvas().waitForSelector( '.wp-block', { timeout: 120000 } ); diff --git a/packages/edit-site/src/components/block-editor/index.js b/packages/edit-site/src/components/block-editor/index.js index 45e15e23101f1c..23ffe2e0676611 100644 --- a/packages/edit-site/src/components/block-editor/index.js +++ b/packages/edit-site/src/components/block-editor/index.js @@ -17,7 +17,7 @@ import { __unstableUseEditorStyles as useEditorStyles, __unstableIframe as Iframe, } from '@wordpress/block-editor'; -import { DropZoneProvider } from '@wordpress/components'; +import { DropZoneProvider, Popover } from '@wordpress/components'; /** * Internal dependencies @@ -91,14 +91,17 @@ export default function BlockEditor( { setIsInserterOpen } ) { - +
+ + +
); } diff --git a/packages/edit-site/src/components/editor/index.js b/packages/edit-site/src/components/editor/index.js index 0bee13e82359c7..14ba7b558d5da0 100644 --- a/packages/edit-site/src/components/editor/index.js +++ b/packages/edit-site/src/components/editor/index.js @@ -242,9 +242,8 @@ function Editor() { /> } content={ -
+ <> - { template && ( ) } -
+ } actions={ <> From 1423753bb540a206903c3a44655f46fd1925e0d4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ella=20van=C2=A0Durpe?= Date: Thu, 10 Dec 2020 14:14:21 +0200 Subject: [PATCH 03/17] Remove perf artifacts --- .github/workflows/performance.yml | 7 ------- packages/e2e-tests/jest.performance.config.js | 1 - 2 files changed, 8 deletions(-) diff --git a/.github/workflows/performance.yml b/.github/workflows/performance.yml index 748ad31e2038e0..1c6be548c7a519 100644 --- a/.github/workflows/performance.yml +++ b/.github/workflows/performance.yml @@ -38,10 +38,3 @@ jobs: - name: Run the performance tests run: ./bin/plugin/cli.js perf --ci $GITHUB_SHA master --tests-branch $GITHUB_SHA - - - name: Archive debug artifacts (screenshots, HTML snapshots) - uses: actions/upload-artifact@v2 - if: always() - with: - name: failures-artifacts - path: artifacts diff --git a/packages/e2e-tests/jest.performance.config.js b/packages/e2e-tests/jest.performance.config.js index 780aab42d51fb0..a13d06b943c95f 100644 --- a/packages/e2e-tests/jest.performance.config.js +++ b/packages/e2e-tests/jest.performance.config.js @@ -3,7 +3,6 @@ module.exports = { testMatch: [ '**/performance/*.test.js' ], setupFiles: [ '/config/gutenberg-phase.js' ], setupFilesAfterEnv: [ - '/config/setup-debug-artifacts.js', '/config/setup-performance-test.js', '@wordpress/jest-console', '@wordpress/jest-puppeteer-axe', From adee506b6edea305e43909723551007742d3975a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ella=20van=C2=A0Durpe?= Date: Thu, 10 Dec 2020 17:08:33 +0200 Subject: [PATCH 04/17] Simplify --- .../src/components/iframe/index.js | 231 +++++++----------- .../src/components/block-editor/index.js | 16 +- .../src/components/editor/style.scss | 7 + 3 files changed, 111 insertions(+), 143 deletions(-) diff --git a/packages/block-editor/src/components/iframe/index.js b/packages/block-editor/src/components/iframe/index.js index 0677c594956ba0..d647e46673e062 100644 --- a/packages/block-editor/src/components/iframe/index.js +++ b/packages/block-editor/src/components/iframe/index.js @@ -1,9 +1,8 @@ /** * WordPress dependencies */ -import { useState, useEffect, createPortal, useRef } from '@wordpress/element'; +import { useState, createPortal } from '@wordpress/element'; import { __ } from '@wordpress/i18n'; -import { useResizeObserver } from '@wordpress/compose'; const BODY_CLASS_NAME = 'editor-styles-wrapper'; const BLOCK_PREFIX = 'wp-block'; @@ -18,36 +17,34 @@ const BLOCK_PREFIX = 'wp-block'; * * @param {Document} doc The document to append cloned stylesheets to. */ -function useStyleSheetsCompat( doc ) { - useEffect( () => { - // Search the document for stylesheets targetting the editor canvas. - Array.from( document.styleSheets ).forEach( ( styleSheet ) => { - try { - // May fail for external styles. - // eslint-disable-next-line no-unused-expressions - styleSheet.cssRules; - } catch ( e ) { - return; - } +function styleSheetsCompat( doc ) { + // Search the document for stylesheets targetting the editor canvas. + Array.from( document.styleSheets ).forEach( ( styleSheet ) => { + try { + // May fail for external styles. + // eslint-disable-next-line no-unused-expressions + styleSheet.cssRules; + } catch ( e ) { + return; + } - const { ownerNode, cssRules } = styleSheet; + const { ownerNode, cssRules } = styleSheet; - if ( ! cssRules ) { - return; - } + if ( ! cssRules ) { + return; + } - const isMatch = Array.from( cssRules ).find( - ( { selectorText } ) => - selectorText && - ( selectorText.includes( `.${ BODY_CLASS_NAME }` ) || - selectorText.includes( `.${ BLOCK_PREFIX }` ) ) - ); + const isMatch = Array.from( cssRules ).find( + ( { selectorText } ) => + selectorText && + ( selectorText.includes( `.${ BODY_CLASS_NAME }` ) || + selectorText.includes( `.${ BLOCK_PREFIX }` ) ) + ); - if ( isMatch && ! doc.getElementById( ownerNode.id ) ) { - doc.head.appendChild( ownerNode.cloneNode( true ) ); - } - } ); - }, [] ); + if ( isMatch && ! doc.getElementById( ownerNode.id ) ) { + doc.head.appendChild( ownerNode.cloneNode( true ) ); + } + } ); } /** @@ -60,48 +57,40 @@ function useStyleSheetsCompat( doc ) { * * @param {Document} doc Document to attach listeners to. */ -function useBubbleEvents( doc ) { - useEffect( () => { - const { defaultView } = doc; - const { frameElement } = defaultView; - - function bubbleEvent( event ) { - const prototype = Object.getPrototypeOf( event ); - const constructorName = prototype.constructor.name; - const Constructor = window[ constructorName ]; +function bubbleEvents( doc ) { + const { defaultView } = doc; + const { frameElement } = defaultView; - const init = {}; + function bubbleEvent( event ) { + const prototype = Object.getPrototypeOf( event ); + const constructorName = prototype.constructor.name; + const Constructor = window[ constructorName ]; - for ( const key in event ) { - init[ key ] = event[ key ]; - } + const init = {}; - if ( event instanceof defaultView.MouseEvent ) { - const rect = frameElement.getBoundingClientRect(); - init.clientX += rect.left; - init.clientY += rect.top; - } - - const newEvent = new Constructor( event.type, init ); - const cancelled = ! frameElement.dispatchEvent( newEvent ); + for ( const key in event ) { + init[ key ] = event[ key ]; + } - if ( cancelled ) { - event.preventDefault(); - } + if ( event instanceof defaultView.MouseEvent ) { + const rect = frameElement.getBoundingClientRect(); + init.clientX += rect.left; + init.clientY += rect.top; } - const eventTypes = [ 'keydown', 'keypress', 'dragover' ]; + const newEvent = new Constructor( event.type, init ); + const cancelled = ! frameElement.dispatchEvent( newEvent ); - for ( const name of eventTypes ) { - doc.addEventListener( name, bubbleEvent ); + if ( cancelled ) { + event.preventDefault(); } + } - return () => { - for ( const name of eventTypes ) { - doc.removeEventListener( name, bubbleEvent ); - } - }; - }, [] ); + const eventTypes = [ 'keydown', 'keypress', 'dragover' ]; + + for ( const name of eventTypes ) { + doc.addEventListener( name, bubbleEvent ); + } } /** @@ -114,36 +103,15 @@ function useBubbleEvents( doc ) { * * @param {Document} doc Document to add class name to. */ -function useBodyClassName( doc ) { - useEffect( () => { - doc.dir = document.dir; - doc.body.className = BODY_CLASS_NAME; - - for ( const name of document.body.classList ) { - if ( name.startsWith( 'admin-color-' ) ) { - doc.body.classList.add( name ); - } - } - }, [] ); -} +function setBodyClassName( doc ) { + doc.dir = document.dir; + doc.body.className = BODY_CLASS_NAME; -/** - * Positions the body element so that the resize listener works correctly. We're - * using an absolute position here because the resize listener doesn't seem to - * report shrinking when the position is relative, causing the iframe not to - * shrink when content is removed. - * - * @see https://github.com/FezVrasta/react-resize-aware#usage - * - * @param {Document} doc Document to set styles to. - */ -function useResizeListenerCompat( doc ) { - useEffect( () => { - // Necessary for the resize listener to work correctly. - doc.body.style.position = 'absolute'; - doc.body.style.right = '0'; - doc.body.style.left = '0'; - }, [] ); + for ( const name of document.body.classList ) { + if ( name.startsWith( 'admin-color-' ) ) { + doc.body.classList.add( name ); + } + } } /** @@ -152,48 +120,45 @@ function useResizeListenerCompat( doc ) { * @param {Document} doc Document to set the head for. * @param {string} head HTML to set as the head. */ -function useHead( doc, head ) { - useEffect( () => { - doc.head.innerHTML = - // Body margin must be overridable by themes. - '' + - '' + - head; - }, [] ); +function setHead( doc, head ) { + doc.head.innerHTML = + // Body margin must be overridable by themes. + '' + head; } -function IframeContent( { doc, head, children } ) { - useHead( doc, head ); - useStyleSheetsCompat( doc ); - useBubbleEvents( doc ); - useBodyClassName( doc ); - useResizeListenerCompat( doc ); - return createPortal( children, doc.body ); -} - -export default function Iframe( { children, head, style = {}, ...props } ) { - const [ resizeListener, sizes ] = useResizeObserver(); - const [ contentDocument, setContentDocument ] = useState(); - const ref = useRef(); - - function setDocumentIfReady( doc ) { - const { readyState } = doc; - - if ( readyState === 'interactive' || readyState === 'complete' ) { - setContentDocument( doc ); +export default function Iframe( { + contentRef, + children, + head, + style = {}, + ...props +} ) { + const [ iframeDocument, setIframeDocument ] = useState(); + + function setRef( node ) { + if ( ! node ) { + return; } - } - useEffect( () => { - setDocumentIfReady( ref.current.contentDocument ); - }, [] ); + function setDocumentIfReady() { + const { contentDocument } = node; + const { readyState } = contentDocument; + + if ( readyState === 'interactive' || readyState === 'complete' ) { + setIframeDocument( contentDocument ); + setHead( contentDocument, head ); + setBodyClassName( contentDocument ); + styleSheetsCompat( contentDocument ); + bubbleEvents( contentDocument ); + setBodyClassName( contentDocument ); + contentRef.current = contentDocument.body; + } + } - function setRef( newRef ) { - ref.current = newRef; + setDocumentIfReady(); - if ( newRef ) { - setDocumentIfReady( newRef.contentDocument ); - } + // Document is not immediately loaded in Firefox. + node.addEventListener( 'load', setDocumentIfReady ); } return ( @@ -202,25 +167,15 @@ export default function Iframe( { children, head, style = {}, ...props } ) { style={ { display: 'block', width: '100%', - height: sizes.height + 'px', - minHeight: '100%', + height: '100%', ...style, } } ref={ setRef } tabIndex="0" title={ __( 'Editor canvas' ) } name="editor-canvas" - onLoad={ () => { - // Document is not immediately loaded in Firefox. - setDocumentIfReady( ref.current.contentDocument ); - } } > - { contentDocument && ( - - { children( { current: contentDocument.body } ) } - { resizeListener } - - ) } + { iframeDocument && createPortal( children, iframeDocument.body ) } ); } diff --git a/packages/edit-site/src/components/block-editor/index.js b/packages/edit-site/src/components/block-editor/index.js index 23ffe2e0676611..47f6b1474d7879 100644 --- a/packages/edit-site/src/components/block-editor/index.js +++ b/packages/edit-site/src/components/block-editor/index.js @@ -2,7 +2,7 @@ * WordPress dependencies */ import { useSelect, useDispatch } from '@wordpress/data'; -import { useCallback } from '@wordpress/element'; +import { useCallback, useRef } from '@wordpress/element'; import { useEntityBlockEditor } from '@wordpress/core-data'; import { BlockEditorProvider, @@ -65,6 +65,13 @@ export default function BlockEditor( { setIsInserterOpen } ) { const { setPage } = useDispatch( 'core/edit-site' ); const resizedCanvasStyles = useResizeCanvas( deviceType, true ); + const contentRef = useRef(); + + // Allow scrolling "through" popovers over the canvas. This is only called + // for as long as the pointer is over a popover. + function onWheel( { deltaX, deltaY } ) { + contentRef.current.scrollBy( deltaX, deltaY ); + } return ( -
+
diff --git a/packages/edit-site/src/components/editor/style.scss b/packages/edit-site/src/components/editor/style.scss index 9c3dd1e0a0d5f9..d6e9824d17d104 100644 --- a/packages/edit-site/src/components/editor/style.scss +++ b/packages/edit-site/src/components/editor/style.scss @@ -20,8 +20,15 @@ } } +.interface-interface-skeleton__content { + // The iframe is scrollable, so no need for the content area to be + // scrollable too. + overflow: visible; +} + .edit-site-visual-editor { position: relative; + height: 100%; iframe { background-color: $white; From 5041b14973d2c21506d063571e4072ff6f7aca37 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ella=20van=C2=A0Durpe?= Date: Fri, 11 Dec 2020 13:42:46 +0200 Subject: [PATCH 05/17] Fix some issues --- .../src/components/iframe/index.js | 20 ++++--------------- .../experiments/multi-entity-editing.test.js | 10 ++++++++++ .../src/components/editor/style.scss | 9 +++------ 3 files changed, 17 insertions(+), 22 deletions(-) diff --git a/packages/block-editor/src/components/iframe/index.js b/packages/block-editor/src/components/iframe/index.js index d647e46673e062..315dd813e1e2cd 100644 --- a/packages/block-editor/src/components/iframe/index.js +++ b/packages/block-editor/src/components/iframe/index.js @@ -1,7 +1,7 @@ /** * WordPress dependencies */ -import { useState, createPortal } from '@wordpress/element'; +import { useState, createPortal, useCallback } from '@wordpress/element'; import { __ } from '@wordpress/i18n'; const BODY_CLASS_NAME = 'editor-styles-wrapper'; @@ -126,16 +126,10 @@ function setHead( doc, head ) { '' + head; } -export default function Iframe( { - contentRef, - children, - head, - style = {}, - ...props -} ) { +export default function Iframe( { contentRef, children, head, ...props } ) { const [ iframeDocument, setIframeDocument ] = useState(); - function setRef( node ) { + const setRef = useCallback( ( node ) => { if ( ! node ) { return; } @@ -159,17 +153,11 @@ export default function Iframe( { // Document is not immediately loaded in Firefox. node.addEventListener( 'load', setDocumentIfReady ); - } + }, [] ); return ( ); } + +export default forwardRef( Iframe ); diff --git a/packages/block-editor/src/components/index.js b/packages/block-editor/src/components/index.js index b9378e2c76f320..0949671bc525ba 100644 --- a/packages/block-editor/src/components/index.js +++ b/packages/block-editor/src/components/index.js @@ -108,6 +108,7 @@ export { default as NavigableToolbar } from './navigable-toolbar'; export { default as ObserveTyping, useTypingObserver as __unstableUseTypingObserver, + useMouseMoveTypingReset as __unstableUseMouseMoveTypingReset, } from './observe-typing'; export { default as PreserveScrollInReorder } from './preserve-scroll-in-reorder'; export { default as SkipToSelectedBlock } from './skip-to-selected-block'; diff --git a/packages/block-editor/src/components/observe-typing/index.js b/packages/block-editor/src/components/observe-typing/index.js index e33628c92b374a..abd680d77409ef 100644 --- a/packages/block-editor/src/components/observe-typing/index.js +++ b/packages/block-editor/src/components/observe-typing/index.js @@ -43,12 +43,62 @@ function isKeyDownEligibleForStartTyping( event ) { return ! shiftKey && KEY_DOWN_ELIGIBLE_KEY_CODES.has( keyCode ); } +export function useMouseMoveTypingReset( ref ) { + const isTyping = useSelect( ( select ) => + select( 'core/block-editor' ).isTyping() + ); + const { stopTyping } = useDispatch( 'core/block-editor' ); + + useEffect( () => { + if ( ! isTyping ) { + return; + } + + const element = ref.current; + const { ownerDocument } = element; + let lastClientX; + let lastClientY; + + /** + * On mouse move, unset typing flag if user has moved cursor. + * + * @param {MouseEvent} event Mousemove event. + */ + function stopTypingOnMouseMove( event ) { + const { clientX, clientY } = event; + + // We need to check that the mouse really moved because Safari + // triggers mousemove events when shift or ctrl are pressed. + if ( + lastClientX && + lastClientY && + ( lastClientX !== clientX || lastClientY !== clientY ) + ) { + stopTyping(); + } + + lastClientX = clientX; + lastClientY = clientY; + } + + ownerDocument.addEventListener( 'mousemove', stopTypingOnMouseMove ); + + return () => { + ownerDocument.removeEventListener( + 'mousemove', + stopTypingOnMouseMove + ); + }; + }, [ isTyping, stopTyping ] ); +} + export function useTypingObserver( ref ) { const isTyping = useSelect( ( select ) => select( 'core/block-editor' ).isTyping() ); const { startTyping, stopTyping } = useDispatch( 'core/block-editor' ); + useMouseMoveTypingReset( ref ); useEffect( () => { const element = ref.current; const { ownerDocument } = element; @@ -108,41 +158,12 @@ export function useTypingObserver( ref ) { } } - let lastClientX; - let lastClientY; - - /** - * On mouse move, unset typing flag if user has moved cursor. - * - * @param {MouseEvent} event Mousemove event. - */ - function stopTypingOnMouseMove( event ) { - const { clientX, clientY } = event; - - // We need to check that the mouse really moved because Safari - // triggers mousemove events when shift or ctrl are pressed. - if ( - lastClientX && - lastClientY && - ( lastClientX !== clientX || lastClientY !== clientY ) - ) { - stopTyping(); - } - - lastClientX = clientX; - lastClientY = clientY; - } - element.addEventListener( 'focus', stopTypingOnNonTextField ); element.addEventListener( 'keydown', stopTypingOnEscapeKey ); ownerDocument.addEventListener( 'selectionchange', stopTypingOnSelectionUncollapse ); - ownerDocument.addEventListener( - 'mousemove', - stopTypingOnMouseMove - ); return () => { defaultView.clearTimeout( timerId ); @@ -155,10 +176,6 @@ export function useTypingObserver( ref ) { 'selectionchange', stopTypingOnSelectionUncollapse ); - ownerDocument.removeEventListener( - 'mousemove', - stopTypingOnMouseMove - ); }; } diff --git a/packages/block-editor/src/components/use-resize-canvas/index.js b/packages/block-editor/src/components/use-resize-canvas/index.js index d1ac582f900966..bd55ab5d6b3b21 100644 --- a/packages/block-editor/src/components/use-resize-canvas/index.js +++ b/packages/block-editor/src/components/use-resize-canvas/index.js @@ -11,14 +11,14 @@ import { default as useSimulatedMediaQuery } from '../../components/use-simulate /** * Function to resize the editor window. * - * @param {string} deviceType Used for determining the size of the container (e.g. Desktop, Tablet, Mobile) - * @param {boolean} __unstableDisableSimulate + * @param {string} deviceType Used for determining the size of the container (e.g. Desktop, Tablet, Mobile) + * @param {boolean} __unstableDisableSimulation Whether to disable media query simulation. * * @return {Object} Inline styles to be added to resizable container. */ export default function useResizeCanvas( deviceType, - __unstableDisableSimulate + __unstableDisableSimulation ) { const [ actualWidth, updateActualWidth ] = useState( window.innerWidth ); @@ -73,7 +73,7 @@ export default function useResizeCanvas( } }; - const width = __unstableDisableSimulate + const width = __unstableDisableSimulation ? null : getCanvasWidth( deviceType ); diff --git a/packages/components/src/popover/index.js b/packages/components/src/popover/index.js index 783a9266f0aa03..539b8980fe2ff0 100644 --- a/packages/components/src/popover/index.js +++ b/packages/components/src/popover/index.js @@ -434,6 +434,8 @@ const Popover = ( { const anchorDocument = getAnchorDocument( anchorRef ); + // If the anchor is within an iframe, the popover position also needs + // to refrest when the iframe content is scrolled or resized. if ( anchorDocument && anchorDocument !== ownerDocument ) { anchorDocument.defaultView.addEventListener( 'resize', refresh ); anchorDocument.defaultView.addEventListener( diff --git a/packages/e2e-test-utils/src/show-block-toolbar.js b/packages/e2e-test-utils/src/show-block-toolbar.js index cda28f80d3c66d..f1ac720e028d4f 100644 --- a/packages/e2e-test-utils/src/show-block-toolbar.js +++ b/packages/e2e-test-utils/src/show-block-toolbar.js @@ -3,9 +3,7 @@ * Call this function to reveal it. */ export async function showBlockToolbar() { - const content = await page.$( '.interface-interface-skeleton__content' ); - const { x, y } = await content.boundingBox(); // Move the mouse to disable the isTyping mode - await page.mouse.move( x + 50, y + 50 ); - await page.mouse.move( x + 100, y + 100 ); + await page.mouse.move( 50, 50 ); + await page.mouse.move( 100, 100 ); } diff --git a/packages/e2e-tests/specs/experiments/multi-entity-editing.test.js b/packages/e2e-tests/specs/experiments/multi-entity-editing.test.js index b090ede4bd9ebd..f2df4b95c7bd44 100644 --- a/packages/e2e-tests/specs/experiments/multi-entity-editing.test.js +++ b/packages/e2e-tests/specs/experiments/multi-entity-editing.test.js @@ -51,8 +51,8 @@ const createTemplatePart = async ( await page.keyboard.type( templatePartName ); }; -const editTemplatePart = async ( textToAdd, isNested = false, p = page ) => { - await p.click( +const editTemplatePart = async ( textToAdd, isNested = false ) => { + await page.click( `${ isNested ? '.wp-block-template-part .wp-block-template-part' diff --git a/packages/edit-site/src/components/block-editor/index.js b/packages/edit-site/src/components/block-editor/index.js index 47f6b1474d7879..cbcbc092648c0f 100644 --- a/packages/edit-site/src/components/block-editor/index.js +++ b/packages/edit-site/src/components/block-editor/index.js @@ -14,6 +14,7 @@ import { __experimentalUseResizeCanvas as useResizeCanvas, __unstableUseBlockSelectionClearer as useBlockSelectionClearer, __unstableUseTypingObserver as useTypingObserver, + __unstableUseMouseMoveTypingReset as useMouseMoveTypingReset, __unstableUseEditorStyles as useEditorStyles, __unstableIframe as Iframe, } from '@wordpress/block-editor'; @@ -65,8 +66,11 @@ export default function BlockEditor( { setIsInserterOpen } ) { const { setPage } = useDispatch( 'core/edit-site' ); const resizedCanvasStyles = useResizeCanvas( deviceType, true ); + const ref = useRef(); const contentRef = useRef(); + useMouseMoveTypingReset( ref ); + // Allow scrolling "through" popovers over the canvas. This is only called // for as long as the pointer is over a popover. function onWheel( { deltaX, deltaY } ) { @@ -103,6 +107,7 @@ export default function BlockEditor( { setIsInserterOpen } ) { <__experimentalBlockSettingsMenuFirstItem> { ( { onClose } ) => ( diff --git a/packages/editor/src/components/post-title/index.js b/packages/editor/src/components/post-title/index.js index 493931b5003e3e..952855d1e17cb5 100644 --- a/packages/editor/src/components/post-title/index.js +++ b/packages/editor/src/components/post-title/index.js @@ -26,6 +26,20 @@ import PostTypeSupportCheck from '../post-type-support-check'; */ const REGEXP_NEWLINES = /[\r\n]+/g; +/** + * Checks if the document or any parent document has focus. + * + * @param {Document} doc Document to check. + */ +function hasFocus( doc ) { + const { activeElement, body, defaultView } = doc; + return ( + ( activeElement && activeElement !== body ) || + ( defaultView.parent !== defaultView && + hasFocus( defaultView.parent.document ) ) + ); +} + export default function PostTitle() { const instanceId = useInstanceId( PostTitle ); const ref = useRef(); @@ -65,13 +79,12 @@ export default function PostTitle() { useEffect( () => { const { ownerDocument } = ref.current; - const { activeElement, body } = ownerDocument; // Only autofocus the title when the post is entirely empty. This should // only happen for a new post, which means we focus the title on new // post so the author can start typing right away, without needing to // click anything. - if ( isCleanNewPost && ( ! activeElement || body === activeElement ) ) { + if ( isCleanNewPost && ! hasFocus( ownerDocument ) ) { ref.current.focus(); } }, [ isCleanNewPost ] ); diff --git a/packages/editor/src/components/post-title/style.scss b/packages/editor/src/components/post-title/style.scss index 125825b5cb5a64..aef98632d9d271 100644 --- a/packages/editor/src/components/post-title/style.scss +++ b/packages/editor/src/components/post-title/style.scss @@ -80,3 +80,34 @@ } } } + +// The base width of blocks +.block-editor-block-list__block { + margin-left: auto; + margin-right: auto; +} + +// Ideally this wrapper div is not needed but if we waant to match the positionning of blocks +// .block-editor-block-list__layout and block-editor-block-list__block +// We need to have two DOM elements. +.edit-post-visual-editor__post-title-wrapper { + // This padding is needed to match the block root container padding + padding-left: $block-padding; + padding-right: $block-padding; + + @include break-small() { + padding-left: $block-side-ui-width; + padding-right: $block-side-ui-width; + } + + .editor-post-title { + // Center. + margin-left: auto; + margin-right: auto; + + // Apply default block margin below the post title. + // This ensures the first block on the page is in a good position. + // This rule can be retired once the title becomes an actual block. + margin-bottom: ($block-padding * 2) + $block-spacing; // This matches 2em in the vanilla style. + } +} diff --git a/packages/editor/src/editor-styles.scss b/packages/editor/src/editor-styles.scss index 1921e7388ddd7d..3c549ccfde64fe 100644 --- a/packages/editor/src/editor-styles.scss +++ b/packages/editor/src/editor-styles.scss @@ -22,6 +22,9 @@ body { margin-right: -10px; } +* { + box-sizing: border-box; +} /* Headings */ // Adjust line spacing for larger headings. From 2742944148ddd5264ead434a38bc7792839f55f1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ella=20van=C2=A0Durpe?= Date: Thu, 10 Dec 2020 12:40:19 +0200 Subject: [PATCH 12/17] Rebase fix --- packages/components/src/popover/index.js | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/packages/components/src/popover/index.js b/packages/components/src/popover/index.js index 006ce27edd4172..f7021928317f3c 100644 --- a/packages/components/src/popover/index.js +++ b/packages/components/src/popover/index.js @@ -7,7 +7,12 @@ import mergeRefs from 'react-merge-refs'; /** * WordPress dependencies */ -import { useRef, useState, useLayoutEffect } from '@wordpress/element'; +import { + useRef, + useState, + useLayoutEffect, + useEffect, +} from '@wordpress/element'; import { getRectangleFromRange } from '@wordpress/dom'; import { ESCAPE } from '@wordpress/keycodes'; import deprecated from '@wordpress/deprecated'; From 2575547e911f3521214abcdbcfabb88b7eb3ee63 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ella=20van=C2=A0Durpe?= Date: Fri, 11 Dec 2020 14:05:04 +0200 Subject: [PATCH 13/17] rebase fixes --- packages/block-editor/src/components/iframe/index.js | 5 ++++- packages/edit-post/src/components/visual-editor/index.js | 2 +- packages/edit-post/src/components/visual-editor/style.scss | 7 +++++++ 3 files changed, 12 insertions(+), 2 deletions(-) diff --git a/packages/block-editor/src/components/iframe/index.js b/packages/block-editor/src/components/iframe/index.js index 707f4f2149c4de..38d96318669236 100644 --- a/packages/block-editor/src/components/iframe/index.js +++ b/packages/block-editor/src/components/iframe/index.js @@ -158,7 +158,10 @@ function Iframe( { contentRef, children, head, ...props }, ref ) { styleSheetsCompat( contentDocument ); bubbleEvents( contentDocument ); setBodyClassName( contentDocument ); - contentRef.current = contentDocument.body; + + if ( contentRef ) { + contentRef.current = contentDocument.body; + } return true; } diff --git a/packages/edit-post/src/components/visual-editor/index.js b/packages/edit-post/src/components/visual-editor/index.js index 759f4445623121..b937f0d2e23d23 100644 --- a/packages/edit-post/src/components/visual-editor/index.js +++ b/packages/edit-post/src/components/visual-editor/index.js @@ -106,7 +106,7 @@ export default function VisualEditor( { settings } ) { style={ resizedCanvasStyles } head={ window.__editorStyles.html } > - { () => } + <__experimentalBlockSettingsMenuFirstItem> { ( { onClose } ) => ( diff --git a/packages/edit-post/src/components/visual-editor/style.scss b/packages/edit-post/src/components/visual-editor/style.scss index 51099a6c339407..a3b3d8256f6366 100644 --- a/packages/edit-post/src/components/visual-editor/style.scss +++ b/packages/edit-post/src/components/visual-editor/style.scss @@ -1,5 +1,12 @@ .edit-post-visual-editor { position: relative; + height: 100%; + + iframe { + display: block; + width: 100%; + height: 100%; + } // The button element easily inherits styles that are meant for the editor style. // These rules enhance the specificity to reduce that inheritance. From 0997839a2cfb9cdca296e7a1510bc38d012dae3c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ella=20van=C2=A0Durpe?= Date: Mon, 14 Dec 2020 11:14:34 +0200 Subject: [PATCH 14/17] Add missing typing observer --- packages/edit-post/src/components/visual-editor/index.js | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/packages/edit-post/src/components/visual-editor/index.js b/packages/edit-post/src/components/visual-editor/index.js index b937f0d2e23d23..f56ae63a27b950 100644 --- a/packages/edit-post/src/components/visual-editor/index.js +++ b/packages/edit-post/src/components/visual-editor/index.js @@ -12,6 +12,7 @@ import { __unstableUseTypewriter as useTypewriter, __unstableUseClipboardHandler as useClipboardHandler, __unstableUseTypingObserver as useTypingObserver, + __unstableUseMouseMoveTypingReset as useMouseMoveTypingReset, __unstableUseScrollMultiSelectionIntoView as useScrollMultiSelectionIntoView, __experimentalBlockSettingsMenuFirstItem, __experimentalUseResizeCanvas as useResizeCanvas, @@ -30,7 +31,6 @@ import { useSelect } from '@wordpress/data'; function Canvas( { settings } ) { const ref = useRef(); - const html = useRef(); const body = useRef(); useScrollMultiSelectionIntoView( ref ); @@ -38,7 +38,6 @@ function Canvas( { settings } ) { useTypewriter( ref ); useClipboardHandler( ref ); useTypingObserver( ref ); - useCanvasClickRedirect( html ); useCanvasClickRedirect( body ); useEditorStyles( ref, settings.styles ); @@ -68,11 +67,9 @@ function Canvas( { settings } ) { if ( newRef ) { ref.current = newRef; body.current = newRef.ownerDocument.body; - html.current = newRef.ownerDocument.documentElement; } else { ref.current = null; body.current = null; - html.current = null; } } @@ -97,12 +94,16 @@ export default function VisualEditor( { settings } ) { return select( 'core/edit-post' ).__experimentalGetPreviewDeviceType(); }, [] ); const resizedCanvasStyles = useResizeCanvas( deviceType, true ); + const ref = useRef(); + + useMouseMoveTypingReset( ref ); return (