Skip to content

Commit

Permalink
Paste: apply active formats when pasting inline
Browse files Browse the repository at this point in the history
  • Loading branch information
ellatrix committed Dec 11, 2019
1 parent 989132f commit 72cb5ad
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 2 deletions.
14 changes: 13 additions & 1 deletion packages/block-editor/src/components/rich-text/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ class RichTextWrapper extends Component {
}
}

onPaste( { value, onChange, html, plainText, files } ) {
onPaste( { value, onChange, html, plainText, files, activeFormats } ) {
const {
onReplace,
onSplit,
Expand Down Expand Up @@ -175,6 +175,18 @@ class RichTextWrapper extends Component {
if ( typeof content === 'string' ) {
let valueToInsert = create( { html: content } );

// If there are active formats, merge them with the pasted formats.
if ( activeFormats.length ) {
let index = valueToInsert.formats.length;

while ( index-- ) {
valueToInsert.formats[ index ] = [
...activeFormats,
...( valueToInsert.formats[ index ] || [] ),
];
}
}

// If the content should be multiline, we should process text
// separated by a line break as separate lines.
if ( multiline ) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`RichText should apply active formatting for inline paste 1`] = `
"<!-- wp:paragraph -->
<p><strong>132</strong>3</p>
<!-- /wp:paragraph -->"
`;

exports[`RichText should apply formatting when selection is collapsed 1`] = `
"<!-- wp:paragraph -->
<p>Some <strong>bold</strong>.</p>
Expand Down
17 changes: 17 additions & 0 deletions packages/e2e-tests/specs/editor/various/rich-text.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -336,4 +336,21 @@ describe( 'RichText', () => {

expect( await getEditedPostContent() ).toMatchSnapshot();
} );

it( 'should apply active formatting for inline paste', async () => {
await clickBlockAppender();
await pressKeyWithModifier( 'primary', 'b' );
await page.keyboard.type( '1' );
await page.keyboard.type( '2' );
await pressKeyWithModifier( 'primary', 'b' );
await page.keyboard.type( '3' );
await pressKeyWithModifier( 'shift', 'ArrowLeft' );
await pressKeyWithModifier( 'primary', 'c' );
await page.keyboard.press( 'ArrowLeft' );
await page.keyboard.press( 'ArrowLeft' );
await page.keyboard.press( 'ArrowLeft' );
await pressKeyWithModifier( 'primary', 'v' );

expect( await getEditedPostContent() ).toMatchSnapshot();
} );
} );
4 changes: 3 additions & 1 deletion packages/rich-text/src/component/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,7 @@ class RichText extends Component {
onPaste,
__unstableIsSelected: isSelected,
} = this.props;
const { activeFormats = [] } = this.state;

if ( ! isSelected ) {
event.preventDefault();
Expand Down Expand Up @@ -331,6 +332,7 @@ class RichText extends Component {
html,
plainText,
files,
activeFormats,
} );
}
}
Expand Down Expand Up @@ -421,7 +423,7 @@ class RichText extends Component {
inputType = event.inputType;
}

if ( ! inputType ) {
if ( ! inputType && event && event.nativeEvent ) {
inputType = event.nativeEvent.inputType;
}

Expand Down

0 comments on commit 72cb5ad

Please sign in to comment.