Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Simplify
  • Loading branch information
ellatrix committed Jun 27, 2019
commit a7c46f9400505f8799f802349d71570ae170aa24
5 changes: 5 additions & 0 deletions packages/format-library/src/link/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
applyFormat,
removeFormat,
slice,
isCollapsed,
} from '@wordpress/rich-text';
import { isURL, isEmail } from '@wordpress/url';
import { RichTextToolbarButton, RichTextShortcut } from '@wordpress/block-editor';
Expand All @@ -32,6 +33,10 @@ export const link = {
target: 'target',
},
__unstablePasteRule( value, { html, plainText } ) {
if ( isCollapsed( value ) ) {
return value;
}

const pastedText = ( html || plainText ).replace( /<[^>]+>/g, '' ).trim();

// A URL was pasted, turn the selection into a link
Expand Down
46 changes: 14 additions & 32 deletions packages/rich-text/src/component/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -228,29 +228,18 @@ class RichText extends Component {
window.console.log( 'Received plain text:\n\n', plainText );

const record = this.getRecord();

const pasteRules = formatTypes.reduce( ( accumlator, { __unstablePasteRule } ) => {
if ( __unstablePasteRule ) {
accumlator.push( __unstablePasteRule );
const transformed = formatTypes.reduce( ( accumlator, { __unstablePasteRule } ) => {
// Only allow one transform.
if ( __unstablePasteRule && accumlator === record ) {
accumlator = __unstablePasteRule( record, { html, plainText } );
}

return accumlator;
}, [] );

if ( pasteRules.length ) {
const transformed = pasteRules.reduce( ( accumlator, transform ) => {
// Only allow one transform.
if ( accumlator === record ) {
accumlator = transform( record, { html, plainText } );
}
}, record );

return accumlator;
}, record );

if ( transformed !== record ) {
this.onChange( transformed );
return;
}
if ( transformed !== record ) {
this.onChange( transformed );
return;
}

if ( onPaste ) {
Expand Down Expand Up @@ -367,24 +356,17 @@ class RichText extends Component {
inputRule( change, this.valueToFormat );
}

const inputRules = formatTypes.reduce( ( accumlator, { __unstableInputRule } ) => {
const transformed = formatTypes.reduce( ( accumlator, { __unstableInputRule } ) => {
if ( __unstableInputRule ) {
accumlator.push( __unstableInputRule );
accumlator = __unstableInputRule( accumlator );
}

return accumlator;
}, [] );

if ( inputRules.length ) {
const transformed = inputRules.reduce(
( accumlator, transform ) => transform( accumlator ),
change
);
}, change );

if ( transformed !== change ) {
this.onCreateUndoLevel();
this.onChange( { ...transformed, activeFormats } );
}
if ( transformed !== change ) {
this.onCreateUndoLevel();
this.onChange( { ...transformed, activeFormats } );
}

// Create an undo level when input stops for over a second.
Expand Down