Skip to content

Commit

Permalink
Remove multiline prop from base rich text
Browse files Browse the repository at this point in the history
  • Loading branch information
ellatrix committed Sep 8, 2023
1 parent a6ad1fa commit 67c320c
Show file tree
Hide file tree
Showing 31 changed files with 108 additions and 1,073 deletions.
6 changes: 1 addition & 5 deletions packages/block-editor/src/components/rich-text/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ import { useInputEvents } from './use-input-events';
import { useInsertReplacementText } from './use-insert-replacement-text';
import { useFirefoxCompat } from './use-firefox-compat';
import FormatEdit from './format-edit';
import { getMultilineTag, getAllowedFormats } from './utils';
import { getAllowedFormats } from './utils';
import { Content } from './content';
import RichTextMultiline from './multiline';

Expand Down Expand Up @@ -146,7 +146,6 @@ export function RichTextWrapper(
const { getSelectionStart, getSelectionEnd, getBlockRootClientId } =
useSelect( blockEditorStore );
const { selectionChange } = useDispatch( blockEditorStore );
const multilineTag = getMultilineTag( multiline );
const adjustedAllowedFormats = getAllowedFormats( {
allowedFormats,
disableFormats,
Expand Down Expand Up @@ -261,7 +260,6 @@ export function RichTextWrapper(
onSelectionChange,
placeholder,
__unstableIsSelected: isSelected,
__unstableMultilineTag: multilineTag,
__unstableDisableFormats: disableFormats,
preserveWhiteSpace,
__unstableDependencies: [ ...dependencies, tagName ],
Expand Down Expand Up @@ -349,7 +347,6 @@ export function RichTextWrapper(
onReplace,
onSplit,
__unstableEmbedURLOnPaste,
multilineTag,
preserveWhiteSpace,
pastePlainText,
} ),
Expand All @@ -363,7 +360,6 @@ export function RichTextWrapper(
value,
onReplace,
onSplit,
multilineTag,
onChange,
disableLineBreaks,
onSplitAtEnd,
Expand Down
56 changes: 14 additions & 42 deletions packages/block-editor/src/components/rich-text/index.native.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,9 @@ import {
__experimentalRichText as RichText,
__unstableCreateElement,
isEmpty,
__unstableIsEmptyLine as isEmptyLine,
insert,
__unstableInsertLineSeparator as insertLineSeparator,
create,
replace,
split,
__UNSTABLE_LINE_SEPARATOR as LINE_SEPARATOR,
toHTMLString,
slice,
} from '@wordpress/rich-text';
Expand Down Expand Up @@ -338,32 +334,20 @@ function RichTextWrapper(
onCustomEnter();
}

if ( multiline ) {
if ( shiftKey ) {
if ( ! disableLineBreaks ) {
onChange( insert( value, '\n' ) );
}
} else if ( canSplit && isEmptyLine( value ) ) {
splitValue( value );
} else {
onChange( insertLineSeparator( value ) );
}
} else {
const { text, start: splitStart, end: splitEnd } = value;
const canSplitAtEnd =
onSplitAtEnd &&
splitStart === splitEnd &&
splitEnd === text.length;

if ( shiftKey || ( ! canSplit && ! canSplitAtEnd ) ) {
if ( ! disableLineBreaks ) {
onChange( insert( value, '\n' ) );
}
} else if ( ! canSplit && canSplitAtEnd ) {
onSplitAtEnd();
} else if ( canSplit ) {
splitValue( value );
const { text, start: splitStart, end: splitEnd } = value;
const canSplitAtEnd =
onSplitAtEnd &&
splitStart === splitEnd &&
splitEnd === text.length;

if ( shiftKey || ( ! canSplit && ! canSplitAtEnd ) ) {
if ( ! disableLineBreaks ) {
onChange( insert( value, '\n' ) );
}
} else if ( ! canSplit && canSplitAtEnd ) {
onSplitAtEnd();
} else if ( canSplit ) {
splitValue( value );
}
},
// eslint-disable-next-line react-hooks/exhaustive-deps
Expand Down Expand Up @@ -471,20 +455,8 @@ function RichTextWrapper(
} );

if ( typeof content === 'string' ) {
let valueToInsert = create( { html: content } );

const valueToInsert = create( { html: content } );
addActiveFormats( valueToInsert, activeFormats );

// If the content should be multiline, we should process text
// separated by a line break as separate lines.
if ( multilineTag ) {
valueToInsert = replace(
valueToInsert,
/\n+/g,
LINE_SEPARATOR
);
}

onChange( insert( value, valueToInsert ) );
} else if ( content.length > 0 ) {
// When an URL is pasted in an empty paragraph then the EmbedHandlerPicker should showcase options allowing the transformation of that URL
Expand Down
3 changes: 2 additions & 1 deletion packages/block-editor/src/components/rich-text/multiline.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { useDispatch } from '@wordpress/data';
import { RichTextWrapper } from './';
import { store as blockEditorStore } from '../../store';
import { useBlockEditContext } from '../block-edit';
import { getMultilineTag } from './utils';

function RichTextMultiline(
{
Expand All @@ -34,7 +35,7 @@ function RichTextMultiline(
const { clientId } = useBlockEditContext();
const { selectionChange } = useDispatch( blockEditorStore );

const multilineTagName = multiline.toLowerCase();
const multilineTagName = getMultilineTag( multiline );
value = value || `<${ multilineTagName }></${ multilineTagName }>`;
const padded = `</${ multilineTagName }>${ value }<${ multilineTagName }>`;
const values = padded.split(
Expand Down
24 changes: 3 additions & 21 deletions packages/block-editor/src/components/rich-text/split-value.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,7 @@ import { isEmpty, split, toHTMLString } from '@wordpress/rich-text';
* as a result of splitting the block by pressing enter, or with blocks as a
* result of splitting the block by pasting block content in the instance.
*/
export function splitValue( {
value,
pastedBlocks = [],
onReplace,
onSplit,
multilineTag,
} ) {
export function splitValue( { value, pastedBlocks = [], onReplace, onSplit } ) {
if ( ! onReplace || ! onSplit ) {
return;
}
Expand All @@ -38,13 +32,7 @@ export function splitValue( {
// the enter key.
if ( ! hasPastedBlocks || ! isEmpty( before ) ) {
blocks.push(
onSplit(
toHTMLString( {
value: before,
multilineTag,
} ),
! isAfterOriginal
)
onSplit( toHTMLString( { value: before } ), ! isAfterOriginal )
);
lastPastedBlockIndex += 1;
}
Expand All @@ -60,13 +48,7 @@ export function splitValue( {
// the enter key.
if ( ! hasPastedBlocks || ! isEmpty( after ) ) {
blocks.push(
onSplit(
toHTMLString( {
value: after,
multilineTag,
} ),
isAfterOriginal
)
onSplit( toHTMLString( { value: after } ), isAfterOriginal )
);
}

Expand Down
53 changes: 15 additions & 38 deletions packages/block-editor/src/components/rich-text/use-enter.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,7 @@
import { useRef } from '@wordpress/element';
import { useRefEffect } from '@wordpress/compose';
import { ENTER } from '@wordpress/keycodes';
import {
insert,
__unstableIsEmptyLine as isEmptyLine,
__unstableInsertLineSeparator as insertLineSeparator,
} from '@wordpress/rich-text';
import { insert } from '@wordpress/rich-text';
import { getBlockTransforms, findTransform } from '@wordpress/blocks';
import { useDispatch } from '@wordpress/data';

Expand Down Expand Up @@ -37,7 +33,6 @@ export function useEnter( props ) {
value,
onReplace,
onSplit,
multilineTag,
onChange,
disableLineBreaks,
onSplitAtEnd,
Expand Down Expand Up @@ -67,40 +62,22 @@ export function useEnter( props ) {
}
}

if ( multilineTag ) {
if ( event.shiftKey ) {
if ( ! disableLineBreaks ) {
onChange( insert( _value, '\n' ) );
}
} else if ( canSplit && isEmptyLine( _value ) ) {
splitValue( {
value: _value,
onReplace,
onSplit,
multilineTag,
} );
} else {
onChange( insertLineSeparator( _value ) );
}
} else {
const { text, start, end } = _value;
const canSplitAtEnd =
onSplitAtEnd && start === end && end === text.length;
const { text, start, end } = _value;
const canSplitAtEnd =
onSplitAtEnd && start === end && end === text.length;

if ( event.shiftKey || ( ! canSplit && ! canSplitAtEnd ) ) {
if ( ! disableLineBreaks ) {
onChange( insert( _value, '\n' ) );
}
} else if ( ! canSplit && canSplitAtEnd ) {
onSplitAtEnd();
} else if ( canSplit ) {
splitValue( {
value: _value,
onReplace,
onSplit,
multilineTag,
} );
if ( event.shiftKey || ( ! canSplit && ! canSplitAtEnd ) ) {
if ( ! disableLineBreaks ) {
onChange( insert( _value, '\n' ) );
}
} else if ( ! canSplit && canSplitAtEnd ) {
onSplitAtEnd();
} else if ( canSplit ) {
splitValue( {
value: _value,
onReplace,
onSplit,
} );
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,7 @@ import {
findTransform,
getBlockTransforms,
} from '@wordpress/blocks';
import {
isEmpty,
insert,
create,
replace,
__UNSTABLE_LINE_SEPARATOR as LINE_SEPARATOR,
} from '@wordpress/rich-text';
import { isEmpty, insert, create } from '@wordpress/rich-text';
import { isURL } from '@wordpress/url';

/**
Expand All @@ -27,23 +21,6 @@ import { shouldDismissPastedFiles } from '../../utils/pasting';

/** @typedef {import('@wordpress/rich-text').RichTextValue} RichTextValue */

/**
* Replaces line separators with line breaks if not multiline.
* Replaces line breaks with line separators if multiline.
*
* @param {RichTextValue} value Value to adjust.
* @param {boolean} isMultiline Whether to adjust to multiline or not.
*
* @return {RichTextValue} Adjusted value.
*/
function adjustLines( value, isMultiline ) {
if ( isMultiline ) {
return replace( value, /\n+/g, LINE_SEPARATOR );
}

return replace( value, new RegExp( LINE_SEPARATOR, 'g' ), '\n' );
}

export function usePasteHandler( props ) {
const propsRef = useRef( props );
propsRef.current = props;
Expand All @@ -59,7 +36,6 @@ export function usePasteHandler( props ) {
onReplace,
onSplit,
__unstableEmbedURLOnPaste,
multilineTag,
preserveWhiteSpace,
pastePlainText,
} = propsRef.current;
Expand Down Expand Up @@ -178,7 +154,6 @@ export function usePasteHandler( props ) {
pastedBlocks: blocks,
onReplace,
onSplit,
multilineTag,
} );
}

Expand Down Expand Up @@ -220,12 +195,7 @@ export function usePasteHandler( props ) {
} );

if ( typeof content === 'string' ) {
let valueToInsert = create( { html: content } );

// If the content should be multiline, we should process text
// separated by a line break as separate lines.
valueToInsert = adjustLines( valueToInsert, !! multilineTag );

const valueToInsert = create( { html: content } );
addActiveFormats( valueToInsert, value.activeFormats );
onChange( insert( value, valueToInsert ) );
} else if ( content.length > 0 ) {
Expand All @@ -237,7 +207,6 @@ export function usePasteHandler( props ) {
pastedBlocks: content,
onReplace,
onSplit,
multilineTag,
} );
}
}
Expand Down
13 changes: 3 additions & 10 deletions packages/block-editor/src/store/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,7 @@
* @return {Object} The mapped object.
*/
export function mapRichTextSettings( attributeDefinition ) {
const {
multiline: multilineTag,
__unstableMultilineWrapperTags: multilineWrapperTags,
__unstablePreserveWhiteSpace: preserveWhiteSpace,
} = attributeDefinition;
return {
multilineTag,
multilineWrapperTags,
preserveWhiteSpace,
};
const { __unstablePreserveWhiteSpace: preserveWhiteSpace } =
attributeDefinition;
return { preserveWhiteSpace };
}
21 changes: 8 additions & 13 deletions packages/block-library/src/pullquote/deprecated.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,6 @@ import {
useBlockProps,
} from '@wordpress/block-editor';
import { select } from '@wordpress/data';
import {
create,
replace,
toHTMLString,
__UNSTABLE_LINE_SEPARATOR,
} from '@wordpress/rich-text';

/**
* Internal dependencies
Expand Down Expand Up @@ -64,13 +58,14 @@ function parseBorderColor( styleString ) {
}

function multilineToInline( value ) {
return toHTMLString( {
value: replace(
create( { html: value, multilineTag: 'p' } ),
new RegExp( __UNSTABLE_LINE_SEPARATOR, 'g' ),
'\n'
),
} );
value = value || `<p></p>`;
const padded = `</p>${ value }<p>`;
const values = padded.split( `</p><p>` );

values.shift();
values.pop();

return values.join( '\n' );
}

const v5 = {
Expand Down
Loading

0 comments on commit 67c320c

Please sign in to comment.