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

Writing Flow: Always add an empty at the end of the block list #3623

Merged
merged 6 commits into from
Nov 24, 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 editor/components/block-list/block.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ import { __, sprintf } from '@wordpress/i18n';
* Internal dependencies
*/
import BlockMover from '../block-mover';
import BlockDropZone from '../block-drop-zone';
import BlockSettingsMenu from '../block-settings-menu';
import InvalidBlockWarning from './invalid-block-warning';
import BlockCrashWarning from './block-crash-warning';
import BlockCrashBoundary from './block-crash-boundary';
import BlockDropZone from './block-drop-zone';
import BlockHtml from './block-html';
import BlockContextualToolbar from './block-contextual-toolbar';
import BlockMultiControls from './multi-controls';
Expand Down
29 changes: 2 additions & 27 deletions editor/components/block-list/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import {
invert,
isEqual,
mapValues,
noop,
sortBy,
throttle,
} from 'lodash';
Expand All @@ -18,17 +17,15 @@ import 'element-closest';
/**
* WordPress dependencies
*/
import { __ } from '@wordpress/i18n';
import { Component } from '@wordpress/element';
import { serialize, getDefaultBlockName, createBlock } from '@wordpress/blocks';
import { serialize } from '@wordpress/blocks';

/**
* Internal dependencies
*/
import './style.scss';
import BlockListBlock from './block';
import BlockListSiblingInserter from './sibling-inserter';
import BlockDropZone from './block-drop-zone';
import {
getBlockUids,
getMultiSelectedBlocksStartUid,
Expand All @@ -37,7 +34,7 @@ import {
getMultiSelectedBlockUids,
getSelectedBlock,
} from '../../selectors';
import { insertBlock, startMultiSelect, stopMultiSelect, multiSelect, selectBlock } from '../../actions';
import { startMultiSelect, stopMultiSelect, multiSelect, selectBlock } from '../../actions';

class BlockList extends Component {
constructor( props ) {
Expand All @@ -49,7 +46,6 @@ class BlockList extends Component {
this.onCopy = this.onCopy.bind( this );
this.onCut = this.onCut.bind( this );
this.setBlockRef = this.setBlockRef.bind( this );
this.appendDefaultBlock = this.appendDefaultBlock.bind( this );
this.setLastClientY = this.setLastClientY.bind( this );
this.onPointerMove = throttle( this.onPointerMove.bind( this ), 100 );
// Browser does not fire `*move` event when the pointer position changes
Expand Down Expand Up @@ -200,11 +196,6 @@ class BlockList extends Component {
}
}

appendDefaultBlock() {
const newBlock = createBlock( getDefaultBlockName() );
this.props.onInsertBlock( newBlock );
}

render() {
const { blocks, showContextualToolbar } = this.props;

Expand All @@ -225,19 +216,6 @@ class BlockList extends Component {
uid={ uid }
/>,
] ) }
{ ! blocks.length &&
<div className="editor-block-list__placeholder">
<BlockDropZone />
<input
type="text"
readOnly
value={ __( 'Write your story' ) }
onFocus={ this.appendDefaultBlock }
onClick={ noop }
onKeyDown={ noop }
/>
</div>
}
</div>
);
}
Expand All @@ -253,9 +231,6 @@ export default connect(
selectedBlock: getSelectedBlock( state ),
} ),
( dispatch ) => ( {
onInsertBlock( block ) {
dispatch( insertBlock( block ) );
},
onStartMultiSelect() {
dispatch( startMultiSelect() );
},
Expand Down
41 changes: 0 additions & 41 deletions editor/components/block-list/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -323,47 +323,6 @@
}
}

.editor-block-list__placeholder {
max-width: $visual-editor-max-width + ( 2 * $block-mover-padding-visible );
padding: 0;
clear: left;
margin: 0 auto;
position: relative;

@include break-small() {
padding: 0 ( $block-padding + $block-mover-padding-visible );
}

input[type=text] {
height: $text-editor-font-size * 4; // same height as an empty paragraph
margin: 0px 0px 4px 0px;
outline: 1px solid transparent;
border: none;
background: none;
box-shadow: none;
display: block;
transition: 0.2s outline;
text-align: left;
width: 100%;
color: $dark-gray-300;
font-size: $editor-font-size;
line-height: $editor-line-height;
cursor: text;
max-width: none; // fixes a bleed issue from the admin

&:hover {
outline: 1px solid $light-gray-500;
}

padding: ( $block-padding - 2px ) $block-padding;

@include break-small() {
margin-left: -$block-padding;
margin-right: -$block-padding;
}
}
}

.editor-block-list__block .blocks-visual-editor__block-html-textarea {
display: block;
margin: 0;
Expand Down
71 changes: 71 additions & 0 deletions editor/components/default-block-appender/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/**
* External dependencies
*/
import { connect } from 'react-redux';
import classnames from 'classnames';
import 'element-closest';

/**
* WordPress dependencies
*/
import { __ } from '@wordpress/i18n';
import { Component } from '@wordpress/element';
import { getDefaultBlockName, createBlock } from '@wordpress/blocks';

/**
* Internal dependencies
*/
import './style.scss';
import BlockDropZone from '../block-drop-zone';
import { insertBlock } from '../../actions';
import { getBlockCount } from '../../selectors';

class DefaultBlockAppender extends Component {
constructor( props ) {
super( props );
this.appendDefaultBlock = this.appendDefaultBlock.bind( this );
}

appendDefaultBlock() {
const newBlock = createBlock( getDefaultBlockName() );
Copy link
Member

Choose a reason for hiding this comment

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

Would it make sense to update createBlock definition to simplify this call to:

export function createBlock( name = getDefaultBlockName(), blockAttributes = {} ) { ... }

this.props.onInsertBlock( createBlock() );

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't know :)

this.props.onInsertBlock( newBlock );
}

render() {
const { count } = this.props;

const className = classnames( 'editor-default-block-appender', {
'is-visible-placeholder': count === 0,
} );

return (
<div className={ className }>
<BlockDropZone />
{ count === 0 &&
<input
className="editor-default-block-appender__content"
type="text"
readOnly
onFocus={ this.appendDefaultBlock }
onClick={ this.appendDefaultBlock }
onKeyDown={ this.appendDefaultBlock }
Copy link
Member

Choose a reason for hiding this comment

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

In what case could a keydown event occur without the block already being focused? (And therefore triggering the identical focus event handler)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good catch, probably a useless event handler.

value={ __( 'Write your story' ) }
/>
}
{ count !== 0 &&
<button
className="editor-default-block-appender__content"
onClick={ this.appendDefaultBlock }
/>
}
</div>
);
}
}

export default connect(
Copy link
Member

Choose a reason for hiding this comment

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

it would be nice to have 3 unit tests:

  • count > 0 (snapshot test)
  • count = 1 check if there is class is-visible-placeholder and Write your story text
  • spy on insertBlock

I can add them, just ping me :)

( state ) => ( {
count: getBlockCount( state ),
} ),
{ onInsertBlock: insertBlock }
)( DefaultBlockAppender );
32 changes: 32 additions & 0 deletions editor/components/default-block-appender/style.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
$empty-paragraph-height: $text-editor-font-size * 4;

.editor-default-block-appender {
&.is-visible-placeholder .editor-default-block-appender__content {
height: $empty-paragraph-height;
color: $dark-gray-300;
outline: 1px solid transparent;
transition: 0.2s outline;

&:hover {
outline: 1px solid $light-gray-500;
}
}
}

.editor-default-block-appender__content,
input[type=text].editor-default-block-appender__content {
border: none;
background: none;
box-shadow: none;
display: block;
width: 100%;
height: $empty-paragraph-height * 1.5;
font-size: $editor-font-size;
line-height: $editor-line-height;
cursor: text;
max-width: none; // fixes a bleed issue from the admin

&:focus {
outline: 1px solid $light-gray-500;
}
}
1 change: 1 addition & 0 deletions editor/components/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ export { default as BlockList } from './block-list';
export { default as BlockMover } from './block-mover';
export { default as BlockSettingsMenu } from './block-settings-menu';
export { default as BlockToolbar } from './block-toolbar';
export { default as DefaultBlockAppender } from './default-block-appender';
export { default as ErrorBoundary } from './error-boundary';
export { default as Inserter } from './inserter';
export { default as Warning } from './warning';
Expand Down
5 changes: 2 additions & 3 deletions editor/edit-post/modes/visual-editor/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ import { KeyboardShortcuts } from '@wordpress/components';
* Internal dependencies
*/
import './style.scss';
import VisualEditorInserter from './inserter';
import { BlockList, PostTitle, WritingFlow } from '../../../components';
import { BlockList, PostTitle, WritingFlow, DefaultBlockAppender } from '../../../components';
import { getBlockUids, getMultiSelectedBlockUids, isFeatureActive } from '../../../selectors';
import { clearSelectedBlock, multiSelect, redo, undo, removeBlocks } from '../../../actions';

Expand Down Expand Up @@ -98,7 +97,7 @@ class VisualEditor extends Component {
showContextualToolbar={ ! this.props.hasFixedToolbar }
/>
</WritingFlow>
<VisualEditorInserter />
<DefaultBlockAppender />
</div>
);
/* eslint-enable jsx-a11y/no-static-element-interactions */
Expand Down
88 changes: 0 additions & 88 deletions editor/edit-post/modes/visual-editor/inserter.js

This file was deleted.

Loading