Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Editor: Extract BlockList as reusable component #3546

Merged
merged 2 commits into from
Nov 21, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion blocks/editable/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,7 @@ export default class Editable extends Component {
// Find the parent "relative" positioned container
const container = this.props.inlineToolbar ?
this.editor.getBody().closest( '.blocks-editable' ) :
this.editor.getBody().closest( '.editor-visual-editor__block' );
this.editor.getBody().closest( '.editor-block-list__block' );
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this a good opportunity to stop using a hard-coded selector for this? What are some other options?

Copy link
Contributor

@ephox-mogran ephox-mogran Nov 20, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could also use context and some HOCs to specify the component to use for anchoring directly as well (rather than relying on DOM hierarchy and selectors).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this a good opportunity to stop using a hard-coded selector for this? What are some other options?
We could also use context and some HOCs to specify the component to use for anchoring directly as well (rather than relying on DOM hierarchy and selectors).

I definitely agree with the desire here, and hope to discourage these hard-coded selectors from being introduced in the first place (this pull request serving as evidence of the maintenance pain they cause). But I'd rather address these separate from the intent of this pull request.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Although given some of the immediate concerns of moving this as raised in #3546 (comment), maybe it is a prime time 😕

const containerPosition = container.getBoundingClientRect();
const blockPadding = 14;
const blockMoverMargin = 18;
Expand Down
2 changes: 1 addition & 1 deletion blocks/library/button/editor.scss
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
$blocks-button__height: 46px;

.editor-visual-editor__block[data-type="core/button"] {
.editor-block-list__block[data-type="core/button"] {
&[data-align="center"] {
text-align: center;
}
Expand Down
4 changes: 1 addition & 3 deletions blocks/library/categories/editor.scss
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@

.editor-visual-editor__block[data-type="core/categories"] {

.editor-block-list__block[data-type="core/categories"] {
.wp-block-categories ul {
padding-left: 2.5em;

Expand Down
2 changes: 1 addition & 1 deletion blocks/library/cover-image/editor.scss
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
.editor-visual-editor__block[data-type="core/cover-image"] {
.editor-block-list__block[data-type="core/cover-image"] {
.blocks-editable__tinymce[data-is-empty="true"]:before {
position: inherit;
}
Expand Down
2 changes: 1 addition & 1 deletion blocks/library/freeform/editor.scss
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
.editor-visual-editor__block[data-type="core/freeform"] .blocks-editable__tinymce {
.editor-block-list__block[data-type="core/freeform"] .blocks-editable__tinymce {
margin-top: $item-spacing;

p {
Expand Down
2 changes: 1 addition & 1 deletion blocks/library/gallery/editor.scss
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
margin-bottom: -16px;
}

.editor-visual-editor__block[data-type="core/gallery"] .editor-visual-editor__block-edit {
.editor-block-list__block[data-type="core/gallery"] .editor-block-list__block-edit {
overflow: hidden;
}

Expand Down
2 changes: 1 addition & 1 deletion blocks/library/heading/editor.scss
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
.editor-visual-editor__block[data-type="core/heading"] {
.editor-block-list__block[data-type="core/heading"] {
h1,
h2,
h3,
Expand Down
6 changes: 3 additions & 3 deletions blocks/library/image/editor.scss
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
left: -6px !important;
}

.editor-visual-editor__block[data-type="core/image"] {
.editor-block-list__block[data-type="core/image"] {
.blocks-format-toolbar__link-modal {
top: 0;
left: 0;
Expand All @@ -73,8 +73,8 @@
}
}

.editor-visual-editor__block[data-type="core/image"][data-align="right"],
.editor-visual-editor__block[data-type="core/image"][data-align="left"] {
.editor-block-list__block[data-type="core/image"][data-align="right"],
.editor-block-list__block[data-type="core/image"][data-align="left"] {
max-width: none !important;
&[data-resized="false"] {
max-width: 370px !important;
Expand Down
2 changes: 1 addition & 1 deletion blocks/library/latest-posts/editor.scss
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

.editor-visual-editor__block[data-type="core/latest-posts"] {
.editor-block-list__block[data-type="core/latest-posts"] {

.wp-block-latest-posts {
padding-left: 2.5em;
Expand Down
6 changes: 3 additions & 3 deletions blocks/library/more/editor.scss
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
.editor-visual-editor__block[data-type="core/more"] {
.editor-block-list__block[data-type="core/more"] {
max-width: 100%;
text-align: center;
}

.editor-visual-editor__block .wp-block-more {
.editor-block-list__block .wp-block-more {
input {
font-size: 12px;
text-transform: uppercase;
Expand All @@ -29,6 +29,6 @@
left: $block-mover-padding-visible + $block-padding;
right: $block-mover-padding-visible + $block-padding;
border-top: 3px dashed $light-gray-700;
z-index: z-index( '.editor-visual-editor__block .wp-block-more:before' );
z-index: z-index( '.editor-block-list__block .wp-block-more:before' );
}
}
2 changes: 1 addition & 1 deletion blocks/library/pullquote/editor.scss
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
.editor-visual-editor__block[data-type="core/pullquote"] {
.editor-block-list__block[data-type="core/pullquote"] {
&[data-align="left"],
&[data-align="right"] {
max-width: 400px;
Expand Down
2 changes: 1 addition & 1 deletion blocks/library/table/editor.scss
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
.editor-visual-editor__block[data-type="core/table"] {
.editor-block-list__block[data-type="core/table"] {

.editor-block-toolbar__group > div:not(.editor-block-toolbar__mobile-tools) {
display: flex;
Expand Down
8 changes: 4 additions & 4 deletions editor/assets/stylesheets/_z-index.scss
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@

$z-layers: (
'.editor-block-switcher__arrow': 1,
'.editor-visual-editor__block:before': -1,
'.editor-visual-editor__block .wp-block-more:before': -1,
'.editor-visual-editor__block {core/image aligned left or right}': 20,
'.editor-block-list__block:before': -1,
'.editor-block-list__block .wp-block-more:before': -1,
'.editor-block-list__block {core/image aligned left or right}': 20,
'.freeform-toolbar': 10,
'.editor-warning': 1,
'.editor-visual-editor__sibling-inserter': 1,
'.editor-block-list__sibling-inserter': 1,
'.components-form-toggle__input': 1,
'.editor-format-list__menu': 1,
'.editor-inserter__tabs': 1,
Expand Down
2 changes: 1 addition & 1 deletion editor/assets/stylesheets/main.scss
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ body.gutenberg-editor-page {
}

.editor-post-title,
.editor-visual-editor__block {
.editor-block-list__block {
input,
textarea {
border-radius: 4px;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { __ } from '@wordpress/i18n';
* Internal dependencies
*/
import NavigableToolbar from '../../navigable-toolbar';
import { BlockToolbar } from '../../components';
import { BlockToolbar } from '../';
import { isFeatureActive } from '../../selectors';

function BlockContextualToolbar( { hasFixedToolbar } ) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { __ } from '@wordpress/i18n';
/**
* Internal dependencies
*/
import { Warning } from '../../components';
import Warning from '../warning';

const warning = (
<Warning>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ class BlockHTML extends Component {
const { html } = this.state;
return (
<TextareaAutosize
className="blocks-visual-editor__block-html-textarea"
className="editor-block-list__block-html-textarea"
value={ html }
onBlur={ this.onBlur }
onChange={ this.onChange }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ import { __, sprintf } from '@wordpress/i18n';
/**
* Internal dependencies
*/
import BlockMover from '../../components/block-mover';
import BlockSettingsMenu from '../../components/block-settings-menu';
import BlockMover from '../block-mover';
import BlockSettingsMenu from '../block-settings-menu';
import InvalidBlockWarning from './invalid-block-warning';
import BlockCrashWarning from './block-crash-warning';
import BlockCrashBoundary from './block-crash-boundary';
Expand Down Expand Up @@ -56,7 +56,31 @@ import {

const { BACKSPACE, ESCAPE, DELETE, ENTER, UP, RIGHT, DOWN, LEFT } = keycodes;

class VisualEditorBlock extends Component {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Making this reusable means we need to use a prop or something to get a selector for the parent scrollable. https://github.com/WordPress/gutenberg/pull/3546/files#diff-bc66d5701a2fea1111bb8f1bf0150910R96

It's not guaranteed to be the same in all layouts.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In d8be96b I reimplemented the behavior to a generic "find closest scrollable container".

/**
* Given a DOM node, finds the closest scrollable container node.
*
* @param {Element} node Node from which to start
* @return {?Element} Scrollable container node, if found
*/
function getScrollContainer( node ) {
if ( ! node ) {
return;
}

// Scrollable if scrollable height exceeds displayed...
if ( node.scrollHeight > node.clientHeight ) {
// ...except when overflow is defined to be hidden or visible
const { overflowY } = window.getComputedStyle( node );
if ( /(auto|scroll)/.test( overflowY ) ) {
return node;
}
}

// Continue traversing
return getScrollContainer( node.parentNode );
}

class BlockListBlock extends Component {
constructor() {
super( ...arguments );

Expand Down Expand Up @@ -91,9 +115,6 @@ class VisualEditorBlock extends Component {
if ( this.props.isTyping ) {
document.addEventListener( 'mousemove', this.stopTypingOnMouseMove );
}

// Not Ideal, but it's the easiest way to get the scrollable container
this.editorLayout = document.querySelector( '.editor-layout__content' );
}

componentWillReceiveProps( newProps ) {
Expand All @@ -109,9 +130,13 @@ class VisualEditorBlock extends Component {
componentDidUpdate( prevProps ) {
// Preserve scroll prosition when block rearranged
if ( this.previousOffset ) {
this.editorLayout.scrollTop = this.editorLayout.scrollTop +
this.node.getBoundingClientRect().top -
this.previousOffset;
const scrollContainer = getScrollContainer( this.node );
if ( scrollContainer ) {
scrollContainer.scrollTop = scrollContainer.scrollTop +
this.node.getBoundingClientRect().top -
this.previousOffset;
}

this.previousOffset = null;
}

Expand Down Expand Up @@ -324,7 +349,7 @@ class VisualEditorBlock extends Component {
const { isHovered, isSelected, isMultiSelected, isFirstMultiSelected, focus } = this.props;
const showUI = isSelected && ( ! this.props.isTyping || ( focus && focus.collapsed === false ) );
const { error } = this.state;
const wrapperClassName = classnames( 'editor-visual-editor__block', {
const wrapperClassName = classnames( 'editor-block-list__block', {
'has-warning': ! isValid || !! error,
'is-selected': showUI,
'is-multi-selected': isMultiSelected,
Expand Down Expand Up @@ -369,7 +394,7 @@ class VisualEditorBlock extends Component {
onMouseDown={ this.onPointerDown }
onKeyDown={ this.onKeyDown }
onFocus={ this.onFocus }
className="editor-visual-editor__block-edit"
className="editor-block-list__block-edit"
tabIndex="0"
aria-label={ blockLabel }
>
Expand Down Expand Up @@ -487,4 +512,4 @@ export default connect(
dispatch( editPost( { meta } ) );
},
} )
)( VisualEditorBlock );
)( BlockListBlock );
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@ import { serialize, getDefaultBlockName, createBlock } from '@wordpress/blocks';
/**
* Internal dependencies
*/
import VisualEditorBlock from './block';
import VisualEditorSiblingInserter from './sibling-inserter';
import './style.scss';
import BlockListBlock from './block';
import BlockListSiblingInserter from './sibling-inserter';
import BlockDropZone from './block-drop-zone';
import {
getBlockUids,
Expand All @@ -38,7 +39,7 @@ import {
} from '../../selectors';
import { insertBlock, startMultiSelect, stopMultiSelect, multiSelect, selectBlock } from '../../actions';

class VisualEditorBlockList extends Component {
class BlockList extends Component {
constructor( props ) {
super( props );

Expand Down Expand Up @@ -209,22 +210,22 @@ class VisualEditorBlockList extends Component {

return (
<div>
{ !! blocks.length && <VisualEditorSiblingInserter /> }
{ !! blocks.length && <BlockListSiblingInserter /> }
{ flatMap( blocks, ( uid ) => [
<VisualEditorBlock
<BlockListBlock
key={ 'block-' + uid }
uid={ uid }
blockRef={ this.setBlockRef }
onSelectionStart={ this.onSelectionStart }
onShiftSelection={ this.onShiftSelection }
/>,
<VisualEditorSiblingInserter
<BlockListSiblingInserter
key={ 'sibling-inserter-' + uid }
uid={ uid }
/>,
] ) }
{ ! blocks.length &&
<div className="editor-visual-editor__placeholder">
<div className="editor-block-list__placeholder">
<BlockDropZone />
<input
type="text"
Expand Down Expand Up @@ -270,4 +271,4 @@ export default connect(
dispatch( { type: 'REMOVE_BLOCKS', uids } );
},
} )
)( VisualEditorBlockList );
)( BlockList );
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,6 @@ import { connect } from 'react-redux';
*/
import { __, sprintf } from '@wordpress/i18n';
import { Button } from '@wordpress/components';

/**
* Internal dependencies
*/
import { Warning } from '../../components';
import {
getBlockType,
getUnknownTypeHandlerName,
Expand All @@ -23,6 +18,7 @@ import {
* Internal dependencies
*/
import { replaceBlock } from '../../actions';
import Warning from '../warning';

function InvalidBlockWarning( { ignoreInvalid, switchToBlockType } ) {
const htmlBlockName = 'core/html';
Expand All @@ -37,7 +33,7 @@ function InvalidBlockWarning( { ignoreInvalid, switchToBlockType } ) {
'Overwrite the external changes or Convert to %s or %s to keep ' +
'your changes.'
), defaultBlockType.title, htmlBlockType.title ) }</p>
<p className="visual-editor__invalid-block-warning-buttons">
<p>
<Button
onClick={ ignoreInvalid }
isLarge
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ import { connect } from 'react-redux';
/**
* Internal dependencies
*/
import BlockMover from '../../components/block-mover';
import BlockSettingsMenu from '../../components/block-settings-menu';
import BlockMover from '../block-mover';
import BlockSettingsMenu from '../block-settings-menu';
import {
getMultiSelectedBlockUids,
isMultiSelecting,
} from '../../selectors';

function VisualEditorBlockMultiControls( { multiSelectedBlockUids, isSelecting } ) {
function BlockListMultiControls( { multiSelectedBlockUids, isSelecting } ) {
if ( isSelecting ) {
return null;
}
Expand All @@ -36,4 +36,4 @@ export default connect( ( state ) => {
multiSelectedBlockUids: getMultiSelectedBlockUids( state ),
isSelecting: isMultiSelecting( state ),
};
} )( VisualEditorBlockMultiControls );
} )( BlockListMultiControls );
Loading