Skip to content

Commit

Permalink
Fix e2e tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ellatrix committed Feb 25, 2021
1 parent 64518e3 commit 1019f3f
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 22 deletions.
55 changes: 41 additions & 14 deletions packages/block-editor/src/components/rich-text/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,19 @@ import { store as blockEditorStore } from '../../store';
const wrapperClasses = 'block-editor-rich-text';
const classes = 'block-editor-rich-text__editable';

function addActiveFormats( value, activeFormats ) {
if ( activeFormats.length ) {
let index = value.formats.length;

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

/**
* Get the multiline tag based on the multiline prop.
*
Expand Down Expand Up @@ -412,7 +425,31 @@ function RichTextWrapper(
);

const onPaste = useCallback(
( { value, onChange, html, plainText, files, activeFormats } ) => {
( {
value,
onChange,
html,
plainText,
isInternal,
files,
activeFormats,
} ) => {
// If the data comes from a rich text instance, we can directly use it
// without filtering the data. The filters are only meant for externally
// pasted content and remove inline styles.
if ( isInternal ) {
const pastedValue = create( {
html,
multilineTag,
multilineWrapperTags:
multilineTag === 'li' ? [ 'ul', 'ol' ] : undefined,
preserveWhiteSpace,
} );
addActiveFormats( pastedValue, activeFormats );
onChange( insert( value, pastedValue ) );
return;
}

if ( pastePlainText ) {
onChange( insert( value, create( { text: plainText } ) ) );
return;
Expand Down Expand Up @@ -474,21 +511,11 @@ function RichTextWrapper(
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 ] || [] ),
];
}
}
addActiveFormats( valueToInsert, activeFormats );

// If the content should be multiline, we should process text
// separated by a line break as separate lines.
if ( multiline ) {
if ( multilineTag ) {
valueToInsert = replace(
valueToInsert,
/\n+/g,
Expand All @@ -511,7 +538,7 @@ function RichTextWrapper(
onSplit,
splitValue,
__unstableEmbedURLOnPaste,
multiline,
multilineTag,
preserveWhiteSpace,
pastePlainText,
]
Expand Down
15 changes: 7 additions & 8 deletions packages/rich-text/src/component/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,11 @@ function RichText(
}

function handleCopy( event ) {
// Something other than the browser already set data.
if ( event.clipboardData.getData( 'text/plain' ) ) {
return;
}

const selectedRecord = slice( record.current );
const plainText = getTextContent( selectedRecord );
const html = toHTMLString( {
Expand Down Expand Up @@ -393,22 +398,16 @@ function RichText(
return;
}

// If the data comes from a rich text instance, we can directly use it
// without filtering the data. The filters are only meant for externally
// pasted content and remove inline styles.
if ( clipboardData.getData( 'rich-text' ) === 'true' ) {
handleChange( insert( record.current, formatToValue( html ) ) );
return;
}

if ( onPaste ) {
const files = getFilesFromDataTransfer( clipboardData );
const isInternal = clipboardData.getData( 'rich-text' ) === 'true';

onPaste( {
value: removeEditorOnlyFormats( record.current ),
onChange: handleChange,
html,
plainText,
isInternal,
files: [ ...files ],
activeFormats,
} );
Expand Down

0 comments on commit 1019f3f

Please sign in to comment.