Skip to content

Commit

Permalink
Native Multi Block Selection
Browse files Browse the repository at this point in the history
  • Loading branch information
ellatrix committed Sep 17, 2019
1 parent f594632 commit 02af322
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 30 deletions.
31 changes: 31 additions & 0 deletions packages/block-editor/src/components/block-list/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,37 @@ class BlockList extends Component {
window.removeEventListener( 'mousemove', this.setLastClientY );
}

componentDidUpdate() {
const {
hasMultiSelection,
selectionStart,
selectionEnd,
blockClientIds,
} = this.props;

if ( ! hasMultiSelection ) {
return;
}

const startIndex = blockClientIds.indexOf( selectionStart );

// The selected block is not in this block list.
if ( startIndex === -1 ) {
return;
}

const startNode = document.querySelector( `[data-block="${ selectionStart }"]` );
const endNode = document.querySelector( `[data-block="${ selectionEnd }"]` );
const selection = window.getSelection();
const range = document.createRange();

range.setStartBefore( startNode );
range.setEndAfter( endNode );

selection.removeAllRanges();
selection.addRange( range );
}

setLastClientY( { clientY } ) {
this.lastClientY = clientY;
}
Expand Down
30 changes: 0 additions & 30 deletions packages/block-editor/src/components/block-list/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -247,36 +247,6 @@
* Cross-block selection
*/

.block-editor-block-list__layout .block-editor-block-list__block {
::-moz-selection {
background-color: $blue-medium-highlight;
}

::selection {
background-color: $blue-medium-highlight;
}

// Selection style for multiple blocks.
&.is-multi-selected *::selection {
background-color: transparent;
}

&.is-multi-selected .block-editor-block-list__block-edit::before {
background: $blue-medium-highlight;

// Use opacity to work in various editor styles.
mix-blend-mode: multiply;

// Collapse extra vertical padding on selection.
top: -$block-padding;
bottom: -$block-padding;

.is-dark-theme & {
mix-blend-mode: soft-light;
}
}
}


/**
* Block styles and alignments
Expand Down
4 changes: 4 additions & 0 deletions packages/block-editor/src/components/rich-text/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,7 @@ class RichTextWrapper extends Component {
undo,
placeholder,
keepPlaceholderOnFocus,
hasMultiSelection,
// eslint-disable-next-line no-unused-vars
allowedFormats,
withoutInteractiveFormatting,
Expand Down Expand Up @@ -404,6 +405,7 @@ class RichTextWrapper extends Component {
__unstableMarkAutomaticChange={ this.markAutomaticChange }
__unstableDidAutomaticChange={ didAutomaticChange }
__unstableUndo={ undo }
__unstableContentEditable={ ! hasMultiSelection }
>
{ ( { isSelected, value, onChange, Editable } ) =>
<>
Expand Down Expand Up @@ -465,6 +467,7 @@ const RichTextContainer = compose( [
getSelectionEnd,
getSettings,
didAutomaticChange,
hasMultiSelection,
} = select( 'core/block-editor' );

const selectionStart = getSelectionStart();
Expand All @@ -486,6 +489,7 @@ const RichTextContainer = compose( [
selectionEnd: isSelected ? selectionEnd.offset : undefined,
isSelected,
didAutomaticChange: didAutomaticChange(),
hasMultiSelection: hasMultiSelection(),
};
} ),
withDispatch( ( dispatch, {
Expand Down
4 changes: 4 additions & 0 deletions packages/rich-text/src/component/editable.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ export default class Editable extends Component {
this.editorNode.reversed = nextProps.reversed;
}

if ( this.props.contentEditable !== nextProps.contentEditable ) {
this.editorNode.contentEditable = nextProps.contentEditable;
}

const { removedKeys, updatedKeys } = diffAriaProps( this.props, nextProps );
removedKeys.forEach( ( key ) =>
this.editorNode.removeAttribute( key ) );
Expand Down
2 changes: 2 additions & 0 deletions packages/rich-text/src/component/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -941,6 +941,7 @@ class RichText extends Component {
style,
className,
placeholder,
__unstableContentEditable,
} = this.props;
// Generating a key that includes `tagName` ensures that if the tag
// changes, we replace the relevant element. This is needed because we
Expand Down Expand Up @@ -974,6 +975,7 @@ class RichText extends Component {
onKeyUp={ this.onSelectionChange }
onMouseUp={ this.onSelectionChange }
onTouchEnd={ this.onSelectionChange }
contentEditable={ __unstableContentEditable }
/>
);
}
Expand Down

0 comments on commit 02af322

Please sign in to comment.