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

RichText: try alternative list shortcuts (to tab) #14343

Merged
merged 6 commits into from
Mar 13, 2019
Merged
Changes from 1 commit
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
Next Next commit
RichText: try alternative list shortcuts
  • Loading branch information
ellatrix committed Mar 13, 2019
commit b983288bdda364041909bdcdd9de8ef8c68287b9
61 changes: 50 additions & 11 deletions packages/block-editor/src/components/rich-text/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import memize from 'memize';
import { Component, Fragment, RawHTML } from '@wordpress/element';
import { isHorizontalEdge } from '@wordpress/dom';
import { createBlobURL } from '@wordpress/blob';
import { BACKSPACE, DELETE, ENTER, LEFT, RIGHT } from '@wordpress/keycodes';
import { BACKSPACE, DELETE, ENTER, LEFT, RIGHT, SPACE } from '@wordpress/keycodes';
import { withDispatch, withSelect } from '@wordpress/data';
import { pasteHandler, children, getBlockTransforms, findTransform } from '@wordpress/blocks';
import { withInstanceId, withSafeTimeout, compose } from '@wordpress/compose';
Expand All @@ -44,6 +44,7 @@ import {
isCollapsed,
LINE_SEPARATOR,
charAt,
indentListItems,
} from '@wordpress/rich-text';
import { decodeEntities } from '@wordpress/html-entities';
import { withFilters, IsolatedEventContainer } from '@wordpress/components';
Expand Down Expand Up @@ -599,8 +600,23 @@ export class RichText extends Component {
this.handleHorizontalNavigation( event );
}

if ( keyCode === SPACE && this.multilineTag === 'li' ) {
const value = this.createRecord();

if ( isCollapsed( value ) ) {
const { text, start } = value;
const characterBefore = text[ start - 1 ];

if ( ! characterBefore || characterBefore === LINE_SEPARATOR ) {
this.onChange( indentListItems( value, { type: this.props.tagName } ) );
event.preventDefault();
}
}
}

if ( keyCode === DELETE || keyCode === BACKSPACE ) {
const value = this.createRecord();
const { formats } = value;
const start = getSelectionStart( value );
const end = getSelectionEnd( value );

Expand All @@ -615,22 +631,45 @@ export class RichText extends Component {
let newValue;

if ( keyCode === BACKSPACE ) {
if ( charAt( value, start - 1 ) === LINE_SEPARATOR ) {
const index = start - 1;

if ( charAt( value, index ) === LINE_SEPARATOR ) {
if ( isCollapsed( value ) && formats[ index ] && formats[ index ].length ) {
const newFormats = formats.slice();

newFormats[ index ] = formats[ index ].slice( 0, -1 );
newValue = {
...value,
formats: newFormats,
};
} else {
newValue = remove(
value,
// Only remove the line if the selection is
// collapsed.
isCollapsed( value ) ? start - 1 : start,
end
);
}
}
} else if ( charAt( value, end ) === LINE_SEPARATOR ) {
if ( isCollapsed( value ) && formats[ end ] && formats[ end ].length ) {
const newFormats = formats.slice();

newFormats[ end ] = formats[ end ].slice( 0, -1 );
newValue = {
...value,
formats: newFormats,
};
} else {
newValue = remove(
value,
start,
// Only remove the line if the selection is
// collapsed.
isCollapsed( value ) ? start - 1 : start,
end
isCollapsed( value ) ? end + 1 : end,
);
}
} else if ( charAt( value, end ) === LINE_SEPARATOR ) {
newValue = remove(
value,
start,
// Only remove the line if the selection is collapsed.
isCollapsed( value ) ? end + 1 : end,
);
}

if ( newValue ) {
Expand Down