Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
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