Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Mobile] Implement pasteHandler with plain text fallback #14044

Merged
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
23 changes: 22 additions & 1 deletion packages/editor/src/components/rich-text/index.native.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,27 @@ const unescapeSpaces = ( text ) => {
return text.replace( / | /gi, ' ' );
};

/**
* Calls {@link pasteHandler} with a fallback to plain text when HTML processing
* results in errors
*
* @param {Object} [options] The options to pass to {@link pasteHandler}
*
* @return {Array|string} A list of blocks or a string, depending on
* `handlerMode`.
*/
const saferPasteHandler = ( options ) => {
try {
return pasteHandler( options );
} catch ( error ) {
window.console.log( 'Pasting HTML failed:', error );
window.console.log( 'HTML:', options.HTML );
window.console.log( 'Falling back to plain text.' );
// fallback to plain text
return pasteHandler( { ...options, HTML: '' } );
}
};

const gutenbergFormatNamesToAztec = {
'core/bold': 'bold',
'core/italic': 'italic',
Expand Down Expand Up @@ -308,7 +329,7 @@ export class RichText extends Component {
mode = 'AUTO';
}

const pastedContent = pasteHandler( {
const pastedContent = saferPasteHandler( {
HTML: pastedHtml,
plainText: pastedText,
mode,
Expand Down