Skip to content

Commit

Permalink
Partial multi-select: fix error with dead key (#39850)
Browse files Browse the repository at this point in the history
  • Loading branch information
ellatrix authored Mar 30, 2022
1 parent 16b89d6 commit 25c6b5e
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 0 deletions.
29 changes: 29 additions & 0 deletions packages/block-editor/src/components/writing-flow/use-input.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ export default function useInput() {
}

if ( event.keyCode === ENTER ) {
node.contentEditable = false;
event.preventDefault();
if ( __unstableIsFullySelected() ) {
replaceBlocks(
Expand All @@ -53,6 +54,7 @@ export default function useInput() {
event.keyCode === BACKSPACE ||
event.keyCode === DELETE
) {
node.contentEditable = false;
event.preventDefault();
if ( __unstableIsFullySelected() ) {
removeBlocks( getSelectedBlockClientIds() );
Expand All @@ -67,17 +69,44 @@ export default function useInput() {
event.key.length === 1 &&
! ( event.metaKey || event.ctrlKey )
) {
node.contentEditable = false;
if ( __unstableIsSelectionMergeable() ) {
__unstableDeleteSelection( event.keyCode === DELETE );
} else {
event.preventDefault();
// Safari does not stop default behaviour with either
// event.preventDefault() or node.contentEditable = false, so
// remove the selection to stop browser manipulation.
node.ownerDocument.defaultView
.getSelection()
.removeAllRanges();
}
}
}

function onCompositionStart( event ) {
if ( ! hasMultiSelection() ) {
return;
}

node.contentEditable = false;

if ( __unstableIsSelectionMergeable() ) {
__unstableDeleteSelection();
} else {
event.preventDefault();
// Safari does not stop default behaviour with either
// event.preventDefault() or node.contentEditable = false, so
// remove the selection to stop browser manipulation.
node.ownerDocument.defaultView.getSelection().removeAllRanges();
}
}

node.addEventListener( 'keydown', onKeyDown );
node.addEventListener( 'compositionstart', onCompositionStart );
return () => {
node.removeEventListener( 'keydown', onKeyDown );
node.removeEventListener( 'compositionstart', onCompositionStart );
};
}, [] );
}
Original file line number Diff line number Diff line change
Expand Up @@ -317,3 +317,19 @@ exports[`Multi-block selection should use selection direction to determine verti
<p>3</p>
<!-- /wp:paragraph -->"
`;
exports[`Multi-block selection should write over selection 1`] = `
"<!-- wp:paragraph -->
<p>1[</p>
<!-- /wp:paragraph -->
<!-- wp:paragraph -->
<p>]2</p>
<!-- /wp:paragraph -->"
`;
exports[`Multi-block selection should write over selection 2`] = `
"<!-- wp:paragraph -->
<p>1...2</p>
<!-- /wp:paragraph -->"
`;
Original file line number Diff line number Diff line change
Expand Up @@ -858,6 +858,27 @@ describe( 'Multi-block selection', () => {
expect( await getEditedPostContent() ).toMatchSnapshot();
} );

it( 'should write over selection', async () => {
await clickBlockAppender();
await page.keyboard.type( '1[' );
await page.keyboard.press( 'Enter' );
await page.keyboard.type( ']2' );
await page.keyboard.press( 'ArrowLeft' );
// Select everything between [].
await pressKeyWithModifier( 'shift', 'ArrowLeft' );
await pressKeyWithModifier( 'shift', 'ArrowLeft' );
await pressKeyWithModifier( 'shift', 'ArrowLeft' );

// Test setup.
expect( await getEditedPostContent() ).toMatchSnapshot();

// Ensure selection is in the correct place.
await page.keyboard.type( '...' );

// Expect a heading with "1&2" as its content.
expect( await getEditedPostContent() ).toMatchSnapshot();
} );

it( 'should handle Enter across blocks', async () => {
await clickBlockAppender();
await page.keyboard.type( '1[' );
Expand Down

0 comments on commit 25c6b5e

Please sign in to comment.