Skip to content

Commit

Permalink
Remove duplication of editor styles (#28837)
Browse files Browse the repository at this point in the history
* Revert "Fix global styles in iframed site editor  (#28731)"

This reverts commit 9c206dd.

* Iframe editor styles: remove duplication

* Polish

* Use useMergeRefs

* Use React for rendering style elements

* Fix package.json
  • Loading branch information
ellatrix authored and oandregal committed Feb 11, 2021
1 parent 95551d9 commit bbb9934
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 111 deletions.
67 changes: 31 additions & 36 deletions packages/block-editor/src/components/editor-styles/index.js
Original file line number Diff line number Diff line change
@@ -1,62 +1,57 @@
/**
* External dependencies
*/
import { compact, map } from 'lodash';
import tinycolor from 'tinycolor2';

/**
* WordPress dependencies
*/
import { useCallback, useRef } from '@wordpress/element';
import { useCallback } from '@wordpress/element';

/**
* Internal dependencies
*/
import transformStyles from '../../utils/transform-styles';

function syncDarkThemeBodyClassname( node ) {
const backgroundColor = window
.getComputedStyle( node, null )
.getPropertyValue( 'background-color' );

const { ownerDocument } = node;
const body = ownerDocument.getElementsByTagName( 'body' )[ 0 ];

if ( tinycolor( backgroundColor ).getLuminance() > 0.5 ) {
body.classList.remove( 'is-dark-theme' );
} else {
body.classList.add( 'is-dark-theme' );
}
}

export default function useEditorStyles( styles ) {
const nodes = useRef( [] );
const EDITOR_STYLES_SELECTOR = '.editor-styles-wrapper';

function useDarkThemeBodyClassName( styles ) {
return useCallback(
( node ) => {
if ( ! node ) {
nodes.current.forEach( ( styleElement ) =>
styleElement.ownerDocument.body.removeChild( styleElement )
);
return;
}

const updatedStyles = transformStyles(
styles,
'.editor-styles-wrapper'
);

const { ownerDocument } = node;
nodes.current = map( compact( updatedStyles ), ( updatedCSS ) => {
const styleElement = ownerDocument.createElement( 'style' );
styleElement.innerHTML = updatedCSS;
ownerDocument.body.appendChild( styleElement );

return styleElement;
} );

syncDarkThemeBodyClassname( node );
const { defaultView, body } = ownerDocument;
const canvas = ownerDocument.querySelector(
EDITOR_STYLES_SELECTOR
);
const backgroundColor = defaultView
.getComputedStyle( canvas, null )
.getPropertyValue( 'background-color' );

if ( tinycolor( backgroundColor ).getLuminance() > 0.5 ) {
body.classList.remove( 'is-dark-theme' );
} else {
body.classList.add( 'is-dark-theme' );
}
},
[ styles ]
);
}

export default function EditorStyles( { styles } ) {
return (
<>
{ /* Use an empty style element to have a document reference,
but this could be any element. */ }
<style ref={ useDarkThemeBodyClassName( styles ) } />
{ transformStyles( styles, EDITOR_STYLES_SELECTOR ).map(
( css, index ) => (
<style key={ index }>{ css }</style>
)
) }
</>
);
}
55 changes: 11 additions & 44 deletions packages/block-editor/src/components/iframe/index.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,15 @@
/**
* External dependencies
*/
import { compact, map } from 'lodash';
import tinycolor from 'tinycolor2';

/**
* WordPress dependencies
*/
import {
useState,
createPortal,
useCallback,
useEffect,
forwardRef,
} from '@wordpress/element';
import { __ } from '@wordpress/i18n';
import { useMergeRefs } from '@wordpress/compose';

/**
* Internal dependencies
*/
import transformStyles from '../../utils/transform-styles';

const BODY_CLASS_NAME = 'editor-styles-wrapper';
const BLOCK_PREFIX = 'wp-block';

Expand Down Expand Up @@ -144,56 +132,34 @@ function setHead( doc, head ) {
'<style>body{margin:0}</style>' + head;
}

function updateEditorStyles( doc, styles ) {
if ( ! doc ) {
return;
}

const backgroundColor = window
.getComputedStyle( doc.body, null )
.getPropertyValue( 'background-color' );
if ( tinycolor( backgroundColor ).getLuminance() > 0.5 ) {
doc.body.classList.remove( 'is-dark-theme' );
} else {
doc.body.classList.add( 'is-dark-theme' );
}

const updatedStyles = transformStyles( styles, '.editor-styles-wrapper' );
map( compact( updatedStyles ), ( updatedStyle ) => {
const styleElement = doc.createElement( 'style' );
styleElement.innerHTML = updatedStyle;
doc.body.appendChild( styleElement );
} );
}

function Iframe( { contentRef, editorStyles, children, head, ...props }, ref ) {
function Iframe( { contentRef, children, head, headHTML, ...props }, ref ) {
const [ iframeDocument, setIframeDocument ] = useState();

useEffect( () => {
updateEditorStyles( iframeDocument, editorStyles );
}, [ editorStyles ] );

const setRef = useCallback( ( node ) => {
if ( ! node ) {
return;
}

function setDocumentIfReady() {
const { contentDocument } = node;
const { readyState } = contentDocument;
const { readyState, body } = contentDocument;

if ( readyState !== 'interactive' && readyState !== 'complete' ) {
return false;
}

contentRef.current = contentDocument.body;
setIframeDocument( contentDocument );
setHead( contentDocument, head );
if ( typeof contentRef === 'function' ) {
contentRef( body );
} else if ( contentRef ) {
contentRef.current = body;
}

setHead( contentDocument, headHTML );
setBodyClassName( contentDocument );
styleSheetsCompat( contentDocument );
updateEditorStyles( contentDocument, editorStyles );
bubbleEvents( contentDocument );
setBodyClassName( contentDocument );
setIframeDocument( contentDocument );

return true;
}
Expand All @@ -217,6 +183,7 @@ function Iframe( { contentRef, editorStyles, children, head, ...props }, ref ) {
name="editor-canvas"
>
{ iframeDocument && createPortal( children, iframeDocument.body ) }
{ iframeDocument && createPortal( head, iframeDocument.head ) }
</iframe>
);
}
Expand Down
2 changes: 1 addition & 1 deletion packages/block-editor/src/components/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ export {
useClipboardHandler as __unstableUseClipboardHandler,
} from './copy-handler';
export { default as DefaultBlockAppender } from './default-block-appender';
export { default as __unstableUseEditorStyles } from './editor-styles';
export { default as __unstableEditorStyles } from './editor-styles';
export { default as Inserter } from './inserter';
export { default as __experimentalLibrary } from './inserter/library';
export { default as __experimentalSearchForm } from './inserter/search-form';
Expand Down
7 changes: 3 additions & 4 deletions packages/edit-post/src/components/visual-editor/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,10 @@ import {
__experimentalBlockSettingsMenuFirstItem,
__experimentalUseResizeCanvas as useResizeCanvas,
__unstableUseCanvasClickRedirect as useCanvasClickRedirect,
__unstableUseEditorStyles as useEditorStyles,
__unstableEditorStyles as EditorStyles,
} from '@wordpress/block-editor';
import { Popover } from '@wordpress/components';
import { useRef } from '@wordpress/element';
import { useMergeRefs } from '@wordpress/compose';

/**
* Internal dependencies
Expand Down Expand Up @@ -59,14 +58,14 @@ export default function VisualEditor( { styles } ) {
useClipboardHandler( ref );
useTypingObserver( ref );
useCanvasClickRedirect( ref );
const editorStylesRef = useEditorStyles( styles );

return (
<div className="edit-post-visual-editor">
<EditorStyles styles={ styles } />
<VisualEditorGlobalKeyboardShortcuts />
<Popover.Slot name="block-toolbar" />
<div
ref={ useMergeRefs( [ ref, editorStylesRef ] ) }
ref={ ref }
className="editor-styles-wrapper"
style={ resizedCanvasStyles || desktopCanvasStyles }
>
Expand Down
18 changes: 5 additions & 13 deletions packages/edit-site/src/components/block-editor/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {
__unstableUseBlockSelectionClearer as useBlockSelectionClearer,
__unstableUseTypingObserver as useTypingObserver,
__unstableUseMouseMoveTypingReset as useMouseMoveTypingReset,
__unstableUseEditorStyles as useEditorStyles,
__unstableEditorStyles as EditorStyles,
__unstableIframe as Iframe,
} from '@wordpress/block-editor';
import { DropZoneProvider, Popover } from '@wordpress/components';
Expand Down Expand Up @@ -69,10 +69,6 @@ export default function BlockEditor( { setIsInserterOpen } ) {
const contentRef = useRef();

useMouseMoveTypingReset( ref );
// This updates the host document styles.
// It is necessary to make sure the preset CSS Custom Properties
// are in scope for the sidebar UI controls that use them.
const editorStylesRef = useEditorStyles( settings.styles );

// Allow scrolling "through" popovers over the canvas. This is only called
// for as long as the pointer is over a popover.
Expand Down Expand Up @@ -105,20 +101,16 @@ export default function BlockEditor( { setIsInserterOpen } ) {
<SidebarInspectorFill>
<BlockInspector />
</SidebarInspectorFill>
<div
ref={ editorStylesRef }
className="edit-site-visual-editor"
onWheel={ onWheel }
>
<div className="edit-site-visual-editor" onWheel={ onWheel }>
<Popover.Slot name="block-toolbar" />
<Iframe
style={ resizedCanvasStyles }
editorStyles={ settings.styles }
head={ window.__editorStyles.html }
headHTML={ window.__editorStyles.html }
head={ <EditorStyles styles={ settings.styles } /> }
ref={ ref }
contentRef={ contentRef }
>
<Canvas body={ contentRef } styles={ settings.styles } />
<Canvas body={ contentRef } />
</Iframe>
</div>
</BlockEditorProvider>
Expand Down
22 changes: 10 additions & 12 deletions packages/edit-widgets/src/components/layout/interface.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,7 @@ import {
useViewportMatch,
} from '@wordpress/compose';
import { close } from '@wordpress/icons';
import {
__experimentalLibrary as Library,
__unstableUseEditorStyles as useEditorStyles,
} from '@wordpress/block-editor';
import { __experimentalLibrary as Library } from '@wordpress/block-editor';
import { useEffect } from '@wordpress/element';
import { useDispatch, useSelect } from '@wordpress/data';
import {
Expand Down Expand Up @@ -45,13 +42,15 @@ function Interface( { blockEditorSettings } ) {
);
const { rootClientId, insertionIndex } = useWidgetLibraryInsertionPoint();

const { hasSidebarEnabled, isInserterOpened } = useSelect( ( select ) => ( {
hasSidebarEnabled: !! select(
interfaceStore
).getActiveComplementaryArea( editWidgetsStore ),
isInserterOpened: !! select( editWidgetsStore ).isInserterOpened(),
} ) );
const editorStylesRef = useEditorStyles( blockEditorSettings.styles );
const { hasSidebarEnabled, isInserterOpened } = useSelect(
( select ) => ( {
hasSidebarEnabled: !! select(
interfaceStore
).getActiveComplementaryArea( editWidgetsStore.name ),
isInserterOpened: !! select( editWidgetsStore ).isInserterOpened(),
} ),
[]
);

// Inserter and Sidebars are mutually exclusive
useEffect( () => {
Expand All @@ -72,7 +71,6 @@ function Interface( { blockEditorSettings } ) {

return (
<InterfaceSkeleton
ref={ editorStylesRef }
labels={ interfaceLabels }
header={ <Header /> }
secondarySidebar={
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
BlockSelectionClearer,
WritingFlow,
ObserveTyping,
__unstableEditorStyles as EditorStyles,
} from '@wordpress/block-editor';

/**
Expand All @@ -16,9 +17,12 @@ import {
import Notices from '../notices';
import KeyboardShortcuts from '../keyboard-shortcuts';

export default function WidgetAreasBlockEditorContent() {
export default function WidgetAreasBlockEditorContent( {
blockEditorSettings,
} ) {
return (
<div className="edit-widgets-block-editor editor-styles-wrapper">
<EditorStyles styles={ blockEditorSettings.styles } />
<KeyboardShortcuts />
<BlockEditorKeyboardShortcuts />
<Notices />
Expand Down

0 comments on commit bbb9934

Please sign in to comment.