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 Oct 4, 2019
1 parent f68a107 commit 3fb633a
Show file tree
Hide file tree
Showing 5 changed files with 44 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 @@ -352,6 +352,7 @@ class RichTextWrapper extends Component {
undo,
placeholder,
keepPlaceholderOnFocus,
hasMultiSelection,
// eslint-disable-next-line no-unused-vars
allowedFormats,
withoutInteractiveFormatting,
Expand Down Expand Up @@ -420,6 +421,7 @@ class RichTextWrapper extends Component {
__unstableMarkAutomaticChange={ markAutomaticChange }
__unstableDidAutomaticChange={ didAutomaticChange }
__unstableUndo={ undo }
__unstableContentEditable={ ! hasMultiSelection }
>
{ ( { isSelected, value, onChange, Editable } ) =>
<>
Expand Down Expand Up @@ -491,6 +493,7 @@ const RichTextContainer = compose( [
getSelectionEnd,
getSettings,
didAutomaticChange,
hasMultiSelection,
} = select( 'core/block-editor' );

const selectionStart = getSelectionStart();
Expand All @@ -512,6 +515,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 @@ -46,6 +46,10 @@ class Editable extends Component {
element.reversed = nextProps.reversed;
}

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

const { removedKeys, updatedKeys } = diffAriaProps( this.props, nextProps );
removedKeys.forEach( ( key ) =>
element.removeAttribute( key ) );
Expand Down
5 changes: 5 additions & 0 deletions packages/rich-text/src/component/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -951,7 +951,11 @@ class RichText extends Component {
style,
className,
placeholder,
<<<<<<< HEAD
forwardedRef,
=======
__unstableContentEditable,
>>>>>>> Native Multi Block Selection
} = 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 @@ -988,6 +992,7 @@ class RichText extends Component {
onKeyUp={ this.onSelectionChange }
onMouseUp={ this.onSelectionChange }
onTouchEnd={ this.onSelectionChange }
contentEditable={ __unstableContentEditable }
/>
);
}
Expand Down

0 comments on commit 3fb633a

Please sign in to comment.